def do_pyomo_work(profit_rate_windows): Products = ['Doors', 'Windows'] ProfitRate = {'Doors': 300, 'Windows': profit_rate_windows} Plants = ['Door Fab', 'Window Fab', 'Assembly'] HoursAvailable = {'Door Fab': 4, 'Window Fab': 12, 'Assembly': 18} HoursPerUnit = { ('Doors', 'Door Fab'): 1, ('Windows', 'Window Fab'): 2, ('Doors', 'Assembly'): 3, ('Windows', 'Assembly'): 2, ('Windows', 'Door Fab'): 0, ('Doors', 'Window Fab'): 0 } #Concrete Model model = ConcreteModel() #Decision Variables model.WeeklyProd = Var(Products, within=NonNegativeReals) #Objective model.obj = Objective(expr=sum(ProfitRate[i] * model.WeeklyProd[i] for i in Products), sense=maximize) def CapacityRule(model, p): """User Defined Capacity Rule Accepts a pyomo Concrete Model as the first positional argument, and a list of Plants as a second positional argument """ return sum(HoursPerUnit[i, p] * model.WeeklyProd[i] for i in Products) <= HoursAvailable[p] model.Capacity = Constraint(Plants, rule=CapacityRule) opt = SolverFactory("glpk") instance = model.create() results = opt.solve(instance) #results.write() return results.Solution()
# Decision variables model.WeeklyProd = Var(Products, within=NonNegativeReals) # Objective model.obj = Objective(expr = sum(ProfitRate[i] * model.WeeklyProd[i] for i in Products), sense=maximize) def CapacityRule(model, p): """User defined capacity rule - Accepts a pyomo Concrete Model as the first positional argument, and a plant index as a second positional argument """ return sum(HoursPerUnit[i,p] * model.WeeklyProd[i] for i in Products) <= HoursAvailable[p] # Constraint model.Capacity = Constraint(Plants, rule=CapacityRule) #This is an optional code path that allows the script to be run outside of #pyomo command-line. For example: python wyndor.py if __name__ == '__main__': #This replicates what the pyomo command-line tools does from coopr.opt import SolverFactory opt = SolverFactory("glpk") instance = model.create() results = opt.solve(instance) #sends results to stdout results.write()
#Objective model.obj = Objective(expr= sum(ProfitRate[i] * model.WeeklyProd[i] for i in Products), sense = maximize) for i in Products: print model.WeeklyProd[i] def CapacityRule(model, p): """User Defined Capacity Rule Accepts a pyomo Concrete Model as the first positional argument, and and a plant index as a second positional argument """ return sum(HoursPerUnit[i,p] * model.WeeklyProd[i] for i in Products) <= HoursAvailable[p] #This statement is what Pyomo needs to generate one constraint for each plant model.Capacity = Constraint(Plants, rule = CapacityRule) #This is an optional code path that allows the script to be run outside of #pyomo command-line. For example: python wyndor.py if __name__ == '__main__': #This replicates what the pyomo command-line tools does from coopr.opt import SolverFactory opt = SolverFactory("glpk") instance = model.create() results = opt.solve(instance) #sends results to stdout results.write()
sense=maximize) # Constraint - uses a Capacity Rule and Land Area function # meaning: for each plant, the resource used should not exceed the total resource available def CapacityRule(model, p): """User defined capacity rule - Accepts a pyomo ConcreteModel as the first positional argument, and a plant index as a second positional argument""" return sum(Resource_required[i,p] * model.SeasonProd[i] for i in Plants) <= Resource_available[p] # meaning: sum of all planting area should not exceed the total area owned by farmer def LandAreaRule(model): """User-defined maximum area""" return sum(model.SeasonProd[i] for i in Plants) <= Area model.Capacity = Constraint(Resource, rule=CapacityRule) model.LandArea = Constraint(rule=LandAreaRule) #This is an optional code path that allows the script to be run outside of #pyomo command-line. For example: python farmer.py if __name__ == '__main__': #This replicates what the pyomo command-line tools does from coopr.opt import SolverFactory opt = SolverFactory("glpk") instance = model.create() results = opt.solve(instance) #sends results to stdout results.write()