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