@@ -90,12 +90,14 @@ for this example is found in :download:`WhiskasModel1.py <../../../examples/Whis
9090The start of the your file should then be headed with a short commenting section outlining the purpose of the program. For example:
9191
9292.. literalinclude :: ../../../examples/WhiskasModel1.py
93- :lines: 1-5
93+ :start-after: # BEGIN file_docstring
94+ :end-before: # END file_docstring
9495
9596Then you will import PuLP's functions for use in your code:
9697
9798.. literalinclude :: ../../../examples/WhiskasModel1.py
98- :lines: 7-8
99+ :start-after: # BEGIN import_pulp
100+ :end-before: # END import_pulp
99101
100102A variable called ``prob `` (although its name is not important) is
101103created 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
104106type of LP you are trying to solve:
105107
106108.. literalinclude :: ../../../examples/WhiskasModel1.py
107- :lines: 10-11
109+ :start-after: # BEGIN define_prob
110+ :end-before: # END define_prob
108111
109112The problem variables ``x1 `` and ``x2 `` are created using the
110113:class: `~pulp.LpVariable ` class. It has four parameters, the first is the
@@ -132,15 +135,17 @@ or::
132135To explicitly create the two variables needed for this problem:
133136
134137.. literalinclude :: ../../../examples/WhiskasModel1.py
135- :lines: 13-15
138+ :start-after: # BEGIN chicken_beef_vars
139+ :end-before: # END chicken_beef_vars
136140
137141The variable ``prob `` now begins collecting problem data with the
138142``+= `` operator. The objective function is logically entered first, with
139143an important comma ``, `` at the end of the statement and a short string
140144explaining what this objective function is:
141145
142146.. literalinclude :: ../../../examples/WhiskasModel1.py
143- :lines: 17-18
147+ :start-after: # BEGIN obj_func
148+ :end-before: # END obj_func
144149
145150The constraints are now entered (Note: any "non-negative"
146151constraints 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
150155the cause of that constraint:
151156
152157.. literalinclude :: ../../../examples/WhiskasModel1.py
153- :lines: 20-25
158+ :start-after: # BEGIN constraints
159+ :end-before: # END constraints
154160
155161Now that all the problem data is entered, the :meth: `~pulp.LpProblem.writeLP ` function
156162can 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):
166172
167173
168174.. literalinclude :: ../../../examples/WhiskasModel1.py
169- :lines: 27-28
175+ :start-after: # BEGIN lp_file
176+ :end-before: # END lp_file
170177
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
173180used to specify which solver to use (e.g ``prob.solve(CPLEX()) `` ):
174181
175182.. literalinclude :: ../../../examples/WhiskasModel1.py
176- :lines: 30-31
183+ :start-after: # BEGIN prob_solve
184+ :end-before: # END prob_solve
177185
178186Now the results of the solver call can be displayed as output to
179187us. Firstly, we request the status of the solution, which can be one of
@@ -184,13 +192,15 @@ to its significant text meaning using the
184192:attr: `~pulp.constants.LpStatus ` is a dictionary(:obj: `dict `), its input must be in square brackets:
185193
186194.. literalinclude :: ../../../examples/WhiskasModel1.py
187- :lines: 33-34
195+ :start-after: # BEGIN print_status
196+ :end-before: # END print_status
188197
189198The variables and their resolved optimum values can now be printed
190199to the screen.
191200
192201.. literalinclude :: ../../../examples/WhiskasModel1.py
193- :lines: 36-38
202+ :start-after: # BEGIN print_var_value
203+ :end-before: # END print_var_value
194204
195205The ``for `` loop makes ``variable `` cycle through all
196206the 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
207217``prob ``:
208218
209219.. literalinclude :: ../../../examples/WhiskasModel1.py
210- :lines: 40-41
220+ :start-after: # BEGIN print_obj
221+ :end-before: # END print_obj
211222
212223Running this file should then produce the output to show that
213224Chicken 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
291302purpose, and the author name and date. Importing of the PuLP functions is also done in the same way:
292303
293304.. literalinclude :: ../../../examples/WhiskasModel2.py
294- :lines: 1-8
305+ :start-after: # BEGIN docstring_imports
306+ :end-before: # END docstring_imports
295307
296308Next, before the ``prob `` variable or type of problem are defined,
297309the 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
299311percentage of each of the four nutrients. These values are clearly laid
300312out and could easily be changed by someone with little knowledge of
301313programming. The ingredients are the reference keys, with the numbers as
302314the data.
303315
304316.. literalinclude :: ../../../examples/WhiskasModel2.py
305- :lines: 10-61
317+ :start-after: # BEGIN problem_data
318+ :end-before: # END problem_data
306319
307320The ``prob `` variable is created to contain the formulation, and the
308321usual parameters are passed into :class: `~pulp.LpProblem `.
309322
310323.. literalinclude :: ../../../examples/WhiskasModel2.py
311- :lines: 63-64
324+ :start-after: # BEGIN define_prob
325+ :end-before: # END define_prob
312326
313327A dictionary called ``ingredient_vars `` is created which contains
314328the LP variables, with their defined lower bound of zero. The reference
315329keys to the dictionary are the Ingredient names, and the data is
316330``Ingr_IngredientName ``. (e.g. MUTTON: Ingr_MUTTON)
317331
318332.. literalinclude :: ../../../examples/WhiskasModel2.py
319- :lines: 66-67
333+ :start-after: # BEGIN ingredient_vars
334+ :end-before: # END ingredient_vars
320335
321336Since ``costs `` and ``ingredient_vars `` are now dictionaries with the
322337reference 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
325340entered and assigned a name:
326341
327342.. literalinclude :: ../../../examples/WhiskasModel2.py
328- :lines: 69-73
343+ :start-after: # BEGIN obj_function
344+ :end-before: # END obj_function
329345
330346Further list comprehensions are used to define the other 5 constraints, which are also each given names describing them.
331347
332348.. literalinclude :: ../../../examples/WhiskasModel2.py
333- :lines: 75-92
349+ :start-after: # BEGIN constraints
350+ :end-before: # END constraints
334351
335352Following this, the :meth: `~pulp.LpProblem.writeLP ` line etc follow exactly the same as
336353in the simplified example.
0 commit comments