ʻOhana
Population structure, admixture history, and selection using learning methods.
Data Structures | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes
jade::basic_lemke< TValue > Class Template Reference

A template for a class that implements Lemke's algorithm. More...

#include <jade.lemke.hpp>

+ Collaboration diagram for jade::basic_lemke< TValue >:

Data Structures

struct  state
 Possible states of the algorithm. More...
 

Public Types

typedef TValue value_type
 The value type. More...
 
typedef basic_matrix< value_typematrix_type
 The matrix type. More...
 
typedef std::vector< size_t > labels_type
 The labels type. More...
 
typedef state::type state_type
 The state type. More...
 

Public Member Functions

 basic_lemke (const matrix_type &tableau)
 Initializes a new instance of the class based on the specified tableau. More...
 
 basic_lemke (const matrix_type &m, const matrix_type &q)
 Initializes a new instance of the class based on the specified M and Q matrices. More...
 
 basic_lemke (const matrix_type &q, const matrix_type &a, const matrix_type &c, const matrix_type &b)
 Initializes a new instance of the class based on the specified Q and A matrices and c and b vectors. More...
 
std::string format_label (const size_t label) const
 
const labels_typeget_labels () const
 
matrix_type get_output () const
 
size_t get_pivot_col () const
 
size_t get_pivot_row () const
 
state_type get_state () const
 
const matrix_typeget_tableau () const
 
bool is_executing () const
 
bool iterate ()
 Performs one step of the algorithm. More...
 
bool solve ()
 Executes the algorithm until it has completed or has aborted. More...
 
std::string str () const
 

Static Public Member Functions

static bool solve (matrix_type &out, const matrix_type &tableau)
 Attempts to solve the linear complementarity problem using Lemke's Algorithm. If successful, this method stores the z vector into the output parameter. More...
 
static bool solve (matrix_type &out, const matrix_type &m, const matrix_type &q)
 Attempts to solve the linear complementarity problem using Lemke's Algorithm. If successful, this method stores the z vector into the output parameter. More...
 
static bool solve (matrix_type &out, const matrix_type &q, const matrix_type &a, const matrix_type &c, const matrix_type &b)
 Attempts to solve the linear complementarity problem using Lemke's Algorithm. If successful, this method stores the z vector into the output parameter. More...
 

Static Public Attributes

static constexpr auto invalid_index
 The value associated with an invalid or unassigned index. More...
 

Detailed Description

template<typename TValue>
class jade::basic_lemke< TValue >

A template for a class that implements Lemke's algorithm.

Definition at line 18 of file jade.lemke.hpp.

Member Typedef Documentation

◆ labels_type

template<typename TValue >
typedef std::vector<size_t> jade::basic_lemke< TValue >::labels_type

The labels type.

Definition at line 72 of file jade.lemke.hpp.

◆ matrix_type

template<typename TValue >
typedef basic_matrix<value_type> jade::basic_lemke< TValue >::matrix_type

The matrix type.

Definition at line 69 of file jade.lemke.hpp.

◆ state_type

template<typename TValue >
typedef state::type jade::basic_lemke< TValue >::state_type

The state type.

Definition at line 75 of file jade.lemke.hpp.

◆ value_type

template<typename TValue >
typedef TValue jade::basic_lemke< TValue >::value_type

The value type.

Definition at line 66 of file jade.lemke.hpp.

Constructor & Destructor Documentation

◆ basic_lemke() [1/3]

template<typename TValue >
jade::basic_lemke< TValue >::basic_lemke ( const matrix_type tableau)
inlineexplicit

Initializes a new instance of the class based on the specified tableau.

Parameters
tableauThe tableau.

Definition at line 81 of file jade.lemke.hpp.

83  : _labels ()
84  , _pivot_col (invalid_index)
85  , _pivot_row (invalid_index)
86  , _state (state::executing)
87  , _tableau (tableau)
88  {
89  assert(tableau.get_height() > 0);
90  assert(tableau.get_width() == tableau.get_height() * 2 + 2);
91 
92  const auto & t = _tableau;
93  const auto n = t.get_height();
94  const auto z0 = n + n;
95 
96  //
97  // Initially add the labels.
98  //
99  _labels.reserve(n);
100  for (size_t i = 0; i < n; i++)
101  _labels.push_back(i);
102 
103  //
104  // The first pivot column is z_0; attempt to find the first pivot
105  // row, but abort if necessary.
106  //
107  _pivot_col = z0;
108  if (!_find_initial_pivot_row())
109  _terminate(state::aborted_initialization);
110  }
+ Here is the call graph for this function:

◆ basic_lemke() [2/3]

template<typename TValue >
jade::basic_lemke< TValue >::basic_lemke ( const matrix_type m,
const matrix_type q 
)
inline

Initializes a new instance of the class based on the specified M and Q matrices.

Parameters
mThe M matrix.
qThe Q matrix.

Definition at line 116 of file jade.lemke.hpp.

119  : basic_lemke(_create_tableau(m, q))
120  {
121  }

◆ basic_lemke() [3/3]

template<typename TValue >
jade::basic_lemke< TValue >::basic_lemke ( const matrix_type q,
const matrix_type a,
const matrix_type c,
const matrix_type b 
)
inline

Initializes a new instance of the class based on the specified Q and A matrices and c and b vectors.

Parameters
qThe Q matrix.
aThe A matrix.
cThe c vector.
bThe b vector.

Definition at line 127 of file jade.lemke.hpp.

132  : basic_lemke(_create_tableau(q, a, c, b))
133  {
134  }

Member Function Documentation

◆ format_label()

template<typename TValue >
std::string jade::basic_lemke< TValue >::format_label ( const size_t  label) const
inline
Returns
The specified label index as a string; labels are formatted as w_1, w_2, ..., w_n; z_0, z_1, ..., z_n; or q.
Parameters
labelThe label to format.

Definition at line 140 of file jade.lemke.hpp.

143  {
144  const auto & t = _tableau;
145  const auto n = t.get_height();
146  const auto z1 = n;
147  const auto z0 = n + n;
148  const auto q = n + n + 1;
149 
150  std::ostringstream out;
151  if (label < z1) out << "w_" << label + 1;
152  else if (label < z0) out << "z_" << label + 1 - n;
153  else if (label == z0) out << "z_0";
154  else if (label == q) out << "q";
155  else out << label;
156  return out.str();
157  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_labels()

template<typename TValue >
const labels_type& jade::basic_lemke< TValue >::get_labels ( ) const
inline
Returns
The vector of labels.

Definition at line 162 of file jade.lemke.hpp.

163  {
164  return _labels;
165  }

◆ get_output()

template<typename TValue >
matrix_type jade::basic_lemke< TValue >::get_output ( ) const
inline
Returns
The output.

Definition at line 170 of file jade.lemke.hpp.

171  {
172  const auto & t = _tableau;
173  const auto n = t.get_height();
174  const auto z1 = n;
175  const auto q = n + n + 1;
176 
177  matrix_type out (n, 1);
178 
179  for (size_t i = 0; i < n; i++)
180  {
181  const auto label = _labels[i];
182  if (label >= z1)
183  out[label - z1] = t(i, q);
184  }
185 
186  return out;
187  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_pivot_col()

template<typename TValue >
size_t jade::basic_lemke< TValue >::get_pivot_col ( ) const
inline
Returns
The pivot column, or invalid_index if the algorithm has terminated.

Definition at line 193 of file jade.lemke.hpp.

194  {
195  return _pivot_col;
196  }

◆ get_pivot_row()

template<typename TValue >
size_t jade::basic_lemke< TValue >::get_pivot_row ( ) const
inline
Returns
The pivot row, or invalid_index if the algorithm has terminated.

Definition at line 202 of file jade.lemke.hpp.

203  {
204  return _pivot_row;
205  }

◆ get_state()

template<typename TValue >
state_type jade::basic_lemke< TValue >::get_state ( ) const
inline
Returns
The state.

Definition at line 210 of file jade.lemke.hpp.

211  {
212  return _state;
213  }

◆ get_tableau()

template<typename TValue >
const matrix_type& jade::basic_lemke< TValue >::get_tableau ( ) const
inline
Returns
The tableau.

Definition at line 218 of file jade.lemke.hpp.

219  {
220  return _tableau;
221  }

◆ is_executing()

template<typename TValue >
bool jade::basic_lemke< TValue >::is_executing ( ) const
inline
Returns
True if the algorithm is still executing; otherwise, false.

Definition at line 226 of file jade.lemke.hpp.

227  {
228  return _state == state::executing;
229  }

◆ iterate()

template<typename TValue >
bool jade::basic_lemke< TValue >::iterate ( )
inline

Performs one step of the algorithm.

Returns
True if the still executing; otherwise, false.

Definition at line 235 of file jade.lemke.hpp.

236  {
237  if (_state != state::executing)
238  return false;
239 
240  if (!_eliminate())
241  return _terminate(state::aborted_elimination);
242 
243  if (!_relabel())
244  return _terminate(state::completed);
245 
246  if (!_find_pivot_row())
247  return _terminate(state::aborted_pivot);
248 
249  return true;
250  }
+ Here is the caller graph for this function:

◆ solve() [1/4]

template<typename TValue >
bool jade::basic_lemke< TValue >::solve ( )
inline

Executes the algorithm until it has completed or has aborted.

Returns
True if completed; otherwise, false.

Definition at line 256 of file jade.lemke.hpp.

257  {
258  while (_state == state::executing)
259  iterate();
260 
261  return _state == state::completed;
262  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ solve() [2/4]

template<typename TValue >
static bool jade::basic_lemke< TValue >::solve ( matrix_type out,
const matrix_type m,
const matrix_type q 
)
inlinestatic

Attempts to solve the linear complementarity problem using Lemke's Algorithm. If successful, this method stores the z vector into the output parameter.

Returns
True if successful; otherwise, false.
Parameters
outThe output vector.
mThe M matrix.
qThe q vector.

Definition at line 290 of file jade.lemke.hpp.

294  {
295  return solve(out, _create_tableau(m, q));
296  }
+ Here is the call graph for this function:

◆ solve() [3/4]

template<typename TValue >
static bool jade::basic_lemke< TValue >::solve ( matrix_type out,
const matrix_type q,
const matrix_type a,
const matrix_type c,
const matrix_type b 
)
inlinestatic

Attempts to solve the linear complementarity problem using Lemke's Algorithm. If successful, this method stores the z vector into the output parameter.

Returns
True if successful; otherwise, false.
Parameters
outThe output vector.
qThe Q matrix.
aThe A matrix.
cThe c vector.
bThe b vector.

Definition at line 305 of file jade.lemke.hpp.

311  {
312  return solve(out, _create_tableau(q, a, c, b));
313  }
+ Here is the call graph for this function:

◆ solve() [4/4]

template<typename TValue >
static bool jade::basic_lemke< TValue >::solve ( matrix_type out,
const matrix_type tableau 
)
inlinestatic

Attempts to solve the linear complementarity problem using Lemke's Algorithm. If successful, this method stores the z vector into the output parameter.

Returns
True if successful; otherwise, false.
Parameters
outThe output vector.
tableauThe tableau.

Definition at line 271 of file jade.lemke.hpp.

274  {
275  basic_lemke lemke (tableau);
276  if (!lemke.solve())
277  return false;
278 
279  out = lemke.get_output();
280  return true;
281  }
+ Here is the call graph for this function:

◆ str()

template<typename TValue >
std::string jade::basic_lemke< TValue >::str ( ) const
inline
Returns
A string representation of the class.

Definition at line 318 of file jade.lemke.hpp.

319  {
320  const auto & t = _tableau;
321  const auto n = t.get_height();
322  const auto q = n + n + 1;
323 
324  static const size_t cx = 8;
325  std::ostringstream out;
326  out << std::setprecision(3)
327  << std::setw(cx) << "BV";
328 
329  for (size_t j = 0; j <= q; j++)
330  out << std::setw(cx) << format_label(j);
331  out << std::endl;
332 
333  for (size_t i = 0; i < n; i++)
334  {
335  out << std::setw(cx) << format_label(_labels[i]);
336  for (size_t j = 0; j <= q; j++)
337  out << std::setw(cx) << t(i, j);
338  out << std::endl;
339  }
340 
341  out << std::endl
342  << "state: " << state::str(_state) << std::endl
343  << "pivot: ";
344 
345  if (_pivot_row == invalid_index)
346  out << "<none>";
347  else
348  out << "{ " << format_label(_pivot_row)
349  << ", " << format_label(_pivot_col) << " }";
350 
351  out << std::endl << std::endl;
352  return out.str();
353  }
+ Here is the call graph for this function:

Field Documentation

◆ invalid_index

template<typename TValue >
constexpr auto jade::basic_lemke< TValue >::invalid_index
staticconstexpr
Initial value:
=
std::numeric_limits<size_t>::max()

The value associated with an invalid or unassigned index.

Definition at line 24 of file jade.lemke.hpp.


The documentation for this class was generated from the following file:
jade::basic_lemke::solve
bool solve()
Executes the algorithm until it has completed or has aborted.
Definition: jade.lemke.hpp:256
jade::basic_lemke::format_label
std::string format_label(const size_t label) const
Definition: jade.lemke.hpp:140
jade::basic_lemke::state::aborted_pivot
@ aborted_pivot
Aborted finding the pivot.
Definition: jade.lemke.hpp:41
jade::basic_lemke::basic_lemke
basic_lemke(const matrix_type &tableau)
Initializes a new instance of the class based on the specified tableau.
Definition: jade.lemke.hpp:81
jade::basic_lemke::invalid_index
static constexpr auto invalid_index
The value associated with an invalid or unassigned index.
Definition: jade.lemke.hpp:24
jade::basic_lemke::matrix_type
basic_matrix< value_type > matrix_type
The matrix type.
Definition: jade.lemke.hpp:69
jade::basic_lemke::state::aborted_initialization
@ aborted_initialization
Aborted during initialization.
Definition: jade.lemke.hpp:39
jade::basic_lemke::state::str
static const char * str(const type value)
Definition: jade.lemke.hpp:47
jade::basic_lemke::state::completed
@ completed
The algorithm completed.
Definition: jade.lemke.hpp:38
jade::basic_matrix::get_height
size_t get_height() const
Definition: jade.matrix.hpp:603
jade::basic_lemke::iterate
bool iterate()
Performs one step of the algorithm.
Definition: jade.lemke.hpp:235
jade::basic_lemke::state::executing
@ executing
The algorithm is executing.
Definition: jade.lemke.hpp:37
jade::basic_lemke::state::aborted_elimination
@ aborted_elimination
Aborted with an invalid pivot.
Definition: jade.lemke.hpp:40