Skip to content

Instantly share code, notes, and snippets.

@matiasherranz
Created December 4, 2016 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matiasherranz/3efe0ec42a9589d6d4038b69122a31df to your computer and use it in GitHub Desktop.
Save matiasherranz/3efe0ec42a9589d6d4038b69122a31df to your computer and use it in GitHub Desktop.
import numpy as np
import pulp
# create the LP object, set up as a maximization problem
prob = pulp.LpProblem('Giapetto', pulp.LpMaximize)
# set up decision variables
soldiers = pulp.LpVariable('soldiers', lowBound=0, cat='Integer')
trains = pulp.LpVariable('trains', lowBound=0, cat='Integer')
# model weekly production costs
raw_material_costs = 10 * soldiers + 9 * trains
variable_costs = 14 * soldiers + 10 * trains
# model weekly revenues from toy sales
revenues = 27 * soldiers + 21 * trains
# use weekly profit as the objective function to maximize
profit = revenues - (raw_material_costs + variable_costs)
prob += profit # here's where we actually add it to the obj function
# add constraints for available labor hours
carpentry_hours = soldiers + trains
prob += (carpentry_hours <= 80)
finishing_hours = 2*soldiers + trains
prob += (finishing_hours <= 100)
# add constraint representing demand for soldiers
prob += (soldiers <= 40)
print(prob)
# solve the LP using the default solver
optimization_result = prob.solve()
# make sure we got an optimal solution
assert optimization_result == pulp.LpStatusOptimal
# display the results
for var in (soldiers, trains):
print('Optimal weekly number of {} to produce: {:1.0f}'.format(var.name, var.value()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment