How to include float exponents into objective function?
回答済みDear community,
I screened the recent posts, however, did not find a solution to my problem.
I would like to use exponents as floats in my objective function. However, it does not work. Here is an abbreviated example:
m.setObjective(
sum( f * x[i] + (v * ( x[i] * Q[i])) ** (0.4) for i in I)
, GRB.MINIMIZE)
I receive the following error message:
unsupported operand type(s) for ** or pow(): 'gurobipy.LinExpr' and 'float'
I would be happy to receive some guidance how to include the exponent into the optimization problem correctly in Python.
Thank you.
-
正式なコメント
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
Hi,
You have to use the addGenConstrPow() function in order to model exponentiation in Gurobi.
Best regards,
Jaromił1 -
Hi Jaromił,
thank you very much for your comment. I applied it in this way:
m.setObjective(
sum( f * x[i] + y for i in I)
, GRB.MINIMIZE)m.addGenConstrPow(((v * ( x[i] * Q[i])) for i in I), y, 0.4, "gf", "FuncPieces=1000")
However, I receive this error term now:
GurobiError: Unsupported type (<class 'gurobipy.tupledict'>) for LinExpr addition argumentDo you have an idea how to solve it?
Thank you very much.
0 -
Hi,
As noted in the documentation of addGenConstrPow(), this function requires two single variables as input.
This means that you have to introduce a new auxiliary variable and an additional equality for each \(\texttt{i}\) in \(\texttt{I}\) to model the relationship \(\texttt{myAuxVar = v *(x * Q)}\) first. Then, you can use
m.addGenConstrPow(myAuxVar, y, 0.4, "gf", "FuncPieces=1000")
Best regards,
Jaromił1 -
Hi Jaromił,
thank you!
I added the constraint as above. However, I have problems with the auxiliary variable since it refers to a function. How would you add it? Furthermore, I have more than 1 million i's so I am looking for an efficient way to include this without additional equalities for each i in I. Do you have an idea how to solve it?
x = m.addVars(I, vtype=GRB.BINARY, name="x")
myAuxVar = m.addVars( ?? )Thank you!
0 -
Hi,
Unfortunately, you will have to add 2 auxiliary variables and 1 equality constraint for each \(\texttt{i}\).
The code would look something like this:
x = m.addVars(I, vtype=GRB.BINARY, name="x")
myAuxVar = m.addVars(I, vtype=GRB.CONTINUOUS, lb=-GRB.INFINITY, name="myAuxVar" )
y = m.addVars(I, vtype=GRB.CONTINUOUS, name="y")
for i in I:
m.addConstr(myAuxVar[i] == v[i] * (x[i] * Q[i]), "auxConstr_"+str(i))
m.addGenConstrPow(myAuxVar[i], y[i], 0.4, "gf_"+str(i), "FuncPieces=1000")
m.setObjective( sum( f * x[i] + y[i] for i in I), GRB.MINIMIZE)I would most likely also decrease the number of pieces to 100 or even 10 for the general constraints to improve performance. However, this depends on how accurate your solution has to be.
Best regards,
Jaromił0 -
Hi Jaromił,
thank you very much for the code example. I added it to my problem and now I receive the following error message:
GurobiError: lower bound of x variable is less than zero for POW functionI am suprised since x is binary so that the auxiliary variable can only become 0. What would I need to adapt to avoid this error message?
Thank you!
0 -
Hi,
\(x^{0.4}\) is not defined for \(x<0\), small but important detail :) The above code should read
myAuxVar = m.addVars(I, vtype=GRB.CONTINUOUS, name="myAuxVar" )The lower bound of all \(\texttt{myAuxVar}\)s has now the default value 0.
Best regards,
Jaromił1
投稿コメントは受け付けていません。
コメント
8件のコメント