how to set negative to positive index in Model variable
Answeredimport gurobipy as gp
import numpy as np
import scipy.sparse as sp
m = gp.Model()
t=range(4)
g=range(-300,300) x = m.addMVar((g,t), name='x')
-
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?. -
I am not sure that I understand your question fully. When I execute the code above, it returns with the following error because the Model.addMVar() is expecting a shape for its first argument (see examples below).
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-03bee3810f57> in <module>
5 t=range(4)
6 g=range(-300,300)
----> 7 x = m.addMVar((g,t), name='x')
src/gurobipy/model.pxi in gurobipy.Model.addMVar()
TypeError: unsupported operand type(s) for *=: 'int' and 'range'If you would like to create a 600x4 matrix of variables, you can use
x = model.addMVar((600,4), name="x") # add a 600x4 2-D array of variables
Is this what you are looking to achieve? If not, can you explain what you are looking for in more detail?
0 -
thank you very much Alison Cozad,
my question is about how to set -ve to +ve range of index for the matrix variable.
for example
import gurobipy as gp
import numpy as np
import scipy.sparse as sp
m = gp.Model()
x={}
for j in range(4):
for i in range(-300,300):
x[j,i]=m.addVar( vtype=GRB.BINARY, name="x(%s,%s)"%(i,j))0 -
When using Model.addMVar(), you can only control the shape of the MVar object. The resulting indices start at \( 0. \) In Python, negative indices have specific meanings for array-like objects: an index of \( -1 \) accesses the last element, \( -2 \) accesses the second-to-last element, etc.
You can:
- Create a \( \texttt{shape=(600,4)} \) MVar (like Alison suggested), or
- Use Model.addVar()/Model.addVars() to build a dict/tupledict that has negative integers as the keys. This approach creates Var objects instead of MVar objects.
0
Post is closed for comments.
Comments
4 comments