68def guss_call(scenario_dict, mi, solve_statement, opt=None, mi_opt=None, output=None):
69 if scenario_dict.dimension != 3:
70 raise GamsException("Scenario dictionary needs to be 3-dimensional")
71
72 scen_name = scenario_dict.first_record((" ", "scenario", " ")).key(0)
73 scen_symbol = scenario_dict.database[scen_name]
74 modifier_list = []
75 for rec in scenario_dict:
76 if rec.key(1).lower() == "scenario":
77 continue
78 if rec.key(1).lower() == "param":
79 modifier_dim = (
80 scenario_dict.database[rec.key(2)].dimension - scen_symbol.dimension
81 )
82 if modifier_dim < 0:
83 raise GamsException("Dimension of " + rec.key(2) + " too small")
84 modifier_list.append(
85 (
86 GamsModifier(
87 mi.sync_db.add_parameter(rec.key(0), modifier_dim, "")
88 ),
89 scenario_dict.database[rec.key(2)],
90 )
91 )
92 elif rec.key(1).lower() in ["lower", "upper", "fixed"]:
93 modifier_dim = (
94 scenario_dict.database[rec.key(2)].dimension - scen_symbol.dimension
95 )
96 if modifier_dim < 0:
97 raise GamsException("Dimension of " + rec.key(2) + " too small")
98 modifier_var = None
99 try:
100 modifier_var = scenario_dict.database[rec.key(0)]
101 except:
102 modifier_var = mi.sync_db.add_variable(
103 rec.key(0), modifier_dim, VarType.Free, ""
104 )
105 if rec.key(1).lower() == "lower":
106 update_action = UpdateAction.Lower
107 elif rec.key(1).lower() == "upper":
108 update_action = UpdateAction.Upper
109 else:
110 update_action = UpdateAction.Fixed
111 modifier_list.append(
112 (
113 GamsModifier(
114 modifier_var,
115 update_action,
116 mi.sync_db.add_parameter(rec.key(2), modifier_dim, ""),
117 ),
118 scenario_dict.database[rec.key(2)],
119 )
120 )
121 elif rec.key(1).lower() in ["level", "marginal"]:
122
123 scenario_dict.database[rec.key(2)]
124 else:
125 raise GamsException("Cannot handle UpdateAction " + rec.key(1))
126
127 ml = [m[0] for m in modifier_list]
128 mi.instantiate(solve_statement, ml, opt)
129 out_list = []
130 for s in scen_symbol:
131 for m, p_scen in modifier_list:
132 p = m.gams_symbol if m.data_symbol is None else m.data_symbol
133 p.clear()
134
135 rec = None
136 filter = [""] * p_scen.dimension
137 for i in range(scen_symbol.dimension):
138 filter[i] = s.key(i)
139 for i in range(scen_symbol.dimension, p_scen.dimension):
140 filter[i] = " "
141 try:
142 rec = p_scen.first_record(filter)
143 except:
144 continue
145
146 while True:
147 my_keys = [
148 rec.key(scen_symbol.dimension + i) for i in range(p.dimension)
149 ]
150 p.add_record(my_keys).value = rec.value
151 if not rec.move_next():
152 break
153
154 mi.solve(SymbolUpdateType.BaseCase, output, mi_opt)
155 if not out_list:
156 for rec in scenario_dict:
157 if rec.key(1).lower() in ["level", "marginal"]:
158 out_list.append(
159 (
160 mi.sync_db[rec.key(0)],
161 scenario_dict.database[rec.key(2)],
162 rec.key(1).lower(),
163 )
164 )
165
166 for tup in out_list:
167 my_keys = [""] * (scen_symbol.dimension + len(tup[0].first_record().keys))
168 for i in range(scen_symbol.dimension):
169 my_keys[i] = s.key(i)
170
171 if isinstance(tup[0], (GamsVariable, GamsEquation)):
172 if tup[2] in ["level", "marginal"]:
173 for rec in tup[0]:
174 for i in range(len(rec.keys)):
175 my_keys[scen_symbol.dimension + i] = s.key(i)
176 tup[1].add_record(my_keys).value = (
177 rec.level if tup[2] == "level" else rec.marginal
178 )
179
180