Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LpMaximize and writeMPS #697

Open
4 of 14 tasks
fuglede opened this issue Oct 27, 2023 · 2 comments
Open
4 of 14 tasks

LpMaximize and writeMPS #697

fuglede opened this issue Oct 27, 2023 · 2 comments

Comments

@fuglede
Copy link

fuglede commented Oct 27, 2023

Details for the issue

What did you do?

Consider writing the following simple LP to an MPS file:

model = LpProblem("Test problem", LpMaximize)
x = LpVariable("x")
model += x
model += x <= 60
model += x >= -30
model.writeMPS('test.mps')

The resulting file looks as follows:

*SENSE:Maximize
NAME          Test_problem
ROWS
 N  OBJ
 L  _C1
 G  _C2
COLUMNS
    x         _C1        1.000000000000e+00
    x         _C2        1.000000000000e+00
    x         OBJ        1.000000000000e+00
RHS
    RHS       _C1        6.000000000000e+01
    RHS       _C2       -3.000000000000e+01
BOUNDS
 FR BND       x       
ENDATA

In particular, the first line, *SENSE:Maximize indicates that we are indeed dealing with a maximization problem. I can't seem to be able to figure out where that's specified though. And I tried passing the MPS to HiGHS and MOSEK, both of which assume that we are dealing with a minimization problem instead.

Wikipedia mentions the following syntax for specifying that a problem is a maximization problem, and e.g. HiGHS does manage to parse that:

OBJSENSE
 MAX

Should I have expected *SENSE:Maximize to be something that solvers can parse, so that I can go on to blame the other solvers, should I maybe have realized that MPS isn't good for maximization problems altogether? If this syntax is PuLP-specific, might it make sense to switch to the possibly-supported syntax from Wikipedia?

Useful extra information

The info below often helps, please fill it out if you're able to. :)

What operating system are you using?

  • Windows: ( version: ___ )
  • Linux: ( distro: Linux 5.15.0-1050-azure 57~20.04.1-Ubuntu SMP Wed Oct 4 17:09:16 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux ___ )
  • Mac OS: ( version: ___ )
  • Other: ___

I'm using python version:

  • 2.7
  • 3.4
  • 3.5
  • 3.6
  • Other: 3.11.5

I installed PuLP via:

Did you also

@fuglede
Copy link
Author

fuglede commented Oct 27, 2023

So indeed, someone did think of this discrepancy at some point; there's a parameter for deciding which syntax to use:

pulp/pulp/mps_lp.py

Lines 252 to 256 in c0c391a

if with_objsense:
f.write("OBJSENSE\n")
f.write(f" {const.LpSensesMPS[mpsSense]}\n")
else:
f.write(f"*SENSE:{const.LpSenses[mpsSense]}\n")

It looks like(?) it's undocumented though, so explaining (e.g. in the documentation for LpProblem.writeMPS itself) that that's something to be aware of, what the caveats are and what one might want to use when.

@tkralphs
Copy link
Member

Just for background, the original specification for the MPS format goes back decades, literally to the time when punch cards were used for input, and only allowed for minimization (which was just the silent default and could not be specified explicitly one way or the other). All MPS files that strictly adhere to that original specification thus describe minimization problems. Over time, the MPS format has been relaxed in various ways to get rid of restrictions that are now not really useful, including the prohibition on describing maximization problems. The OBJSENSE keyword was added informally, but this and other additions has lead to minor incompatibilities between different writers and readers and it is generally safer even today to convert to minimization before writing out an MPS file. On the other hand, if you are aware of the issue, then it is easy to test whether any given reader or writer knows about this keyword.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants