MVar constraint loops
AnsweredI have something equivalent to the following in a model. It takes considerably longer to set up than it takes to solve. Is there a quicker way?
xsize = 1000
ysize = 100
zsize = 4
xyz = model.addMVar((xsize,ysize,zsize))
xy = model.addMVar((xsize,ysize))
for i in range(xsize):
for j in range(ysize):
model.addConstr(xyz[i,j,:].sum() == xy[i,j])
-
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 why not try our AI Gurobot?. -
Hi Robert,
One possible way would be to use the list version of your \(\texttt{MVars}\) when adding the constraints, e.g.,
xsize = 1000
ysize = 100
zsize = 4
xyz = model.addMVar((xsize,ysize,zsize))
xy = model.addMVar((xsize,ysize))
xyz_list = xyz.tolist()
xy_list = xy.tolist()
for i in range(xsize):
for j in range(ysize):
model.addConstr(gp.quicksum(xyz_list[i][j][z] for z in range(zsize)) == xy_list[i][j])Best regards,
Jaromił0 -
Thanks very much! That did the job nicely. I noticed a related and more severe problem is that setting ub on xyz can make creating the MVar extremely slow (like about 5 minutes) if each component has a different ub. I can avoid that by setting a single value ub, and using constraints. That also seems wrong, but at least there is a workaround.
0
Post is closed for comments.
Comments
3 comments