• Gurobi Staff

Hi,

Because of your question and you mentioning "risk", I suspect you may be interested in the Gurobi Finance project. The linked page includes a nice and clean way to express this risk formula using Gurobi matrix notation. The project contains many more articles showing how to express related concepts with Gurobi.

• If you include risk as an objective, you may not need to apply sqrt at all
• If you add a constraint to limit risk, you would take the square of the right-hand side (constant) to avoid having to take sqrt of the left-hand-side (formula involving your decision variables)

Let me know if this helps!

Kind regards,
Ronald

my constraint is result = np.sqrt(np.dot(np.dot(variable_array.T,covariance_matrix_array),variable_array))

where result should be less than 5.56%. Now to remove the sqrt from left hand side if I square the 0.056 then the answer will not be right.

• Gurobi Staff

What is the problem you're observing?

I am creating a constraint the contraint is risk should be less that 5.56% to caluclate the risk we have formula risk = np.sqrt(np.dot(np.dot(variable_array.T,covariance_matrix_array),variable_array)). Now that gurobi does not support sqrt so how can I create the constraint?

• Gurobi Staff

Hi,

What I meant was - have you implemented the approach I suggested and/or tested the model that is discussed in Gurobi Finance? If that leads to errors, could you share a reproducible piece of code to better understand what you're trying to do?

If risk = sqrt(f(vars)) and your only constraint on risk is risk<=0.056, then defining riskSquared=f(vars) and riskSquared<=0.056**2 should give the right result.

Just for completeness - Gurobi does support the square root, using the addGenConstrPow function. You would first define a variable riskSquared as above, and then another variable risk which you would link by calling addGenConstrPow(riskSquared, risk, 0.5). For performance reasons however you should prefer the approach I shared earlier.

Kind regards,
Ronald

I have implemented the solution now the error is not coming but a new problem arrives here is the code

portfolio_risk = gp.QuadExpr()for i in range(len(variable)):    for j in range(len(variable)):        portfolio_risk += variable[i] * covariance_matrix_array[i, j] * variable[j]model.addQConstr(portfolio_risk <= 0.00287296, "portfolio_risk")model.update()

The output is

<gurobi.QConstr Not Yet Added>

Why the constraint is not get added when I also tried to update the model after adding the constraint

• Gurobi Staff

Does the error occur at the last line of your code (model.update) or in a different place? It would typically occur when you try referencing a constraint later, but I notice you don't store the result of model.addQConstr in a Python variable for use later.

Kind regards,
Ronald

It's not get added even after doing model.update()

see the code

portfolio_risk = gp.QuadExpr()for i in range(len(variable)):    for j in range(len(variable)):        portfolio_risk += variable[i] * covariance_matrix_array[i, j] * variable[j] model.addQConstr(portfolio_risk <= 0.00287296, "portfolio_risk")

Output:

<gurobi.QConstr Not Yet Added>

The solution provided by you

model.update()

Checking for the constraint in the model

pending_constraints = model.getConstrs()# Print details of each pending constraintfor constr in pending_constraints:    print(constr)

No output None
This is how my portflolio_risk look like

<gurobi.QuadExpr: 0.0 + [ 8.430925170068025e-05 x1 ^ 2 + 0.000510914923469388 x2 * x1 + -0.00022443623724489845 x3 * x1 + -0.0003129861011904759 x4 * x1 + 0.0003389425255102042 x5 * x1 + -0.0001981796896258504 x6 * x1 + 0.0006025277593537417 x7 * x1 + -0.0002102003188775508 x8 * x1 + 0.0005607082695578237 x9 * x1 + 0.00013936514455782303 x10 * x1 + 0.0003629431972789119 x11 * x1 + 0.00041692561649659884 x12 * x1 + 0.00036367504251700705 x13 * x1 + 2.4184438775510693e-06 x14 * x1 + 0.0011259449319727898 x15 * x1]

The problem is solve

pending_constraints = model.getConstrs()

I have using getConstrs() for getting

addQConstr

I have used model.getQConstrs() now it's showing Thank Your for the help sir.