[Gurobi] Problems with model.addVar()

1,601 views
Skip to first unread message

Eldon Gunn

unread,
May 24, 2010, 11:50:11 PM5/24/10
to Gurobi Optimization
I am trying to extract a small model from a larger model. I started
with a model "mod " and am trying to create a smaller model "
smallm". However, the smallm.AddVar() is giving me an error when I
try to include a column object in the add. If I don't include the
column object, I can do the addVar but I end up with a model with no
coefficients.

I am attaching the script that I am running along with the run
results . The run shows the error message

Traceback (most recent call last):
File "<stdin>", line 7, in <module>
File "model.pxi", line 1237, in gurobipy.Model.addVar (../../src/
python/gurobipy.c:23217)
gurobipy.GurobiError: Problem adding variables

It looks to me as if this is an internal Gurobi error but I don't know
what it means

If I run the script with the "column =" commented out, it runs fine
but I end up with no coefficients in the model.

I am new to both Python and Gurobi so I may just be doing something
stupid. If you can see what it is, please let me know and excuse my
ignorance.

As you can see from the runs, I am using Python 2.6.1 on a Mac
powerbook running Snow Leopard 10.6.3

Thanks for any help you can give me

Eldon Gunn


Script

import sys
import math
from gurobipy import *

mod=read("500_1.mps")
mod.ModelSense=-1
smallm=Model("Small Model")

mod.printStats()
cons = mod.getConstrs()
nrows=mod.NumConstrs

nvars=mod.NumVars
numGub=0


for i in range(nrows):
if cons[i].ConstrName[0:3] == 'ARE' :
numGub=numGub+1
else:
smallm.addConstr(0,cons[i].Sense, cons[i].RHS,
cons[i].ConstrName)

smallm.update()
numGub

vars=mod.getVars()

lastP=0
for i in range(nvars):
if vars[i].VarName[0] =='P' :
lastP=lastP+1
else:
colob=mod.getCol(vars[i])
newvar= smallm.addVar(lb=vars[i].LB, ub=vars[i].UB,
obj=vars[i].Obj, \
vtype=vars[i].Vtype, name=vars[i].VarName),
column=colob)

lastP

smallm.update()
smallm



Run results:

Last login: Sun May 23 11:32:06 on ttys000
Macintosh-140:~ eldongunn1$
Macintosh-140:~ eldongunn1$ bash
bash-3.2$ cd Doc*/500*y
bash-3.2$ python
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import math
>>> from gurobipy import *
>>>
>>> mod=read("500_1.mps")
Read MPS format model from file 500_1.mps
Reading time = 0.01 seconds
FORESTLP: 584 Rows, 4779 Columns, 23889 NonZeros
>>> mod.ModelSense=-1
>>> smallm=Model("Small Model")
>>>
>>> mod.printStats()

Statistics for model FORESTLP :
Sizes : 584 Constrs, 4779 Vars, 23889 NZs
Matrix coefficient range : [ 1, 416.91 ]
Objective coefficient range : [ 0.52033, 10000 ]
RHS coefficient range : [ 0.01, 600935 ]
>>> cons = mod.getConstrs()
>>> nrows=mod.NumConstrs
>>>
>>> nvars=mod.NumVars
>>> numGub=0
>>>
>>>
>>> for i in range(nrows):
... if cons[i].ConstrName[0:3] == 'ARE' :
... numGub=numGub+1
... else:
... smallm.addConstr(0,cons[i].Sense, cons[i].RHS,
cons[i].ConstrName)
...
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
<gurobi.Constr Not Yet Added>
>>> smallm.update()
>>> numGub
500
>>>
>>> vars=mod.getVars()
>>>
>>> lastP=0
>>> for i in range(nvars):
... if vars[i].VarName[0] =='P' :
... lastP=lastP+1
... else:
... colob=mod.getCol(vars[i])
... newvar= smallm.addVar(lb=vars[i].LB, ub=vars[i].UB,
obj=vars[i].Obj, \
... vtype=vars[i].Vtype, name=vars[i].VarName,
column=colob)
...
Traceback (most recent call last):
File "<stdin>", line 7, in <module>
File "model.pxi", line 1237, in gurobipy.Model.addVar (../../src/
python/gurobipy.c:23217)
gurobipy.GurobiError: Problem adding variables
>>> lastP
4735
>>>
>>> smallm.update()
>>> smallm
<gurobi.Model Continuous instance Small Model: 84 constrs, 0 vars,
Parameter changes: LogFile=gurobi.log>
>>>
>>>
>>>
>>>
>>>














Ed Rothberg

unread,
May 25, 2010, 11:25:17 PM5/25/10
to Gurobi Optimization
>         colob=mod.getCol(vars[i])
>         newvar= smallm.addVar(..., column=colob)

All variable and constraint objects are associated with a specific
model. In this case, 'colub' contains a list of constraints from
model 'mod', but you are trying to use them in model 'smallm'.

Eldon Gunn

unread,
May 28, 2010, 1:34:53 PM5/28/10
to Gurobi Optimization
Ed,

Thanks for the comment. However this is exactly what I want to do;
extract a small model from a larger model 'mod' and maintain the same
names for the columns and constraints in the smaller model 'smallm'.
How tight is the association. If I extract the names from the column
object and then create a new column object with the same names and
coefficient, can I then add this column into the model 'smallm' or do
I have to create totally different names for the rows in 'smallm'.

Eldon

Ed Rothberg

unread,
May 28, 2010, 1:56:05 PM5/28/10
to Gurobi Optimization

It sounds like the easiest way to do what you want would be to make a
copy of the original model (smallm = mod.copy()) and then just remove
the parts you don't want.

Reply all
Reply to author
Forward
0 new messages