#!/usr/bin/python # Copyright 2016, 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 >= 1 # x, y, z binary 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") # Create continuous variables x = m.addVar(vtype=GRB.CONTINUOUS, name="x", ub=1) y = m.addVar(vtype=GRB.CONTINUOUS, name="y", ub=1) z = m.addVar(vtype=GRB.CONTINUOUS, name="z", ub=1) # Integrate new variables m.update() # Set objective m.setObjective(2*x + 2*y + 4 * z, GRB.MAXIMIZE) # Add constraint: x + 2 y + 3 z <= 4 m.addConstr(x + 2 * y + 3 * z <= 4, "c0") m.optimize() for v in m.getVars(): print('%s %g' % (v.varName, v.x)) print('Obj: %g' % m.objVal) except GurobiError: print('Encountered a Gurobi error') except AttributeError: print('Encountered an attribute error')