1
- # <special for agnostic debugging DLW Aug 2023>
2
1
# In this example, GAMS is the guest language.
3
2
# NOTE: unlike everywhere else, we are using xbar instead of xbars (no biggy)
4
3
29
28
30
29
class GAMS_guest ():
31
30
"""
32
- Provide an interface to a model file for an AMPL guest.
31
+ Provide an interface to a model file for a GAMS guest.
33
32
34
33
Args:
35
34
model_file_name (str): name of Python file that has functions like scenario_creator
36
- ampl_file_name (str): name of AMPL file that is passed to the model file
35
+ gams_file_name (str): name of GAMS file that is passed to the model file
37
36
nonants_name_pairs (list of (str,str)): list of (non_ant_support_set_name, non_ant_variable_name)
37
+ cfg (pyomo config object)
38
38
"""
39
- def __init__ (self , model_file_name , new_file_name , nonants_name_pairs ):
39
+ def __init__ (self , model_file_name , new_file_name , nonants_name_pairs , cfg ):
40
40
self .model_file_name = model_file_name
41
41
self .model_module = sputils .module_name_to_module (model_file_name )
42
42
self .new_file_name = new_file_name
43
43
self .nonants_name_pairs = nonants_name_pairs
44
+ self .cfg = cfg
44
45
45
46
def scenario_creator (self , scenario_name , ** kwargs ):
46
47
""" Wrap the guest (GAMS in this case) scenario creator
@@ -50,12 +51,31 @@ def scenario_creator(self, scenario_name, **kwargs):
50
51
Name of the scenario to construct.
51
52
52
53
"""
53
- mi , nonants_name_pairs , nonant_set_sync_dict = self .model_module .scenario_creator (scenario_name ,
54
- self .new_file_name ,
55
- self .nonants_name_pairs ,
56
- ** kwargs )
54
+ new_file_name = self .new_file_name
55
+ assert new_file_name is not None
56
+ stoch_param_name_pairs = self .model_module .stoch_param_name_pairs_creator ()
57
+
58
+ ws = gams .GamsWorkspace (working_directory = this_dir , system_directory = gamspy_base_dir )
59
+
60
+ ### Calling this function is required regardless of the model
61
+ # This function creates a model instance not instantiated yet, and gathers in glist all the parameters and variables that need to be modifiable
62
+ mi , job , glist , all_ph_parameters_dicts , xlo_dict , xup_dict , x_out_dict = pre_instantiation_for_PH (ws , new_file_name , self .nonants_name_pairs , stoch_param_name_pairs )
63
+
64
+ opt = ws .add_options ()
65
+ opt .all_model_types = self .cfg .solver_name
66
+ if LINEARIZED :
67
+ mi .instantiate ("simple using lp minimizing objective_ph" , glist , opt )
68
+ else :
69
+ mi .instantiate ("simple using qcp minimizing objective_ph" , glist , opt )
70
+
71
+ ### Calling this function is required regardless of the model
72
+ # This functions initializes, by adding records (and values), all the parameters that appear due to PH
73
+ nonant_set_sync_dict = adding_record_for_PH (self .nonants_name_pairs , self .cfg , all_ph_parameters_dicts , xlo_dict , xup_dict , x_out_dict , job )
74
+
75
+ # delete this line (end of factor)
76
+ mi = self .model_module .scenario_creator (scenario_name , mi , ** kwargs )
57
77
mi .solve ()
58
- nonant_variable_list = [nonant_var for (_ , nonant_variables_name ) in nonants_name_pairs for nonant_var in mi .sync_db .get_variable (nonant_variables_name )]
78
+ nonant_variable_list = [nonant_var for (_ , nonant_variables_name ) in self . nonants_name_pairs for nonant_var in mi .sync_db .get_variable (nonant_variables_name )]
59
79
60
80
gd = {
61
81
"scenario" : mi ,
@@ -65,7 +85,7 @@ def scenario_creator(self, scenario_name, **kwargs):
65
85
"probability" : "uniform" ,
66
86
"sense" : pyo .minimize ,
67
87
"BFs" : None ,
68
- "nonants_name_pairs" : nonants_name_pairs ,
88
+ "nonants_name_pairs" : self . nonants_name_pairs ,
69
89
"nonant_set_sync_dict" : nonant_set_sync_dict
70
90
}
71
91
return gd
@@ -474,8 +494,9 @@ def file_name_creator(original_file_path):
474
494
Args:
475
495
original_file_path (str): the path (including the name) of the original gms path
476
496
"""
477
- # Get the directory and filename
478
- directory , filename = os .path .split (original_file_path )
497
+ # Get the directory and filename
498
+
499
+ directory , filename = os .path .split (os .path .abspath (original_file_path ))
479
500
name , ext = os .path .splitext (filename )
480
501
481
502
assert ext == ".gms" , "the original data file should be a gms file"
@@ -492,7 +513,7 @@ def file_name_creator(original_file_path):
492
513
493
514
### Generic functions called inside the specific scenario creator
494
515
def _add_or_get_set (mi , out_set ):
495
- # Captures the set, thanks to the data of the out_database. If it hasn't been added yet to the model insatnce it adds it as well
516
+ # Captures the set using data from the out_database. If it hasn't been added yet to the model insatnce it adds it as well
496
517
try :
497
518
return mi .sync_db .add_set (out_set .name , out_set ._dim , out_set .text )
498
519
except gams .GamsException :
@@ -505,22 +526,24 @@ def pre_instantiation_for_PH(ws, new_file_name, nonants_name_pairs, stoch_param_
505
526
Args:
506
527
ws (GamsWorkspace): the workspace to create the model instance
507
528
new_file_name (str): the gms file in which is created the gams model with the ph_objective
508
- nonants_name_pairs (list of pairs (str, str)): for each non-anticipative variable, the name of the support set must be given with the name of the parameter
529
+ nonants_name_pairs (list of pairs (str, str)): for each non-anticipative variable, the name of the support set must be given with the name of the paramete
509
530
stoch_param_name_pairs (_type_): for each stochastic parameter, the name of the support set must be given with the name of the variable
510
531
511
532
Returns:
512
533
tuple: include everything needed for creating the model instance
513
- nonant_set_sync_dict gives the name of all t
534
+ nonant_set_sync_dict gives the name of all the elements of the sets presented as tuples. It is useful if the set is a cartesian set: i,j then the elements
535
+ will be of the shape (element_in_i,element_in_j). Some functions iterate over this set.
536
+
514
537
"""
515
538
### First create the model instance
516
- job = ws .add_job_from_file (new_file_name )
539
+ job = ws .add_job_from_file (new_file_name . replace ( ".gms" , "" ) )
517
540
cp = ws .add_checkpoint ()
518
541
mi = cp .add_modelinstance ()
519
542
520
- job .run (checkpoint = cp ) # at this point the model with bad values is solved, it creates the file _gams_py_gjo0.lst
543
+ job .run (checkpoint = cp ) # at this point the model ( with what data?) is solved, it creates the file _gams_py_gjo0.lst
521
544
522
545
### Add to the elements that should be modified the stochastic parameters
523
- # The parameters don't exist yet in the model instance, so they need to be redefined thanks to the job
546
+ # The parameters don't exist yet in the model instance, so they need to be redefined using the job
524
547
stoch_sets_out_dict = {param_name : [job .out_db .get_set (elementary_set ) for elementary_set in set_name .split ("," )] for set_name , param_name in stoch_param_name_pairs }
525
548
stoch_sets_sync_dict = {param_name : [_add_or_get_set (mi , out_elementary_set ) for out_elementary_set in out_elementary_sets ] for param_name , out_elementary_sets in stoch_sets_out_dict .items ()}
526
549
glist = [gams .GamsModifier (mi .sync_db .add_parameter_dc (param_name , [sync_elementary_set for sync_elementary_set in sync_elementary_sets ])) for param_name , sync_elementary_sets in stoch_sets_sync_dict .items ()]
0 commit comments