Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pulp import *
- #Setting up the maximization problem
- prob = LpProblem("Maximize Calories",LpMaximize)
- # 3 variables representing Running, Walking, and Swimming; continuous variables because he could excercise for partial hours.
- x1=LpVariable("Running",0,None,LpContinuous)
- x2=LpVariable("Walking",0, None, LpContinuous)
- x3=LpVariable("Swimming",0,None,LpContinuous)
- # The objective function is added to the linear program
- prob += 528*x1 + 348*x2 + 492*x3, "Maximize Calories Burned"
- #The constraints are entered
- prob += x1 + x2+ x3 <= 12, "Total Exercise Time"
- prob += x1 <= 4, "Running Time Limit"
- prob += x2 - 3*x3 >= 0, "Walk to Swim ratio"
- #Write and solve the problem to a file
- prob.writeLP("Question_6.lp")
- prob.solve()
- #Print the values solved by the linear program
- for v in prob.variables():
- print(v.name, "=", v.varValue)
- #Evaluate the objective function
- total_calories = 528*4 + 348*2 + 492*2
- print("Maximum Calories Burned: ",total_calories)
- ############OUTPUT###############
- #Running = 4.0
- #Swimming = 2.0
- #Walking = 6.0
- #Maximum Calories Burned: 3792
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement