How can I construct an optimization problem inside another optimization problem?
回答済みI 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
-
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
サインインしてコメントを残してください。
コメント
1件のコメント