|
1 | | -How to export models in PuLP |
2 | | -====================================== |
| 1 | +How to import and export models in PuLP |
| 2 | +========================================== |
3 | 3 |
|
4 | | -Exporting a model can be useful when the building time takes too long or when the model needs to be passed to another computer to solve. Or many other reason. |
5 | | -PuLP offers a way to export a model into a dictionary of a json file. The json file saves enough data to be able to rebuild a new model on reading it. |
| 4 | +Exporting a model can be useful when the building time takes too long or when the model needs to be passed to another computer to solve. Or any other reason. |
| 5 | +PuLP offers two ways to export a model: to an mps file or to a dictionary /json file. Each offers advantages over the other. |
| 6 | + |
| 7 | +**The mps format** is an industry standard. But it is not very flexible so some information cannot be stored. It stores only variables and constraints. It does not store the values of variables. |
| 8 | + |
| 9 | +**The dictionary/ json format** is made to fit how pulp stores the information and so it does not lose information: this format file saves enough data to be able to restore a complete pulp model on reading it. |
| 10 | + |
| 11 | +The interface to import and export for both formats is similar as can be seen in the Example 1 below. |
6 | 12 |
|
7 | 13 | Considerations |
8 | 14 | ------------------ |
9 | 15 |
|
10 | 16 | The following considerations need to be taken into account: |
11 | 17 |
|
12 | 18 | #. Variable names need to be unique. PuLP permits having variable names because it uses an internal code for each one. But we do not export that code. So we identify variables by their name only. |
13 | | -#. Variables are not exported in a grouped way. This means that if you had several `dictionaries of many variables each` you will end up with a very long list of variables. This can be seen in the Example 2. |
14 | | -#. Output information is also written. This means that the status, solution status, the values of variables and shadow prices / reduced costs are exported too. This means that it is possible to export a model that has been solved and then read it again only to see the values of the variables. |
| 19 | +#. Variables are not exported in a grouped way. This means that if you have several `dictionaries of many variables each` you will end up with a very long list of variables. This can be seen in the Example 2. |
| 20 | +#. Output information is also written to the json format. This means that the status, solution status, the values of variables and shadow prices / reduced costs are exported too. This means that it is possible to export a model that has been solved and then read it again only to see the values of the variables. |
15 | 21 | #. For json, we use the base `json` package. But if `ujson` is available, we use that so the import / export can be really fast. |
16 | 22 |
|
17 | | -Example 1 |
| 23 | +Example 1: json |
18 | 24 | ---------------- |
19 | 25 |
|
20 | 26 | A very simple example taken from the internal tests. Imagine the following problem:: |
21 | 27 |
|
22 | 28 | from pulp import * |
23 | | - prob = LpProblem("test_export_dict_MIP", const.LpMinimize) |
| 29 | + |
| 30 | + prob = LpProblem("test_export_dict_MIP", LpMinimize) |
24 | 31 | x = LpVariable("x", 0, 4) |
25 | 32 | y = LpVariable("y", -1, 1) |
26 | | - z = LpVariable("z", 0, None, const.LpInteger) |
| 33 | + z = LpVariable("z", 0, None, LpInteger) |
27 | 34 | prob += x + 4 * y + 9 * z, "obj" |
28 | 35 | prob += x + y <= 5, "c1" |
29 | 36 | prob += x + z >= 10, "c2" |
@@ -82,7 +89,6 @@ We now have a dictionary with a lot of data:: |
82 | 89 | 'upBound': None, |
83 | 90 | 'varValue': None}]} |
84 | 91 |
|
85 | | - |
86 | 92 | We can now import this dictionary:: |
87 | 93 |
|
88 | 94 | var1, prob1 = LpProblem.from_dict(data) |
@@ -111,9 +117,48 @@ And the result will be available in our *new* variables:: |
111 | 117 | # 3.0 |
112 | 118 |
|
113 | 119 |
|
114 | | -Example 2 |
| 120 | +Example 1: mps |
115 | 121 | ---------------- |
116 | 122 |
|
| 123 | +The same model:: |
| 124 | + |
| 125 | + from pulp import * |
| 126 | + prob = LpProblem("test_export_dict_MIP", LpMinimize) |
| 127 | + x = LpVariable("x", 0, 4) |
| 128 | + y = LpVariable("y", -1, 1) |
| 129 | + z = LpVariable("z", 0, None, LpInteger) |
| 130 | + prob += x + 4 * y + 9 * z, "obj" |
| 131 | + prob += x + y <= 5, "c1" |
| 132 | + prob += x + z >= 10, "c2" |
| 133 | + prob += -y + z == 7.5, "c3" |
| 134 | + |
| 135 | +We can now export the problem into an mps file:: |
| 136 | + |
| 137 | + prob.writeMPS("test.mps") |
| 138 | + |
| 139 | +We can now import this file:: |
| 140 | + |
| 141 | + var1, prob1 = LpProblem.fromMPS("test.mps") |
| 142 | + var1 |
| 143 | + # {'x': x, 'y': y, 'z': z} |
| 144 | + prob1 |
| 145 | + # test_export_dict_MIP: |
| 146 | + # MINIMIZE |
| 147 | + # 1*x + 4*y + 9*z + 0 |
| 148 | + # SUBJECT TO |
| 149 | + # c1: x + y <= 5 |
| 150 | + # c2: x + z >= 10 |
| 151 | + # c3: - y + z = 7.5 |
| 152 | + # VARIABLES |
| 153 | + # x <= 4 Continuous |
| 154 | + # -1 <= y <= 1 Continuous |
| 155 | + # 0 <= z Integer |
| 156 | + |
| 157 | +The resulting tuple is exactly the same format as the previous one. |
| 158 | + |
| 159 | +Example 2: json |
| 160 | +------------------ |
| 161 | + |
117 | 162 | We will use as example the model in :ref:`set-partitioning-problem`:: |
118 | 163 |
|
119 | 164 | import pulp |
@@ -179,3 +224,10 @@ And inspect some of the values:: |
179 | 224 |
|
180 | 225 | wedding_vars["table_('M',_'N')"].value() |
181 | 226 | # 1.0 |
| 227 | + |
| 228 | + |
| 229 | +Grouping variables |
| 230 | +------------------------------------ |
| 231 | + |
| 232 | +As the "Considerations" section mentions, the grouping of variables is not restored automatically. Nevertheless, by using some strict naming convention on variable names and clever parsing, one can reconstruct the original structure of the variables. |
| 233 | + |
0 commit comments