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

Linear program solver using the Simplex method. More...

#include <Simplex.H>

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

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
 
Simplexoperator= (const Simplex &)=delete
 
 Simplex (Simplex &&)=default
 
Simplexoperator= (Simplex &&)=default
 
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 Tget_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.
 
Tput_restriction (const T *coefs=nullptr, ConstraintType type=ConstraintType::LE)
 Adds a constraint to the linear program.
 
Tput_ge_restriction (const T *coefs)
 Adds a ≥ constraint.
 
Tput_eq_restriction (const T *coefs)
 Adds an = constraint.
 
Tput_restriction (const DynArray< T > &coefs, ConstraintType type=ConstraintType::LE)
 Adds a constraint from a DynArray.
 
Tget_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.
 
Tget_objetive_function () noexcept
 Gets the objective function coefficients array.
 
const Tget_objetive_function () const noexcept
 Gets the objective function coefficients array (const).
 
Tget_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 Tget_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 SimplexStatsget_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< Tobjective_sensitivity (size_t var_idx) const
 Computes sensitivity range for an objective coefficient.
 
SensitivityRange< Trhs_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< Tget_all_shadow_prices () const
 Gets all shadow prices as a vector.
 
std::vector< Tget_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

int compute_pivot_col () const noexcept
 
int compute_pivot_row (int p) const noexcept
 
State select_pivot (int &p, int &q) noexcept
 
void to_pivot (size_t p, size_t q)
 
T find_value (const size_t j) const noexcept
 
void verify_var_index (const size_t i) const
 
void verify_rest_index (const size_t i) const
 
Tcreate_restriction (ConstraintType type=ConstraintType::LE)
 
size_t count_artificial_vars () const noexcept
 
void create_matrix ()
 

Private Attributes

std::unique_ptr< DynMatrix< T > > m
 
std::unique_ptr< T[]> objetive
 
DynDlist< T * > rest_list
 
DynDlist< ConstraintTyperest_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< Tpivot_row_buffer
 
std::vector< Tpivot_col_buffer
 
std::vector< intbasic_vars
 

Detailed Description

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

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.

Standard Form

The standard form of a linear program is:

...
x₁, x₂, ..., xₙ 0
DynList< T > maps(const C &c, Op op)
Classic map operation.

Non-Standard Constraints (Automatic Handling)

This implementation now handles non-standard forms directly:

  1. Minimization: Use set_minimize() instead of negating coefficients
  2. ≥ constraints: Use put_restriction(coefs, ConstraintType::GE)
  3. **= constraints**: Use put_restriction(coefs, ConstraintType::EQ)
  4. Unrestricted variables: Replace xᵢ with (xᵢ⁺ - xᵢ⁻) where both ≥ 0

Complete Example: Production Planning Problem

A factory produces two products (A and B) with limited resources:

  • Product A: profit $40/unit, needs 1 hr labor, 2 hrs machine time
  • Product B: profit $30/unit, needs 1 hr labor, 1 hr machine time
  • Available: 40 labor hours, 60 machine hours per day

Mathematical formulation:

Code:

#include <Simplex.H>
#include <iostream>
int main() {
using namespace Aleph;
// Create solver with 2 decision variables
// Set objective function: maximize 40*x_A + 30*x_B
solver.put_objetive_function_coef(0, 40.0); // x_A coefficient
solver.put_objetive_function_coef(1, 30.0); // x_B coefficient
// Add constraints (format: {coef_xA, coef_xB, RHS})
double labor[] = {1.0, 1.0, 40.0}; // x_A + x_B <= 40
double machine[] = {2.0, 1.0, 60.0}; // 2*x_A + x_B <= 60
solver.put_restriction(labor);
solver.put_restriction(machine);
// Prepare tableau and solve
solver.prepare_linear_program();
auto state = solver.solve();
solver.load_solution();
std::cout << "Optimal solution found!\n";
std::cout << "Product A: " << solver.get_solution(0) << " units\n";
std::cout << "Product B: " << solver.get_solution(1) << " units\n";
std::cout << "Maximum profit: $" << solver.objetive_value() << "\n";
if (solver.verify_solution())
std::cout << "Solution verified: all constraints satisfied.\n";
}
std::cout << "Problem is unbounded (check constraints).\n";
}
return 0;
}
// Output:
// Optimal solution found!
// Product A: 20 units
// Product B: 20 units
// Maximum profit: $1400
// Solution verified: all constraints satisfied.
Simplex algorithm for linear programming.
int main()
Linear program solver using the Simplex method.
Definition Simplex.H:227
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89

Algorithm Complexity

  • Time: O(2ⁿ) worst case, but typically polynomial in practice
  • Space: O((m+1) × (n+m+1)) for the tableau matrix

Where n = number of variables, m = number of constraints.

Template Parameters
TNumeric type for coefficients and values (typically double).
See also
DynMatrix

Definition at line 226 of file Simplex.H.

Member Enumeration Documentation

◆ PivotRule

Pivot selection rule.

  • Dantzig: Standard most-negative rule (may cycle on degenerate problems).
  • Bland: Anti-cycling rule (guaranteed termination, slower).
Enumerator
Dantzig 
Bland 

Definition at line 244 of file Simplex.H.

◆ 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 system is unbounded (design error in constraints).
  • Solved: An optimal solution has been found.
  • Unfeasible: No feasible solution exists.
Enumerator
Not_Solved 
Solving 
Unbounded 
Solved 
Unfeasible 

Definition at line 237 of file Simplex.H.

Constructor & Destructor Documentation

◆ Simplex() [1/3]

template<typename T >
Aleph::Simplex< T >::Simplex ( const size_t  n,
OptimizationType  type = OptimizationType::Maximize 
)
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.

Parameters
[in]nNumber of decision variables in the system.
[in]typeOptimization type: Maximize (default) or Minimize.
Exceptions
std::bad_allocIf memory allocation fails.
std::invalid_argumentIf n is zero.

Definition at line 605 of file Simplex.H.

References ah_invalid_argument_if, and Aleph::Simplex< T >::objetive.

◆ ~Simplex()

template<typename T >
Aleph::Simplex< T >::~Simplex ( )
inline

Destructor.

Frees all allocated restriction arrays.

Definition at line 624 of file Simplex.H.

References FunctionalMethods< Container, T >::for_each(), and Aleph::Simplex< T >::rest_list.

◆ Simplex() [2/3]

template<typename T >
Aleph::Simplex< T >::Simplex ( const Simplex< T > &  )
delete

◆ Simplex() [3/3]

template<typename T >
Aleph::Simplex< T >::Simplex ( Simplex< T > &&  )
default

Member Function Documentation

◆ compute_pivot_col()

◆ compute_pivot_row()

◆ count_artificial_vars()

template<typename T >
size_t Aleph::Simplex< T >::count_artificial_vars ( ) const
inlineprivatenoexcept

◆ create_matrix()

◆ create_restriction()

◆ dual_solve()

template<typename T >
State Aleph::Simplex< T >::dual_solve ( )
inline

Performs dual simplex iteration.

Used for re-optimization when the RHS values change. The current solution must be dual feasible (reduced costs ≥ 0 for max).

Returns
New solution state after re-optimization.
Precondition
solve() must have been called and returned Solved.

Definition at line 1525 of file Simplex.H.

References ah_logic_error_if, Aleph::Simplex< T >::basic_vars, Aleph::SimplexStats::elapsed_ms, Aleph::Simplex< T >::eps, Aleph::SimplexStats::iterations, Aleph::Simplex< T >::m, Aleph::maps(), Aleph::Simplex< T >::num_artificial, Aleph::Simplex< T >::num_rest, Aleph::Simplex< T >::num_var, Aleph::SimplexStats::pivots, Aleph::Simplex< T >::Solved, Aleph::Simplex< T >::Solving, Aleph::Simplex< T >::state, Aleph::Simplex< T >::stats, Aleph::Simplex< T >::to_pivot(), and Aleph::Simplex< T >::Unfeasible.

Referenced by Aleph::Simplex< T >::update_rhs_and_reoptimize().

◆ enable_bland_rule()

template<typename T >
void Aleph::Simplex< T >::enable_bland_rule ( )
inlinenoexcept

Enables Bland's anti-cycling rule.

Use this when dealing with potentially degenerate problems.

Definition at line 684 of file Simplex.H.

References Aleph::Simplex< T >::Bland, Aleph::SimplexStats::bland_rule_used, Aleph::Simplex< T >::pivot_rule, and Aleph::Simplex< T >::stats.

◆ find_value()

◆ get_all_reduced_costs()

template<typename T >
std::vector< T > Aleph::Simplex< T >::get_all_reduced_costs ( ) const
inline

Gets all reduced costs as a vector.

Returns
Vector of reduced costs for all variables.
Precondition
solve() must have been called and returned Solved.

Definition at line 1505 of file Simplex.H.

References ah_logic_error_if, Aleph::maps(), Aleph::Simplex< T >::num_var, Aleph::Simplex< T >::reduced_cost(), Aleph::Simplex< T >::Solved, and Aleph::Simplex< T >::state.

◆ get_all_shadow_prices()

template<typename T >
std::vector< T > Aleph::Simplex< T >::get_all_shadow_prices ( ) const
inline

Gets all shadow prices as a vector.

Returns
Vector of shadow prices for all constraints.
Precondition
solve() must have been called and returned Solved.

Definition at line 1490 of file Simplex.H.

References ah_logic_error_if, Aleph::maps(), Aleph::Simplex< T >::num_rest, Aleph::Simplex< T >::shadow_price(), Aleph::Simplex< T >::Solved, and Aleph::Simplex< T >::state.

◆ get_degenerate_pivot_count()

template<typename T >
size_t Aleph::Simplex< T >::get_degenerate_pivot_count ( ) const
inlinenoexcept

Gets the number of degenerate pivots.

A degenerate pivot is one that doesn't improve the objective.

Returns
Number of degenerate pivots in the last solve.

Definition at line 1271 of file Simplex.H.

References Aleph::SimplexStats::degenerate_pivots, and Aleph::Simplex< T >::stats.

◆ get_elapsed_time_ms()

template<typename T >
double Aleph::Simplex< T >::get_elapsed_time_ms ( ) const
inlinenoexcept

Gets the elapsed solving time.

Returns
Elapsed time in milliseconds.

Definition at line 1280 of file Simplex.H.

References Aleph::SimplexStats::elapsed_ms, and Aleph::Simplex< T >::stats.

◆ get_iteration_count()

template<typename T >
size_t Aleph::Simplex< T >::get_iteration_count ( ) const
inlinenoexcept

Gets the number of iterations performed.

Returns
Number of simplex iterations in the last solve.

Definition at line 1251 of file Simplex.H.

References Aleph::SimplexStats::iterations, and Aleph::Simplex< T >::stats.

◆ get_num_restrictions()

template<typename T >
size_t Aleph::Simplex< T >::get_num_restrictions ( ) const
inlinenoexcept

Gets the number of constraints.

Returns
Number of constraints added to the linear program.

Definition at line 852 of file Simplex.H.

References Aleph::Simplex< T >::num_rest.

◆ get_num_vars()

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

Gets the number of decision variables.

Returns
Number of variables in the linear program.

Definition at line 861 of file Simplex.H.

References Aleph::Simplex< T >::num_var.

◆ get_objetive_function() [1/2]

template<typename T >
const T * Aleph::Simplex< T >::get_objetive_function ( ) const
inlinenoexcept

Gets the objective function coefficients array (const).

Returns
Const pointer to the objective function coefficients.

Definition at line 879 of file Simplex.H.

References Aleph::DynList< T >::get(), and Aleph::Simplex< T >::objetive.

◆ get_objetive_function() [2/2]

template<typename T >
T * Aleph::Simplex< T >::get_objetive_function ( )
inlinenoexcept

Gets the objective function coefficients array.

Returns
Pointer to the objective function coefficients.

Definition at line 870 of file Simplex.H.

References Aleph::DynList< T >::get(), and Aleph::Simplex< T >::objetive.

◆ get_objetive_function_coef()

template<typename T >
const T & Aleph::Simplex< T >::get_objetive_function_coef ( size_t  i) const
inline

Gets a coefficient from the objective function.

Parameters
[in]iVariable index (0-based).
Returns
The coefficient value.
Exceptions
std::out_of_rangeIf i >= number of variables.

Definition at line 730 of file Simplex.H.

References Aleph::Simplex< T >::objetive, and Aleph::Simplex< T >::verify_var_index().

◆ get_optimization_type()

template<typename T >
OptimizationType Aleph::Simplex< T >::get_optimization_type ( ) const
inlinenoexcept

Gets the current optimization type.

Returns
Maximize or Minimize.

Definition at line 703 of file Simplex.H.

References Aleph::Simplex< T >::opt_type.

◆ get_pivot_count()

template<typename T >
size_t Aleph::Simplex< T >::get_pivot_count ( ) const
inlinenoexcept

Gets the number of pivot operations.

Returns
Number of pivots performed in the last solve.

Definition at line 1260 of file Simplex.H.

References Aleph::SimplexStats::pivots, and Aleph::Simplex< T >::stats.

◆ get_restriction()

template<typename T >
T * Aleph::Simplex< T >::get_restriction ( const size_t  rest_num)
inline

Gets a pointer to a constraint array.

Parameters
[in]rest_numConstraint index (0-based).
Returns
Pointer to the constraint coefficients array.
Exceptions
std::out_of_rangeIf rest_num >= number of constraints.

Definition at line 836 of file Simplex.H.

References LocateFunctions< Container, Type >::get_it(), Aleph::maps(), Aleph::Simplex< T >::rest_list, and Aleph::Simplex< T >::verify_rest_index().

Referenced by Aleph::Simplex< T >::get_restriction_coef().

◆ get_restriction_coef()

template<typename T >
T & Aleph::Simplex< T >::get_restriction_coef ( const size_t  rest_num,
size_t  idx 
)
inline

Gets a specific coefficient from a constraint.

Parameters
[in]rest_numConstraint index.
[in]idxVariable index within the constraint.
Returns
Reference to the coefficient.
Exceptions
std::out_of_rangeIf indices are out of range.

Definition at line 892 of file Simplex.H.

References Aleph::Simplex< T >::get_restriction(), Aleph::maps(), and Aleph::Simplex< T >::verify_var_index().

Referenced by Aleph::Simplex< T >::put_restriction_coef().

◆ get_solution()

template<typename T >
const T & Aleph::Simplex< T >::get_solution ( size_t  i) const
inlinenoexcept

Gets the solution value for a specific variable.

Parameters
[in]iVariable index (0-based).
Returns
The optimal value of variable x_i.
Precondition
load_solution() must have been called first.
Note
Does not validate index for performance. Use with valid indices.

Definition at line 1025 of file Simplex.H.

References Aleph::maps(), Aleph::Simplex< T >::num_var, and Aleph::Simplex< T >::solution.

◆ get_state()

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

Gets the current state of the solver.

Returns
Current solution state.

Definition at line 1000 of file Simplex.H.

References Aleph::Simplex< T >::state.

◆ get_stats()

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

Gets execution statistics.

Returns statistics about the most recent solve() call.

Returns
Const reference to SimplexStats structure.

Definition at line 1242 of file Simplex.H.

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

◆ is_basic_variable()

template<typename T >
bool Aleph::Simplex< T >::is_basic_variable ( size_t  var_idx) const
inline

Checks if a variable is basic in the optimal solution.

Parameters
[in]var_idxVariable index (0-based).
Returns
true if variable is in the basis, false otherwise.
Precondition
solve() must have been called and returned Solved.

Definition at line 1475 of file Simplex.H.

References Aleph::Simplex< T >::basic_vars, Aleph::maps(), Aleph::Simplex< T >::num_rest, and Aleph::Simplex< T >::verify_var_index().

Referenced by Aleph::Simplex< T >::print_sensitivity_analysis().

◆ latex_linear_program()

template<typename T >
void Aleph::Simplex< T >::latex_linear_program ( const std::string &  name) const
inline

Exports the linear program to a LaTeX file.

Generates a LaTeX representation of the objective function and all constraints.

Parameters
[in]nameOutput file name.

Definition at line 1149 of file Simplex.H.

References LocateFunctions< Container, Type >::get_it(), Aleph::maps(), Aleph::Simplex< T >::num_var, Aleph::Simplex< T >::objetive, and Aleph::Simplex< T >::rest_list.

◆ latex_matrix()

template<typename T >
void Aleph::Simplex< T >::latex_matrix ( const std::string &  name,
const int  d = 2,
const int  p = -1,
const int  q = -1 
) const
inline

Exports the simplex tableau to a LaTeX file.

Parameters
[in]nameOutput file name.
[in]dNumber of decimal places (default 2).
[in]pPivot row to highlight (-1 for none).
[in]qPivot column to highlight (-1 for none).

Definition at line 1110 of file Simplex.H.

References Aleph::Simplex< T >::m, Aleph::maps(), Aleph::Simplex< T >::num_rest, and Aleph::Simplex< T >::num_var.

Referenced by Aleph::Simplex< T >::latex_solve().

◆ latex_solve()

template<typename T >
State Aleph::Simplex< T >::latex_solve ( const char name = nullptr)
inline

Solves with LaTeX output at each iteration.

Similar to solve(), but outputs LaTeX files showing each pivot operation for educational purposes.

Parameters
[in]nameBase name for output files.
Returns
Solution state.

Definition at line 1211 of file Simplex.H.

References Aleph::Simplex< T >::latex_matrix(), Aleph::maps(), Aleph::Simplex< T >::select_pivot(), Aleph::Simplex< T >::Solved, Aleph::Simplex< T >::to_pivot(), and Aleph::Simplex< T >::Unbounded.

◆ load_solution()

template<typename T >
void Aleph::Simplex< T >::load_solution ( )
inlinenoexcept

Loads the solution values into the solution array.

Must be called after solve() returns Solved state. After this, individual variable values can be retrieved with get_solution().

Definition at line 1010 of file Simplex.H.

References Aleph::Simplex< T >::find_value(), Aleph::Simplex< T >::num_var, and Aleph::Simplex< T >::solution.

Referenced by Aleph::Simplex< T >::solve().

◆ objective_sensitivity()

template<typename T >
SensitivityRange< T > Aleph::Simplex< T >::objective_sensitivity ( size_t  var_idx) const
inline

Computes sensitivity range for an objective coefficient.

Determines the range [lower, upper] within which the objective coefficient c_j can vary without changing the optimal basis.

Parameters
[in]var_idxVariable index (0-based).
Returns
SensitivityRange structure with bounds and current value.
Precondition
solve() must have been called and returned Solved.
Exceptions
std::out_of_rangeIf var_idx is out of range.
std::logic_errorIf not solved yet.

Definition at line 1299 of file Simplex.H.

References ah_logic_error_if, Aleph::Simplex< T >::basic_vars, Aleph::SensitivityRange< T >::current_value, Aleph::Simplex< T >::eps, Aleph::SensitivityRange< T >::lower_bound, Aleph::Simplex< T >::m, Aleph::maps(), Aleph::Maximize, Aleph::Simplex< T >::num_artificial, Aleph::Simplex< T >::num_rest, Aleph::Simplex< T >::num_var, Aleph::Simplex< T >::objetive, Aleph::Simplex< T >::opt_type, Aleph::Simplex< T >::reduced_cost(), Aleph::Simplex< T >::Solved, Aleph::Simplex< T >::state, Aleph::SensitivityRange< T >::upper_bound, and Aleph::Simplex< T >::verify_var_index().

Referenced by Aleph::Simplex< T >::print_sensitivity_analysis().

◆ objetive_value()

template<typename T >
T Aleph::Simplex< T >::objetive_value ( ) const
inlinenoexcept

Computes and returns the objective function value.

Calculates Z = sum(c_i * x_i) using the current solution.

Returns
The optimal value of the objective function.
Precondition
load_solution() must have been called first.

Definition at line 1039 of file Simplex.H.

References Aleph::Simplex< T >::num_var, Aleph::Simplex< T >::objetive, Aleph::Simplex< T >::solution, and Aleph::sum().

◆ operator=() [1/2]

template<typename T >
Simplex & Aleph::Simplex< T >::operator= ( const Simplex< T > &  )
delete

◆ operator=() [2/2]

template<typename T >
Simplex & Aleph::Simplex< T >::operator= ( Simplex< T > &&  )
default

◆ prepare_linear_program()

template<typename T >
void Aleph::Simplex< T >::prepare_linear_program ( )
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 slack variables.

Exceptions
std::bad_allocIf memory allocation fails.
std::logic_errorIf no constraints have been added.

Definition at line 921 of file Simplex.H.

References ah_logic_error_if, Aleph::Simplex< T >::create_matrix(), and Aleph::Simplex< T >::num_rest.

◆ print_matrix()

template<typename T >
void Aleph::Simplex< T >::print_matrix ( ) const
inline

Prints the simplex tableau to stdout.

Useful for debugging and understanding the algorithm's progress.

Definition at line 1092 of file Simplex.H.

References Aleph::Simplex< T >::m, Aleph::maps(), Aleph::Simplex< T >::num_rest, and Aleph::Simplex< T >::num_var.

◆ print_sensitivity_analysis()

◆ print_stats()

◆ put_eq_restriction()

template<typename T >
T * Aleph::Simplex< T >::put_eq_restriction ( const T coefs)
inline

Adds an = constraint.

Convenience method for adding equality constraints.

Parameters
[in]coefsArray of n+1 coefficients.
Returns
Pointer to the internal constraint array.

Definition at line 805 of file Simplex.H.

References Aleph::EQ, Aleph::maps(), and Aleph::Simplex< T >::put_restriction().

◆ put_ge_restriction()

template<typename T >
T * Aleph::Simplex< T >::put_ge_restriction ( const T coefs)
inline

Adds a ≥ constraint.

Convenience method for adding greater-than-or-equal constraints.

Parameters
[in]coefsArray of n+1 coefficients.
Returns
Pointer to the internal constraint array.

Definition at line 793 of file Simplex.H.

References Aleph::GE, Aleph::maps(), and Aleph::Simplex< T >::put_restriction().

◆ put_objetive_function() [1/2]

template<typename T >
void Aleph::Simplex< T >::put_objetive_function ( const DynArray< T > &  coefs)
inline

Sets all objective function coefficients from a DynArray.

Parameters
[in]coefsArray of coefficients. Must have at least num_var elements.

Definition at line 740 of file Simplex.H.

References Aleph::maps(), Aleph::Simplex< T >::num_var, and Aleph::Simplex< T >::objetive.

◆ put_objetive_function() [2/2]

template<typename T >
void Aleph::Simplex< T >::put_objetive_function ( const T  coefs[])
inline

Sets all objective function coefficients from a C array.

Parameters
[in]coefsArray of coefficients. Must have at least num_var elements.

Definition at line 750 of file Simplex.H.

References Aleph::maps(), Aleph::Simplex< T >::num_var, and Aleph::Simplex< T >::objetive.

◆ put_objetive_function_coef()

template<typename T >
void Aleph::Simplex< T >::put_objetive_function_coef ( size_t  i,
const T coef 
)
inline

Sets a coefficient in the objective function.

Sets the coefficient of variable x_i in the objective function.

Parameters
[in]iVariable index (0-based).
[in]coefCoefficient value.
Exceptions
std::out_of_rangeIf i >= number of variables.

Definition at line 717 of file Simplex.H.

References Aleph::Simplex< T >::objetive, and Aleph::Simplex< T >::verify_var_index().

◆ put_restriction() [1/2]

template<typename T >
T * Aleph::Simplex< T >::put_restriction ( const DynArray< T > &  coefs,
ConstraintType  type = ConstraintType::LE 
)
inline

Adds a constraint from a DynArray.

Parameters
[in]coefsArray of n+1 coefficients.
[in]typeConstraint type: LE (≤), GE (≥), or EQ (=). Default is LE.
Returns
Pointer to the internal constraint array.
Exceptions
std::bad_allocIf memory allocation fails.

Definition at line 818 of file Simplex.H.

References Aleph::Simplex< T >::create_restriction(), Aleph::maps(), and Aleph::Simplex< T >::num_var.

◆ put_restriction() [2/2]

template<typename T >
T * Aleph::Simplex< T >::put_restriction ( const T coefs = nullptr,
ConstraintType  type = ConstraintType::LE 
)
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 =).

Parameters
[in]coefsArray of n+1 coefficients (n variable coefficients + RHS). If nullptr, creates a zero-initialized constraint.
[in]typeConstraint type: LE (≤), GE (≥), or EQ (=). Default is LE.
Returns
Pointer to the internal constraint array (can be modified).
Exceptions
std::bad_allocIf memory allocation fails.

Definition at line 772 of file Simplex.H.

References Aleph::Simplex< T >::create_restriction(), Aleph::maps(), and Aleph::Simplex< T >::num_var.

Referenced by Aleph::Simplex< T >::put_eq_restriction(), and Aleph::Simplex< T >::put_ge_restriction().

◆ put_restriction_coef()

template<typename T >
void Aleph::Simplex< T >::put_restriction_coef ( const size_t  rest_num,
const size_t  idx,
const T coef 
)
inline

Sets a specific coefficient in a constraint.

Parameters
[in]rest_numConstraint index.
[in]idxVariable index within the constraint.
[in]coefNew coefficient value.
Exceptions
std::out_of_rangeIf indices are out of range.

Definition at line 906 of file Simplex.H.

References Aleph::Simplex< T >::get_restriction_coef(), and Aleph::maps().

◆ reduced_cost()

template<typename T >
T Aleph::Simplex< T >::reduced_cost ( size_t  var_idx) const
inline

Gets the reduced cost for a variable.

The reduced cost indicates how much the objective would improve per unit increase in a non-basic variable.

Parameters
[in]var_idxVariable index (0-based).
Returns
Reduced cost value.
Precondition
solve() must have been called and returned Solved.
Exceptions
std::out_of_rangeIf var_idx is out of range.
std::logic_errorIf not solved yet.

Definition at line 1454 of file Simplex.H.

References ah_logic_error_if, Aleph::Simplex< T >::m, Aleph::maps(), Aleph::Maximize, Aleph::Simplex< T >::opt_type, Aleph::Simplex< T >::Solved, Aleph::Simplex< T >::state, and Aleph::Simplex< T >::verify_var_index().

Referenced by Aleph::Simplex< T >::get_all_reduced_costs(), Aleph::Simplex< T >::objective_sensitivity(), and Aleph::Simplex< T >::print_sensitivity_analysis().

◆ rhs_sensitivity()

template<typename T >
SensitivityRange< T > Aleph::Simplex< T >::rhs_sensitivity ( size_t  rest_idx) const
inline

Computes sensitivity range for a RHS value.

Determines the range within which the right-hand side value b_i can vary without changing the optimal basis.

Parameters
[in]rest_idxConstraint index (0-based).
Returns
SensitivityRange structure with bounds and current value.
Precondition
solve() must have been called and returned Solved.
Exceptions
std::out_of_rangeIf rest_idx is out of range.
std::logic_errorIf not solved yet.

Definition at line 1368 of file Simplex.H.

References ah_logic_error_if, Aleph::SensitivityRange< T >::current_value, Aleph::Simplex< T >::eps, LocateFunctions< Container, Type >::get_it(), Aleph::SensitivityRange< T >::lower_bound, Aleph::Simplex< T >::m, Aleph::maps(), Aleph::Simplex< T >::num_artificial, Aleph::Simplex< T >::num_rest, Aleph::Simplex< T >::num_var, Aleph::Simplex< T >::rest_list, Aleph::Simplex< T >::Solved, Aleph::Simplex< T >::state, Aleph::SensitivityRange< T >::upper_bound, and Aleph::Simplex< T >::verify_rest_index().

Referenced by Aleph::Simplex< T >::print_sensitivity_analysis().

◆ select_pivot()

◆ set_epsilon()

template<typename T >
void Aleph::Simplex< T >::set_epsilon ( T  epsilon)
inlinenoexcept

Sets the floating-point comparison tolerance.

Parameters
[in]epsilonTolerance for comparing values to zero.

Definition at line 694 of file Simplex.H.

References Aleph::Simplex< T >::eps.

◆ set_maximize()

template<typename T >
void Aleph::Simplex< T >::set_maximize ( )
inlinenoexcept

Sets maximization mode.

Convenience method equivalent to set_optimization_type(Maximize).

Definition at line 663 of file Simplex.H.

References Aleph::Maximize, and Aleph::Simplex< T >::opt_type.

◆ set_minimize()

template<typename T >
void Aleph::Simplex< T >::set_minimize ( )
inlinenoexcept

Sets minimization mode.

Convenience method equivalent to set_optimization_type(Minimize).

Definition at line 654 of file Simplex.H.

References Aleph::Minimize, and Aleph::Simplex< T >::opt_type.

◆ set_optimization_type()

template<typename T >
void Aleph::Simplex< T >::set_optimization_type ( OptimizationType  type)
inlinenoexcept

Sets the optimization type.

Parameters
[in]typeMaximize or Minimize.
Note
Must be called before prepare_linear_program().

Definition at line 645 of file Simplex.H.

References Aleph::Simplex< T >::opt_type.

◆ set_pivot_rule()

template<typename T >
void Aleph::Simplex< T >::set_pivot_rule ( PivotRule  rule)
inlinenoexcept

Sets the pivot selection rule.

Parameters
[in]ruleDantzig (default, faster) or Bland (anti-cycling).

Bland's rule guarantees termination on degenerate problems but may be slower on non-degenerate problems.

Definition at line 675 of file Simplex.H.

References Aleph::maps(), and Aleph::Simplex< T >::pivot_rule.

◆ shadow_price()

template<typename T >
T Aleph::Simplex< T >::shadow_price ( size_t  rest_idx) const
inline

Gets the shadow price (dual value) for a constraint.

The shadow price represents the marginal value of relaxing a constraint by one unit.

Parameters
[in]rest_idxConstraint index (0-based).
Returns
Shadow price (dual variable value).
Precondition
solve() must have been called and returned Solved.
Exceptions
std::out_of_rangeIf rest_idx is out of range.
std::logic_errorIf not solved yet.

Definition at line 1426 of file Simplex.H.

References ah_logic_error_if, Aleph::Simplex< T >::m, Aleph::maps(), Aleph::Minimize, Aleph::Simplex< T >::num_var, Aleph::Simplex< T >::opt_type, Aleph::Simplex< T >::Solved, Aleph::Simplex< T >::state, and Aleph::Simplex< T >::verify_rest_index().

Referenced by Aleph::Simplex< T >::get_all_shadow_prices(), and Aleph::Simplex< T >::print_sensitivity_analysis().

◆ solve()

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

Solves the linear program.

Solves a correctly and completely specified linear program using the simplex algorithm.

Returns
Solution state:
  • Unbounded: System is unbounded (constraint error).
  • Solved: Optimal solution found.
  • Unfeasible: No feasible solution exists.
Note
After solving, call load_solution() to retrieve variable values.
Warning
The solution may not satisfy all constraints if the problem was incorrectly formulated. Use verify_solution() to check.
Exceptions
std::logic_errorIf solve() has already been called.
std::logic_errorIf no constraints have been added.
std::logic_errorIf prepare_linear_program() was not called.

Definition at line 947 of file Simplex.H.

References ah_logic_error_if, Aleph::Simplex< T >::basic_vars, Aleph::SimplexStats::degenerate_pivots, Aleph::SimplexStats::elapsed_ms, Aleph::Simplex< T >::eps, Aleph::SimplexStats::iterations, Aleph::Simplex< T >::load_solution(), Aleph::Simplex< T >::m, Aleph::maps(), Aleph::Simplex< T >::Not_Solved, Aleph::Simplex< T >::num_artificial, Aleph::Simplex< T >::num_rest, Aleph::Simplex< T >::num_var, Aleph::SimplexStats::pivots, Aleph::SimplexStats::reset(), Aleph::Simplex< T >::select_pivot(), Aleph::Simplex< T >::Solved, Aleph::Simplex< T >::state, Aleph::Simplex< T >::stats, Aleph::Simplex< T >::to_pivot(), and Aleph::Simplex< T >::Unbounded.

◆ to_pivot()

◆ update_rhs_and_reoptimize()

template<typename T >
State Aleph::Simplex< T >::update_rhs_and_reoptimize ( size_t  rest_idx,
T  new_rhs 
)
inline

Updates RHS value and re-optimizes using dual simplex.

Efficiently re-solves the problem when only RHS values change.

Parameters
[in]rest_idxConstraint index to update.
[in]new_rhsNew right-hand side value.
Returns
Solution state after re-optimization.
Precondition
solve() must have been called and returned Solved.

Definition at line 1605 of file Simplex.H.

References ah_logic_error_if, Aleph::Simplex< T >::dual_solve(), LocateFunctions< Container, Type >::get_it(), Aleph::Simplex< T >::m, Aleph::maps(), Aleph::Simplex< T >::num_artificial, Aleph::Simplex< T >::num_rest, Aleph::Simplex< T >::num_var, Aleph::Simplex< T >::rest_list, Aleph::Simplex< T >::Solved, Aleph::Simplex< T >::state, and Aleph::Simplex< T >::verify_rest_index().

◆ verify_rest_index()

◆ verify_solution()

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

Verifies that the solution satisfies all constraints.

Checks each constraint to ensure the solution does not violate any less-than-or-equal-to bounds.

Returns
true if all constraints are satisfied, false otherwise.
Precondition
load_solution() must have been called first.

Definition at line 1056 of file Simplex.H.

References Aleph::Simplex< T >::eps, Aleph::EQ, Aleph::GE, LocateFunctions< Container, Type >::get_it(), Aleph::LE, Aleph::maps(), Aleph::Simplex< T >::num_var, Aleph::Simplex< T >::rest_list, Aleph::Simplex< T >::rest_types, Aleph::Simplex< T >::solution, and Aleph::sum().

◆ verify_var_index()

Member Data Documentation

◆ basic_vars

◆ eps

◆ m

◆ num_artificial

◆ num_rest

◆ num_var

◆ objetive

◆ opt_type

◆ pivot_col_buffer

template<typename T >
std::vector<T> Aleph::Simplex< T >::pivot_col_buffer
mutableprivate

Definition at line 439 of file Simplex.H.

Referenced by Aleph::Simplex< T >::to_pivot().

◆ pivot_row_buffer

template<typename T >
std::vector<T> Aleph::Simplex< T >::pivot_row_buffer
mutableprivate

Definition at line 438 of file Simplex.H.

Referenced by Aleph::Simplex< T >::to_pivot().

◆ pivot_rule

◆ rest_list

◆ rest_types

◆ solution

◆ state

◆ stats


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