Skip to content

Commit

Permalink
try to add mpigap (untested)
Browse files Browse the repository at this point in the history
  • Loading branch information
DLWoodruff committed Aug 15, 2024
1 parent 744fe5f commit f339c4c
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions mpisppy/agnostic/ampl_guest.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,18 @@ def scenario_creator(self, scenario_name, **kwargs):
scenario_name (str):
Name of the scenario to construct.
"""
def _has_ints(s):
for _,v in s.getVariables():
if "binary" in str(v) or "integer" in str(v):
return True
return False

s, prob, nonant_vardata_list, obj_fct = self.model_module.scenario_creator(scenario_name,
self.ampl_file_name,
**kwargs)
if len(nonant_vardata_list) == 0:
raise RuntimeError(f"model file {self.model_file_name} has an empty "
f" nonant_vardata_list for {scenario_name =}")
### TBD: assert that this is minimization?
# In general, be sure to process variables in the same order has the guest does (so indexes match)
nonant_vars = nonant_vardata_list # typing aid
def _vname(v):
Expand All @@ -86,7 +91,8 @@ def _vname(v):
"probability": prob,
"obj_fct": obj_fct,
"sense": pyo.minimize,
"BFs": None
"BFs": None,
"has_ints": _has_ints(s),
}
##?xxxxx ? create nonant vars and put them on the ampl model,
##?xxxxx create constraints to make them equal to the original nonants
Expand Down Expand Up @@ -183,6 +189,7 @@ def _vname(i):
gs.eval("param xbars{nonant_indices};")
obj_fct = gd["obj_fct"]
objstr = str(obj_fct)
assert objstr.split (' ')[0] == "minimize", "We currently assume minimization"

# Dual term (weights W) (This is where indexes are an issue)
phobjstr = ""
Expand Down Expand Up @@ -274,12 +281,14 @@ def solve_one(self, Ag, s, solve_keyword_args, gripe, tee=False):

# For AMPL mips, we need to use the gap option to compute bounds
# https://amplmp.readthedocs.io/rst/features-guide.html
# As of Aug 2024, this is not tested...
mipgap = gs.getValue('_mipgap') if gd["has_ints"] else 0
objobj = gs.get_current_objective() # different for xhatters
objval = objobj.value()
if gd["sense"] == pyo.minimize:
s._mpisppy_data.outer_bound = objval
s._mpisppy_data.outer_bound = objval - mipgap
else:
s._mpisppy_data.inner_bound = objval
s._mpisppy_data.inner_bound = objval + mpigap

# 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 Down

0 comments on commit f339c4c

Please sign in to comment.