Skip to content

Commit fbc3198

Browse files
committed
added mps to docs
1 parent 81cd3d4 commit fbc3198

File tree

4 files changed

+73
-17
lines changed

4 files changed

+73
-17
lines changed

HISTORY

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@
22
# Copyright S.A.Mitchell ([email protected]), 2007-
33
# Copyright F.Peschiera ([email protected]), 2019-
44
# See the LICENSE file for copyright information.
5+
2.4 2020-12-22
6+
added mps reading support
7+
updated docs
8+
fix bug with no objective in prob.toDict()
59
2.3.1 2020-10-22
6-
change naming of solver utility functions to camelcase.
7-
fixed gurobi license detection.
10+
change naming of solver utility functions to camelcase
11+
fixed gurobi license detection
812
fixed scip options and added timeLimit argument
9-
changed docs.
13+
changed docs
1014
2.3 2020-08-04
1115
Added plugin page in docs
1216
Standardize arguments of solvers to appear in docs
1317
Fixes to import and export of LpProblem and solver
1418
Added warm start to GUROBI
1519
2.2 2020-04-06
1620
Contribution files
17-
Standard arguments: logPath, gapRel, gapAbs.
21+
Standard arguments: logPath, gapRel, gapAbs
1822
Import and export solver objects
1923
Import and export LpProblem
2024
Took out amply to its own package

doc/source/guides/how_to_export_models.rst

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
1-
How to export models in PuLP
2-
======================================
1+
How to import and export models in PuLP
2+
==========================================
33

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.
612

713
Considerations
814
------------------
915

1016
The following considerations need to be taken into account:
1117

1218
#. 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.
1521
#. For json, we use the base `json` package. But if `ujson` is available, we use that so the import / export can be really fast.
1622

17-
Example 1
23+
Example 1: json
1824
----------------
1925

2026
A very simple example taken from the internal tests. Imagine the following problem::
2127

2228
from pulp import *
23-
prob = LpProblem("test_export_dict_MIP", const.LpMinimize)
29+
30+
prob = LpProblem("test_export_dict_MIP", LpMinimize)
2431
x = LpVariable("x", 0, 4)
2532
y = LpVariable("y", -1, 1)
26-
z = LpVariable("z", 0, None, const.LpInteger)
33+
z = LpVariable("z", 0, None, LpInteger)
2734
prob += x + 4 * y + 9 * z, "obj"
2835
prob += x + y <= 5, "c1"
2936
prob += x + z >= 10, "c2"
@@ -82,7 +89,6 @@ We now have a dictionary with a lot of data::
8289
'upBound': None,
8390
'varValue': None}]}
8491

85-
8692
We can now import this dictionary::
8793

8894
var1, prob1 = LpProblem.from_dict(data)
@@ -111,9 +117,48 @@ And the result will be available in our *new* variables::
111117
# 3.0
112118

113119

114-
Example 2
120+
Example 1: mps
115121
----------------
116122

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+
117162
We will use as example the model in :ref:`set-partitioning-problem`::
118163

119164
import pulp
@@ -179,3 +224,10 @@ And inspect some of the values::
179224

180225
wedding_vars["table_('M',_'N')"].value()
181226
# 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+

pulp/apis/coin_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def getOptions(self):
179179
)
180180

181181
return [v.format(self.optionsDict[k]) for k, v in params_eq.items()
182-
if k in self.optionsDict]
182+
if self.optionsDict.get(k) is not None]
183183

184184
def readsol_MPS(self, filename, lp, vs, variablesNames, constraintsNames, objectiveName=None):
185185
"""

pulp/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
This file contains the constant definitions for PuLP
2828
Note that hopefully these will be changed into something more pythonic
2929
"""
30-
VERSION = '2.3.1'
30+
VERSION = '2.4'
3131
EPS = 1e-7
3232

3333
# variable categories

0 commit comments

Comments
 (0)