Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. import pyomo.environ as pe
  2. infinity = float('inf')
  3.  
  4. model = pe.AbstractModel()
  5. data = pe.DataPortal()
  6.  
  7. # Sets
  8. model.F = pe.Set()
  9. model.N = pe.Set()
  10.  
  11. # Parameters
  12. model.c = pe.Param(model.F, within=pe.PositiveReals) # cost of serving of each food F
  13. model.a = pe.Param(model.F,model.N, within=pe.NonNegativeReals) # amount of nutirent N in food F
  14. model.Nmin = pe.Param(model.N, within=pe.NonNegativeReals, default=0.0) # minimum level of nutrient N
  15. model.Nmax = pe.Param(model.N, within=pe.NonNegativeReals, default=infinity) # maximum level of nutrient N
  16. model.V = pe.Param(model.F, within=pe.PositiveReals) # volume per serving of food F
  17. model.Vmax = pe.Param(within=pe.PositiveReals) # maximum volume of consumed food
  18.  
  19. # Variables
  20. model.x = pe.Var(model.F,within=pe.NonNegativeIntegers) # number of servings of food i to consume
  21.  
  22. @model.Constraint(model.N) # limit nutrient consumption for each nutrient
  23. def _nut_min_limit(m,j):
  24. value = sum(m.a[i,j]*m.x[i] for i in model.F)
  25. return m.Nmin[j] <= value <= m.Nmax[j]
  26.  
  27. @model.Constraint() # limit the voluem of food consumed
  28. def _vol_max_limit(m):
  29. return sum(m.V[i]*m.x[i] for i in model.F) <= m.Vmax
  30.  
  31. @model.Constraint() # consumption lower bound
  32. def _min_consumption(m):
  33. return (m.x[i] for i in model.F) >= 0
  34.  
  35. @model.Objective()
  36. def _obj(m):
  37. return sum(m.c[i]*m.x[i] for i in model.F)
  38.  
  39. data.load(filename='diet.dat')
  40.  
  41. solver = pe.SolverFactory('glpk')
  42. solver.solve(model)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement