solverimpl.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) 2013-2017, Nucleic Development Team.
  3. |
  4. | Distributed under the terms of the Modified BSD License.
  5. |
  6. | The full license is in the file LICENSE, distributed with this software.
  7. |----------------------------------------------------------------------------*/
  8. #pragma once
  9. #include <algorithm>
  10. #include <limits>
  11. #include <memory>
  12. #include <vector>
  13. #include "constraint.h"
  14. #include "errors.h"
  15. #include "expression.h"
  16. #include "maptype.h"
  17. #include "row.h"
  18. #include "symbol.h"
  19. #include "term.h"
  20. #include "util.h"
  21. #include "variable.h"
  22. namespace kiwi
  23. {
  24. namespace impl
  25. {
  26. class SolverImpl
  27. {
  28. friend class DebugHelper;
  29. struct Tag
  30. {
  31. Symbol marker;
  32. Symbol other;
  33. };
  34. struct EditInfo
  35. {
  36. Tag tag;
  37. Constraint constraint;
  38. double constant;
  39. };
  40. using VarMap = MapType<Variable, Symbol>;
  41. using RowMap = MapType<Symbol, Row*>;
  42. using CnMap = MapType<Constraint, Tag>;
  43. using EditMap = MapType<Variable, EditInfo>;
  44. struct DualOptimizeGuard
  45. {
  46. DualOptimizeGuard( SolverImpl& impl ) : m_impl( impl ) {}
  47. ~DualOptimizeGuard() { m_impl.dualOptimize(); }
  48. SolverImpl& m_impl;
  49. };
  50. public:
  51. SolverImpl() : m_objective( new Row() ), m_id_tick( 1 ) {}
  52. SolverImpl( const SolverImpl& ) = delete;
  53. SolverImpl( SolverImpl&& ) = delete;
  54. ~SolverImpl() { clearRows(); }
  55. /* Add a constraint to the solver.
  56. Throws
  57. ------
  58. DuplicateConstraint
  59. The given constraint has already been added to the solver.
  60. UnsatisfiableConstraint
  61. The given constraint is required and cannot be satisfied.
  62. */
  63. void addConstraint( const Constraint& constraint )
  64. {
  65. if( m_cns.find( constraint ) != m_cns.end() )
  66. throw DuplicateConstraint( constraint );
  67. // Creating a row causes symbols to be reserved for the variables
  68. // in the constraint. If this method exits with an exception,
  69. // then its possible those variables will linger in the var map.
  70. // Since its likely that those variables will be used in other
  71. // constraints and since exceptional conditions are uncommon,
  72. // i'm not too worried about aggressive cleanup of the var map.
  73. Tag tag;
  74. std::unique_ptr<Row> rowptr( createRow( constraint, tag ) );
  75. Symbol subject( chooseSubject( *rowptr, tag ) );
  76. // If chooseSubject could not find a valid entering symbol, one
  77. // last option is available if the entire row is composed of
  78. // dummy variables. If the constant of the row is zero, then
  79. // this represents redundant constraints and the new dummy
  80. // marker can enter the basis. If the constant is non-zero,
  81. // then it represents an unsatisfiable constraint.
  82. if( subject.type() == Symbol::Invalid && allDummies( *rowptr ) )
  83. {
  84. if( !nearZero( rowptr->constant() ) )
  85. throw UnsatisfiableConstraint( constraint );
  86. else
  87. subject = tag.marker;
  88. }
  89. // If an entering symbol still isn't found, then the row must
  90. // be added using an artificial variable. If that fails, then
  91. // the row represents an unsatisfiable constraint.
  92. if( subject.type() == Symbol::Invalid )
  93. {
  94. if( !addWithArtificialVariable( *rowptr ) )
  95. throw UnsatisfiableConstraint( constraint );
  96. }
  97. else
  98. {
  99. rowptr->solveFor( subject );
  100. substitute( subject, *rowptr );
  101. m_rows[ subject ] = rowptr.release();
  102. }
  103. m_cns[ constraint ] = tag;
  104. // Optimizing after each constraint is added performs less
  105. // aggregate work due to a smaller average system size. It
  106. // also ensures the solver remains in a consistent state.
  107. optimize( *m_objective );
  108. }
  109. /* Remove a constraint from the solver.
  110. Throws
  111. ------
  112. UnknownConstraint
  113. The given constraint has not been added to the solver.
  114. */
  115. void removeConstraint( const Constraint& constraint )
  116. {
  117. auto cn_it = m_cns.find( constraint );
  118. if( cn_it == m_cns.end() )
  119. throw UnknownConstraint( constraint );
  120. Tag tag( cn_it->second );
  121. m_cns.erase( cn_it );
  122. // Remove the error effects from the objective function
  123. // *before* pivoting, or substitutions into the objective
  124. // will lead to incorrect solver results.
  125. removeConstraintEffects( constraint, tag );
  126. // If the marker is basic, simply drop the row. Otherwise,
  127. // pivot the marker into the basis and then drop the row.
  128. auto row_it = m_rows.find( tag.marker );
  129. if( row_it != m_rows.end() )
  130. {
  131. std::unique_ptr<Row> rowptr( row_it->second );
  132. m_rows.erase( row_it );
  133. }
  134. else
  135. {
  136. row_it = getMarkerLeavingRow( tag.marker );
  137. if( row_it == m_rows.end() )
  138. throw InternalSolverError( "failed to find leaving row" );
  139. Symbol leaving( row_it->first );
  140. std::unique_ptr<Row> rowptr( row_it->second );
  141. m_rows.erase( row_it );
  142. rowptr->solveFor( leaving, tag.marker );
  143. substitute( tag.marker, *rowptr );
  144. }
  145. // Optimizing after each constraint is removed ensures that the
  146. // solver remains consistent. It makes the solver api easier to
  147. // use at a small tradeoff for speed.
  148. optimize( *m_objective );
  149. }
  150. /* Test whether a constraint has been added to the solver.
  151. */
  152. bool hasConstraint( const Constraint& constraint ) const
  153. {
  154. return m_cns.find( constraint ) != m_cns.end();
  155. }
  156. /* Add an edit variable to the solver.
  157. This method should be called before the `suggestValue` method is
  158. used to supply a suggested value for the given edit variable.
  159. Throws
  160. ------
  161. DuplicateEditVariable
  162. The given edit variable has already been added to the solver.
  163. BadRequiredStrength
  164. The given strength is >= required.
  165. */
  166. void addEditVariable( const Variable& variable, double strength )
  167. {
  168. if( m_edits.find( variable ) != m_edits.end() )
  169. throw DuplicateEditVariable( variable );
  170. strength = strength::clip( strength );
  171. if( strength == strength::required )
  172. throw BadRequiredStrength();
  173. Constraint cn( Expression( variable ), OP_EQ, strength );
  174. addConstraint( cn );
  175. EditInfo info;
  176. info.tag = m_cns[ cn ];
  177. info.constraint = cn;
  178. info.constant = 0.0;
  179. m_edits[ variable ] = info;
  180. }
  181. /* Remove an edit variable from the solver.
  182. Throws
  183. ------
  184. UnknownEditVariable
  185. The given edit variable has not been added to the solver.
  186. */
  187. void removeEditVariable( const Variable& variable )
  188. {
  189. auto it = m_edits.find( variable );
  190. if( it == m_edits.end() )
  191. throw UnknownEditVariable( variable );
  192. removeConstraint( it->second.constraint );
  193. m_edits.erase( it );
  194. }
  195. /* Test whether an edit variable has been added to the solver.
  196. */
  197. bool hasEditVariable( const Variable& variable ) const
  198. {
  199. return m_edits.find( variable ) != m_edits.end();
  200. }
  201. /* Suggest a value for the given edit variable.
  202. This method should be used after an edit variable as been added to
  203. the solver in order to suggest the value for that variable.
  204. Throws
  205. ------
  206. UnknownEditVariable
  207. The given edit variable has not been added to the solver.
  208. */
  209. void suggestValue( const Variable& variable, double value )
  210. {
  211. auto it = m_edits.find( variable );
  212. if( it == m_edits.end() )
  213. throw UnknownEditVariable( variable );
  214. DualOptimizeGuard guard( *this );
  215. EditInfo& info = it->second;
  216. double delta = value - info.constant;
  217. info.constant = value;
  218. // Check first if the positive error variable is basic.
  219. auto row_it = m_rows.find( info.tag.marker );
  220. if( row_it != m_rows.end() )
  221. {
  222. if( row_it->second->add( -delta ) < 0.0 )
  223. m_infeasible_rows.push_back( row_it->first );
  224. return;
  225. }
  226. // Check next if the negative error variable is basic.
  227. row_it = m_rows.find( info.tag.other );
  228. if( row_it != m_rows.end() )
  229. {
  230. if( row_it->second->add( delta ) < 0.0 )
  231. m_infeasible_rows.push_back( row_it->first );
  232. return;
  233. }
  234. // Otherwise update each row where the error variables exist.
  235. for (const auto & rowPair : m_rows)
  236. {
  237. double coeff = rowPair.second->coefficientFor( info.tag.marker );
  238. if( coeff != 0.0 &&
  239. rowPair.second->add( delta * coeff ) < 0.0 &&
  240. rowPair.first.type() != Symbol::External )
  241. m_infeasible_rows.push_back( rowPair.first );
  242. }
  243. }
  244. /* Update the values of the external solver variables.
  245. */
  246. void updateVariables()
  247. {
  248. auto row_end = m_rows.end();
  249. for (auto &varPair : m_vars)
  250. {
  251. Variable& var = varPair.first;
  252. auto row_it = m_rows.find( varPair.second );
  253. if( row_it == row_end )
  254. var.setValue( 0.0 );
  255. else
  256. var.setValue( row_it->second->constant() );
  257. }
  258. }
  259. /* Reset the solver to the empty starting condition.
  260. This method resets the internal solver state to the empty starting
  261. condition, as if no constraints or edit variables have been added.
  262. This can be faster than deleting the solver and creating a new one
  263. when the entire system must change, since it can avoid unecessary
  264. heap (de)allocations.
  265. */
  266. void reset()
  267. {
  268. clearRows();
  269. m_cns.clear();
  270. m_vars.clear();
  271. m_edits.clear();
  272. m_infeasible_rows.clear();
  273. m_objective.reset( new Row() );
  274. m_artificial.reset();
  275. m_id_tick = 1;
  276. }
  277. SolverImpl& operator=( const SolverImpl& ) = delete;
  278. SolverImpl& operator=( SolverImpl&& ) = delete;
  279. private:
  280. struct RowDeleter
  281. {
  282. template<typename T>
  283. void operator()( T& pair ) { delete pair.second; }
  284. };
  285. void clearRows()
  286. {
  287. std::for_each( m_rows.begin(), m_rows.end(), RowDeleter() );
  288. m_rows.clear();
  289. }
  290. /* Get the symbol for the given variable.
  291. If a symbol does not exist for the variable, one will be created.
  292. */
  293. Symbol getVarSymbol( const Variable& variable )
  294. {
  295. auto it = m_vars.find( variable );
  296. if( it != m_vars.end() )
  297. return it->second;
  298. Symbol symbol( Symbol::External, m_id_tick++ );
  299. m_vars[ variable ] = symbol;
  300. return symbol;
  301. }
  302. /* Create a new Row object for the given constraint.
  303. The terms in the constraint will be converted to cells in the row.
  304. Any term in the constraint with a coefficient of zero is ignored.
  305. This method uses the `getVarSymbol` method to get the symbol for
  306. the variables added to the row. If the symbol for a given cell
  307. variable is basic, the cell variable will be substituted with the
  308. basic row.
  309. The necessary slack and error variables will be added to the row.
  310. If the constant for the row is negative, the sign for the row
  311. will be inverted so the constant becomes positive.
  312. The tag will be updated with the marker and error symbols to use
  313. for tracking the movement of the constraint in the tableau.
  314. */
  315. std::unique_ptr<Row> createRow( const Constraint& constraint, Tag& tag )
  316. {
  317. const Expression& expr( constraint.expression() );
  318. std::unique_ptr<Row> row( new Row( expr.constant() ) );
  319. // Substitute the current basic variables into the row.
  320. for (const auto &term : expr.terms())
  321. {
  322. if( !nearZero( term.coefficient() ) )
  323. {
  324. Symbol symbol( getVarSymbol( term.variable() ) );
  325. auto row_it = m_rows.find( symbol );
  326. if( row_it != m_rows.end() )
  327. row->insert( *row_it->second, term.coefficient() );
  328. else
  329. row->insert( symbol, term.coefficient() );
  330. }
  331. }
  332. // Add the necessary slack, error, and dummy variables.
  333. switch( constraint.op() )
  334. {
  335. case OP_LE:
  336. case OP_GE:
  337. {
  338. double coeff = constraint.op() == OP_LE ? 1.0 : -1.0;
  339. Symbol slack( Symbol::Slack, m_id_tick++ );
  340. tag.marker = slack;
  341. row->insert( slack, coeff );
  342. if( constraint.strength() < strength::required )
  343. {
  344. Symbol error( Symbol::Error, m_id_tick++ );
  345. tag.other = error;
  346. row->insert( error, -coeff );
  347. m_objective->insert( error, constraint.strength() );
  348. }
  349. break;
  350. }
  351. case OP_EQ:
  352. {
  353. if( constraint.strength() < strength::required )
  354. {
  355. Symbol errplus( Symbol::Error, m_id_tick++ );
  356. Symbol errminus( Symbol::Error, m_id_tick++ );
  357. tag.marker = errplus;
  358. tag.other = errminus;
  359. row->insert( errplus, -1.0 ); // v = eplus - eminus
  360. row->insert( errminus, 1.0 ); // v - eplus + eminus = 0
  361. m_objective->insert( errplus, constraint.strength() );
  362. m_objective->insert( errminus, constraint.strength() );
  363. }
  364. else
  365. {
  366. Symbol dummy( Symbol::Dummy, m_id_tick++ );
  367. tag.marker = dummy;
  368. row->insert( dummy );
  369. }
  370. break;
  371. }
  372. }
  373. // Ensure the row as a positive constant.
  374. if( row->constant() < 0.0 )
  375. row->reverseSign();
  376. return row;
  377. }
  378. /* Choose the subject for solving for the row.
  379. This method will choose the best subject for using as the solve
  380. target for the row. An invalid symbol will be returned if there
  381. is no valid target.
  382. The symbols are chosen according to the following precedence:
  383. 1) The first symbol representing an external variable.
  384. 2) A negative slack or error tag variable.
  385. If a subject cannot be found, an invalid symbol will be returned.
  386. */
  387. Symbol chooseSubject( const Row& row, const Tag& tag ) const
  388. {
  389. for (const auto &cellPair : row.cells())
  390. {
  391. if( cellPair.first.type() == Symbol::External )
  392. return cellPair.first;
  393. }
  394. if( tag.marker.type() == Symbol::Slack || tag.marker.type() == Symbol::Error )
  395. {
  396. if( row.coefficientFor( tag.marker ) < 0.0 )
  397. return tag.marker;
  398. }
  399. if( tag.other.type() == Symbol::Slack || tag.other.type() == Symbol::Error )
  400. {
  401. if( row.coefficientFor( tag.other ) < 0.0 )
  402. return tag.other;
  403. }
  404. return Symbol();
  405. }
  406. /* Add the row to the tableau using an artificial variable.
  407. This will return false if the constraint cannot be satisfied.
  408. */
  409. bool addWithArtificialVariable( const Row& row )
  410. {
  411. // Create and add the artificial variable to the tableau
  412. Symbol art( Symbol::Slack, m_id_tick++ );
  413. m_rows[ art ] = new Row( row );
  414. m_artificial.reset( new Row( row ) );
  415. // Optimize the artificial objective. This is successful
  416. // only if the artificial objective is optimized to zero.
  417. optimize( *m_artificial );
  418. bool success = nearZero( m_artificial->constant() );
  419. m_artificial.reset();
  420. // If the artificial variable is not basic, pivot the row so that
  421. // it becomes basic. If the row is constant, exit early.
  422. auto it = m_rows.find( art );
  423. if( it != m_rows.end() )
  424. {
  425. std::unique_ptr<Row> rowptr( it->second );
  426. m_rows.erase( it );
  427. if( rowptr->cells().empty() )
  428. return success;
  429. Symbol entering( anyPivotableSymbol( *rowptr ) );
  430. if( entering.type() == Symbol::Invalid )
  431. return false; // unsatisfiable (will this ever happen?)
  432. rowptr->solveFor( art, entering );
  433. substitute( entering, *rowptr );
  434. m_rows[ entering ] = rowptr.release();
  435. }
  436. // Remove the artificial variable from the tableau.
  437. for (auto &rowPair : m_rows)
  438. rowPair.second->remove(art);
  439. m_objective->remove( art );
  440. return success;
  441. }
  442. /* Substitute the parametric symbol with the given row.
  443. This method will substitute all instances of the parametric symbol
  444. in the tableau and the objective function with the given row.
  445. */
  446. void substitute( const Symbol& symbol, const Row& row )
  447. {
  448. for( auto& rowPair : m_rows )
  449. {
  450. rowPair.second->substitute( symbol, row );
  451. if( rowPair.first.type() != Symbol::External &&
  452. rowPair.second->constant() < 0.0 )
  453. m_infeasible_rows.push_back( rowPair.first );
  454. }
  455. m_objective->substitute( symbol, row );
  456. if( m_artificial.get() )
  457. m_artificial->substitute( symbol, row );
  458. }
  459. /* Optimize the system for the given objective function.
  460. This method performs iterations of Phase 2 of the simplex method
  461. until the objective function reaches a minimum.
  462. Throws
  463. ------
  464. InternalSolverError
  465. The value of the objective function is unbounded.
  466. */
  467. void optimize( const Row& objective )
  468. {
  469. while( true )
  470. {
  471. Symbol entering( getEnteringSymbol( objective ) );
  472. if( entering.type() == Symbol::Invalid )
  473. return;
  474. auto it = getLeavingRow( entering );
  475. if( it == m_rows.end() )
  476. throw InternalSolverError( "The objective is unbounded." );
  477. // pivot the entering symbol into the basis
  478. Symbol leaving( it->first );
  479. Row* row = it->second;
  480. m_rows.erase( it );
  481. row->solveFor( leaving, entering );
  482. substitute( entering, *row );
  483. m_rows[ entering ] = row;
  484. }
  485. }
  486. /* Optimize the system using the dual of the simplex method.
  487. The current state of the system should be such that the objective
  488. function is optimal, but not feasible. This method will perform
  489. an iteration of the dual simplex method to make the solution both
  490. optimal and feasible.
  491. Throws
  492. ------
  493. InternalSolverError
  494. The system cannot be dual optimized.
  495. */
  496. void dualOptimize()
  497. {
  498. while( !m_infeasible_rows.empty() )
  499. {
  500. Symbol leaving( m_infeasible_rows.back() );
  501. m_infeasible_rows.pop_back();
  502. auto it = m_rows.find( leaving );
  503. if( it != m_rows.end() && !nearZero( it->second->constant() ) &&
  504. it->second->constant() < 0.0 )
  505. {
  506. Symbol entering( getDualEnteringSymbol( *it->second ) );
  507. if( entering.type() == Symbol::Invalid )
  508. throw InternalSolverError( "Dual optimize failed." );
  509. // pivot the entering symbol into the basis
  510. Row* row = it->second;
  511. m_rows.erase( it );
  512. row->solveFor( leaving, entering );
  513. substitute( entering, *row );
  514. m_rows[ entering ] = row;
  515. }
  516. }
  517. }
  518. /* Compute the entering variable for a pivot operation.
  519. This method will return first symbol in the objective function which
  520. is non-dummy and has a coefficient less than zero. If no symbol meets
  521. the criteria, it means the objective function is at a minimum, and an
  522. invalid symbol is returned.
  523. */
  524. Symbol getEnteringSymbol( const Row& objective ) const
  525. {
  526. for (const auto &cellPair : objective.cells())
  527. {
  528. if( cellPair.first.type() != Symbol::Dummy && cellPair.second < 0.0 )
  529. return cellPair.first;
  530. }
  531. return Symbol();
  532. }
  533. /* Compute the entering symbol for the dual optimize operation.
  534. This method will return the symbol in the row which has a positive
  535. coefficient and yields the minimum ratio for its respective symbol
  536. in the objective function. The provided row *must* be infeasible.
  537. If no symbol is found which meats the criteria, an invalid symbol
  538. is returned.
  539. */
  540. Symbol getDualEnteringSymbol( const Row& row ) const
  541. {
  542. Symbol entering;
  543. double ratio = std::numeric_limits<double>::max();
  544. for (const auto &cellPair : row.cells())
  545. {
  546. if( cellPair.second > 0.0 && cellPair.first.type() != Symbol::Dummy )
  547. {
  548. double coeff = m_objective->coefficientFor( cellPair.first );
  549. double r = coeff / cellPair.second;
  550. if( r < ratio )
  551. {
  552. ratio = r;
  553. entering = cellPair.first;
  554. }
  555. }
  556. }
  557. return entering;
  558. }
  559. /* Get the first Slack or Error symbol in the row.
  560. If no such symbol is present, and Invalid symbol will be returned.
  561. */
  562. Symbol anyPivotableSymbol( const Row& row ) const
  563. {
  564. for (const auto &cellPair : row.cells())
  565. {
  566. const Symbol& sym( cellPair.first );
  567. if( sym.type() == Symbol::Slack || sym.type() == Symbol::Error )
  568. return sym;
  569. }
  570. return Symbol();
  571. }
  572. /* Compute the row which holds the exit symbol for a pivot.
  573. This method will return an iterator to the row in the row map
  574. which holds the exit symbol. If no appropriate exit symbol is
  575. found, the end() iterator will be returned. This indicates that
  576. the objective function is unbounded.
  577. */
  578. RowMap::iterator getLeavingRow( const Symbol& entering )
  579. {
  580. double ratio = std::numeric_limits<double>::max();
  581. auto end = m_rows.end();
  582. auto found = m_rows.end();
  583. for( auto it = m_rows.begin(); it != end; ++it )
  584. {
  585. if( it->first.type() != Symbol::External )
  586. {
  587. double temp = it->second->coefficientFor( entering );
  588. if( temp < 0.0 )
  589. {
  590. double temp_ratio = -it->second->constant() / temp;
  591. if( temp_ratio < ratio )
  592. {
  593. ratio = temp_ratio;
  594. found = it;
  595. }
  596. }
  597. }
  598. }
  599. return found;
  600. }
  601. /* Compute the leaving row for a marker variable.
  602. This method will return an iterator to the row in the row map
  603. which holds the given marker variable. The row will be chosen
  604. according to the following precedence:
  605. 1) The row with a restricted basic varible and a negative coefficient
  606. for the marker with the smallest ratio of -constant / coefficient.
  607. 2) The row with a restricted basic variable and the smallest ratio
  608. of constant / coefficient.
  609. 3) The last unrestricted row which contains the marker.
  610. If the marker does not exist in any row, the row map end() iterator
  611. will be returned. This indicates an internal solver error since
  612. the marker *should* exist somewhere in the tableau.
  613. */
  614. RowMap::iterator getMarkerLeavingRow( const Symbol& marker )
  615. {
  616. const double dmax = std::numeric_limits<double>::max();
  617. double r1 = dmax;
  618. double r2 = dmax;
  619. auto end = m_rows.end();
  620. auto first = end;
  621. auto second = end;
  622. auto third = end;
  623. for( auto it = m_rows.begin(); it != end; ++it )
  624. {
  625. double c = it->second->coefficientFor( marker );
  626. if( c == 0.0 )
  627. continue;
  628. if( it->first.type() == Symbol::External )
  629. {
  630. third = it;
  631. }
  632. else if( c < 0.0 )
  633. {
  634. double r = -it->second->constant() / c;
  635. if( r < r1 )
  636. {
  637. r1 = r;
  638. first = it;
  639. }
  640. }
  641. else
  642. {
  643. double r = it->second->constant() / c;
  644. if( r < r2 )
  645. {
  646. r2 = r;
  647. second = it;
  648. }
  649. }
  650. }
  651. if( first != end )
  652. return first;
  653. if( second != end )
  654. return second;
  655. return third;
  656. }
  657. /* Remove the effects of a constraint on the objective function.
  658. */
  659. void removeConstraintEffects( const Constraint& cn, const Tag& tag )
  660. {
  661. if( tag.marker.type() == Symbol::Error )
  662. removeMarkerEffects( tag.marker, cn.strength() );
  663. if( tag.other.type() == Symbol::Error )
  664. removeMarkerEffects( tag.other, cn.strength() );
  665. }
  666. /* Remove the effects of an error marker on the objective function.
  667. */
  668. void removeMarkerEffects( const Symbol& marker, double strength )
  669. {
  670. auto row_it = m_rows.find( marker );
  671. if( row_it != m_rows.end() )
  672. m_objective->insert( *row_it->second, -strength );
  673. else
  674. m_objective->insert( marker, -strength );
  675. }
  676. /* Test whether a row is composed of all dummy variables.
  677. */
  678. bool allDummies( const Row& row ) const
  679. {
  680. for (const auto &rowPair : row.cells())
  681. {
  682. if( rowPair.first.type() != Symbol::Dummy )
  683. return false;
  684. }
  685. return true;
  686. }
  687. CnMap m_cns;
  688. RowMap m_rows;
  689. VarMap m_vars;
  690. EditMap m_edits;
  691. std::vector<Symbol> m_infeasible_rows;
  692. std::unique_ptr<Row> m_objective;
  693. std::unique_ptr<Row> m_artificial;
  694. Symbol::Id m_id_tick;
  695. };
  696. } // namespace impl
  697. } // namespace kiwi