-
Notifications
You must be signed in to change notification settings - Fork 0
/
diet.py
48 lines (40 loc) · 1.59 KB
/
diet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from pyomo.environ import *
infinity = float('inf')
model = AbstractModel()
#Sets ###########################
# Foods
model.F = Set()
# Nutrients
model.N = Set()
#Parameters######################
# Cost of each food
model.c = Param(model.F, within=PositiveReals)
# Amount of nutrient in each food
model.a = Param(model.F, model.N, within=NonNegativeReals)
# Lower and upper bound on each nutrient
model.Nmin = Param(model.N, within=NonNegativeReals, default=0.0)
model.Nmax = Param(model.N, within=NonNegativeReals, default=infinity)
# Volume per serving of food
model.V = Param(model.F, within=PositiveReals)
# Maximum volume of food consumed
model.Vmax = Param(default=75.0, within=PositiveReals, mutable=True)
#Variables#######################
# Number of servings consumed of each food
model.x = Var(model.F, within=NonNegativeReals)
#Objective Function
# Minimize the cost of food that is consumed
def cost_rule(model):
return sum(model.c[i]*model.x[i] for i in model.F)
model.cost = Objective(rule=cost_rule)
#Constraints
# Limit nutrient consumption for each nutrient
def nutrient_rule_min(model, j):
return model.Nmin[j] <= sum(model.a[i,j]*model.x[i] for i in model.F)
model.nutrient_limit_min = Constraint(model.N, rule=nutrient_rule_min)
def nutrient_rule_max(model, j):
return sum(model.a[i,j]*model.x[i] for i in model.F) <= model.Nmax[j]
model.nutrient_limit_max = Constraint(model.N, rule=nutrient_rule_max)
# Limit the volume of food consumed
def volume_rule(model):
return sum(model.V[i]*model.x[i] for i in model.F) <= model.Vmax
model.volume = Constraint(rule=volume_rule)