Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
82 changes: 72 additions & 10 deletions pyomo/contrib/gdpopt/branch_and_bound.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@


from pyomo.common.collections import ComponentMap
from pyomo.common.config import document_kwargs_from_configdict
from pyomo.common.config import (
ConfigBlock,
ConfigValue,
document_kwargs_from_configdict,
)
from pyomo.common.errors import InfeasibleConstraintException
from pyomo.contrib.fbbt.fbbt import fbbt
from pyomo.contrib.gdpopt.algorithm_base_class import _GDPoptAlgorithm
Expand All @@ -32,6 +36,7 @@
_add_nlp_solve_configs,
)
from pyomo.contrib.gdpopt.nlp_initialization import restore_vars_to_original_values
from pyomo.contrib.gdpopt.solve_subproblem import detect_unfixed_discrete_vars
from pyomo.contrib.gdpopt.util import (
copy_var_list_values,
SuppressInfeasibleWarning,
Expand Down Expand Up @@ -76,6 +81,26 @@ class GDP_LBB_Solver(_GDPoptAlgorithm):
CONFIG = _GDPoptAlgorithm.CONFIG()
_add_mip_solver_configs(CONFIG)
_add_nlp_solver_configs(CONFIG, default_solver='ipopt')
CONFIG.declare(
"relaxed_nlp_solver",
ConfigValue(
default=None,
description="""
Continuous nonlinear solver to use for transformed LBB node
subproblems with no unfixed discrete variables. If unset, LBB uses
the relevant mixed-integer node solver to preserve existing global
bounding behavior.""",
),
)
CONFIG.declare(
"relaxed_nlp_solver_args",
ConfigBlock(
description="""
Keyword arguments to send to the relaxed NLP subsolver solve()
invocation.""",
implicit=True,
),
)
_add_nlp_solve_configs(
CONFIG, default_nlp_init_method=restore_vars_to_original_values
)
Expand Down Expand Up @@ -413,6 +438,40 @@ def _evaluate_node(self, node_data, node_model, config):
)
return new_node_data

def _get_rnGDP_subproblem_solver(
self, subproblem, config, discrete_solver, discrete_solver_args
):
"""Choose the node solver based on the remaining discrete variables."""
unfixed_discrete_vars = detect_unfixed_discrete_vars(subproblem)
if len(unfixed_discrete_vars) == 0:
if config.relaxed_nlp_solver is not None:
config.logger.debug(
"Transformed node subproblem has no unfixed discrete variables. "
"Solving with relaxed NLP solver %s." % config.relaxed_nlp_solver
)
return config.relaxed_nlp_solver, dict(config.relaxed_nlp_solver_args)
else:
config.logger.debug(
"Transformed node subproblem has no unfixed discrete variables, "
"but relaxed_nlp_solver is not specified. Solving with "
"mixed-integer solver %s." % discrete_solver
)
return discrete_solver, dict(discrete_solver_args)
else:
config.logger.debug(
"Transformed node subproblem has unfixed discrete variables. "
"Solving with mixed-integer solver %s." % discrete_solver
)
return discrete_solver, dict(discrete_solver_args)

def _apply_rnGDP_subproblem_time_limit(self, solver_name, solver_args, config):
"""Apply the remaining GAMS time limit to this solve's arguments."""
if config.time_limit and solver_name == 'gams':
elapsed = get_main_elapsed_time(self.timing)
remaining = max(config.time_limit - elapsed, 1)
solver_args['add_options'] = list(solver_args.get('add_options', []))
solver_args['add_options'].append('option reslim=%s;' % remaining)

def _solve_rnGDP_subproblem(self, model, config):
subproblem = TransformationFactory('gdp.bigm').create_using(model)
obj_sense_correction = self.objective_sense != minimize
Expand All @@ -431,13 +490,11 @@ def _solve_rnGDP_subproblem(self, model, config):
ignore_integrality=True,
)
return float('inf'), float('inf')
minlp_args = dict(config.minlp_solver_args)
if config.time_limit and config.minlp_solver == 'gams':
elapsed = get_main_elapsed_time(self.timing)
remaining = max(config.time_limit - elapsed, 1)
minlp_args['add_options'] = minlp_args.get('add_options', [])
minlp_args['add_options'].append('option reslim=%s;' % remaining)
result = SolverFactory(config.minlp_solver).solve(subproblem, **minlp_args)
solver_name, solver_args = self._get_rnGDP_subproblem_solver(
subproblem, config, config.minlp_solver, config.minlp_solver_args
)
self._apply_rnGDP_subproblem_time_limit(solver_name, solver_args, config)
result = SolverFactory(solver_name).solve(subproblem, **solver_args)
term_cond = result.solver.termination_condition
if term_cond == tc.optimal:
assert result.solver.status is SolverStatus.ok
Expand Down Expand Up @@ -515,9 +572,14 @@ def _solve_local_rnGDP_subproblem(self, model, config):
model_utils = model.component(self.original_util_block.name)

with SuppressInfeasibleWarning():
result = SolverFactory(config.local_minlp_solver).solve(
subproblem, **config.local_minlp_solver_args
solver_name, solver_args = self._get_rnGDP_subproblem_solver(
subproblem,
config,
config.local_minlp_solver,
config.local_minlp_solver_args,
)
self._apply_rnGDP_subproblem_time_limit(solver_name, solver_args, config)
result = SolverFactory(solver_name).solve(subproblem, **solver_args)
term_cond = result.solver.termination_condition
if term_cond == tc.optimal:
assert result.solver.status is SolverStatus.ok
Expand Down
180 changes: 178 additions & 2 deletions pyomo/contrib/gdpopt/tests/test_LBB.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,25 @@

from pyomo.common.fileutils import import_file
from pyomo.common.log import LoggingIntercept
from pyomo.common.tee import capture_output
import pyomo.contrib.gdpopt.tests.common_tests as ct
from pyomo.contrib.gdpopt.branch_and_bound import GDP_LBB_Solver
from pyomo.contrib.gdpopt.create_oa_subproblems import (
add_algebraic_variable_list,
add_util_block,
)
from pyomo.contrib.gdpopt.util import time_code
from pyomo.contrib.satsolver.satsolver import z3_available
from pyomo.environ import (
Binary,
Constraint,
SolverFactory,
value,
ConcreteModel,
Constraint,
Var,
Objective,
minimize,
maximize,
minimize,
)
from pyomo.gdp import Disjunct, Disjunction
from pyomo.opt import SolverResults, TerminationCondition
Expand Down Expand Up @@ -70,6 +73,179 @@ def test_time_limit_returns_pyomo_results_object(self):
self.assertEqual(results.problem.lower_bound, float('-inf'))
self.assertEqual(results.problem.upper_bound, float('inf'))

def test_node_time_limit_does_not_accumulate_in_configured_gams_options(self):
m = ConcreteModel()
m.x = Var(bounds=(0, 1))
m.obj = Objective(expr=m.x)
solver = GDP_LBB_Solver()
config = solver.CONFIG()
config.time_limit = 10
config.minlp_solver = 'gams'
config.minlp_solver_args = {'add_options': ['option optcr=0;']}
config.relaxed_nlp_solver_args = {'add_options': ['option optca=0;']}

with time_code(solver.timing, 'total', is_main_timer=True):
for relaxed_solver in (None, 'gams'):
config.relaxed_nlp_solver = relaxed_solver
for _ in range(2):
solver_name, solver_args = solver._get_rnGDP_subproblem_solver(
m, config, config.minlp_solver, config.minlp_solver_args
)
solver._apply_rnGDP_subproblem_time_limit(
solver_name, solver_args, config
)
self.assertEqual(len(solver_args['add_options']), 2)
self.assertRegex(
solver_args['add_options'][1], r'^option reslim=.+;$'
)

self.assertEqual(config.minlp_solver_args['add_options'], ['option optcr=0;'])
self.assertEqual(
config.relaxed_nlp_solver_args['add_options'], ['option optca=0;']
)


@unittest.skipUnless(
SolverFactory('glpk').available(exception_flag=False)
and SolverFactory('ipopt').available(exception_flag=False),
"The LBB node dispatch tests require GLPK and Ipopt",
)
class TestGDPoptLBBNodeSolverDispatch(unittest.TestCase):
def _make_lbb_solver(self, model):
solver = GDP_LBB_Solver()
solver.pyomo_results = SolverResults()
solver.pyomo_results.problem.sense = minimize
solver.original_util_block = add_util_block(model)
add_algebraic_variable_list(solver.original_util_block)
return solver

def _make_config(self, solver, relaxed_nlp_solver=None):
config = solver.CONFIG()
config.minlp_solver = 'glpk'
config.minlp_solver_args = dict(tee=True, options={'mipgap': 0.05})
config.nlp_solver = 'sentinel_nlp'
config.local_minlp_solver = 'glpk'
config.local_minlp_solver_args = dict(tee=True, options={'mipgap': 0.1})
config.relaxed_nlp_solver = relaxed_nlp_solver
config.relaxed_nlp_solver_args = dict(
tee=True, options={'tol': 1e-9, 'print_user_options': 'yes'}
)
config.integer_tolerance = 1e-5
config.time_limit = None
return config

def _assert_ipopt_output(self, output):
self.assertIn('This is Ipopt version', output)
self.assertRegex(output, r'\btol\s*=\s*1e-09\s+yes')

def _assert_glpk_output(self, output, mipgap):
self.assertIn('GLPSOL--GLPK LP/MIP Solver', output)
self.assertRegex(
output, r'--mipgap\s+' + str(mipgap).replace('.', r'\.') + r'\b'
)

def _make_mixed_integer_model(self):
m = ConcreteModel()
m.x = Var(bounds=(0, 2))
m.y = Var(domain=Binary)
# FBBT must leave y unfixed so this exercises mixed-integer dispatch.
m.c = Constraint(expr=m.x + m.y >= 0.5)
m.obj = Objective(expr=m.x + 0.75 * m.y)
return m

def test_continuous_node_subproblem_uses_relaxed_nlp_solver(self):
m = ConcreteModel()
m.x = Var(bounds=(0, 2), initialize=1.5)
m.obj = Objective(expr=(m.x - 1) ** 2)

solver = self._make_lbb_solver(m)
config = self._make_config(solver, relaxed_nlp_solver='ipopt')
with capture_output() as output:
solver._solve_rnGDP_subproblem(m, config)

self._assert_ipopt_output(output.getvalue())
self.assertAlmostEqual(value(m.x), 1.0, places=6)

def test_continuous_node_subproblem_defaults_to_minlp_solver(self):
m = ConcreteModel()
m.x = Var(bounds=(1, 2))
m.obj = Objective(expr=m.x)

solver = self._make_lbb_solver(m)
config = self._make_config(solver)
with capture_output() as output:
solver._solve_rnGDP_subproblem(m, config)

self._assert_glpk_output(output.getvalue(), 0.05)
self.assertAlmostEqual(value(m.x), 1.0)
Comment on lines +156 to +180

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't see how these tests test the solver you're using. They just check that you get the right answer both times and that the second time you get it exactly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added solver-specific options and checks on the solver output. I also changed the mixed-integer test because FBBT was fixing its binary variable before dispatch.


def test_mixed_integer_node_subproblem_uses_minlp_solver(self):
m = self._make_mixed_integer_model()

solver = self._make_lbb_solver(m)
config = self._make_config(solver, relaxed_nlp_solver='ipopt')
with capture_output() as output:
solver._solve_rnGDP_subproblem(m, config)

self._assert_glpk_output(output.getvalue(), 0.05)
self.assertAlmostEqual(value(m.x), 0.5)
self.assertAlmostEqual(value(m.y), 0.0)

def test_continuous_local_node_subproblem_uses_relaxed_nlp_solver(self):
m = ConcreteModel()
m.x = Var(bounds=(0, 2), initialize=1.5)
m.obj = Objective(expr=(m.x - 1) ** 2)

solver = self._make_lbb_solver(m)
config = self._make_config(solver, relaxed_nlp_solver='ipopt')
with capture_output() as output:
solver._solve_local_rnGDP_subproblem(m, config)

self._assert_ipopt_output(output.getvalue())
self.assertAlmostEqual(value(m.x), 1.0, places=6)
Comment on lines +194 to +205

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same question here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The local tests now check the solver output too. They use different GLPK options from the global tests to check that the local arguments are used.


def test_continuous_local_node_subproblem_defaults_to_local_minlp_solver(self):
m = ConcreteModel()
m.x = Var(bounds=(1, 2))
m.obj = Objective(expr=m.x)

solver = self._make_lbb_solver(m)
config = self._make_config(solver)
with capture_output() as output:
solver._solve_local_rnGDP_subproblem(m, config)

self._assert_glpk_output(output.getvalue(), 0.1)
self.assertAlmostEqual(value(m.x), 1.0)

def test_mixed_integer_local_node_subproblem_uses_local_minlp_solver(self):
m = self._make_mixed_integer_model()

solver = self._make_lbb_solver(m)
config = self._make_config(solver, relaxed_nlp_solver='ipopt')
with capture_output() as output:
solver._solve_local_rnGDP_subproblem(m, config)

self._assert_glpk_output(output.getvalue(), 0.1)
self.assertAlmostEqual(value(m.x), 0.5)
self.assertAlmostEqual(value(m.y), 0.0)

def test_fixed_discrete_node_subproblem_uses_relaxed_nlp_solver(self):
for method in ('_solve_rnGDP_subproblem', '_solve_local_rnGDP_subproblem'):
with self.subTest(method=method):
m = ConcreteModel()
m.x = Var(bounds=(0, 2), initialize=1.5)
m.y = Var(domain=Binary, initialize=1)
m.y.fix()
m.obj = Objective(expr=(m.x - m.y) ** 2)

solver = self._make_lbb_solver(m)
config = self._make_config(solver, relaxed_nlp_solver='ipopt')
with capture_output() as output:
getattr(solver, method)(m, config)

self._assert_ipopt_output(output.getvalue())
self.assertAlmostEqual(value(m.x), 1.0, places=6)


@unittest.skipUnless(
solver_available, "Required subsolver %s is not available" % (minlp_solver,)
Expand Down
Loading