Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pyomo.environ as pe
- infinity = float('inf')
- model = pe.AbstractModel()
- data = pe.DataPortal()
- # Sets
- model.F = pe.Set()
- model.N = pe.Set()
- # Parameters
- model.c = pe.Param(model.F, within=pe.PositiveReals) # cost of serving of each food F
- model.a = pe.Param(model.F,model.N, within=pe.NonNegativeReals) # amount of nutirent N in food F
- model.Nmin = pe.Param(model.N, within=pe.NonNegativeReals, default=0.0) # minimum level of nutrient N
- model.Nmax = pe.Param(model.N, within=pe.NonNegativeReals, default=infinity) # maximum level of nutrient N
- model.V = pe.Param(model.F, within=pe.PositiveReals) # volume per serving of food F
- model.Vmax = pe.Param(within=pe.PositiveReals) # maximum volume of consumed food
- # Variables
- model.x = pe.Var(model.F,within=pe.NonNegativeIntegers) # number of servings of food i to consume
- @model.Constraint(model.N) # limit nutrient consumption for each nutrient
- def _nut_min_limit(m,j):
- value = sum(m.a[i,j]*m.x[i] for i in model.F)
- return m.Nmin[j] <= value <= m.Nmax[j]
- @model.Constraint() # limit the voluem of food consumed
- def _vol_max_limit(m):
- return sum(m.V[i]*m.x[i] for i in model.F) <= m.Vmax
- @model.Constraint() # consumption lower bound
- def _min_consumption(m):
- return (m.x[i] for i in model.F) >= 0
- @model.Objective()
- def _obj(m):
- return sum(m.c[i]*m.x[i] for i in model.F)
- data.load(filename='diet.dat')
- solver = pe.SolverFactory('glpk')
- solver.solve(model)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement