confusion about adding MVar.fromlist()
AnsweredDear Gurobi-Team,
I tried to use MVar.fromlist() (https://www.gurobi.com/documentation/10.0/refman/py_mvar_fromlist.html) but the given example does not work:
import gurobipy as gp
model = gp.Model("model")
x0 = model.addVar()
x1 = model.addVar()
x2 = model.addVar()
x3 = model.addVar()
x_1d = MVar.fromlist([x0, x1, x2, x3]) # 1-D MVar
x_2d = MVar.fromlist([[x0, x1], [x2, x3]]) # 2-D MVar
I use Spyder and it already warns before runnig the script, that MVar is undefined. That's obviously true, so I added a "dummy MVar":
import gurobipy as gp
model = gp.Model("model")
x0 = model.addVar()
x1 = model.addVar()
x2 = model.addVar()
x3 = model.addVar()
MVar = model.addMVar(0)
x_1d = MVar.fromlist([x0, x1, x2, x3]) # 1-D MVar
x_2d = MVar.fromlist([[x0, x1], [x2, x3]]) # 2-D MVar
That seems to work, but is that the intended way of use?
Is it a problem, that the dummy MVar has a shape of zero and the following MVars built on it have different shapes?
Does the dummy need to remain in the model, or can I delete it after declaring the really wanted MVars?
Can I reuse the dummy as often as I want, like I did before, or do I need a dummy for each MVar in the model, as in the following?
import gurobipy as gp
model = gp.Model("model")
x0 = model.addVar()
x1 = model.addVar()
x2 = model.addVar()
x3 = model.addVar()
MVar1 = model.addMVar(0)
x_1d = MVar1.fromlist([x0, x1, x2, x3]) # 1-D MVar
MVar2 = model.addMVar(0)
x_2d = MVar2.fromlist([[x0, x1], [x2, x3]]) # 2-D MVar
Best regards
Thomas
-
Official comment
This post is more than two 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. -
Hi Thomas,
Our example is incomplete in the sense that it doesn't show the imports. It's working on the assumption that the following import exists:
from gurobipy import *
You can make it work in your example like this:
x_1d = gp.MVar.fromlist([x0, x1, x2, x3]) # 1-D MVar x_2d = gp.MVar.fromlist([[x0, x1], [x2, x3]]) # 2-D MVar
- Riley
0 -
Thanks, that works :-)
0
Post is closed for comments.
Comments
3 comments