Does gurobi automatically assume positive variable values?

4,808 views
Skip to first unread message

Adwaitvedant Mathkar

unread,
May 13, 2012, 1:10:35 AM5/13/12
to Gurobi Optimization
Hey All,
I'm running the following optimization problem on gurobi5.0.0:

maximize
x - y - 2 z
subject to
x + 2 y + z <= 4
x + y + z >= -1000

The solution gurobi is providing is x=4, y=z=0.

Whereas the solution x = 4, z= -1000, y=-4, provides a larger
objective and satisfies constraints.
One possibility is that guobi automatically assumes non negative
variable values?

If so how do you get around it?

(exact code and output listed below)

Thanks!

Best,
Adwait
------------------------------------------------------------------------
Code;
#!/usr/bin/python

# Copyright 2012, Gurobi Optimization, Inc.

# This example formulates and solves the following simple MIP model:
# maximize
# x - y - 2 z
# subject to
# x + 2 y + 3 z <= 4
# x + y + z >= -1000

from gurobipy import *

try:

# Create a new model
m = Model("mip1")

# Create variables
x = m.addVar(name="x")
y = m.addVar( name="y")
z = m.addVar( name="z")

# Integrate new variables
m.update()

# Set objective
m.setObjective(x - y - 2 * z, GRB.MAXIMIZE)

# Add constraint: x + 2 y + 3 z <= 4
m.addConstr(x + 2 * y + 3 * z <= 4, "c0")

# Add constraint: x + y + z >= 1
m.addConstr(x + y + z>= -1000, "c1")

m.optimize()

for v in m.getVars():
print v.varName, v.x

print 'Obj:', m.objVal



except GurobiError:
print 'Error reported'
---------------------------------------------------------------------------------
Output

Optimize a model with 2 rows, 3 columns and 6 nonzeros
Presolve removed 2 rows and 3 columns
Presolve time: 0.00s
Presolve: All rows and columns removed
Iteration Objective Primal Inf. Dual Inf. Time
0 4.0000000e+00 0.000000e+00 0.000000e+00 0s

Solved in 0 iterations and 0.00 seconds
Optimal objective 4.000000000e+00
x 4.0
y 0.0
z 0.0
Obj: 4.0

Christopher Maes

unread,
May 14, 2012, 12:21:43 PM5/14/12
to gur...@googlegroups.com
> One possibility is that guobi automatically assumes non negative
> variable values?
>
> If so how do you get around it?
>
>    # Create variables
>    x = m.addVar(name="x")
>    y = m.addVar( name="y")
>    z = m.addVar( name="z")

Please take a look at the documentation for addVar():
http://www.gurobi.com/documentation/5.0/reference-manual/node540

addVar (lb=0.0, ub=GRB.INFINITY, obj=0.0, vtype=GRB.CONTINUOUS,
name='''', column=None )

The default value of the lb argument is 0.0. If you want to change the
lower bound, you can do something like:

x = m.addVar(lb=-1.0, name="x")

Chris
Reply all
Reply to author
Forward
0 new messages