Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::RevisedSimplex< T > Class Template Reference

Revised Simplex algorithm for linear programming. More...

#include <Simplex.H>

Collaboration diagram for Aleph::RevisedSimplex< T >:
[legend]

Public Types

enum  State {
  Not_Solved , Solving , Unbounded , Solved ,
  Unfeasible
}
 Solution states for the linear program. More...
 

Public Member Functions

 RevisedSimplex (size_t num_vars, size_t num_constraints)
 Constructs a Revised Simplex solver.
 
 RevisedSimplex (const RevisedSimplex &other)=default
 Copies the program of another solver.
 
 RevisedSimplex (RevisedSimplex &&other) noexcept
 Takes the program of another solver, which is left as an empty program, without variables nor constraints.
 
RevisedSimplex & operator= (const RevisedSimplex &other)=default
 Copies the program of another solver.
 
RevisedSimplex & operator= (RevisedSimplex &&other) noexcept
 Exchanges this solver with another, which is left with the previous program of this one.
 
void swap (RevisedSimplex &other) noexcept
 Exchanges in O(1) this solver and another.
 
void set_objective (size_t j, T coef)
 Sets objective coefficient.
 
void set_objective (const T *coefs)
 Sets all objective coefficients.
 
void set_constraint (size_t i, size_t j, T coef)
 Sets constraint coefficient.
 
void set_rhs (size_t i, T rhs)
 Sets RHS value for a constraint.
 
void set_constraint_row (size_t i, const T *coefs, T rhs)
 Adds a complete constraint.
 
void set_refactorization_frequency (size_t freq)
 Sets refactorization frequency.
 
State solve ()
 Solves the linear program.
 
T get_solution (size_t j) const
 Gets solution value for variable j.
 
T objective_value () const
 Computes objective function value.
 
State get_state () const noexcept
 Gets current state.
 
const SimplexStats & get_stats () const noexcept
 Gets execution statistics.
 
size_t get_num_vars () const noexcept
 Gets number of variables.
 
size_t get_num_constraints () const noexcept
 Gets number of constraints.
 
bool verify_solution () const
 Verifies solution satisfies all constraints.
 
void print_stats () const
 Prints statistics to stdout.
 

Private Member Functions

size_t num_columns () const noexcept
 
bool is_artificial (const size_t j) const noexcept
 
void get_column (size_t j, std::vector< T > &col) const
 
void ftran (const std::vector< T > &a, std::vector< T > &y) const
 
void btran (const std::vector< T > &c_B, std::vector< T > &pi_out) const
 
T compute_reduced_cost (size_t j) const
 
bool bland_now () const noexcept
 
int select_entering_variable (const T &tol)
 
int select_leaving_variable (int entering, T &theta)
 
void update_basis_inverse (int leaving_row, const std::vector< T > &d_col)
 
void pivot (int leaving, int entering, const T &theta)
 
void refactorize ()
 
State iterate (const T &tol)
 
void drive_out_artificials ()
 
void scale ()
 
 RevisedSimplex () noexcept
 

Static Private Member Functions

static T scale_of (const T &magnitude) noexcept
 

Private Attributes

size_t m
 
size_t n
 
std::vector< T > c
 
std::vector< std::vector< T > > A
 
std::vector< T > b
 
std::vector< std::vector< T > > As
 
std::vector< T > bs
 
std::vector< T > cs
 
std::vector< T > row_scale
 
std::vector< T > col_scale
 
std::vector< size_t > art_row
 
std::vector< int > basic
 
std::vector< bool > is_basic
 
std::vector< std::vector< T > > B_inv_explicit
 
std::vector< T > x_B
 
std::vector< T > solution
 
std::vector< T > pi
 
std::vector< T > d
 
std::vector< T > work
 
std::vector< T > cost
 
T eps
 
size_t refactor_frequency
 
size_t refactorizations = 0
 
size_t since_refactor = 0
 
T tol_coef = T{0}
 
T tol_rhs = T{0}
 
T tol_cost = T{0}
 
size_t degenerate_run = 0
 
State state
 
SimplexStats stats
 

Detailed Description

template<typename T>
class Aleph::RevisedSimplex< T >

Revised Simplex algorithm for linear programming.

Maximizes c·x subject to A·x ≤ b and x ≥ 0, where the entries of b may be negative. The Revised Simplex method is a variant of the standard Simplex algorithm that maintains only the basis inverse B⁻¹ instead of the full tableau, which saves memory and work when there are many more variables than constraints.

Key Differences from Standard Simplex

Aspect Standard Simplex Revised Simplex
Storage Full tableau O((m+1)×(n+m+1)) B⁻¹ + original data O(m² + mn)
Pivot Update entire tableau Update only B⁻¹
Pricing Read from tableau Compute π = c_B·B⁻¹, then c̄_j = c_j - π·a_j
Best for Small/dense problems Large/sparse problems

Algorithm Overview

  1. Initialize: the basis has the slack variable of each row whose b_i is >= 0, and an artificial variable, of column -e_i, of each other row
  2. Phase I, if there are artificial variables: maximize minus their sum; if it stays below zero, the problem is infeasible. The artificial variables that are still basic, at zero, leave the basis, but for those of rows that are combinations of others, and an artificial variable never enters it again
  3. Phase II, from that basis, on the objective; each phase iterates:
    • Pricing: compute dual prices π = c_B · B⁻¹
    • Select entering: the j with the most positive c̄_j = c_j - π · a_j (with Bland's rule, the first one)
    • FTRAN: compute d = B⁻¹ · a_j (tableau column)
    • Ratio test: find the leaving variable using d
    • Update: B⁻¹ by the pivot on d, in place

Every refactorization-frequency pivots, B⁻¹ is computed again from the basis by Gauss-Jordan elimination, and so are the values of the basic variables, so that the rounding of the updates does not build up. As Simplex, it works on the program with each row, each column and the objective multiplied by a power of two, so that its tolerances do not depend on the units of the data, and scales the solution back. A run of degenerate pivots switches to Bland's rule until a pivot improves the objective, so that the method always ends.

Performance

  • Memory: O(m² + mn) vs O(mn + m²) for standard
  • Time per iteration: O(m² + mn), and O(m³) for a refactorization
  • Best for: n >> m (many variables, few constraints)
Template Parameters
TNumeric type for coefficients (typically double).
See also
Simplex

Definition at line 2175 of file Simplex.H.

Member Enumeration Documentation

◆ State

template<typename T >
enum Aleph::RevisedSimplex::State

Solution states for the linear program.

Enumerator
Not_Solved 
Solving 
Unbounded 
Solved 
Unfeasible 

Definition at line 2179 of file Simplex.H.

Constructor & Destructor Documentation

◆ RevisedSimplex() [1/4]

template<typename T >
Aleph::RevisedSimplex< T >::RevisedSimplex ( )
inlineprivatenoexcept

Definition at line 2616 of file Simplex.H.

◆ RevisedSimplex() [2/4]

template<typename T >
Aleph::RevisedSimplex< T >::RevisedSimplex ( size_t  num_vars,
size_t  num_constraints 
)
inline

Constructs a Revised Simplex solver.

Parameters
[in]num_varsNumber of decision variables.
[in]num_constraintsNumber of constraints.
Exceptions
std::invalid_argumentIf num_vars or num_constraints is zero.

Definition at line 2629 of file Simplex.H.

References ah_invalid_argument_if.

◆ RevisedSimplex() [3/4]

template<typename T >
Aleph::RevisedSimplex< T >::RevisedSimplex ( const RevisedSimplex< T > &  other)
default

Copies the program of another solver.

Parameters
[in]othersolver to copy.
Exceptions
std::bad_allocIf memory allocation fails.

◆ RevisedSimplex() [4/4]

template<typename T >
Aleph::RevisedSimplex< T >::RevisedSimplex ( RevisedSimplex< T > &&  other)
inlinenoexcept

Takes the program of another solver, which is left as an empty program, without variables nor constraints.

The implicit move took the vectors but copied their sizes: the moved-from solver then passed its index checks and wrote into vectors that it no longer had.

Parameters
[in,out]othersolver to move from.

Definition at line 2662 of file Simplex.H.

References Aleph::RevisedSimplex< T >::swap().

Member Function Documentation

◆ bland_now()

◆ btran()

template<typename T >
void Aleph::RevisedSimplex< T >::btran ( const std::vector< T > &  c_B,
std::vector< T > &  pi_out 
) const
inlineprivate

◆ compute_reduced_cost()

◆ drive_out_artificials()

◆ ftran()

template<typename T >
void Aleph::RevisedSimplex< T >::ftran ( const std::vector< T > &  a,
std::vector< T > &  y 
) const
inlineprivate

◆ get_column()

◆ get_num_constraints()

template<typename T >
size_t Aleph::RevisedSimplex< T >::get_num_constraints ( ) const
inlinenoexcept

◆ get_num_vars()

template<typename T >
size_t Aleph::RevisedSimplex< T >::get_num_vars ( ) const
inlinenoexcept

◆ get_solution()

template<typename T >
T Aleph::RevisedSimplex< T >::get_solution ( size_t  j) const
inline

Gets solution value for variable j.

Parameters
[in]jVariable index.
Returns
Optimal value of variable x_j.
Exceptions
std::out_of_rangeIf j is out of range.

Definition at line 2910 of file Simplex.H.

References ah_out_of_range_error_if, Aleph::RevisedSimplex< T >::n, and Aleph::RevisedSimplex< T >::solution.

Referenced by TEST(), TEST_F(), and Moved_From_Case< RevisedSimplex< double > >::use().

◆ get_state()

template<typename T >
State Aleph::RevisedSimplex< T >::get_state ( ) const
inlinenoexcept

◆ get_stats()

template<typename T >
const SimplexStats & Aleph::RevisedSimplex< T >::get_stats ( ) const
inlinenoexcept

Gets execution statistics.

Definition at line 2932 of file Simplex.H.

References Aleph::RevisedSimplex< T >::stats.

Referenced by TEST(), TEST_F(), and TEST_F().

◆ is_artificial()

template<typename T >
bool Aleph::RevisedSimplex< T >::is_artificial ( const size_t  j) const
inlineprivatenoexcept

◆ iterate()

◆ num_columns()

template<typename T >
size_t Aleph::RevisedSimplex< T >::num_columns ( ) const
inlineprivatenoexcept

◆ objective_value()

template<typename T >
T Aleph::RevisedSimplex< T >::objective_value ( ) const
inline

Computes objective function value.

Returns
Optimal objective value.

Definition at line 2920 of file Simplex.H.

References Aleph::RevisedSimplex< T >::c, Aleph::RevisedSimplex< T >::n, Aleph::RevisedSimplex< T >::solution, and Aleph::sum().

Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ operator=() [1/2]

template<typename T >
RevisedSimplex & Aleph::RevisedSimplex< T >::operator= ( const RevisedSimplex< T > &  other)
default

Copies the program of another solver.

Parameters
[in]othersolver to copy.
Returns
this solver.
Exceptions
std::bad_allocIf memory allocation fails.

◆ operator=() [2/2]

template<typename T >
RevisedSimplex & Aleph::RevisedSimplex< T >::operator= ( RevisedSimplex< T > &&  other)
inlinenoexcept

Exchanges this solver with another, which is left with the previous program of this one.

Parameters
[in,out]othersolver to move from.
Returns
this solver.

Definition at line 2681 of file Simplex.H.

References Aleph::RevisedSimplex< T >::swap().

◆ pivot()

◆ print_stats()

◆ refactorize()

◆ scale()

◆ scale_of()

template<typename T >
static T Aleph::RevisedSimplex< T >::scale_of ( const T &  magnitude)
inlinestaticprivatenoexcept

Definition at line 2567 of file Simplex.H.

Referenced by Aleph::RevisedSimplex< T >::scale().

◆ select_entering_variable()

◆ select_leaving_variable()

◆ set_constraint()

template<typename T >
void Aleph::RevisedSimplex< T >::set_constraint ( size_t  i,
size_t  j,
T  coef 
)
inline

Sets constraint coefficient.

Parameters
[in]iConstraint index.
[in]jVariable index.
[in]coefCoefficient value.
Exceptions
std::out_of_rangeIf i or j is out of range.

Definition at line 2756 of file Simplex.H.

References Aleph::RevisedSimplex< T >::A, ah_out_of_range_error_if, Aleph::RevisedSimplex< T >::m, and Aleph::RevisedSimplex< T >::n.

Referenced by TEST(), TEST(), TEST(), TEST(), TEST_F(), and Moved_From_Case< RevisedSimplex< double > >::use().

◆ set_constraint_row()

template<typename T >
void Aleph::RevisedSimplex< T >::set_constraint_row ( size_t  i,
const T *  coefs,
T  rhs 
)
inline

Adds a complete constraint.

Parameters
[in]iConstraint index.
[in]coefsArray of n coefficients.
[in]rhsRight-hand side value, which may be negative.
Exceptions
std::out_of_rangeIf i is out of range.

Definition at line 2784 of file Simplex.H.

References Aleph::RevisedSimplex< T >::A, ah_out_of_range_error_if, Aleph::RevisedSimplex< T >::b, Aleph::RevisedSimplex< T >::m, and Aleph::RevisedSimplex< T >::n.

Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ set_objective() [1/2]

template<typename T >
void Aleph::RevisedSimplex< T >::set_objective ( const T *  coefs)
inline

Sets all objective coefficients.

Parameters
[in]coefsArray of n coefficients.

Definition at line 2742 of file Simplex.H.

References Aleph::RevisedSimplex< T >::c, and Aleph::RevisedSimplex< T >::n.

◆ set_objective() [2/2]

template<typename T >
void Aleph::RevisedSimplex< T >::set_objective ( size_t  j,
T  coef 
)
inline

Sets objective coefficient.

Parameters
[in]jVariable index.
[in]coefCoefficient value.
Exceptions
std::out_of_rangeIf j is out of range.

Definition at line 2732 of file Simplex.H.

References ah_out_of_range_error_if, Aleph::RevisedSimplex< T >::c, and Aleph::RevisedSimplex< T >::n.

Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and Moved_From_Case< RevisedSimplex< double > >::use().

◆ set_refactorization_frequency()

template<typename T >
void Aleph::RevisedSimplex< T >::set_refactorization_frequency ( size_t  freq)
inline

Sets refactorization frequency.

B⁻¹ is recomputed explicitly every N pivots to maintain numerical stability.

Parameters
[in]freqRefactorization frequency (default: max(50, m)).
Exceptions
std::invalid_argumentIf freq is zero.

Definition at line 2801 of file Simplex.H.

References ah_invalid_argument_if, and Aleph::RevisedSimplex< T >::refactor_frequency.

Referenced by TEST().

◆ set_rhs()

template<typename T >
void Aleph::RevisedSimplex< T >::set_rhs ( size_t  i,
T  rhs 
)
inline

Sets RHS value for a constraint.

Parameters
[in]iConstraint index.
[in]rhsRight-hand side value, which may be negative.
Exceptions
std::out_of_rangeIf i is out of range.

Definition at line 2770 of file Simplex.H.

References ah_out_of_range_error_if, Aleph::RevisedSimplex< T >::b, and Aleph::RevisedSimplex< T >::m.

Referenced by TEST(), TEST(), TEST(), TEST(), TEST_F(), and Moved_From_Case< RevisedSimplex< double > >::use().

◆ solve()

template<typename T >
State Aleph::RevisedSimplex< T >::solve ( )
inline

Solves the linear program.

Returns
Solution state (Solved, Unbounded, or Unfeasible).
Exceptions
std::logic_errorIf solve() has already been called.

Definition at line 2813 of file Simplex.H.

References ah_logic_error_if, Aleph::RevisedSimplex< T >::art_row, Aleph::RevisedSimplex< T >::As, Aleph::RevisedSimplex< T >::B_inv_explicit, Aleph::RevisedSimplex< T >::basic, Aleph::RevisedSimplex< T >::bs, Aleph::RevisedSimplex< T >::col_scale, Aleph::RevisedSimplex< T >::cost, Aleph::RevisedSimplex< T >::cs, Aleph::RevisedSimplex< T >::drive_out_artificials(), Aleph::SimplexStats::elapsed_ms, Aleph::RevisedSimplex< T >::eps, Aleph::RevisedSimplex< T >::is_artificial(), Aleph::RevisedSimplex< T >::is_basic, Aleph::RevisedSimplex< T >::iterate(), Aleph::RevisedSimplex< T >::m, Aleph::RevisedSimplex< T >::n, Aleph::RevisedSimplex< T >::Not_Solved, Aleph::RevisedSimplex< T >::num_columns(), Aleph::RevisedSimplex< T >::refactorizations, Aleph::SimplexStats::reset(), Aleph::RevisedSimplex< T >::row_scale, Aleph::RevisedSimplex< T >::scale(), Aleph::RevisedSimplex< T >::since_refactor, Aleph::RevisedSimplex< T >::solution, Aleph::RevisedSimplex< T >::Solving, Aleph::RevisedSimplex< T >::state, Aleph::RevisedSimplex< T >::stats, Aleph::sum(), Aleph::RevisedSimplex< T >::tol_coef, Aleph::RevisedSimplex< T >::tol_cost, Aleph::RevisedSimplex< T >::tol_rhs, Aleph::RevisedSimplex< T >::Unfeasible, and Aleph::RevisedSimplex< T >::x_B.

Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and Moved_From_Case< RevisedSimplex< double > >::use().

◆ swap()

◆ update_basis_inverse()

template<typename T >
void Aleph::RevisedSimplex< T >::update_basis_inverse ( int  leaving_row,
const std::vector< T > &  d_col 
)
inlineprivate

◆ verify_solution()

template<typename T >
bool Aleph::RevisedSimplex< T >::verify_solution ( ) const
inline

Verifies 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: 10 times the tolerance of the solver.

Returns
true if feasible, false otherwise.

Definition at line 2948 of file Simplex.H.

References Aleph::RevisedSimplex< T >::A, Aleph::RevisedSimplex< T >::b, Aleph::RevisedSimplex< T >::eps, Aleph::RevisedSimplex< T >::m, Aleph::RevisedSimplex< T >::n, Aleph::size(), and Aleph::RevisedSimplex< T >::solution.

Referenced by TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), and Moved_From_Case< RevisedSimplex< double > >::use().

Member Data Documentation

◆ A

◆ art_row

◆ As

template<typename T >
std::vector<std::vector<T> > Aleph::RevisedSimplex< T >::As
private

◆ b

◆ B_inv_explicit

◆ basic

◆ bs

◆ c

◆ col_scale

template<typename T >
std::vector<T> Aleph::RevisedSimplex< T >::col_scale
private

◆ cost

◆ cs

template<typename T >
std::vector<T> Aleph::RevisedSimplex< T >::cs
private

◆ d

◆ degenerate_run

template<typename T >
size_t Aleph::RevisedSimplex< T >::degenerate_run = 0
private

◆ eps

◆ is_basic

◆ m

◆ n

◆ pi

◆ refactor_frequency

template<typename T >
size_t Aleph::RevisedSimplex< T >::refactor_frequency
private

◆ refactorizations

◆ row_scale

template<typename T >
std::vector<T> Aleph::RevisedSimplex< T >::row_scale
private

◆ since_refactor

◆ solution

◆ state

◆ stats

◆ tol_coef

◆ tol_cost

template<typename T >
T Aleph::RevisedSimplex< T >::tol_cost = T{0}
private

◆ tol_rhs

template<typename T >
T Aleph::RevisedSimplex< T >::tol_rhs = T{0}
private

◆ work

◆ x_B


The documentation for this class was generated from the following file: