gurobipy.GurobiError: Divisor must be a constant
回答済みI was trying to add an objective function to maximize the average probability:
model.setObjectiveN(-sum(x[i,j]*prob[i][j] for i in range(n) for j in range(m))/x.sum(), index=0)
where x is variable defined as:
x = model.addVars(n,m, vtype=GRB.BINARY)
and `prob` is a matrix of size n*m which is previously known data.
But I got error
Traceback (most recent call last):
File "src/gurobipy/linexpr.pxi", line 523, in gurobipy.LinExpr.__truediv__
TypeError: float() argument must be a string or a number, not 'gurobipy.LinExpr'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "constr_solver.py", line 172, in <module>
main(...)
File "constr_solver.py", line 137, in main
x = optimize(...)
File "constr_solver.py", line 72, in optimize
model.setObjectiveN(-sum(x[i,j]*prob[i][j] for i in range(n) for j in range(m))/x.sum(), index=0)
File "src/gurobipy/linexpr.pxi", line 525, in gurobipy.LinExpr.__truediv__
gurobipy.GurobiError: Divisor must be a constant
I want to maximize the average probability for the chosen x. Is there any other way I can define this? 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 try Gurobot, our chatbot interface offering instant, expert-level support. -
Dividing by a variable (or a sum of variables) is currently not supported in Gurobi. However, with a few auxiliary variables and equality constraints, it is possible to formulate your objective function in Gurobi.
To formulate a division by an optimization variable, you should introduce an auxiliary variable \(z_n\) for the numerator expression and an auxiliary variable \(z_d\) for the denominator expression, i.e., add equality constraints \(z_n = \text{numerator}\) and \(z_d = \text{denominator}\).
Next, you can follow the instructions in How do I divide by a variable in Gurobi? to model the division \(\frac{1}{z_d} = x\).
You can then finally use \(x\) and \(z_n\) in your objective function as \(z_n \cdot x\).
Please note that your model is nonconvex and you have to set the NonConvex parameter.Best regards,
Jaromił0 -
Thank you! It helps.
Danning
0
投稿コメントは受け付けていません。
コメント
3件のコメント