Skip to content

Commit

Permalink
fix error where the wrong obj was being used
Browse files Browse the repository at this point in the history
  • Loading branch information
DLWoodruff committed Aug 15, 2024
1 parent 7dfc81a commit 2f6d1df
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions examples/farmer/farmer_ampl_agnostic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import mpisppy.utils.sputils as sputils
import farmer
import numpy as np
import time

# If you need random numbers, use this random stream:
farmerstream = np.random.RandomState()
Expand Down Expand Up @@ -198,6 +199,7 @@ def attach_PH_to_objective(Ag, sname, scenario, add_duals, add_prox):
profitobj.drop()
print(f"{objstr =}")
gs.eval(objstr)
gs.eval("delete minus_profit;")
currentobj = gs.get_current_objective()
# see _copy_Ws_... see also the gams version
WParamDatas = list(gs.get_parameter("W").instances())
Expand All @@ -222,18 +224,27 @@ def solve_one(Ag, s, solve_keyword_args, gripe, tee):

# To acommdate the solve_one call from xhat_eval.py, we need to attach the obj fct value to s

# time.sleep(np.random.uniform()/10)

_copy_Ws_xbars_rho_from_host(s)
gd = s._agnostic_dict
gs = gd["scenario"] # guest scenario handle

#### start debugging
if global_rank == 0:
WParamDatas = list(gs.get_parameter("W").instances())
print(f" in _solve_one {WParamDatas =} {global_rank =}")
xbarsParamDatas = list(gs.get_parameter("xbars").instances())
print(f" in _solve_one {xbarsParamDatas =} {global_rank =}")
rhoParamDatas = list(gs.get_parameter("rho").instances())
print(f" in _solve_one {rhoParamDatas =} {global_rank =}")
if True: # global_rank == 0:
try:
WParamDatas = list(gs.get_parameter("W").instances())
print(f" ^^^ in _solve_one {WParamDatas =} {global_rank =}")
except:
print(f" ^^^^ no W for xhat {global_rank=}")
#prox_on = gs.get_parameter("prox_on").value()
#print(f" ^^^ in _solve_one {prox_on =} {global_rank =}")
#W_on = gs.get_parameter("W_on").value()
#print(f" ^^^ in _solve_one {W_on =} {global_rank =}")
#xbarsParamDatas = list(gs.get_parameter("xbars").instances())
#print(f" in _solve_one {xbarsParamDatas =} {global_rank =}")
#rhoParamDatas = list(gs.get_parameter("rho").instances())
#print(f" in _solve_one {rhoParamDatas =} {global_rank =}")
#### stop debugging

solver_name = s._solver_plugin.name
Expand All @@ -249,6 +260,12 @@ def solve_one(Ag, s, solve_keyword_args, gripe, tee):
results = None
solver_exception = e

# debug
#fname = f"{s.name}_{global_rank}"
#print(f"debug export to {fname}")
#gs.export_model(f"{fname}.mod")
#gs.export_data(f"{fname}.dat")

if gs.solve_result != "solved":
s._mpisppy_data.scenario_feasible = False
if gripe:
Expand All @@ -262,12 +279,12 @@ def solve_one(Ag, s, solve_keyword_args, gripe, tee):
s._mpisppy_data.scenario_feasible = True
# For AMPL mips, we need to use the gap option to compute bounds
# https://amplmp.readthedocs.io/rst/features-guide.html
objval = gs.get_objective("minus_profit").value() # use this?
###phobjval = gs.get_objective("phobj").value() # use this???
objobj = gs.get_current_objective() # different for xhatters
objval = objobj.value()
if gd["sense"] == pyo.minimize:
s._mpisppy_data.outer_bound = objval
else:
s._mpisppy_data.outer_bound = objval
s._mpisppy_data.inner_bound = objval

# copy the nonant x values from gs to s so mpisppy can use them in s
# in general, we need more checks (see the pyomo agnostic guest example)
Expand All @@ -289,13 +306,13 @@ def solve_one(Ag, s, solve_keyword_args, gripe, tee):

s._mpisppy_data.nonant_indices[ndn_i]._value = gxvar.value()

# the next line ignore bundling
# the next line ignores bundling
s._mpisppy_data._obj_from_agnostic = objval

# TBD: deal with other aspects of bundling (see solve_one in spopt.py)


# local helper
# local helper called right before the solve
def _copy_Ws_xbars_rho_from_host(s):
# print(f" debug copy_Ws {s.name =}, {global_rank =}")
gd = s._agnostic_dict
Expand All @@ -315,6 +332,12 @@ def _copy_Ws_xbars_rho_from_host(s):
xbarsdict = {gd["nonant_names"][ndn_i][1]:\
pyo.value(v) for ndn_i, v in s._mpisppy_model.xbars.items()}
gs.get_parameter("xbars").set_values(xbarsdict)
# debug
fname = f"{s.name}_{global_rank}"
print(f"debug export to {fname}")
gs.export_model(f"{fname}.mod")
gs.export_data(f"{fname}.dat")

else:
pass # presumably an xhatter; we should check, I suppose

Expand Down

0 comments on commit 2f6d1df

Please sign in to comment.