Skip to content

Commit 6b3e8bd

Browse files
committed
replace the model name in the new gams file hack that is written
1 parent 530d9cf commit 6b3e8bd

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

Diff for: mpisppy/agnostic/gams_guest.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88

99
LINEARIZED = True
10+
GM_NAME = "PH___Model" # to put in the new GAMS files
1011
import itertools
1112
import os
1213
import gams
@@ -65,11 +66,9 @@ def scenario_creator(self, scenario_name, **kwargs):
6566
opt.all_model_types = self.cfg.solver_name
6667
print(f"about to instantiate {glist=}, {opt=}")
6768
if LINEARIZED:
68-
mi.instantiate("simple using lp minimizing objective_ph", glist, opt)
69+
mi.instantiate(f"{GM_NAME} using lp minimizing objective_ph", glist, opt)
6970
else:
70-
mi.instantiate("simple using qcp minimizing objective_ph", glist, opt)
71-
print("done with instantiate; quitting")
72-
quit()
71+
mi.instantiate(f"{GM_NAME} using qcp minimizing objective_ph", glist, opt)
7372

7473
### Calling this function is required regardless of the model
7574
# This functions initializes, by adding records (and values), all the parameters that appear due to PH
@@ -341,8 +340,14 @@ def create_ph_model(original_file_path, new_file_path, nonants_name_pairs):
341340
# Also captures whether the problem is a minimization or maximization
342341
# problem and modifies the solve line (although it might not be necessary)
343342
for i in range(len(lines)):
344-
index = len(lines)-1-i
343+
index = i # len(lines)-1-i
345344
line = lines[index]
345+
if line.startswith("Model"):
346+
words = re.findall(r'\b\w+\b', line)
347+
gmodel_name = words[1]
348+
line = line.replace(gmodel_name, GM_NAME)
349+
lines[index] = line
350+
346351
if line.startswith("solve"):
347352
# Should be in the last lines. This line
348353
words = re.findall(r'\b\w+\b', line)
@@ -356,10 +361,12 @@ def create_ph_model(original_file_path, new_file_path, nonants_name_pairs):
356361
sign = "-"
357362
else:
358363
raise RuntimeError(f"The line: {line}, doesn't include any sense")
364+
assert gmodel_name == words[1], f"Model line as model name {gmodel_name} but solve line has {words[1]}"
359365
# The word following the sense is the objective value
360366
index_word = words.index(sense)
361367
previous_objective = words[index_word + 1]
362368
line = line.replace(sense, "minimizing")
369+
line = line.replace(gmodel_name, GM_NAME)
363370
new_obj = "objective_ph"
364371
lines[index] = new_obj.join(line.rsplit(previous_objective, 1))
365372

0 commit comments

Comments
 (0)