In [4]:
from gurobipy import *

try:

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

    # Create variables
    x = m.addVar(vtype=GRB.BINARY, name="x")
    y = m.addVar(vtype=GRB.BINARY, name="y")
    z = m.addVar(vtype=GRB.BINARY, 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 >= 1
    m.addConstr(x + y >= 1, "c1")

    m.optimize()

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

    print ('Obj:', m.objVal)

except GurobiError:
    print ('Error reported')


Optimize a model with 2 rows, 3 columns and 5 nonzeros
Variable types: 0 continuous, 3 integer (3 binary)
Coefficient statistics:
  Matrix range     [1e+00, 3e+00]
  Objective range  [1e+00, 2e+00]
  Bounds range     [1e+00, 1e+00]
  RHS range        [1e+00, 4e+00]
Presolve removed 2 rows and 3 columns
Presolve time: 0.00s
Presolve: All rows and columns removed

Explored 0 nodes (0 simplex iterations) in 0.01 seconds
Thread count was 1 (of 8 available processors)

Solution count 1: 3 
Pool objective bound 3

Optimal solution found (tolerance 1.00e-04)
Best objective 3.000000000000e+00, best bound 3.000000000000e+00, gap 0.0000%
x 1.0
y 0.0
z 1.0
Obj: 3.0

Dorian Example


In [9]:
from gurobipy import *

try:
    # Create a new model
    m = Model("Dorian Example")

    # Create variables
    x1 = m.addVar(vtype=GRB.CONTINUOUS, name="x1")
    x2 = m.addVar(vtype=GRB.CONTINUOUS, name="x1")

    # Integrate new variables
    m.update()

    # Set objective
    # Min z = 50x1 + 100x2 	(objective function in $1,000)
    m.setObjective(50 * x1 + 100 * x2, GRB.MINIMIZE)

    # Add constraint: x + 2 y + 3 z <= 4
    # 7x1 + 2x2 ≥ 28	(high-income women)
	# 2x1 + 12x2 ≥ 24	(high-income men)
	# x1, x2 ≥ 0		(non-negativity constraints)
    m.addConstr(7 * x1 + 2* x2 >= 28, "c0")
    m.addConstr(2 * x1 + 12* x2 >= 24, "c1")

    m.optimize()

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

    print ('Obj:', m.objVal)

except GurobiError as e:
    print (e)
    print ('Error reported')


Optimize a model with 2 rows, 2 columns and 4 nonzeros
Coefficient statistics:
  Matrix range     [2e+00, 1e+01]
  Objective range  [5e+01, 1e+02]
  Bounds range     [0e+00, 0e+00]
  RHS range        [2e+01, 3e+01]
Presolve time: 0.01s
Presolved: 2 rows, 2 columns, 4 nonzeros

Iteration    Objective       Primal Inf.    Dual Inf.      Time
       0    0.0000000e+00   5.000000e+00   0.000000e+00      0s
       2    3.2000000e+02   0.000000e+00   0.000000e+00      0s

Solved in 2 iterations and 0.01 seconds
Optimal objective  3.200000000e+02
x1 3.5999999999999996
x1 1.4000000000000001
Obj: 320.0