@@ -90,12 +90,14 @@ for this example is found in :download:`WhiskasModel1.py <../../../examples/Whis
90
90
The start of the your file should then be headed with a short commenting section outlining the purpose of the program. For example:
91
91
92
92
.. literalinclude :: ../../../examples/WhiskasModel1.py
93
- :lines: 1-5
93
+ :start-after: # BEGIN file_docstring
94
+ :end-before: # END file_docstring
94
95
95
96
Then you will import PuLP's functions for use in your code:
96
97
97
98
.. literalinclude :: ../../../examples/WhiskasModel1.py
98
- :lines: 7-8
99
+ :start-after: # BEGIN import_pulp
100
+ :end-before: # END import_pulp
99
101
100
102
A variable called ``prob `` (although its name is not important) is
101
103
created using the :class: `~pulp.LpProblem ` function. It has two parameters, the first
@@ -104,7 +106,8 @@ parameter being either ``LpMinimize`` or ``LpMaximize`` depending on the
104
106
type of LP you are trying to solve:
105
107
106
108
.. literalinclude :: ../../../examples/WhiskasModel1.py
107
- :lines: 10-11
109
+ :start-after: # BEGIN define_prob
110
+ :end-before: # END define_prob
108
111
109
112
The problem variables ``x1 `` and ``x2 `` are created using the
110
113
:class: `~pulp.LpVariable ` class. It has four parameters, the first is the
@@ -132,15 +135,17 @@ or::
132
135
To explicitly create the two variables needed for this problem:
133
136
134
137
.. literalinclude :: ../../../examples/WhiskasModel1.py
135
- :lines: 13-15
138
+ :start-after: # BEGIN chicken_beef_vars
139
+ :end-before: # END chicken_beef_vars
136
140
137
141
The variable ``prob `` now begins collecting problem data with the
138
142
``+= `` operator. The objective function is logically entered first, with
139
143
an important comma ``, `` at the end of the statement and a short string
140
144
explaining what this objective function is:
141
145
142
146
.. literalinclude :: ../../../examples/WhiskasModel1.py
143
- :lines: 17-18
147
+ :start-after: # BEGIN obj_func
148
+ :end-before: # END obj_func
144
149
145
150
The constraints are now entered (Note: any "non-negative"
146
151
constraints were already included when defining the variables). This is
@@ -150,7 +155,8 @@ comma at the end of the constraint equation and a brief description of
150
155
the cause of that constraint:
151
156
152
157
.. literalinclude :: ../../../examples/WhiskasModel1.py
153
- :lines: 20-25
158
+ :start-after: # BEGIN constraints
159
+ :end-before: # END constraints
154
160
155
161
Now that all the problem data is entered, the :meth: `~pulp.LpProblem.writeLP ` function
156
162
can be used to copy this information into a .lp file into the directory
@@ -166,14 +172,16 @@ seen frequently in Object Oriented software (such as this):
166
172
167
173
168
174
.. literalinclude :: ../../../examples/WhiskasModel1.py
169
- :lines: 27-28
175
+ :start-after: # BEGIN lp_file
176
+ :end-before: # END lp_file
170
177
171
- The LP is solved using the solver that PuLP chooses. The input
172
- brackets after :meth: `~pulp.LpProblem.solve ` are left empty in this case, however they can be
178
+ The LP is solved using the solver that PuLP chooses. The input brackets after
179
+ :meth: `~pulp.LpProblem.solve ` are left empty in this case, however they can be
173
180
used to specify which solver to use (e.g ``prob.solve(CPLEX()) `` ):
174
181
175
182
.. literalinclude :: ../../../examples/WhiskasModel1.py
176
- :lines: 30-31
183
+ :start-after: # BEGIN prob_solve
184
+ :end-before: # END prob_solve
177
185
178
186
Now the results of the solver call can be displayed as output to
179
187
us. Firstly, we request the status of the solution, which can be one of
@@ -184,13 +192,15 @@ to its significant text meaning using the
184
192
:attr: `~pulp.constants.LpStatus ` is a dictionary(:obj: `dict `), its input must be in square brackets:
185
193
186
194
.. literalinclude :: ../../../examples/WhiskasModel1.py
187
- :lines: 33-34
195
+ :start-after: # BEGIN print_status
196
+ :end-before: # END print_status
188
197
189
198
The variables and their resolved optimum values can now be printed
190
199
to the screen.
191
200
192
201
.. literalinclude :: ../../../examples/WhiskasModel1.py
193
- :lines: 36-38
202
+ :start-after: # BEGIN print_var_value
203
+ :end-before: # END print_var_value
194
204
195
205
The ``for `` loop makes ``variable `` cycle through all
196
206
the problem variable names (in this case just ``ChickenPercent `` and
@@ -207,7 +217,8 @@ format to be displayed. :attr:`~pulp.LpProblem.objective` is an attribute of the
207
217
``prob ``:
208
218
209
219
.. literalinclude :: ../../../examples/WhiskasModel1.py
210
- :lines: 40-41
220
+ :start-after: # BEGIN print_obj
221
+ :end-before: # END print_obj
211
222
212
223
Running this file should then produce the output to show that
213
224
Chicken will make up 33.33%, Beef will make up 66.67% and the
@@ -291,32 +302,36 @@ As with last time, it is advisable to head your file with commenting on its
291
302
purpose, and the author name and date. Importing of the PuLP functions is also done in the same way:
292
303
293
304
.. literalinclude :: ../../../examples/WhiskasModel2.py
294
- :lines: 1-8
305
+ :start-after: # BEGIN docstring_imports
306
+ :end-before: # END docstring_imports
295
307
296
308
Next, before the ``prob `` variable or type of problem are defined,
297
309
the key problem data is entered into dictionaries. This includes the
298
- list of Ingredients, followed by the cost of each Ingredient, and it's
310
+ list of Ingredients, followed by the cost of each Ingredient, and its
299
311
percentage of each of the four nutrients. These values are clearly laid
300
312
out and could easily be changed by someone with little knowledge of
301
313
programming. The ingredients are the reference keys, with the numbers as
302
314
the data.
303
315
304
316
.. literalinclude :: ../../../examples/WhiskasModel2.py
305
- :lines: 10-61
317
+ :start-after: # BEGIN problem_data
318
+ :end-before: # END problem_data
306
319
307
320
The ``prob `` variable is created to contain the formulation, and the
308
321
usual parameters are passed into :class: `~pulp.LpProblem `.
309
322
310
323
.. literalinclude :: ../../../examples/WhiskasModel2.py
311
- :lines: 63-64
324
+ :start-after: # BEGIN define_prob
325
+ :end-before: # END define_prob
312
326
313
327
A dictionary called ``ingredient_vars `` is created which contains
314
328
the LP variables, with their defined lower bound of zero. The reference
315
329
keys to the dictionary are the Ingredient names, and the data is
316
330
``Ingr_IngredientName ``. (e.g. MUTTON: Ingr_MUTTON)
317
331
318
332
.. literalinclude :: ../../../examples/WhiskasModel2.py
319
- :lines: 66-67
333
+ :start-after: # BEGIN ingredient_vars
334
+ :end-before: # END ingredient_vars
320
335
321
336
Since ``costs `` and ``ingredient_vars `` are now dictionaries with the
322
337
reference keys as the Ingredient names, the data can be simply extracted
@@ -325,12 +340,14 @@ elements of the resulting list. Thus the objective function is simply
325
340
entered and assigned a name:
326
341
327
342
.. literalinclude :: ../../../examples/WhiskasModel2.py
328
- :lines: 69-73
343
+ :start-after: # BEGIN obj_function
344
+ :end-before: # END obj_function
329
345
330
346
Further list comprehensions are used to define the other 5 constraints, which are also each given names describing them.
331
347
332
348
.. literalinclude :: ../../../examples/WhiskasModel2.py
333
- :lines: 75-92
349
+ :start-after: # BEGIN constraints
350
+ :end-before: # END constraints
334
351
335
352
Following this, the :meth: `~pulp.LpProblem.writeLP ` line etc follow exactly the same as
336
353
in the simplified example.
0 commit comments