-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiet.html
362 lines (329 loc) · 28.4 KB
/
diet.html
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>diet.py — DOcplex.MP: Mathematical Programming Modeling for Python V2.14 documentation</title>
<link rel="stylesheet" href="_static/bizstyle.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/language_data.js"></script>
<script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/javascript" src="_static/bizstyle.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="production.py" href="production.html" />
<link rel="prev" title="Examples of mathematical programming" href="samples.html" />
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<!--[if lt IE 9]>
<script type="text/javascript" src="_static/css3-mediaqueries.js"></script>
<![endif]-->
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="production.html" title="production.py"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="samples.html" title="Examples of mathematical programming"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">DOcplex.MP: Mathematical Programming Modeling for Python V2.14 documentation</a> »</li>
<li class="nav-item nav-item-1"><a href="samples.html" accesskey="U">Examples of mathematical programming</a> »</li>
</ul>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h4>Previous topic</h4>
<p class="topless"><a href="samples.html"
title="previous chapter">Examples of mathematical programming</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="production.html"
title="next chapter">production.py</a></p>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="diet-py">
<h1>diet.py<a class="headerlink" href="#diet-py" title="Permalink to this headline">¶</a></h1>
<p>Can linear programming save money on the food budget of the US Army without damaging the nutritional health of members of the armed forces?</p>
<p>This example solves a simple variation of the well-known diet problem that was posed by George Stigler and George Dantzig: how to choose
foods that satisfy nutritional requirements while minimizing costs or maximizing satiety.</p>
<p>Stigler solved his model “by hand” because technology at the time did not yet support more sophisticated methods.
However, in 1947, Jack Laderman, of the US National Bureau of Standards, applied the simplex method (an algorithm
that was recently proposed by George Dantzig)
to Stigler’s model. Laderman and his team of nine linear programmers, working on desk calculators, showed that Stigler’s heuristic approximation was very
close to optimal (only 24 cents per year over the optimum found by the simplex method) and thus demonstrated the practicality of the simplex method
on large-scale, real-world problems.</p>
<p>The problem that is solved in this example is to minimize the cost of a diet that satisfies certain nutritional constraints.</p>
<div class="highlight-python notranslate"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121</pre></div></td><td class="code"><div class="highlight"><pre><span></span><span class="c1"># --------------------------------------------------------------------------</span>
<span class="c1"># Source file provided under Apache License, Version 2.0, January 2004,</span>
<span class="c1"># http://www.apache.org/licenses/</span>
<span class="c1"># (c) Copyright IBM Corp. 2015, 2018</span>
<span class="c1"># --------------------------------------------------------------------------</span>
<span class="c1"># The goal of the diet problem is to select a set of foods that satisfies</span>
<span class="c1"># a set of daily nutritional requirements at minimal cost.</span>
<span class="c1"># Source of data: http://www.neos-guide.org/content/diet-problem-solver</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">namedtuple</span>
<span class="kn">from</span> <span class="nn">docplex.mp.model</span> <span class="kn">import</span> <span class="n">Model</span>
<span class="kn">from</span> <span class="nn">docplex.util.environment</span> <span class="kn">import</span> <span class="n">get_environment</span>
<span class="c1"># ----------------------------------------------------------------------------</span>
<span class="c1"># Initialize the problem data</span>
<span class="c1"># ----------------------------------------------------------------------------</span>
<span class="n">FOODS</span> <span class="o">=</span> <span class="p">[</span>
<span class="p">(</span><span class="s2">"Roasted Chicken"</span><span class="p">,</span> <span class="mf">0.84</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
<span class="p">(</span><span class="s2">"Spaghetti W/ Sauce"</span><span class="p">,</span> <span class="mf">0.78</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
<span class="p">(</span><span class="s2">"Tomato,Red,Ripe,Raw"</span><span class="p">,</span> <span class="mf">0.27</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
<span class="p">(</span><span class="s2">"Apple,Raw,W/Skin"</span><span class="p">,</span> <span class="o">.</span><span class="mi">24</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
<span class="p">(</span><span class="s2">"Grapes"</span><span class="p">,</span> <span class="mf">0.32</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
<span class="p">(</span><span class="s2">"Chocolate Chip Cookies"</span><span class="p">,</span> <span class="mf">0.03</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
<span class="p">(</span><span class="s2">"Lowfat Milk"</span><span class="p">,</span> <span class="mf">0.23</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
<span class="p">(</span><span class="s2">"Raisin Brn"</span><span class="p">,</span> <span class="mf">0.34</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
<span class="p">(</span><span class="s2">"Hotdog"</span><span class="p">,</span> <span class="mf">0.31</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<span class="p">]</span>
<span class="n">NUTRIENTS</span> <span class="o">=</span> <span class="p">[</span>
<span class="p">(</span><span class="s2">"Calories"</span><span class="p">,</span> <span class="mi">2000</span><span class="p">,</span> <span class="mi">2500</span><span class="p">),</span>
<span class="p">(</span><span class="s2">"Calcium"</span><span class="p">,</span> <span class="mi">800</span><span class="p">,</span> <span class="mi">1600</span><span class="p">),</span>
<span class="p">(</span><span class="s2">"Iron"</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">30</span><span class="p">),</span>
<span class="p">(</span><span class="s2">"Vit_A"</span><span class="p">,</span> <span class="mi">5000</span><span class="p">,</span> <span class="mi">50000</span><span class="p">),</span>
<span class="p">(</span><span class="s2">"Dietary_Fiber"</span><span class="p">,</span> <span class="mi">25</span><span class="p">,</span> <span class="mi">100</span><span class="p">),</span>
<span class="p">(</span><span class="s2">"Carbohydrates"</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">300</span><span class="p">),</span>
<span class="p">(</span><span class="s2">"Protein"</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="mi">100</span><span class="p">)</span>
<span class="p">]</span>
<span class="n">FOOD_NUTRIENTS</span> <span class="o">=</span> <span class="p">[</span>
<span class="p">(</span><span class="s2">"Roasted Chicken"</span><span class="p">,</span> <span class="mf">277.4</span><span class="p">,</span> <span class="mf">21.9</span><span class="p">,</span> <span class="mf">1.8</span><span class="p">,</span> <span class="mf">77.4</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">42.2</span><span class="p">),</span>
<span class="p">(</span><span class="s2">"Spaghetti W/ Sauce"</span><span class="p">,</span> <span class="mf">358.2</span><span class="p">,</span> <span class="mf">80.2</span><span class="p">,</span> <span class="mf">2.3</span><span class="p">,</span> <span class="mf">3055.2</span><span class="p">,</span> <span class="mf">11.6</span><span class="p">,</span> <span class="mf">58.3</span><span class="p">,</span> <span class="mf">8.2</span><span class="p">),</span>
<span class="p">(</span><span class="s2">"Tomato,Red,Ripe,Raw"</span><span class="p">,</span> <span class="mf">25.8</span><span class="p">,</span> <span class="mf">6.2</span><span class="p">,</span> <span class="mf">0.6</span><span class="p">,</span> <span class="mf">766.3</span><span class="p">,</span> <span class="mf">1.4</span><span class="p">,</span> <span class="mf">5.7</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
<span class="p">(</span><span class="s2">"Apple,Raw,W/Skin"</span><span class="p">,</span> <span class="mf">81.4</span><span class="p">,</span> <span class="mf">9.7</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">,</span> <span class="mf">73.1</span><span class="p">,</span> <span class="mf">3.7</span><span class="p">,</span> <span class="mi">21</span><span class="p">,</span> <span class="mf">0.3</span><span class="p">),</span>
<span class="p">(</span><span class="s2">"Grapes"</span><span class="p">,</span> <span class="mf">15.1</span><span class="p">,</span> <span class="mf">3.4</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">,</span> <span class="mi">24</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">,</span> <span class="mf">4.1</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">),</span>
<span class="p">(</span><span class="s2">"Chocolate Chip Cookies"</span><span class="p">,</span> <span class="mf">78.1</span><span class="p">,</span> <span class="mf">6.2</span><span class="p">,</span> <span class="mf">0.4</span><span class="p">,</span> <span class="mf">101.8</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">9.3</span><span class="p">,</span> <span class="mf">0.9</span><span class="p">),</span>
<span class="p">(</span><span class="s2">"Lowfat Milk"</span><span class="p">,</span> <span class="mf">121.2</span><span class="p">,</span> <span class="mf">296.7</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">,</span> <span class="mf">500.2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">11.7</span><span class="p">,</span> <span class="mf">8.1</span><span class="p">),</span>
<span class="p">(</span><span class="s2">"Raisin Brn"</span><span class="p">,</span> <span class="mf">115.1</span><span class="p">,</span> <span class="mf">12.9</span><span class="p">,</span> <span class="mf">16.8</span><span class="p">,</span> <span class="mf">1250.2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mf">27.9</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span>
<span class="p">(</span><span class="s2">"Hotdog"</span><span class="p">,</span> <span class="mf">242.1</span><span class="p">,</span> <span class="mf">23.5</span><span class="p">,</span> <span class="mf">2.3</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">18</span><span class="p">,</span> <span class="mf">10.4</span><span class="p">)</span>
<span class="p">]</span>
<span class="n">Food</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s2">"Food"</span><span class="p">,</span> <span class="p">[</span><span class="s2">"name"</span><span class="p">,</span> <span class="s2">"unit_cost"</span><span class="p">,</span> <span class="s2">"qmin"</span><span class="p">,</span> <span class="s2">"qmax"</span><span class="p">])</span>
<span class="n">Nutrient</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s2">"Nutrient"</span><span class="p">,</span> <span class="p">[</span><span class="s2">"name"</span><span class="p">,</span> <span class="s2">"qmin"</span><span class="p">,</span> <span class="s2">"qmax"</span><span class="p">])</span>
<span class="c1"># ----------------------------------------------------------------------------</span>
<span class="c1"># Build the model</span>
<span class="c1"># ----------------------------------------------------------------------------</span>
<span class="k">def</span> <span class="nf">build_diet_model</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">'diet'</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="n">ints</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">'ints'</span><span class="p">,</span> <span class="bp">False</span><span class="p">)</span>
<span class="c1"># Create tuples with named fields for foods and nutrients</span>
<span class="n">foods</span> <span class="o">=</span> <span class="p">[</span><span class="n">Food</span><span class="p">(</span><span class="o">*</span><span class="n">f</span><span class="p">)</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">FOODS</span><span class="p">]</span>
<span class="n">nutrients</span> <span class="o">=</span> <span class="p">[</span><span class="n">Nutrient</span><span class="p">(</span><span class="o">*</span><span class="n">row</span><span class="p">)</span> <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">NUTRIENTS</span><span class="p">]</span>
<span class="n">food_nutrients</span> <span class="o">=</span> <span class="p">{(</span><span class="n">fn</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">nutrients</span><span class="p">[</span><span class="n">n</span><span class="p">]</span><span class="o">.</span><span class="n">name</span><span class="p">):</span>
<span class="n">fn</span><span class="p">[</span><span class="mi">1</span> <span class="o">+</span> <span class="n">n</span><span class="p">]</span> <span class="k">for</span> <span class="n">fn</span> <span class="ow">in</span> <span class="n">FOOD_NUTRIENTS</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">NUTRIENTS</span><span class="p">))}</span>
<span class="c1"># Model</span>
<span class="n">mdl</span> <span class="o">=</span> <span class="n">Model</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="c1"># Decision variables, limited to be >= Food.qmin and <= Food.qmax</span>
<span class="n">ftype</span> <span class="o">=</span> <span class="n">mdl</span><span class="o">.</span><span class="n">integer_vartype</span> <span class="k">if</span> <span class="n">ints</span> <span class="k">else</span> <span class="n">mdl</span><span class="o">.</span><span class="n">continuous_vartype</span>
<span class="n">qty</span> <span class="o">=</span> <span class="n">mdl</span><span class="o">.</span><span class="n">var_dict</span><span class="p">(</span><span class="n">foods</span><span class="p">,</span> <span class="n">ftype</span><span class="p">,</span> <span class="n">lb</span><span class="o">=</span><span class="k">lambda</span> <span class="n">f</span><span class="p">:</span> <span class="n">f</span><span class="o">.</span><span class="n">qmin</span><span class="p">,</span> <span class="n">ub</span><span class="o">=</span><span class="k">lambda</span> <span class="n">f</span><span class="p">:</span> <span class="n">f</span><span class="o">.</span><span class="n">qmax</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="k">lambda</span> <span class="n">f</span><span class="p">:</span> <span class="s2">"q_</span><span class="si">%s</span><span class="s2">"</span> <span class="o">%</span> <span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="c1"># Limit range of nutrients, and mark them as KPIs</span>
<span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">nutrients</span><span class="p">:</span>
<span class="n">amount</span> <span class="o">=</span> <span class="n">mdl</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">qty</span><span class="p">[</span><span class="n">f</span><span class="p">]</span> <span class="o">*</span> <span class="n">food_nutrients</span><span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">n</span><span class="o">.</span><span class="n">name</span><span class="p">]</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">foods</span><span class="p">)</span>
<span class="n">mdl</span><span class="o">.</span><span class="n">add_range</span><span class="p">(</span><span class="n">n</span><span class="o">.</span><span class="n">qmin</span><span class="p">,</span> <span class="n">amount</span><span class="p">,</span> <span class="n">n</span><span class="o">.</span><span class="n">qmax</span><span class="p">)</span>
<span class="n">mdl</span><span class="o">.</span><span class="n">add_kpi</span><span class="p">(</span><span class="n">amount</span><span class="p">,</span> <span class="n">publish_name</span><span class="o">=</span><span class="s2">"Total </span><span class="si">%s</span><span class="s2">"</span> <span class="o">%</span> <span class="n">n</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="c1"># Minimize cost</span>
<span class="n">total_cost</span> <span class="o">=</span> <span class="n">mdl</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">qty</span><span class="p">[</span><span class="n">f</span><span class="p">]</span> <span class="o">*</span> <span class="n">f</span><span class="o">.</span><span class="n">unit_cost</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">foods</span><span class="p">)</span>
<span class="n">mdl</span><span class="o">.</span><span class="n">add_kpi</span><span class="p">(</span><span class="n">total_cost</span><span class="p">,</span> <span class="s1">'Total cost'</span><span class="p">)</span>
<span class="c1"># add a functional KPI , taking a model and a solution as argument</span>
<span class="c1"># this KPI counts the number of foods used.</span>
<span class="k">def</span> <span class="nf">nb_products</span><span class="p">(</span><span class="n">mdl_</span><span class="p">,</span> <span class="n">s_</span><span class="p">):</span>
<span class="n">qvs</span> <span class="o">=</span> <span class="n">mdl_</span><span class="o">.</span><span class="n">find_matching_vars</span><span class="p">(</span><span class="n">pattern</span><span class="o">=</span><span class="s2">"q_"</span><span class="p">)</span>
<span class="k">return</span> <span class="nb">sum</span><span class="p">(</span><span class="mi">1</span> <span class="k">for</span> <span class="n">qv</span> <span class="ow">in</span> <span class="n">qvs</span> <span class="k">if</span> <span class="n">s_</span><span class="p">[</span><span class="n">qv</span><span class="p">]</span> <span class="o">>=</span> <span class="mf">1e-5</span><span class="p">)</span>
<span class="n">mdl</span><span class="o">.</span><span class="n">add_kpi</span><span class="p">(</span><span class="n">nb_products</span><span class="p">,</span> <span class="s1">'Nb foods'</span><span class="p">)</span>
<span class="n">mdl</span><span class="o">.</span><span class="n">minimize</span><span class="p">(</span><span class="n">total_cost</span><span class="p">)</span>
<span class="k">return</span> <span class="n">mdl</span>
<span class="c1"># ----------------------------------------------------------------------------</span>
<span class="c1"># Solve the model and display the result</span>
<span class="c1"># ----------------------------------------------------------------------------</span>
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</span><span class="p">:</span>
<span class="n">mdl</span> <span class="o">=</span> <span class="n">build_diet_model</span><span class="p">(</span><span class="n">ints</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">log_output</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">float_precision</span><span class="o">=</span><span class="mi">6</span><span class="p">)</span>
<span class="n">mdl</span><span class="o">.</span><span class="n">print_information</span><span class="p">()</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">mdl</span><span class="o">.</span><span class="n">solve</span><span class="p">()</span>
<span class="k">if</span> <span class="n">s</span><span class="p">:</span>
<span class="n">qty_vars</span> <span class="o">=</span> <span class="n">mdl</span><span class="o">.</span><span class="n">find_matching_vars</span><span class="p">(</span><span class="n">pattern</span><span class="o">=</span><span class="s2">"q_"</span><span class="p">)</span>
<span class="k">for</span> <span class="n">fv</span> <span class="ow">in</span> <span class="n">qty_vars</span><span class="p">:</span>
<span class="n">food_name</span> <span class="o">=</span> <span class="n">fv</span><span class="o">.</span><span class="n">name</span><span class="p">[</span><span class="mi">2</span><span class="p">:]</span>
<span class="k">print</span><span class="p">(</span><span class="s2">"Buy {0:<25} = {1:9.6g}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">food_name</span><span class="p">,</span> <span class="n">fv</span><span class="o">.</span><span class="n">solution_value</span><span class="p">))</span>
<span class="n">mdl</span><span class="o">.</span><span class="n">report_kpis</span><span class="p">()</span>
<span class="c1"># Save the CPLEX solution as "solution.json" program output</span>
<span class="k">with</span> <span class="n">get_environment</span><span class="p">()</span><span class="o">.</span><span class="n">get_output_stream</span><span class="p">(</span><span class="s2">"solution.json"</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
<span class="n">mdl</span><span class="o">.</span><span class="n">solution</span><span class="o">.</span><span class="n">export</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="s2">"json"</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s2">"* model has no solution"</span><span class="p">)</span>
</pre></div>
</td></tr></table></div>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="production.html" title="production.py"
>next</a> |</li>
<li class="right" >
<a href="samples.html" title="Examples of mathematical programming"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">DOcplex.MP: Mathematical Programming Modeling for Python V2.14 documentation</a> »</li>
<li class="nav-item nav-item-1"><a href="samples.html" >Examples of mathematical programming</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2016-2019, IBM®.
</div>
</body>
</html>