Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation Tweaks for contrib.alternative_solutions #3369

Closed
wants to merge 5 commits into from
Closed
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
49 changes: 48 additions & 1 deletion doc/OnlineDocs/contributed_packages/alternative_solutions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The following functions are defined in the alternative-solutions library:
* Calculates the bounds on each variable by solving a series of min and max optimization problems where each variable is used as the objective function. This can be applied to any class of problem supported by the selected solver.


Usage Example
Basic Usage Example
-------------

Many of functions in the alternative-solutions library have similar options, so we simply illustrate the ``enumerate_binary_solutions`` function. We define a simple knapsack example whose alternative solutions have integer objective values ranging from 0 to 90.
Expand Down Expand Up @@ -88,6 +88,53 @@ Each ``Solution`` object contains information about the objective and variables,
}


Gap Usage Example
-------------
When we only want some of the solutions based off a tolerance away from optimal, this can be done using the ``abs_opt_gap`` parameter. This is shown in the following simple knapsack examples where the weights and values are the same.

.. doctest::
:skipif: not glpk_available

>>> import pyomo.environ as pyo
>>> import pyomo.contrib.alternative_solutions as aos

>>> values = [10,9,2,1,1]
>>> weights = [10,9,2,1,1]

>>> K = len(values)
>>> capacity = 12

>>> m = pyo.ConcreteModel()
>>> m.x = pyo.Var(range(K), within=pyo.Binary)
>>> m.o = pyo.Objective(expr=sum(values[i] * m.x[i] for i in range(K)), sense=pyo.maximize)
>>> m.c = pyo.Constraint(expr=sum(weights[i] * m.x[i] for i in range(K)) <= capacity)

>>> solns = aos.enumerate_binary_solutions(m, num_solutions=10, solver="glpk", abs_opt_gap = 0.0)
>>> assert(len(solns) == 4)

In this example, we only get the four ``Solution`` objects that have an ``objective_value`` of 12.
Note that while we wanted only those four solutions with no optimality gap, using a gap of half the smallest value (in this case .5) will return the same solutions and avoids any machine precision issues.

.. doctest::
:skipif: not glpk_available

>>> import pyomo.environ as pyo
>>> import pyomo.contrib.alternative_solutions as aos

>>> values = [10,9,2,1,1]
>>> weights = [10,9,2,1,1]

>>> K = len(values)
>>> capacity = 12

>>> m = pyo.ConcreteModel()
>>> m.x = pyo.Var(range(K), within=pyo.Binary)
>>> m.o = pyo.Objective(expr=sum(values[i] * m.x[i] for i in range(K)), sense=pyo.maximize)
>>> m.c = pyo.Constraint(expr=sum(weights[i] * m.x[i] for i in range(K)) <= capacity)

>>> solns = aos.enumerate_binary_solutions(m, num_solutions=10, solver="glpk", abs_opt_gap = 0.5)
>>> assert(len(solns) == 4)

Interface Documentation
-----------------------

Expand Down
7 changes: 7 additions & 0 deletions pyomo/contrib/alternative_solutions/balas.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ def enumerate_binary_solutions(
Finds alternative optimal solutions for a binary problem using no-good
cuts.

This function implements a no-good cuts technique inheriting from Balas's work on Canonical Cuts:

Balas, Egon, and Robert Jeroslow.
“Canonical Cuts on the Unit Hypercube.”
SIAM Journal on Applied Mathematics 23, no. 1 (1972): 61–69.
http://www.jstor.org/stable/2099623.

Parameters
----------
model : ConcreteModel
Expand Down