Functions¶
- draw_curve(result: BendersResult) None[source]¶
Draw the convergence curve of the Benders algorithm.
This function plots the lower bound, upper bound, incumbent objective value, and gap over iterations. See this example for a figure generated by this function.
- Parameters:
result (BendersResult) – The result object containing the convergence history of the Benders algorithm, including lists of lower bounds (
lb_list), upper bounds (ub_list), and incumbent objective values (obj_list) at each iteration.
Example
from benderslib.utils import draw_curve BD = ClassicalBenders(mp, sp, complicating_vars) BD.solve() draw_curve(BD.result)
- is_all_integer(vals: list[float], tol=1e-05) bool[source]¶
Check if all values in a list are integers within a specified tolerance.
Sometimes solvers may return values that are very close to integers but not exactly integers due to numerical issues. This function checks if each value in the list is within a specified tolerance of an integer, to determine if the solution can be considered as integer feasible.
- Parameters:
vals (list of float) – The list of values to check.
tol (float, optional) – The tolerance for checking if a value is close enough to an integer.
Example
from benderslib.utils import is_all_integer vals = [1.0, 2.00001, 3.99999] print(is_all_integer(vals))
- normalize_cut(cut: Cut, max_norm: float = 100000.0) Cut[source]¶
Normalize a Benders cut if its L2 norm exceeds a threshold.
This function scales down the cut’s coefficients and right-hand side if the L2 norm of the coefficient vector is greater than
max_norm. This can help improve numerical stability in the master problem solver.- Parameters:
cut (Cut) – The Benders cut to normalize.
max_norm (float, optional) – The maximum allowed L2 norm for the cut’s coefficients.
Example
from benderslib.utils import normalize_cut cut = Cut( vars=['x1', 'x2', 'x3'], coefs=[2.0e5, 1.0e5, 4.5e6], rhs=1e6, sense=CST.LE ) norm_cut = normalize_cut(cut, max_norm=1e5)