33 # include <tpl_dynMat.H> 34 # include <tpl_dynDlist.H> 67 enum State { Not_Solved, Solving, Unbounded, Solved, Unfeasible };
75 int compute_pivot_col()
const noexcept
77 T minimum = numeric_limits<T>::max();
79 for (
int i = 0, M = num_var + num_rest; i < M; i++)
81 const T & c = m->read(0, i);
88 return minimum >= 0 ? -1 : p;
94 int compute_pivot_row(
int p)
const noexcept
96 assert(p >= 0 and p < num_var + num_rest);
99 T min_ratio = numeric_limits<T>::max();
100 for (
int i = q + 1, M = num_var + num_rest; i <= num_rest; i++)
102 const T val = m->read(i, M);
106 const T den = m->read(i, p);
110 const T ratio = val / den;
111 if (ratio < min_ratio)
120 State select_pivot(
int & p,
int & q) noexcept
122 assert(state == Not_Solved or state == Solving);
124 const int col = compute_pivot_col();
126 return state = Solved;
128 const int row = compute_pivot_row(col);
130 return state = Unbounded;
135 return state = Solving;
138 void to_pivot(
size_t p,
size_t q)
140 assert(p <= num_rest and q <= num_var + num_rest);
142 const int M = num_var + num_rest;
143 const T pivot = m->read(p, q);
144 for (
int j = 0; j <= M; j++)
146 m->write(p, j, m->read(p, j) / pivot);
150 for (
int i = 0; i <= num_rest; i++)
151 for (
int j = 0; j <= M; j++)
152 if (i != p and j != q)
153 m->write(i, j, m->read(i,j) - m->read(i,q)*m->read(p,j));
155 for (
int i = 0; i <= num_rest; i++)
160 T find_value(
const size_t j)
const noexcept
165 for (
int i = 1, count = 0; i < num_rest; i++)
167 const T & value = m->read(i, j);
173 ret_val = m->read(i, num_var + num_rest);
182 unique_ptr<DynMatrix<T>> m;
184 unique_ptr<T[]> objetive;
191 unique_ptr<T[]> solution;
199 void verify_var_index(
int i)
202 throw std::out_of_range(
"index " + to_string(i) +
" out of range (" +
203 to_string(num_var - 1) +
"");
206 T * create_restriction()
208 T * ptr =
new T [num_var + 1];
211 for (
int i = 0; i <= num_var; i++)
218 m = unique_ptr<DynMatrix<T>>(
new DynMatrix<T> (num_rest + 1,
219 num_var + num_rest + 1));
222 for (; i < num_var; i++)
223 m->write(0, i, -objetive[i]);
227 for (
auto it = rest_list.
get_it(); it.has_curr(); it.next_ne(), i++)
229 T * rest = it.get_curr_ne();
231 for (
int j = 0; j < num_var; j++)
232 m->write(i, j, rest[j]);
234 m->write(i, num_var + i - 1, 1);
236 m->write(i, num_var + num_rest, rest[num_var]);
252 : m(nullptr), objetive(new T [n]), num_var(n), num_rest(0),
253 solution(new T [num_var]), state(Not_Solved) {}
257 rest_list.
for_each([] (
auto ptr) {
delete [] ptr; });
272 for (
int i = 0; i < num_var; i++)
280 for (
int i = 0; i < num_var; i++)
298 T * rest = create_restriction();
300 if (coefs ==
nullptr)
303 for (
int i = 0; i <= num_var; ++i)
312 if (rest_num >= num_rest)
313 throw std::out_of_range(
"index " + to_string(rest_num) +
314 "out of range (" + to_string(num_rest - 1) +
")");
317 for (
auto it = rest_list.
get_it(); it.has_curr(); it.next_ne())
319 return it.get_curr_ne();
336 T * rest = create_restriction();
338 for (
int i = 0; i <= num_var; ++i)
344 State latex_solve(
char * name =
nullptr)
346 latex_matrix(
string(name) +
"-0.tex");
347 for (
int i, j, k = 1;
true; k++)
351 string str = string(name) +
"-" + to_string(k) +
".tex";
354 latex_matrix(str.c_str());
358 latex_matrix(str.c_str(), 2, i, j);
361 latex_matrix(name, i, j);
380 if (state != Not_Solved)
381 throw std::logic_error(
"solve() has already been called");
383 throw std::logic_error(
"linear program without restrictions");
385 for (
int i, j;
true;)
398 for (
int j = 0; j < num_var; j++)
399 solution[j] = find_value(j);
414 for (
int i = 0; i < num_var; i++)
415 sum += solution[i]*objetive[i];
423 for (
auto it = rest_list.
get_it(); it.has_curr(); it.next_ne())
425 T * rest = it.get_curr_ne();
427 for (
int j = 0; j < num_var; j++)
428 sum += rest[j]*solution[j];
430 if (sum > rest[num_var])
438 for (
int i = 0; i <= num_rest; ++i)
440 for (
int j = 0; j <= num_var + num_rest; j++)
441 cout << float_f(m->read(i, j), 2) <<
" ";
447 void latex_matrix(
const string & name,
int d = 2,
int p = -1,
int q = -1)
449 ofstream out(name, ios::out);
451 const int cols = num_var + num_rest;
453 out <<
"$\\left(\\begin{array}{c";
454 for (
int i = 0; i < cols; i++)
458 for (
int i = 0; i <= num_rest; ++i)
460 for (
int j = 0; j <= cols; j++)
462 if (i == p and j == q)
463 out <<
"\\circled{" << float_f(m->read(i, j), d) <<
"}" <<
" ";
465 out << float_f(m->read(i, j), d) <<
" ";
475 out <<
"\\end{array}\\right)$" << endl;
478 void latex_linear_program(
const string & name)
480 ofstream out(name, ios::out);
481 out <<
"\\begin{equation*}" << endl
483 for (
int i = 0; i < num_var; i++)
485 if (objetive[i] == 0.0)
491 if (objetive[i] != 1.0)
496 <<
"\\end{equation*}" << endl
497 <<
"Sujeto a:" << endl
498 <<
"\\begin{eqnarray*}" << endl;
500 for (
auto it = rest_list.
get_it(); it.has_curr(); it.next_ne())
502 T * rest = it.get_curr_ne();
504 for (
int i = 0; i < num_rest; i++)
517 out <<
" & \\leq & " << rest[num_rest];
519 if (not it.is_in_last())
524 out <<
"\\end{eqnarray*}" << endl;
527 size_t get_num_restrictions()
const noexcept {
return num_rest; }
529 size_t get_num_vars()
const noexcept {
return num_var; }
531 T * get_objetive_function() noexcept
539 verify_var_index(idx);
544 void put_restriction_coef(
int rest_num,
int idx,
const T & coef)
549 void prepare_linear_program()
T * put_restriction(DynArray< T > &coefs)
Definition: Simplex.H:334
T * put_restriction(T *coefs=nullptr)
Definition: Simplex.H:296
T & get_restriction_coef(int rest_num, int idx)
retorna el coeficiente idx de la restricci{on rest_num
Definition: Simplex.H:537
auto get_it() const
Definition: htlist.H:151
Definition: tpl_dynDlist.H:51
void for_each(Operation &operation) noexcept(noexcept(operation))
Definition: htlist.H:589
T objetive_value() const noexcept
Retorna el valor de la función objetivo.
Definition: Simplex.H:411
Definition: tpl_dynMat.H:46
State solve()
Definition: Simplex.H:378
const T & get_solution(size_t i) const noexcept
Definition: Simplex.H:404
void put_objetive_function(T coefs[]) noexcept
Definition: Simplex.H:278
T * get_restriction(int rest_num)
Retorna un punero al arreglo restricción num_rest.
Definition: Simplex.H:310
bool verify_solution() const
Definition: Simplex.H:421
void put_objetive_function_coef(int i, const T &coef)
Definition: Simplex.H:262
State
Estados del sistema.
Definition: Simplex.H:67
Simplex(int n)
Definition: Simplex.H:251
void put_objetive_function(DynArray< T > &coefs) noexcept
Definition: Simplex.H:270
Definition: tpl_dynArray.H:159
T & append(const T &item)
Definition: tpl_dynDlist.H:149
void load_solution() noexcept
Carga los valores de las variables solución.
Definition: Simplex.H:396