Advertisement
Guest User

pulp

a guest
Mar 25th, 2019
823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. from pulp import *
  2.  
  3. #Setting up the maximization problem
  4. prob = LpProblem("Maximize Calories",LpMaximize)
  5.  
  6. # 3 variables representing Running, Walking, and Swimming; continuous variables because he could excercise for partial hours.
  7. x1=LpVariable("Running",0,None,LpContinuous)
  8. x2=LpVariable("Walking",0, None, LpContinuous)
  9. x3=LpVariable("Swimming",0,None,LpContinuous)
  10.  
  11.  
  12. # The objective function is added to the linear program
  13. prob += 528*x1 + 348*x2 + 492*x3, "Maximize Calories Burned"
  14.  
  15. #The constraints are entered
  16. prob += x1 + x2+ x3  <= 12, "Total Exercise Time"
  17. prob += x1 <= 4, "Running Time Limit"
  18. prob += x2 - 3*x3 >= 0, "Walk to Swim ratio"
  19.  
  20. #Write and solve the problem to a file
  21. prob.writeLP("Question_6.lp")
  22. prob.solve()
  23.  
  24. #Print the values solved by the linear program
  25. for v in prob.variables():
  26.     print(v.name, "=", v.varValue)
  27.  
  28. #Evaluate the objective function
  29. total_calories = 528*4 + 348*2 + 492*2
  30.  
  31. print("Maximum Calories Burned: ",total_calories)
  32.  
  33. ############OUTPUT###############
  34. #Running = 4.0
  35. #Swimming = 2.0
  36. #Walking = 6.0
  37. #Maximum Calories Burned:  3792
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement