You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: examples/doc/samples/case_studies/diet/DietProblem.tex
+6-6
Original file line number
Diff line number
Diff line change
@@ -54,15 +54,15 @@ \subsection*{Build the model}
54
54
55
55
The comma indicates that this parameter is over two different sets, and thus is in two dimensions. When we create the data file, we will be able to fill in how much of each nutrient each food contains.
56
56
57
-
At this point we have defined our sets and parameters. However, we have yet to cosnider the amount of food to be bought and eaten. This is the variable weâre trying to solve for, and thus we create an object of the variable class. Since this is just recording how much food to purchase, we create a one dimensional variable over food:
57
+
At this point we have defined our sets and parameters. However, we have yet to consider the amount of food to be bought and eaten. This is the variable weâre trying to solve for, and thus we create an object of the variable class. Since this is just recording how much food to purchase, we create a one dimensional variable over food:
58
58
59
59
\begin{verbatim}model.amount=Var(model.foods, within = NonNegativeReals) \end{verbatim}
60
60
61
61
We restrict our domain to the non-negative reals. If we accepted negative numbers than the model could tell us to buy negative amounts of food, which is an unrealistic---and thus useless---model. We could further restrict the domain to the integers to make it more realistic, but that would make the problem much harder for little gain: if this model is used on a large scale than the difference between the integer solution and the non-integer solution is often irrelevant.
62
62
63
63
At this point we must start defining the rules associated with our parameters and variables. We begin with the most important rule, the cost rule, which will tell the model to try and minimize the overall cost. Logically, the total cost is going to be the sum of how much is spent on each food, and that value in turn is going to be determined by the cost of the food and how much of it is purchased. For example, if three \$5 hamburgers and two \$1 apples are purchased, than the total cost would be $3\cdot5 + 2\cdot1 = 17$. Note that this process is the same as taking the dot product of the amounts vector and the costs vector.
64
64
65
-
To input this, we must define the cost rule, which we creatively call costRule as
65
+
To input this, we must define the cost rule, which we creatively call costRule as
66
66
67
67
\begin{verbatim}def costRule(model):
68
68
return sum(model.costs[n]*model.amount[n] for n in model.foods)
@@ -75,10 +75,10 @@ \subsection*{Build the model}
75
75
76
76
This line defines the objective of the model as the costRule, which Pyomo interprets as the value it needs to minimize; in this case it will minimize our costs. Also, as a note, we defined the objective as ``model.cost'' which is not to be confused with the parameter we defined earlier as ``model.costs,'' despite their similar names. These are two different values and accidentally giving them the same name will cause problems when trying to solve the problem.
77
77
78
-
We must also create a rule for the volume consumed. The construction of this rule is similar to the cost rule as once again we take the dot product, this time between the volume and amount vectors.
78
+
We must also create a rule for the volume consumed. The construction of this rule is similar to the cost rule as once again we take the dot product, this time between the volume and amount vectors.
79
79
80
80
\begin{verbatim}def volumeRule(model):
81
-
return sum(model.volumes[n]*model.amount[n] for n in
81
+
return sum(model.volumes[n]*model.amount[n] for n in
82
82
model.foods) <= model.max_volume
83
83
84
84
model.volume = Constraint(rule=volumeRule)
@@ -90,7 +90,7 @@ \subsection*{Build the model}
90
90
91
91
\begin{verbatim}
92
92
def nutrientRule(n, model):
93
-
value = sum(model.nutrient_value[n,f]*model.amount[f]
93
+
value = sum(model.nutrient_value[n,f]*model.amount[f]
The amount of spaces between each element is irrelevant (as long as there is at least one) so the matrix should be formatted for ease of reading.
162
162
163
-
Now that we have finished both the model and the data file save them both. It's convention to give the model file a .py extension and the data file a .dat extension.
163
+
Now that we have finished both the model and the data file save them both. It's convention to give the model file a .py extension and the data file a .dat extension.
The comma indicates that this parameter is over two different sets, and thus is in two dimensions. When we create the data file, we will be able to fill in how much of each nutrient each food contains.
70
70
71
-
At this point we have defined our sets and parameters. However, we have yet to cosnider the amount of food to be bought and eaten. This is the variable we're trying to solve for, and thus we create an object of the variable class. Since this is just recording how much food to purchase, we create a one dimensional variable over food:
71
+
At this point we have defined our sets and parameters. However, we have yet to consider the amount of food to be bought and eaten. This is the variable we're trying to solve for, and thus we create an object of the variable class. Since this is just recording how much food to purchase, we create a one dimensional variable over food:
72
72
73
73
{{{
74
74
#!python
@@ -79,7 +79,7 @@ We restrict our domain to the non-negative reals. If we accepted negative numbe
79
79
80
80
At this point we must start defining the rules associated with our parameters and variables. We begin with the most important rule, the cost rule, which will tell the model to try and minimize the overall cost. Logically, the total cost is going to be the sum of how much is spent on each food, and that value in turn is going to be determined by the cost of the food and how much of it is purchased. For example, if three !$5 hamburgers and two !$1 apples are purchased, than the total cost would be 3*5 + 2*1 = 17. Note that this process is the same as taking the dot product of the amounts vector and the costs vector.
81
81
82
-
To input this, we must define the cost rule, which we creatively call costRule as
82
+
To input this, we must define the cost rule, which we creatively call costRule as
This line defines the objective of the model as the costRule, which Pyomo interprets as the value it needs to minimize; in this case it will minimize our costs. Also, as a note, we defined the objective as "model.cost" which is not to be confused with the parameter we defined earlier as `"model.costs" despite their similar names. These are two different values and accidentally giving them the same name will cause problems when trying to solve the problem.
97
97
98
-
We must also create a rule for the volume consumed. The construction of this rule is similar to the cost rule as once again we take the dot product, this time between the volume and amount vectors.
98
+
We must also create a rule for the volume consumed. The construction of this rule is similar to the cost rule as once again we take the dot product, this time between the volume and amount vectors.
99
99
100
100
{{{
101
101
#!python
@@ -112,7 +112,7 @@ Finally, we need to add the constraint that ensures we obtain proper amounts of
112
112
{{{
113
113
#!python
114
114
def nutrientRule(n, model):
115
-
value = sum(model.nutrient_value[n,f]*model.amount[f]
115
+
value = sum(model.nutrient_value[n,f]*model.amount[f]
The amount of spaces between each element is irrelevant (as long as there is at least one) so the matrix should be formatted for ease of reading.
181
181
182
-
Now that we have finished both the model and the data file save them both. It's convention to give the model file a .py extension and the data file a .dat extension.
182
+
Now that we have finished both the model and the data file save them both. It's convention to give the model file a .py extension and the data file a .dat extension.
183
183
184
184
== Solution ==
185
185
@@ -193,7 +193,7 @@ Using Pyomo we quickly find the solution to our diet problem. Simply run Pyomo
0 commit comments