7
7
"""
8
8
9
9
LINEARIZED = True
10
+ GM_NAME = "PH___Model" # to put in the new GAMS files
10
11
import itertools
11
12
import os
12
13
import gams
@@ -65,11 +66,9 @@ def scenario_creator(self, scenario_name, **kwargs):
65
66
opt .all_model_types = self .cfg .solver_name
66
67
print (f"about to instantiate { glist = } , { opt = } " )
67
68
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 )
69
70
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 )
73
72
74
73
### Calling this function is required regardless of the model
75
74
# 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):
341
340
# Also captures whether the problem is a minimization or maximization
342
341
# problem and modifies the solve line (although it might not be necessary)
343
342
for i in range (len (lines )):
344
- index = len (lines )- 1 - i
343
+ index = i # len(lines)-1-i
345
344
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
+
346
351
if line .startswith ("solve" ):
347
352
# Should be in the last lines. This line
348
353
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):
356
361
sign = "-"
357
362
else :
358
363
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 ]} "
359
365
# The word following the sense is the objective value
360
366
index_word = words .index (sense )
361
367
previous_objective = words [index_word + 1 ]
362
368
line = line .replace (sense , "minimizing" )
369
+ line = line .replace (gmodel_name , GM_NAME )
363
370
new_obj = "objective_ph"
364
371
lines [index ] = new_obj .join (line .rsplit (previous_objective , 1 ))
365
372
0 commit comments