Class activities Oct 29, 2018 for UBC course Math 441: Copyright Joel Friedman, 2018 Office hours before progress report deadline: Tuesday ICCS X561, Wednesday Math Building 210 -- Mostly to check your models are formulated correctly (decision variables, objective function(s), constraints) # ######################################################## This week: -- The main goal of these next few days is to learn how to build LP/IP/QP's in Gurobi, and get used to the class/objects built by Gurobi: jf_model = Model('scheduler') # sets jf_model as a gurobi.Model object with functions: .addVar, .addVars, .addConstr, .addConstrs, .setObjective (and a number of alternatives, to set up the model) and functions: .optimize, .printAttr, .getVars (to solve the problem and examine the solution) If your LP has many repetitive variables and constraints, YOU WILL WANT TO KNOW .addVars and .addConstrs (with an "s" at the end) !!! my_obj = LinExpr() # initializes my_obj as a gurobi.LinExpr object with functions: .addTerms, .clear() -- We look at mip1.py, qp.py from the Gurobi website -- We build our own simple LP. -- We build our presentation scheduler (i.e., weighted bipartite matching LP): * We cut and paste from pres_sched_4toyObj.txt, which involves some toy objective functions but does not take input from the data file * We examine the data files: data5x4.txt, data8x4_same.txt, data8x4_vary.txt * We review some python (see below) * The full programs: presentation_sched.txt, presentation_sched.py, quiet_scheduler.py # ######################################################## # Python review and new stuff # ######################################################## Summary: - New function(s): type(), random.random() -- Gives a random real between 0 and 1 (excluding 1) random.seed(integer), randdom.guass(mu,sigma) (see https://docs.python.org/2/library/random.html) - More details on lists: lists (we will invoke help() and then LISTS) - Coding Style - More details on loops: loops within loops (useful for LP/IP/QPs) ---------------------------------------------------------- type(): greet = "Hi there." # sets/creates a variable "greet", of type string greet # displays greet print(greet) # prints greet as Python thinks you want to see it type(greet) # tells you that greet is of type 'str' # try these commands on the following variables: x = 12.5 y = 12 from gurobipy import * my_model = Model('coffee_optimization') my_obj = my_model.LinExpr() ----- Random number generators: see https://docs.python.org/2/library/random.html import random random.random() # random number >= 0 and < 1 . Unpredictable... random.seed(123) random.random() random.seed(123) random.random() ---------------------------------------------------------- # lists a = range(5) # this is a list in integers a type(a) range(5) range(7,12) b = [ 'Yes', 'No', 'Perhaps', 'Likely' ] # this is a list of strings b type(b) b[0] b[1:3] ---------------------------------------------------------- # Coding style: # make sure your code is as READABLE and WELL COMMENTED as possible # the stuff below can make your code MORE READABLE or LESS READABLE c = 1,2,3 # this is a tuple, and is NOT THE SAME as a list type(c) d,e,f = 6,8,'Hi there.' # After this, examine d,e,f [d,e,f] = [ 6,8,'Hi there.'] # this also works d = 6 ; e = 8 ; f = 'Hi there' # which do you prefer from the above three? # or do you prefer d = 6 # set d to 6 (is this really necessary) d = 6 # d is the number of days for presentations (better) e = 8 f = 'Hi there' f = 'Hi there' # this sets f to the string 'Hi there' which we shall use to # blah blah blah blah blah blah blah blah blah blah blah # blah blah blah blah blah blah blah blah blah blah blah # blah blah blah blah blah blah blah blah blah blah blah # blah blah blah blah blah blah blah blah blah blah blah # blah blah blah blah blah blah blah blah blah blah blah # blah blah blah blah blah blah blah blah blah blah blah # Here comes the next statement in the program # I will make a comment about the next statement as well Which do you prefer from the five styles below? from gurobipy import * # import the Gurobi tools from gurobipy import * # import the Gurobi tools # from gurobipy import * # import the Gurobi tools from gurobipy import * # #################################### import the Gurobi tools from gurobipy import * ---------------------------------------------------------- # loops within loops for i in range(5): # don't forget the colon (:) !!!! print(i, i * 10, i ** i) for i in b : print(i) # a for loop within another for loop for i in range(5,7): for j in range(12,14): print(i,j,i*j) print("Here are i and j: ",i,j) print("Here are i and j: " + str(i) + ", " + str(j) ) ---------------------------------------------------------- Specific Python words/commands you should know (aside from the basics about variables and assignment): quit(), help(), print(), open(), close(), for, while, range(), greet = "Hello, Alice. My friend, how are you?" greet[0], greet[1], len(greet) greet.split(",") greet.split(" ") int("3"), float("3.4"), str(8.9) ----------------------------------------------------------