|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Linear program solver using the Simplex method. More...
#include <Simplex.H>
Public Types | |
| enum | State { Not_Solved , Solving , Unbounded , Solved , Unfeasible } |
| Solution states for the linear program. More... | |
| enum class | PivotRule { Dantzig , Bland } |
| Pivot selection rule. More... | |
Public Member Functions | |
| Simplex (const size_t n, OptimizationType type=OptimizationType::Maximize) | |
| Constructs a Simplex solver for n variables. | |
| ~Simplex () | |
| Destructor. | |
| Simplex (const Simplex &)=delete | |
| Simplex & | operator= (const Simplex &)=delete |
| Simplex (Simplex &&other) noexcept | |
| Takes the program of another solver, which is left as an empty program, without variables nor restrictions. | |
| Simplex & | operator= (Simplex &&other) noexcept |
| Exchanges this solver with another, which is left with the previous program of this one. | |
| void | swap (Simplex &other) noexcept |
| Exchanges in O(1) this solver and another. | |
| void | set_optimization_type (OptimizationType type) noexcept |
| Sets the optimization type. | |
| void | set_minimize () noexcept |
| Sets minimization mode. | |
| void | set_maximize () noexcept |
| Sets maximization mode. | |
| void | set_pivot_rule (PivotRule rule) noexcept |
| Sets the pivot selection rule. | |
| void | enable_bland_rule () noexcept |
| Enables Bland's anti-cycling rule. | |
| void | set_epsilon (T epsilon) noexcept |
| Sets the floating-point comparison tolerance. | |
| OptimizationType | get_optimization_type () const noexcept |
| Gets the current optimization type. | |
| void | put_objetive_function_coef (size_t i, const T &coef) |
| Sets a coefficient in the objective function. | |
| const T & | get_objetive_function_coef (size_t i) const |
| Gets a coefficient from the objective function. | |
| void | put_objetive_function (const DynArray< T > &coefs) |
| Sets all objective function coefficients from a DynArray. | |
| void | put_objetive_function (const T coefs[]) |
| Sets all objective function coefficients from a C array. | |
| T * | put_restriction (const T *coefs=nullptr, ConstraintType type=ConstraintType::LE) |
| Adds a constraint to the linear program. | |
| T * | put_ge_restriction (const T *coefs) |
| Adds a ≥ constraint. | |
| T * | put_eq_restriction (const T *coefs) |
| Adds an = constraint. | |
| T * | put_restriction (const DynArray< T > &coefs, ConstraintType type=ConstraintType::LE) |
| Adds a constraint from a DynArray. | |
| T * | get_restriction (const size_t rest_num) |
| Gets a pointer to a constraint array. | |
| size_t | get_num_restrictions () const noexcept |
| Gets the number of constraints. | |
| size_t | get_num_vars () const noexcept |
| Gets the number of decision variables. | |
| T * | get_objetive_function () noexcept |
| Gets the objective function coefficients array. | |
| const T * | get_objetive_function () const noexcept |
| Gets the objective function coefficients array (const). | |
| T & | get_restriction_coef (const size_t rest_num, size_t idx) |
| Gets a specific coefficient from a constraint. | |
| void | put_restriction_coef (const size_t rest_num, const size_t idx, const T &coef) |
| Sets a specific coefficient in a constraint. | |
| void | prepare_linear_program () |
| Prepares the linear program for solving. | |
| State | solve () |
| Solves the linear program. | |
| State | get_state () const noexcept |
| Gets the current state of the solver. | |
| void | load_solution () noexcept |
| Loads the solution values into the solution array. | |
| const T & | get_solution (size_t i) const noexcept |
| Gets the solution value for a specific variable. | |
| T | objetive_value () const noexcept |
| Computes and returns the objective function value. | |
| bool | verify_solution () const |
| Verifies that the solution satisfies all constraints. | |
| void | print_matrix () const |
| Prints the simplex tableau to stdout. | |
| void | latex_matrix (const std::string &name, const int d=2, const int p=-1, const int q=-1) const |
| Exports the simplex tableau to a LaTeX file. | |
| void | latex_linear_program (const std::string &name) const |
| Exports the linear program to a LaTeX file. | |
| State | latex_solve (const char *name=nullptr) |
| Solves with LaTeX output at each iteration. | |
| const SimplexStats & | get_stats () const noexcept |
| Gets execution statistics. | |
| size_t | get_iteration_count () const noexcept |
| Gets the number of iterations performed. | |
| size_t | get_pivot_count () const noexcept |
| Gets the number of pivot operations. | |
| size_t | get_degenerate_pivot_count () const noexcept |
| Gets the number of degenerate pivots. | |
| double | get_elapsed_time_ms () const noexcept |
| Gets the elapsed solving time. | |
| SensitivityRange< T > | objective_sensitivity (size_t var_idx) const |
| Computes sensitivity range for an objective coefficient. | |
| SensitivityRange< T > | rhs_sensitivity (size_t rest_idx) const |
| Computes sensitivity range for a RHS value. | |
| T | shadow_price (size_t rest_idx) const |
| Gets the shadow price (dual value) for a constraint. | |
| T | reduced_cost (size_t var_idx) const |
| Gets the reduced cost for a variable. | |
| bool | is_basic_variable (size_t var_idx) const |
| Checks if a variable is basic in the optimal solution. | |
| std::vector< T > | get_all_shadow_prices () const |
| Gets all shadow prices as a vector. | |
| std::vector< T > | get_all_reduced_costs () const |
| Gets all reduced costs as a vector. | |
| State | dual_solve () |
| Performs dual simplex iteration. | |
| State | update_rhs_and_reoptimize (size_t rest_idx, T new_rhs) |
| Updates RHS value and re-optimizes using dual simplex. | |
| void | print_stats () const |
| Prints a summary of statistics to stdout. | |
| void | print_sensitivity_analysis () const |
| Prints sensitivity analysis to stdout. | |
Private Member Functions | |
| size_t | first_artificial () const noexcept |
| size_t | rhs_col () const noexcept |
| bool | is_artificial (const size_t j) const noexcept |
| bool | bland_now () const noexcept |
| bool | same_ratio (const T &a, const T &b) const noexcept |
| int | compute_pivot_col (const T &tol) const noexcept |
| int | compute_pivot_row (const size_t q) const noexcept |
| void | to_pivot (size_t p, size_t q) |
| template<class Hook > | |
| State | iterate (const T &cost_tol, Hook &hook) |
| void | load_phase1_costs () |
| void | load_phase2_costs () |
| template<class Hook > | |
| void | drive_out_artificials (Hook &hook) |
| template<class Hook > | |
| State | run (Hook &hook) |
| void | verify_var_index (const size_t i) const |
| void | verify_rest_index (const size_t i) const |
| T * | create_restriction (ConstraintType type=ConstraintType::LE) |
| void | create_matrix () |
| T * | restriction (const size_t rest_idx) const |
| Simplex () noexcept | |
Static Private Member Functions | |
| static std::string | format_entry (const T &value, const int d) |
| static T * | allocate (const size_t n, const size_t spare=0) |
| static T | scale_of (const T &magnitude) noexcept |
Private Attributes | |
| std::unique_ptr< DynMatrix< T > > | m |
| std::unique_ptr< T[]> | objetive |
| DynDlist< T * > | rest_list |
| DynDlist< ConstraintType > | rest_types |
| size_t | num_var |
| size_t | num_rest |
| size_t | num_artificial |
| std::unique_ptr< T[]> | solution |
| State | state |
| OptimizationType | opt_type |
| PivotRule | pivot_rule |
| T | eps |
| SimplexStats | stats |
| std::vector< T > | pivot_row_buffer |
| std::vector< T > | pivot_col_buffer |
| std::vector< int > | basic_vars |
| std::vector< int > | row_sign |
| std::vector< size_t > | unit_col |
| std::vector< T > | phase2_cost |
| std::vector< T > | row_scale |
| std::vector< T > | col_scale |
| T | obj_scale = T{1} |
| T | tol_coef = T{0} |
| T | tol_rhs = T{0} |
| T | tol_cost = T{0} |
| size_t | degenerate_run = 0 |
| std::vector< T > | tie_cost |
| bool | in_phase1 = false |
Linear program solver using the Simplex method.
Simplex<T> allows expressing linear programs in standard or non-standard forms, supporting both maximization and minimization, as well as ≤, ≥, and = constraints directly.
The standard form of a linear program is:
This implementation handles non-standard forms directly:
set_minimize() instead of negating coefficientsput_restriction(coefs, ConstraintType::GE)put_restriction(coefs, ConstraintType::EQ)prepare_linear_program() builds the tableau. A constraint whose right-hand side is negative is multiplied by -1, which swaps ≤ and ≥, so that every right-hand side is >= 0. A ≤ constraint gets a slack variable, a ≥ one a surplus variable and an artificial one, and an = one an artificial one.
solve() runs two phases. The first one minimizes the sum of the artificial variables: if it stays above zero, the problem is infeasible. Else the artificial variables that are still basic, at zero, leave the basis, but for those of constraints that are combinations of others, and the second phase optimizes the objective from that basis. An artificial variable never enters the basis again. The basic variable of each row is kept, and the solution is read from it.
With Dantzig's rule, a run of degenerate pivots switches to Bland's rule until a pivot improves the objective, so that the method always ends.
prepare_linear_program() scales the program: each constraint, each column and the objective are multiplied by a power of two, which changes no digit, so that their largest magnitudes are in [1/2, 1); one whose largest magnitude is subnormal stays as it is, below the tolerances. The solver works on the scaled program, so that its tolerances do not depend on the units of the data, and scales its results back: the solution, the shadow prices, the reduced costs and the ranges are those of the program as it was given. A value of the scaled program is taken as zero when its magnitude is below the tolerance of set_epsilon(); a right-hand side, when it is below that tolerance times the largest magnitude of the right-hand sides, if this one is above 1.
A factory produces two products (A and B) with limited resources:
Mathematical formulation:
Code:
Where n = number of variables, m = number of constraints.
| T | Numeric type for coefficients and values (typically double). |
|
strong |
| enum Aleph::Simplex::State |
Solution states for the linear program.
Not_Solved: Initial state, solve() has not been called yet.Solving: Algorithm is in progress (internal state).Unbounded: The objective improves without limit over the feasible points.Solved: An optimal solution has been found.Unfeasible: No point satisfies all the constraints. | Enumerator | |
|---|---|
| Not_Solved | |
| Solving | |
| Unbounded | |
| Solved | |
| Unfeasible | |
|
inlineprivatenoexcept |
|
inlineexplicit |
Constructs a Simplex solver for n variables.
Initializes a linear program in standard form without constraints and with all objective function coefficients set to zero.
| [in] | n | Number of decision variables in the system. |
| [in] | type | Optimization type: Maximize (default) or Minimize. |
| std::overflow_error | If the bytes of a restriction of n variables, n + 1 coefficients, do not fit in a size_t. |
| std::bad_alloc | If memory allocation fails. |
| std::invalid_argument | If n is zero. |
Definition at line 895 of file Simplex.H.
References ah_invalid_argument_if.
|
inline |
Takes the program of another solver, which is left as an empty program, without variables nor restrictions.
The defaulted move took the arrays and the lists but copied their sizes: the moved-from solver then passed its index checks and wrote through arrays that it no longer had.
| [in,out] | other | solver to move from. |
Definition at line 933 of file Simplex.H.
References Aleph::swap().
|
inlinestaticprivate |
Definition at line 698 of file Simplex.H.
References ah_overflow_error_if.
|
inlineprivatenoexcept |
Definition at line 379 of file Simplex.H.
References Aleph::Simplex< T >::Bland, Aleph::Simplex< T >::degenerate_run, Aleph::Simplex< T >::num_rest, Aleph::Simplex< T >::num_var, and Aleph::Simplex< T >::pivot_rule.
Referenced by Aleph::Simplex< T >::compute_pivot_col(), Aleph::Simplex< T >::compute_pivot_row(), and Aleph::Simplex< T >::iterate().
|
inlineprivatenoexcept |
Definition at line 395 of file Simplex.H.
References Aleph::and, Aleph::Simplex< T >::bland_now(), Aleph::Simplex< T >::first_artificial(), Aleph::Simplex< T >::in_phase1, Aleph::Simplex< T >::m, Aleph::Simplex< T >::same_ratio(), and Aleph::Simplex< T >::tie_cost.
Referenced by Aleph::Simplex< T >::iterate().
|
inlineprivatenoexcept |
Definition at line 431 of file Simplex.H.
References Aleph::and, Aleph::Simplex< T >::basic_vars, Aleph::Simplex< T >::bland_now(), Aleph::Simplex< T >::m, Aleph::Simplex< T >::num_rest, Aleph::Simplex< T >::rhs_col(), Aleph::Simplex< T >::same_ratio(), and Aleph::Simplex< T >::tol_coef.
Referenced by Aleph::Simplex< T >::iterate().
|
inlineprivate |
|
inlineprivate |
Definition at line 708 of file Simplex.H.
References ah_logic_error_if, Aleph::allocate(), and m.
|
inlineprivate |
Definition at line 598 of file Simplex.H.
References Aleph::Simplex< T >::basic_vars, Aleph::SimplexStats::degenerate_pivots, Aleph::Simplex< T >::first_artificial(), Aleph::Simplex< T >::is_artificial(), Aleph::Simplex< T >::m, Aleph::Simplex< T >::num_rest, Aleph::SimplexStats::pivots, Aleph::Simplex< T >::rhs_col(), Aleph::Simplex< T >::stats, Aleph::Simplex< T >::to_pivot(), and Aleph::Simplex< T >::tol_coef.
|
inline |
Performs dual simplex iteration.
Used for re-optimization when the RHS values change. The current basis must be optimal for the objective (no cost of row 0 below zero), as after solve(); each iteration takes out of the basis a variable whose value is negative. An artificial variable never enters the basis, and one that stays basic, of a constraint that is a combination of others, must stay at zero.
Solved, or Unfeasible if no point satisfies the constraints.| std::logic_error | If the problem is not solved. |
Definition at line 1921 of file Simplex.H.
References ah_logic_error_if, Aleph::and, and m.
|
inlinenoexcept |
|
inlineprivatenoexcept |
Definition at line 368 of file Simplex.H.
References Aleph::Simplex< T >::num_rest, and Aleph::Simplex< T >::num_var.
Referenced by Aleph::Simplex< T >::compute_pivot_col(), Aleph::Simplex< T >::drive_out_artificials(), and Aleph::Simplex< T >::is_artificial().
|
inlinestaticprivate |
|
inline |
Gets all reduced costs as a vector.
Definition at line 1895 of file Simplex.H.
References ah_logic_error_if, and Aleph::reduced_cost().
Referenced by TEST_F().
|
inline |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
Gets the number of constraints.
Definition at line 1216 of file Simplex.H.
Referenced by Moved_From_Case< Simplex< double > >::is_empty(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and Moved_From_Case< Simplex< double > >::use().
|
inlinenoexcept |
Gets the number of decision variables.
Definition at line 1225 of file Simplex.H.
Referenced by Moved_From_Case< Simplex< double > >::add_sum_restriction(), Moved_From_Case< Simplex< double > >::is_empty(), Moved_From_Case< Simplex< double > >::make(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and Moved_From_Case< Simplex< double > >::use().
|
inlinenoexcept |
|
inlinenoexcept |
|
inline |
Gets a coefficient from the objective function.
| [in] | i | Variable index (0-based). |
| std::out_of_range | If i >= number of variables. |
Definition at line 1085 of file Simplex.H.
Referenced by TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and Moved_From_Case< Simplex< double > >::use().
|
inlinenoexcept |
|
inlinenoexcept |
|
inline |
|
inline |
Gets a specific coefficient from a constraint.
| [in] | rest_num | Constraint index. |
| [in] | idx | Variable index within the constraint. |
| std::out_of_range | If indices are out of range. |
Definition at line 1256 of file Simplex.H.
Referenced by TEST_F(), TEST_F(), TEST_F(), and Moved_From_Case< Simplex< double > >::use().
|
inlinenoexcept |
Gets the solution value for a specific variable.
| [in] | i | Variable index (0-based). |
Definition at line 1360 of file Simplex.H.
Referenced by demo_algorithm_steps(), demo_diet_problem(), demo_production_planning(), demo_resource_allocation(), Aleph::MCF_LP_Solver< Net >::solve(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().
|
inlinenoexcept |
Gets the current state of the solver.
Definition at line 1324 of file Simplex.H.
Referenced by Moved_From_Case< Simplex< double > >::is_empty(), TEST(), TEST_F(), TEST_F(), and Moved_From_Case< Simplex< double > >::use().
|
inlinenoexcept |
Gets execution statistics.
Returns statistics about the most recent solve() call.
Definition at line 1618 of file Simplex.H.
Referenced by Aleph::MCF_LP_Solver< Net >::solve(), TEST(), TEST(), TEST_F(), TEST_F(), and TEST_F().
|
inlineprivatenoexcept |
Definition at line 372 of file Simplex.H.
References Aleph::and, Aleph::Simplex< T >::first_artificial(), and Aleph::Simplex< T >::rhs_col().
Referenced by Aleph::Simplex< T >::drive_out_artificials(), and Aleph::Simplex< T >::load_phase1_costs().
|
inline |
|
inlineprivate |
Definition at line 526 of file Simplex.H.
References Aleph::Simplex< T >::bland_now(), Aleph::SimplexStats::bland_rule_used, Aleph::Simplex< T >::compute_pivot_col(), Aleph::Simplex< T >::compute_pivot_row(), Aleph::SimplexStats::degenerate_pivots, Aleph::Simplex< T >::degenerate_run, Aleph::SimplexStats::iterations, Aleph::Simplex< T >::m, Aleph::SimplexStats::pivots, Aleph::Simplex< T >::rhs_col(), Aleph::Simplex< T >::Solved, Aleph::Simplex< T >::stats, Aleph::Simplex< T >::to_pivot(), Aleph::Simplex< T >::tol_rhs, and Aleph::Simplex< T >::Unbounded.
|
inline |
|
inline |
Exports the simplex tableau to a LaTeX file.
| [in] | name | Output file name. |
| [in] | d | Number of decimal places (default 2). |
| [in] | p | Pivot row to highlight (-1 for none). |
| [in] | q | Pivot column to highlight (-1 for none). |
| std::logic_error | If prepare_linear_program() was not called. |
Definition at line 1467 of file Simplex.H.
References ah_logic_error_if, Aleph::and, cols, m, and out.
Referenced by TEST().
|
inline |
Solves with LaTeX output at each iteration.
As solve(), but it writes the tableau to LaTeX files for educational purposes: name-0.tex before the first phase, name-k.tex with the pivot of the pivot k circled, name-k-after.tex after it, and name-k.tex, for the next k, at the end.
| [in] | name | Base name for output files. |
| std::logic_error | As solve(). |
Definition at line 1586 of file Simplex.H.
References ah_logic_error_if, k, and m.
|
inlineprivate |
Definition at line 565 of file Simplex.H.
References Aleph::Simplex< T >::basic_vars, cols, Aleph::Simplex< T >::is_artificial(), Aleph::Simplex< T >::m, Aleph::Simplex< T >::num_rest, Aleph::Simplex< T >::num_var, Aleph::Simplex< T >::phase2_cost, Aleph::Simplex< T >::rhs_col(), and Aleph::Simplex< T >::tie_cost.
|
inlineprivate |
Definition at line 586 of file Simplex.H.
References Aleph::Simplex< T >::m, Aleph::Simplex< T >::rhs_col(), and Aleph::Simplex< T >::tie_cost.
|
inlinenoexcept |
Loads the solution values into the solution array.
Each decision variable takes the value of its row if it is basic, and zero if not. After solve() returns Solved, it is an optimal solution; after Unbounded, the vertex from which the objective improves without limit. After this, individual variable values can be retrieved with get_solution().
Definition at line 1337 of file Simplex.H.
References m.
Referenced by demo_algorithm_steps(), demo_diet_problem(), demo_production_planning(), demo_resource_allocation(), demo_special_cases(), Aleph::MCF_LP_Solver< Net >::solve(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and Moved_From_Case< Simplex< double > >::use().
|
inline |
Computes sensitivity range for an objective coefficient.
Determines the range [lower, upper] within which the objective coefficient c_j can vary, the others fixed, while the optimal basis stays optimal, and so the solution too.
| [in] | var_idx | Variable index (0-based). |
| std::out_of_range | If var_idx is out of range. |
| std::logic_error | If not solved yet. |
Definition at line 1677 of file Simplex.H.
References ah_logic_error_if, Aleph::SensitivityRange< T >::current_value, k, Aleph::SensitivityRange< T >::lower_bound, m, Aleph::Maximize, row, and Aleph::SensitivityRange< T >::upper_bound.
|
inlinenoexcept |
Computes and returns the objective function value.
Calculates Z = sum(c_i * x_i) using the current solution.
Definition at line 1374 of file Simplex.H.
References Aleph::sum().
Referenced by demo_algorithm_steps(), demo_diet_problem(), demo_production_planning(), demo_resource_allocation(), demo_special_cases(), Aleph::MCF_LP_Solver< Net >::solve(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().
|
inlinenoexcept |
Exchanges this solver with another, which is left with the previous program of this one.
| [in,out] | other | solver to move from. |
Definition at line 944 of file Simplex.H.
References Aleph::swap().
|
inline |
Prepares the linear program for solving.
Must be called after all objective function coefficients and constraints have been set, and before calling solve().
Creates the internal simplex tableau matrix, with the slack, surplus and artificial variables, after multiplying by -1 each constraint whose right-hand side is negative.
| std::bad_alloc | If memory allocation fails. |
| std::logic_error | If no constraints have been added. |
Definition at line 1287 of file Simplex.H.
References ah_logic_error_if.
Referenced by demo_algorithm_steps(), demo_diet_problem(), demo_production_planning(), demo_resource_allocation(), demo_special_cases(), Aleph::MCF_LP_Solver< Net >::solve(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and Moved_From_Case< Simplex< double > >::use().
|
inline |
Prints the simplex tableau to stdout.
It is the tableau of the scaled program that the solver works on (see the class documentation). Row 0 holds the costs that the current phase minimizes, and the last column the right-hand sides. Useful for debugging and understanding the algorithm's progress.
| std::logic_error | If prepare_linear_program() was not called. |
Definition at line 1445 of file Simplex.H.
References ah_logic_error_if, and m.
Referenced by TEST().
|
inline |
Prints sensitivity analysis to stdout.
Definition at line 2076 of file Simplex.H.
References ah_logic_error_if, Aleph::range(), and Aleph::reduced_cost().
|
inline |
|
inline |
Adds an = constraint.
Convenience method for adding equality constraints.
| [in] | coefs | Array of n+1 coefficients. |
| std::bad_alloc | If memory allocation fails. |
| std::logic_error | If prepare_linear_program() already built the tableau, as put_restriction() explains. |
Definition at line 1173 of file Simplex.H.
References Aleph::EQ.
Referenced by Aleph::MCF_LP_Solver< Net >::solve(), TEST(), and TEST_F().
|
inline |
Adds a ≥ constraint.
Convenience method for adding greater-than-or-equal constraints.
| [in] | coefs | Array of n+1 coefficients. |
| std::bad_alloc | If memory allocation fails. |
| std::logic_error | If prepare_linear_program() already built the tableau, as put_restriction() explains. |
Definition at line 1157 of file Simplex.H.
References Aleph::GE.
|
inline |
|
inline |
|
inline |
Sets a coefficient in the objective function.
Sets the coefficient of variable x_i in the objective function.
| [in] | i | Variable index (0-based). |
| [in] | coef | Coefficient value. |
| std::out_of_range | If i >= number of variables. |
Definition at line 1072 of file Simplex.H.
Referenced by demo_algorithm_steps(), demo_diet_problem(), demo_production_planning(), demo_resource_allocation(), demo_special_cases(), Moved_From_Case< Simplex< double > >::make(), Aleph::MCF_LP_Solver< Net >::solve(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and Moved_From_Case< Simplex< double > >::use().
|
inline |
Adds a constraint from a DynArray.
| [in] | coefs | Array of n+1 coefficients. |
| [in] | type | Constraint type: LE (≤), GE (≥), or EQ (=). Default is LE. |
| std::bad_alloc | If memory allocation fails. |
| std::logic_error | If prepare_linear_program() already built the tableau, as put_restriction() explains. |
|
inline |
Adds a constraint to the linear program.
Adds a constraint of the form: c[0]*x_0 + c[1]*x_1 + ... + c[n-1]*x_{n-1} [op] c[n]
where c[n] is the RHS (right-hand side) bound and [op] is determined by the constraint type (≤, ≥, or =). The RHS may be negative.
| [in] | coefs | Array of n+1 coefficients (n variable coefficients + RHS). If nullptr, creates a zero-initialized constraint. |
| [in] | type | Constraint type: LE (≤), GE (≥), or EQ (=). Default is LE. |
| std::bad_alloc | If memory allocation fails. |
| std::logic_error | If prepare_linear_program() already built the tableau: it does not grow, and the solver would read past it. The program is left as it was. A preparation that threw, as std::bad_alloc, built none, so the program still takes restrictions. |
Definition at line 1132 of file Simplex.H.
Referenced by Moved_From_Case< Simplex< double > >::add_sum_restriction(), demo_algorithm_steps(), demo_diet_problem(), demo_production_planning(), demo_resource_allocation(), demo_special_cases(), Aleph::MCF_LP_Solver< Net >::solve(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().
|
inline |
|
inline |
Gets the reduced cost for a variable.
The reduced cost is the change of the objective per unit increase of a non-basic variable, the others non-basic at zero: c_j - sum_i shadow_price(i) * a_ij. At the optimum it is <= 0 when maximizing and >= 0 when minimizing, and 0 for a basic variable.
| [in] | var_idx | Variable index (0-based). |
| std::out_of_range | If var_idx is out of range. |
| std::logic_error | If not solved yet. |
Definition at line 1843 of file Simplex.H.
References ah_logic_error_if, m, and Aleph::Maximize.
Referenced by TEST().
|
inlineprivate |
|
inlineprivatenoexcept |
Definition at line 370 of file Simplex.H.
References Aleph::Simplex< T >::num_artificial, Aleph::Simplex< T >::num_rest, and Aleph::Simplex< T >::num_var.
Referenced by Aleph::Simplex< T >::compute_pivot_row(), Aleph::Simplex< T >::drive_out_artificials(), Aleph::Simplex< T >::is_artificial(), Aleph::Simplex< T >::iterate(), Aleph::Simplex< T >::load_phase1_costs(), Aleph::Simplex< T >::load_phase2_costs(), and Aleph::Simplex< T >::to_pivot().
|
inline |
Computes sensitivity range for a RHS value.
Determines the range within which the right-hand side value b_i can vary, the others fixed, while the optimal basis stays feasible, and so optimal: over it, the optimal value changes by shadow_price() per unit.
| [in] | rest_idx | Constraint index (0-based). |
| std::out_of_range | If rest_idx is out of range. |
| std::logic_error | If not solved yet. |
Definition at line 1744 of file Simplex.H.
References ah_logic_error_if, col, Aleph::SensitivityRange< T >::current_value, Aleph::SensitivityRange< T >::lower_bound, m, and Aleph::SensitivityRange< T >::upper_bound.
|
inlineprivate |
|
inlineprivatenoexcept |
Definition at line 385 of file Simplex.H.
References Aleph::Simplex< T >::eps.
Referenced by Aleph::Simplex< T >::compute_pivot_col(), and Aleph::Simplex< T >::compute_pivot_row().
|
inlinestaticprivatenoexcept |
|
inlinenoexcept |
Sets the floating-point comparison tolerance.
A value of the scaled program (see the class documentation) is taken as zero when its magnitude is below epsilon; a right-hand side, when it is below epsilon times the largest magnitude of the right-hand sides, if this one is above 1.
| [in] | epsilon | Tolerance for comparing values to zero. |
Definition at line 1049 of file Simplex.H.
Referenced by TEST().
|
inlinenoexcept |
Sets maximization mode.
Convenience method equivalent to set_optimization_type(Maximize).
Definition at line 1010 of file Simplex.H.
References Aleph::Maximize.
Referenced by TEST_F().
|
inlinenoexcept |
Sets minimization mode.
Convenience method equivalent to set_optimization_type(Minimize).
Definition at line 1001 of file Simplex.H.
References Aleph::Minimize.
Referenced by Aleph::MCF_LP_Solver< Net >::solve(), TEST_F(), and TEST_F().
|
inlinenoexcept |
Sets the optimization type.
| [in] | type | Maximize or Minimize. |
Definition at line 992 of file Simplex.H.
Referenced by TEST().
|
inlinenoexcept |
Sets the pivot selection rule.
| [in] | rule | Dantzig (default, faster) or Bland (anti-cycling). |
Bland's rule guarantees termination on degenerate problems but may be slower on non-degenerate problems. Dantzig's rule switches to it after a run of degenerate pivots, so that it ends too.
Definition at line 1023 of file Simplex.H.
Referenced by TEST().
|
inline |
Gets the shadow price (dual value) for a constraint.
The shadow price is the change of the optimal value of the objective per unit increase of the right-hand side b_i, over the range of rhs_sensitivity(): when maximizing, >= 0 for a ≤ constraint and <= 0 for a ≥ one; when minimizing, the other way. The shadow prices are an optimal solution of the dual program.
| [in] | rest_idx | Constraint index (0-based). |
| std::out_of_range | If rest_idx is out of range. |
| std::logic_error | If not solved yet. |
Definition at line 1812 of file Simplex.H.
References ah_logic_error_if, m, and Aleph::Minimize.
Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inline |
Solves the linear program.
Solves a correctly and completely specified linear program using the two-phase simplex method.
Unbounded: The objective improves without limit.Solved: Optimal solution found.Unfeasible: No feasible solution exists.| std::logic_error | If solve() has already been called. |
| std::logic_error | If no constraints have been added. |
| std::logic_error | If prepare_linear_program() was not called. |
Definition at line 1310 of file Simplex.H.
References ah_logic_error_if, and m.
Referenced by demo_algorithm_steps(), demo_diet_problem(), demo_production_planning(), demo_resource_allocation(), demo_special_cases(), Aleph::MCF_LP_Solver< Net >::solve(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and Moved_From_Case< Simplex< double > >::use().
|
inlinenoexcept |
|
inlineprivate |
Definition at line 465 of file Simplex.H.
References Aleph::and, Aleph::Simplex< T >::basic_vars, cols, Aleph::Simplex< T >::in_phase1, Aleph::Simplex< T >::m, Aleph::Simplex< T >::num_rest, Aleph::Simplex< T >::pivot_col_buffer, Aleph::Simplex< T >::pivot_row_buffer, Aleph::Simplex< T >::rhs_col(), and Aleph::Simplex< T >::tie_cost.
Referenced by Aleph::Simplex< T >::drive_out_artificials(), and Aleph::Simplex< T >::iterate().
|
inline |
Updates RHS value and re-optimizes using dual simplex.
Efficiently re-solves the problem when only RHS values change: the right-hand sides of the tableau change by the column of the inverse of the basis of the constraint, and dual_solve() restores their feasibility.
| [in] | rest_idx | Constraint index to update. |
| [in] | new_rhs | New right-hand side value. |
| std::out_of_range | If rest_idx is out of range. |
| std::logic_error | If the problem is not solved. |
Definition at line 2042 of file Simplex.H.
References ah_logic_error_if, col, m, and row.
|
inlineprivate |
Definition at line 689 of file Simplex.H.
References ah_out_of_range_error_if.
|
inline |
Verifies that the solution satisfies all constraints.
Checks that every variable is >= 0 and that every constraint holds, up to a tolerance relative to the magnitude of its terms: the largest of the tolerance of set_epsilon() and 10000 times the epsilon of T.
Definition at line 1393 of file Simplex.H.
References Aleph::EQ, Aleph::GE, Aleph::LE, Aleph::size(), and Aleph::sum().
Referenced by demo_production_planning(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and Moved_From_Case< Simplex< double > >::use().
|
inlineprivate |
Definition at line 681 of file Simplex.H.
References ah_out_of_range_error_if.
|
mutableprivate |
Definition at line 323 of file Simplex.H.
Referenced by Aleph::Simplex< T >::compute_pivot_row(), Aleph::Simplex< T >::drive_out_artificials(), Aleph::Simplex< T >::load_phase1_costs(), and Aleph::Simplex< T >::to_pivot().
|
private |
|
private |
Definition at line 355 of file Simplex.H.
Referenced by Aleph::Simplex< T >::bland_now(), and Aleph::Simplex< T >::iterate().
|
private |
Definition at line 312 of file Simplex.H.
Referenced by Aleph::Simplex< T >::same_ratio().
|
private |
Definition at line 363 of file Simplex.H.
Referenced by Aleph::Simplex< T >::compute_pivot_col(), and Aleph::Simplex< T >::to_pivot().
|
private |
Definition at line 301 of file Simplex.H.
Referenced by Aleph::Simplex< T >::compute_pivot_col(), Aleph::Simplex< T >::compute_pivot_row(), Aleph::Simplex< T >::drive_out_artificials(), Aleph::Simplex< T >::iterate(), Aleph::Simplex< T >::load_phase1_costs(), Aleph::Simplex< T >::load_phase2_costs(), and Aleph::Simplex< T >::to_pivot().
|
private |
Definition at line 307 of file Simplex.H.
Referenced by Aleph::Simplex< T >::rhs_col().
|
private |
Definition at line 306 of file Simplex.H.
Referenced by Aleph::Simplex< T >::bland_now(), Aleph::Simplex< T >::compute_pivot_row(), Aleph::Simplex< T >::drive_out_artificials(), Aleph::Simplex< T >::first_artificial(), Aleph::Simplex< T >::load_phase1_costs(), Aleph::Simplex< T >::rhs_col(), and Aleph::Simplex< T >::to_pivot().
|
private |
Definition at line 305 of file Simplex.H.
Referenced by Aleph::Simplex< T >::bland_now(), Aleph::Simplex< T >::first_artificial(), Aleph::Simplex< T >::load_phase1_costs(), and Aleph::Simplex< T >::rhs_col().
|
private |
|
private |
|
private |
|
private |
Definition at line 336 of file Simplex.H.
Referenced by Aleph::Simplex< T >::load_phase1_costs().
|
mutableprivate |
Definition at line 319 of file Simplex.H.
Referenced by Aleph::Simplex< T >::to_pivot().
|
mutableprivate |
Definition at line 318 of file Simplex.H.
Referenced by Aleph::Simplex< T >::to_pivot().
|
private |
Definition at line 311 of file Simplex.H.
Referenced by Aleph::Simplex< T >::bland_now().
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
mutableprivate |
Definition at line 315 of file Simplex.H.
Referenced by Aleph::Simplex< T >::drive_out_artificials(), and Aleph::Simplex< T >::iterate().
|
private |
Definition at line 362 of file Simplex.H.
Referenced by Aleph::Simplex< T >::compute_pivot_col(), Aleph::Simplex< T >::load_phase1_costs(), Aleph::Simplex< T >::load_phase2_costs(), and Aleph::Simplex< T >::to_pivot().
|
private |
Definition at line 350 of file Simplex.H.
Referenced by Aleph::Simplex< T >::compute_pivot_row(), and Aleph::Simplex< T >::drive_out_artificials().
|
private |
|
private |
Definition at line 351 of file Simplex.H.
Referenced by Aleph::Simplex< T >::iterate().
|
private |