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
+5-5Lines changed: 5 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -62,7 +62,7 @@ \subsection*{Build the model}
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 = pyo.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.
Copy file name to clipboardExpand all lines: examples/doc/samples/case_studies/diet/README.txt
+13-13Lines changed: 13 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ to import the Pyomo package for use in the code. The next step is to create an
14
14
15
15
{{{
16
16
#!python
17
-
model = pyo.AbstractModel()
17
+
model = pyo.AbstractModel()
18
18
}}}
19
19
The rest of our work will be contained within this object.
20
20
@@ -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