-
-
Notifications
You must be signed in to change notification settings - Fork 389
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
Parsing objective sense in MPS file #656
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
import re | ||
from . import constants as const | ||
|
||
CORE_FILE_OBJSENSE_MODE = "OBJSENSE" | ||
CORE_FILE_ROW_MODE = "ROWS" | ||
CORE_FILE_COL_MODE = "COLUMNS" | ||
CORE_FILE_RHS_MODE = "RHS" | ||
|
@@ -34,7 +35,9 @@ def readMPS(path, sense, dropConsNames=False): | |
This dictionary can be used to generate an LpProblem | ||
|
||
:param path: path of mps file | ||
:param sense: 1 for minimize, -1 for maximize | ||
:param sense: 1 for minimize, -1 for maximize, can be None. If None and the | ||
MPS file doesn't set the sense, then the objective sense defaults to | ||
minimize. | ||
:param dropConsNames: if True, do not store the names of constraints | ||
:return: a dictionary with all the problem data | ||
""" | ||
|
@@ -83,6 +86,12 @@ def readMPS(path, sense, dropConsNames=False): | |
mode = CORE_FILE_BOUNDS_MODE_NAME_GIVEN | ||
else: | ||
mode = CORE_FILE_BOUNDS_MODE_NO_NAME | ||
elif parameters['sense'] is None and line[0] == CORE_FILE_OBJSENSE_MODE: | ||
if len(line) > 1: | ||
parameters['sense'] = const.LpMaximize if line[1] == 'MAX'\ | ||
else const.LpMinimize | ||
else: | ||
mode = CORE_FILE_OBJSENSE_MODE | ||
Comment on lines
+93
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why would we have the same mode regardless of the if clause. Maybe we should put the assignment before the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Following on from the previous comment, we have to enter the
The check of |
||
|
||
# here we query the mode variable | ||
elif mode == CORE_FILE_ROW_MODE: | ||
|
@@ -142,12 +151,20 @@ def readMPS(path, sense, dropConsNames=False): | |
readMPSSetBounds(line, variable_info) | ||
if line[1] not in bnd_names: | ||
bnd_names.append(line[1]) | ||
elif mode == CORE_FILE_OBJSENSE_MODE: | ||
parameters['sense'] = const.LpMaximize if line[0] == 'MAX'\ | ||
else const.LpMinimize | ||
Comment on lines
+154
to
+156
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't we test if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could put in a check here for whether We could put in an assert or throw an exception to protect against future code changes? Something like (after line 154)
|
||
constraints = list(constraints.values()) | ||
if dropConsNames: | ||
for c in constraints: | ||
c["name"] = None | ||
objective["name"] = None | ||
variable_info = list(variable_info.values()) | ||
|
||
# if the objective sense has not been read. Then it defaults to minimize | ||
if parameters['sense'] is None: | ||
parameters['sense'] = const.LpMinimize | ||
|
||
return dict( | ||
parameters=parameters, | ||
objective=objective, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren't you missing a
mode=CORE_FILE_OBJSENSE_MODE
here too?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not in this case. This line is catching the case where you have the objective sense provided in the same line as the keyword
OBJSENSE
, such asIn this case we don't need to enter the OBJSENSE mode.