Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions mpisppy/cylinders/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,29 +74,6 @@ def clear_latest_chars(self):
self.latest_ib_char = None
self.latest_ob_char = None

def compute_gaps(self):
""" Compute the current absolute and relative gaps,
using the current self.BestInnerBound and self.BestOuterBound
"""
if self.opt.is_minimizing:
abs_gap = self.BestInnerBound - self.BestOuterBound
else:
abs_gap = self.BestOuterBound - self.BestInnerBound

## define by the best solution, as is common
nano = float("nan") # typing aid
if (
abs_gap != nano
and abs_gap != float("inf")
and abs_gap != float("-inf")
and self.BestOuterBound != nano
and self.BestOuterBound != 0
):
rel_gap = abs_gap / abs(self.BestOuterBound)
else:
rel_gap = float("inf")
return abs_gap, rel_gap

def get_update_string(self):
if self.latest_ib_char is None and \
self.latest_ob_char is None:
Expand Down
8 changes: 6 additions & 2 deletions mpisppy/cylinders/lagrangian_bounder.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@ def main(self, need_solution=False):

if extensions:
self.opt.extobject.pre_iter0()
self.dk_iter = 1

# setting this for PH extensions used by this Spoke
self.opt._PHIter = 0
self.trivial_bound = self.lagrangian(need_solution=need_solution, warmstart=sputils.WarmstartStatus.USER_SOLUTION)

if extensions:
self.opt.extobject.post_iter0()
self.opt._PHIter += 1

self.opt.current_solver_options = self.opt.iterk_solver_options

Expand All @@ -101,6 +105,6 @@ def main(self, need_solution=False):
self.send_bound(bound)
if extensions:
self.opt.extobject.enditer_after_sync()
self.dk_iter += 1
self.opt._PHIter += 1
else:
self.do_while_waiting_for_new_Ws(need_solution=need_solution, warmstart=sputils.WarmstartStatus.PRIOR_SOLUTION)
20 changes: 20 additions & 0 deletions mpisppy/cylinders/spcommunicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,3 +495,23 @@ def initialize_bound_values(self):
self.BestOuterBound = inf
self._inner_bound_update = lambda new, old : (new > old)
self._outer_bound_update = lambda new, old : (new < old)

def compute_gaps(self):
""" Compute the current absolute and relative gaps,
using the current self.BestInnerBound and self.BestOuterBound
"""
if self.opt.is_minimizing:
abs_gap = self.BestInnerBound - self.BestOuterBound
else:
abs_gap = self.BestOuterBound - self.BestInnerBound

if abs_gap != inf:
rel_gap = ( abs_gap /
max(1e-10,
abs(self.BestOuterBound),
abs(self.BestInnerBound),
)
)
else:
rel_gap = inf
return abs_gap, rel_gap
6 changes: 6 additions & 0 deletions mpisppy/cylinders/spoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ def bound(self):
return self._bound[0]

def send_bound(self, value):
if self.bound_type() == Field.OBJECTIVE_INNER_BOUND:
self.BestInnerBound = self.InnerBoundUpdate(value)
elif self.bound_type() == Field.OBJECTIVE_OUTER_BOUND:
self.BestOuterBound = self.OuterBoundUpdate(value)
else:
raise RuntimeError(f"Unexpected bound_type {self.bound_type()}")
self._append_trace(value)
self._bound[0] = value
self.put_send_buffer(self._bound, self.bound_type())
Expand Down
5 changes: 4 additions & 1 deletion mpisppy/cylinders/subgradient_bounder.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def main(self):

if extensions:
self.opt.extobject.pre_iter0()
self.dk_iter = 1
# setting this for PH extensions used by this Spoke
self.opt._PHIter = 0
self.trivial_bound = self.lagrangian()
if extensions:
self.opt.extobject.post_iter0()
Expand All @@ -30,6 +31,7 @@ def main(self):
if extensions:
self.opt.extobject.post_iter0_after_sync()

self.opt._PHIter += 1
self.opt.current_solver_options = self.opt.iterk_solver_options

# update rho / alpha
Expand All @@ -52,3 +54,4 @@ def main(self):
self.send_bound(bound)
if extensions:
self.opt.extobject.enditer_after_sync()
self.opt._PHIter += 1
53 changes: 43 additions & 10 deletions mpisppy/extensions/mipgapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
extension.
"""

from mpisppy import global_toc
import mpisppy.extensions.extension

class Gapper(mpisppy.extensions.extension.Extension):
Expand All @@ -19,36 +20,68 @@ def __init__(self, ph):
self.ph = ph
self.cylinder_rank = self.ph.cylinder_rank
self.gapperoptions = self.ph.options["gapperoptions"] # required
self.mipgapdict = self.gapperoptions["mipgapdict"]
self.mipgapdict = self.gapperoptions.get("mipgapdict", None)
self.starting_mipgap = self.gapperoptions.get("starting_mipgap", None)
self.mipgap_ratio = self.gapperoptions.get("mipgap_ratio", None)
self.verbose = self.ph.options["verbose"] \
or self.gapperoptions["verbose"]

def _vb(self, str):
if self.verbose and self.cylinder_rank == 0:
print ("(rank0) mipgapper:" + str)
or self.gapperoptions.get("verbose", True)
self.verbose = True
Copy link

Copilot AI May 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verbose output is being forced to True, which ignores any configuration provided via self.ph.options or gapperoptions. Consider removing or modifying this hardcoding to allow configurable verbosity.

Suggested change
self.verbose = True

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to eliminate the verbose flag for the extension and unconditionally print the gap change

self._check_options()

def _check_options(self):
if self.mipgapdict is None and self.starting_mipgap is None:
raise RuntimeError(f"{self.ph._get_cylinder_name()}: Need to either set a mipgapdict or a starting_mipgap for Gapper")
if self.mipgapdict is not None and self.starting_mipgap is not None:
raise RuntimeError(f"{self.ph._get_cylinder_name()} Gapper: Either use a mipgapdict or automatic mode, not both.")
# exactly one is not None
return

def _vb(self, msg):
if self.verbose:
global_toc(f"{self.ph._get_cylinder_name()} {self.__class__.__name__}: {msg}", self.cylinder_rank == 0)

def set_mipgap(self, mipgap):
""" set the mipgap
Args:
float (mipgap): the gap to set
"""
oldgap = None
mipgap = float(mipgap)
if "mipgap" in self.ph.current_solver_options:
oldgap = self.ph.current_solver_options["mipgap"]
self._vb("Changing mipgap from "+str(oldgap)+" to "+str(mipgap))
self.ph.current_solver_options["mipgap"] = float(mipgap)
if oldgap is None or oldgap != mipgap:
oldgap_str = f"{oldgap}" if oldgap is None else f"{oldgap*100:.3f}%"
self._vb(f"Changing mipgap from {oldgap_str} to {mipgap*100:.3f}%")
# no harm in unconditionally setting this, and covers iteration 1
self.ph.current_solver_options["mipgap"] = mipgap

def pre_iter0(self):
if self.mipgapdict is None:
return
if 0 in self.mipgapdict:
# spcomm not yet set in `__init__`, so check this here
if self.ph.spcomm is None:
raise RuntimeError("Automatic gapper can only be used with cylinders -- needs both an upper bound and lower bound cylinder")
self.set_mipgap(self.starting_mipgap)
elif 0 in self.mipgapdict:
self.set_mipgap(self.mipgapdict[0])

def post_iter0(self):
return

def _autoset_mipgap(self):
self.ph.spcomm.receive_innerbounds()
self.ph.spcomm.receive_outerbounds()
_, problem_rel_gap = self.ph.spcomm.compute_gaps()
subproblem_rel_gap = problem_rel_gap * self.mipgap_ratio
# global_toc(f"{self.ph._get_cylinder_name()}: {self.ph.spcomm.BestInnerBound=}, {self.ph.spcomm.BestOuterBound=}", self.ph.cylinder_rank == 0)
if subproblem_rel_gap < self.starting_mipgap:
self.set_mipgap(subproblem_rel_gap)
# current_solver_options changes in iteration 1
elif self.ph._PHIter == 1:
self.set_mipgap(self.starting_mipgap)

def miditer(self):
if self.mipgapdict is None:
self._autoset_mipgap()
return
PHIter = self.ph._PHIter
if PHIter in self.mipgapdict:
Expand Down
25 changes: 8 additions & 17 deletions mpisppy/generic_cylinders.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
from mpisppy.convergers.primal_dual_converger import PrimalDualConverger

from mpisppy.extensions.extension import MultiExtension, Extension
from mpisppy.extensions.fixer import Fixer
from mpisppy.extensions.mipgapper import Gapper
from mpisppy.extensions.norm_rho_updater import NormRhoUpdater
from mpisppy.extensions.primal_dual_rho import PrimalDualRho
from mpisppy.extensions.gradient_extension import Gradient_extension
Expand Down Expand Up @@ -83,6 +81,7 @@ def _parse_args(m):
cfg.relaxed_ph_fixer_args()
cfg.integer_relax_then_enforce_args()
cfg.gapper_args()
cfg.gapper_args(name="lagrangian")
cfg.ph_nonant_args()
cfg.relaxed_ph_args()
cfg.fwph_args()
Expand Down Expand Up @@ -218,24 +217,14 @@ def _do_decomp(module, cfg, scenario_creator, scenario_creator_kwargs, scenario_
# Extend and/or correct the vanilla dictionary
ext_classes = list()
# TBD: add cross_scenario_cuts, which also needs a cylinder
if cfg.mipgaps_json is not None:
ext_classes.append(Gapper)
with open(cfg.mipgaps_json) as fin:
din = json.load(fin)
mipgapdict = {int(i): din[i] for i in din}
hub_dict["opt_kwargs"]["options"]["gapperoptions"] = {
"verbose": cfg.verbose,
"mipgapdict": mipgapdict
}

if cfg.mipgaps_json is not None or cfg.starting_mipgap is not None:
vanilla.add_gapper(hub_dict, cfg)

if cfg.fixer: # cfg_vanilla takes care of the fixer_tol?
assert hasattr(module, "id_fix_list_fct"), "id_fix_list_fct required for --fixer"
ext_classes.append(Fixer)
hub_dict["opt_kwargs"]["options"]["fixeroptions"] = {
"verbose": cfg.verbose,
"boundtol": cfg.fixer_tol,
"id_fix_list_fct": module.id_fix_list_fct,
}
vanilla.add_fixer(hub_dict, cfg)

if cfg.rc_fixer:
vanilla.add_reduced_costs_fixer(hub_dict, cfg)

Expand Down Expand Up @@ -336,6 +325,8 @@ def _do_decomp(module, cfg, scenario_creator, scenario_creator_kwargs, scenario_
rho_setter = rho_setter,
all_nodenames = all_nodenames,
)
if cfg.lagrangian_starting_mipgap is not None:
vanilla.add_gapper(lagrangian_spoke, cfg, "lagrangian")

# ph outer bounder spoke
if cfg.ph_ob:
Expand Down
22 changes: 21 additions & 1 deletion mpisppy/utils/cfg_vanilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
IDIOM: we feel free to have unused dictionary entries."""

import copy
import json

# Hub and spoke SPBase classes
from mpisppy.phbase import PHBase
Expand Down Expand Up @@ -39,6 +40,7 @@
from mpisppy.cylinders.hub import PHNonantHub, PHHub, SubgradientHub, APHHub, FWPHHub
from mpisppy.extensions.extension import MultiExtension
from mpisppy.extensions.fixer import Fixer
from mpisppy.extensions.mipgapper import Gapper
from mpisppy.extensions.integer_relax_then_enforce import IntegerRelaxThenEnforce
from mpisppy.extensions.cross_scen_extension import CrossScenarioExtension
from mpisppy.extensions.reduced_costs_fixer import ReducedCostsFixer
Expand Down Expand Up @@ -317,12 +319,30 @@ def extension_adder(hub_dict,ext_class):
hub_dict["opt_kwargs"]["extensions"] = MultiExtension
return hub_dict

def add_gapper(hub_dict, cfg, name=None):
hub_dict = extension_adder(hub_dict, Gapper)
if name is None and cfg.mipgaps_json is not None:
with open(cfg.mipgaps_json) as fin:
din = json.load(fin)
mipgapdict = {int(i): din[i] for i in din}
else:
mipgapdict = None
if name is None:
name = ""
else:
name = name + "_"
hub_dict["opt_kwargs"]["options"]["gapperoptions"] = {
"verbose": cfg.verbose,
"mipgapdict": mipgapdict,
"starting_mipgap": getattr(cfg, f"{name}starting_mipgap"),
"mipgap_ratio" : getattr(cfg, f"{name}mipgap_ratio"),
}

def add_fixer(hub_dict,
cfg,
):
hub_dict = extension_adder(hub_dict,Fixer)
hub_dict["opt_kwargs"]["options"]["fixeroptions"] = {"verbose":False,
hub_dict["opt_kwargs"]["options"]["fixeroptions"] = {"verbose":cfg.verbose,
"boundtol": cfg.fixer_tol,
"id_fix_list_fct": cfg.id_fix_list_fct}
return hub_dict
Expand Down
24 changes: 20 additions & 4 deletions mpisppy/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,13 +553,29 @@ def coeff_rho_args(self):
default=1.0)


def gapper_args(self):
def gapper_args(self, name=None):
if name is None:
name = ""
else:
name = name+"_"

self.add_to_config('mipgaps_json',
description="path to json file with a mipgap schedule for PH iterations",
domain=str,
if name == "":
self.add_to_config('mipgaps_json',
description="path to json file with a mipgap schedule for PH iterations",
domain=str,
default=None)

self.add_to_config(f'{name}starting_mipgap',
description="Sets automatic gapper mode and the starting and minimum mipgap",
domain=float,
default=None)

self.add_to_config(f'{name}mipgap_ratio',
description="The ratio of the overall relative optimality gap to the subproblem "
"mipgaps. This should be less than 1 for the algorithm to make progress. "
"(default = 0.1)",
domain=float,
default=0.1)

def fwph_args(self):

Expand Down
Loading