Invalid argument to QuadExpr multiplication Error
AnsweredHello,
The problem I want to optimize is the following:
With M an integer variable that takes 4 values, o a binary variable and a f continuous one.
First of all, I expressed M and f to the power of 3 using :
- gc = m.addGenConstrPow( M[i], M_3[i], 3.0, "Cubic Replicas")
- gc_1 = m.addGenConstrPow( f[(i,j)], f_3[(i,j)], 3.0, "Cubic Frequency")
Now the problem is that when I write the objective function in this form :
quicksum( M_3[i] * f_3[(i,j)] * o[(i,j)] for i in range(L) for j in range(R) )
I get this error :
GurobiError: Invalid argument to QuadExpr multiplication
I tried to solve the problem by declaring another variable mul[(i,j)] = f_3[(i,j)] * o[(i,j)] that contains the multiplication of the two variables, in order to decompose the multiplication but the same problem is occurs again:
quicksum( M_3[i] * mul[(i,j)] for i in range(L) for j in range(R) )
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) var.pxi in gurobipy.Var.__mul__() TypeError: float() argument must be a string or a number, not 'gurobipy.QuadExpr' During handling of the above exception, another exception occurred: TypeError Traceback (most recent call last) linexpr.pxi in gurobipy.LinExpr.__imul__() TypeError: float() argument must be a string or a number, not 'gurobipy.QuadExpr' During handling of the above exception, another exception occurred: GurobiError Traceback (most recent call last) <ipython-input-65-99b02cbdd206> in <module> 64 for j in range(round(R/3)): 65 mul[(i,j)] = f_3[(i,j)]*o[(i,j)] ---> 66 D[(i,j)] = M_3[i]*mul[(i,j)] 67 68 var.pxi in gurobipy.Var.__mul__() linexpr.pxi in gurobipy.LinExpr.__imul__() linexpr.pxi in gurobipy.LinExpr.__mul__() quadexpr.pxi in gurobipy.QuadExpr._mul() GurobiError: Invalid argument to QuadExpr multiplication
Remark : I also set the parameter of Nonconvex into 2 as suggested in some post but it didnt help.
Thank you in advance.
Ibtissam
-
Hi Ibtissam,
In the line
quicksum( M_3[i] * f_3[(i,j)] * o[(i,j)] for i in range(L) for j in range(R) )
you are trying to add products of three variables. This is not supported. You can only have products of at most two, i.e., quadratic terms.
You can make the other approach work, but you have to define mul as new variables and then make
mul[(i,j)] = f_3[(i,j)] * o[(i,j)
constraints in the model, i.e.,
m.addConstrs(mul[(i,j)] == f_3[(i,j)] * o[(i,j)] for i in range(L) for j in range(R))
Please note that with (all the bilinear and power constraints) the resulting model may be hard to solve and the result could have approximation/rounding errors.
1 -
Hello Silke,
Thank you for your answer, but the problem still persists.
This is part of what my code looks like :
I'm aware that the problem can be complex to solve, but I'd like to see how it plays out.
Regards,
Ibtissam
0 -
Hi Ibtissam,
Could you try without
mul[(i,j)] = f_3[(i,j)]*o[(i,j)] ?
Best regards,
Silke1 -
Hello Silke,
Indeed it is working !
Thank you.
Best Regards,
Ibtissam
0
Please sign in to leave a comment.
Comments
4 comments