# This is a file that builds a few different objectives for a 5x4 sched LP # (1) first we give no objective (so Gurobi just solves the feasibility # problem) # (2) # import the Gurobi tools if you are running python from gurobipy import * ###################### # Set some parameters n = 5 d = 4 jf_model = Model('scheduler') vars = jf_model.addVars(n,d, vtype=GRB.BINARY, name='sched') vars # type this to examine the variables # Each group must take one day jf_model.addConstrs( (vars.sum(i,'*') == 1 for i in range(n) ), name='Group') # Each day has at most three presentations jf_model.addConstrs( (vars.sum('*',j) <= 3 for j in range(d) ), name='Days') # End of setting up variables and constraints # ######################################################## # ######################################################## # First LP: we do not specify an objective jf_model.optimize() # (If you really want to see all the variables in the optimal solution) for v in jf_model.getVars(): print('%s %g' % (v.varName, v.x)) # (If you only want to see the nonzero variables) jf_model.printAttr("X") # ######################################################## # Second LP: the objective is 0.1 times the index of the variable my_obj = LinExpr() i = 0.0 for v in jf_model.getVars(): i = i + 0.1 my_obj.addTerms( i , v ) # Now poke around a bit my_obj jf_model.setObjective( my_obj, GRB.MAXIMIZE) # Optimize and examine the optimal solution jf_model.optimize() jf_model.printAttr("X") # ######################################################## # A third toy example: the objective coefficient is the square of # the index my_obj.clear() i = 0.0 for v in jf_model.getVars(): i = i + 0.1 my_obj.addTerms( i * i , v ) # Now examine the objective and optimize my_obj jf_model.setObjective( my_obj, GRB.MAXIMIZE) jf_model.optimize() jf_model.printAttr("X") # ######################################################## # A fourth toy example my_obj.clear() for i in range(n): for j in range(d): my_obj.addTerms( i * j * 1.0, vars[i,j] ) # the line below is equivalent # my_obj.addTerms( i * j * 1.0, a[i*d+j] ) # Now examine the objective and optimize my_obj jf_model.setObjective( my_obj, GRB.MAXIMIZE) jf_model.optimize() jf_model.printAttr("X")