Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. ### Task 1
  2. ### Implements a simple LP with the gurobi py interface
  3. from gurobipy import *
  4. import random
  5. import copy
  6.  
  7. ### Parameters definitions
  8. product_type = 2 # {standard, premium}
  9.  
  10. price = [300, 450]
  11. required_painting = [6, 9]
  12. required_assembly = [14, 23]
  13. total_hour_painting = 90
  14. total_hour_assembly = 180
  15.  
  16. mod = Model("motorcycle")
  17. ### Variable definitions
  18. z_p = {} #production of motorcycle of type p
  19. for ii in range(product_type):
  20. # for binary: vtype=GRB.BINARY
  21. z_p[ii] = mod.addVar(lb=0.0, obj=-price[ii], name="x_p{0}".format(ii))
  22. mod.update()
  23. # Objective already set above
  24. ### Constraints
  25. mod.addConstr(quicksum(z_p[ii]*required_painting[ii] for ii in range(product_type)) <= total_hour_painting, "painting hours")
  26. mod.addConstr(quicksum(z_p[ii]*required_assembly[ii] for ii in range(product_type)) <= total_hour_assembly, "assembly hours")
  27. mod.addConstr(z_p[0] <= 12, "c0")
  28. mod.addConstr(z_p[1] >= 1, "c1")
  29. mod.addConstr(z_p[0] <= 11, "c2")
  30. mod.addConstr(z_p[1] <= 1, "c3")
  31.  
  32. # Non-negativity set in variable definition
  33. mod.optimize()
  34.  
  35. print("Optimal objective: {0:.3f}".format(mod.objVal))
  36. for var in mod.getVars():
  37. print("{0}: {1:.3f}".format(var.varName, var.x))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement