# Here is a simple LP, set up entirely in Python or the Gurobi shell # This is modified from Gurobi's example mip1.py on the webpage: # http://www.gurobi.com/documentation/8.0/examples/mip1_py.html#subsubsection:mip1.py from gurobipy import * # Create a model, giving it a name # coffeeLP = Model("MyCoffeeStory") # Create variables x = coffeeLP.addVar(vtype=GRB.CONTINUOUS, name="coffee") y = coffeeLP.addVar(vtype=GRB.CONTINUOUS, name="sugar") # Set objective coffeeLP.setObjective(3 * x , GRB.MAXIMIZE) # Add constraint: 4 x <= 12 coffeeLP.addConstr(4 * x <= 12, "coffee_limit") # Now we optimize: # coffeeLP.optimize() #################################################### # Here are a whole bunch of things to try: #################################################### coffeeLP.printAttr("X") our_vars = coffeeLP.getVars() our_vars x y our_vars[0] # this is the first variable our_vars[1] # this is the second variable our_vars[0].varName # this is the varName (variable name) of our_vars[0] our_vars[0].x y.varName # the variable name of the variable y above our_vars[1].varName # the same thing # a few equivalent ways to examine all the variables # for v in coffeeLP.getVars(): print(v.varName) for v in coffeeLP.getVars(): print('%s ' % (v.varName)) for v in coffeeLP.getVars(): print('%s %g' % (v.varName, v.x)) for v in coffeeLP.getVars(): print('%s has the value: %g' % (v.varName, v.x)) for i in range(0,len(our_vars)) print( our_vars[i].varName )