How can I construct an optimization problem inside another optimization problem?
AnsweredI would like to construct an optimization problem (m) where one of the variables is determined by another operation problem (m1). It works when there is no relation between two problems. But if the variable N is involved in problem m1 as shown in bold, it doesn't work because variable N is not defined in m1. How can I do this kind of calculation?
For example:
m = gp.Model("mip1")
N = m.addVar(ub=10,name="N")
n = 0
for i in range(10):
m1 = gp.Model("mip2")
x = m1.addVar(lb=0,name="x")
y = m1.addVar(lb=0,name="y")
z = m1.addVar(lb=0,name="z")
m1.addConstr(N*x+y+z<=i, "c1")
m1.setObjective(2*x+y, GRB.MAXIMIZE)
m1.optimize()
list = m1.getAttr("x", m1.getVars())
n = n + list[0]
m.setObjective(N*n, GRB.MAXIMIZE)
m.optimize()
0
-
Official comment
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. -
It is not possible to have one variable being attached to more than 1 model. What you could do is to introduce separate variables \(N_{m1}\), solve model \(\texttt{m1}\), extract the solution value for variable \(N_{m1}\) via its X attribute, and finally add a constraint setting \(N = X_{N_{m1}}\).
Best regards,
Jaromił0
Post is closed for comments.
Comments
2 comments