17583db5aSClemens Zeidler /* 27583db5aSClemens Zeidler * Copyright 2010, Clemens Zeidler <haiku@clemens-zeidler.de> 37583db5aSClemens Zeidler * Distributed under the terms of the MIT License. 47583db5aSClemens Zeidler */ 57583db5aSClemens Zeidler #ifndef ACTICE_SET_SOLVER_H 67583db5aSClemens Zeidler #define ACTICE_SET_SOLVER_H 77583db5aSClemens Zeidler 87583db5aSClemens Zeidler 9*82fbed25Sczeidler #include "LinearSpec.h" 107583db5aSClemens Zeidler 117583db5aSClemens Zeidler 127583db5aSClemens Zeidler class EquationSystem { 137583db5aSClemens Zeidler public: 147583db5aSClemens Zeidler EquationSystem(int32 rows, int32 columns); 157583db5aSClemens Zeidler ~EquationSystem(); 167583db5aSClemens Zeidler 177583db5aSClemens Zeidler void SetRows(int32 rows); 187583db5aSClemens Zeidler int32 Rows(); 197583db5aSClemens Zeidler int32 Columns(); 207583db5aSClemens Zeidler 21*82fbed25Sczeidler //! Debug function: check bounds 22*82fbed25Sczeidler bool InRange(int32 row, int32 column); 237583db5aSClemens Zeidler inline double& A(int32 row, int32 column); 247583db5aSClemens Zeidler inline double& B(int32 row); 257583db5aSClemens Zeidler /*! Copy the solved variables into results, keeping the original 267583db5aSClemens Zeidler variable order. */ 277583db5aSClemens Zeidler inline void Results(double* results, int32 size); 287583db5aSClemens Zeidler 297583db5aSClemens Zeidler inline void SwapColumn(int32 i, int32 j); 307583db5aSClemens Zeidler inline void SwapRow(int32 i, int32 j); 317583db5aSClemens Zeidler 32056207eeSczeidler bool GaussianElimination(); 337583db5aSClemens Zeidler bool GaussJordan(); 347583db5aSClemens Zeidler /*! Gauss Jordan elimination just for one column, the diagonal 357583db5aSClemens Zeidler element must be none zero. */ 367583db5aSClemens Zeidler void GaussJordan(int32 column); 377583db5aSClemens Zeidler 387583db5aSClemens Zeidler void RemoveLinearlyDependentRows(); 397583db5aSClemens Zeidler void RemoveUnusedVariables(); 407583db5aSClemens Zeidler 417583db5aSClemens Zeidler void MoveColumnRight(int32 i, int32 target); 427583db5aSClemens Zeidler 437583db5aSClemens Zeidler void Print(); 447583db5aSClemens Zeidler private: 45056207eeSczeidler inline void _EliminateColumn(int32 column, int32 startRow, 46056207eeSczeidler int32 endRow); 47056207eeSczeidler 487583db5aSClemens Zeidler int32* fRowIndices; 497583db5aSClemens Zeidler int32* fColumnIndices; 507583db5aSClemens Zeidler double** fMatrix; 517583db5aSClemens Zeidler double* fB; 527583db5aSClemens Zeidler int32 fRows; 537583db5aSClemens Zeidler int32 fColumns; 547583db5aSClemens Zeidler }; 557583db5aSClemens Zeidler 567583db5aSClemens Zeidler 57*82fbed25Sczeidler class ActiveSetSolver : public LinearProgramming::SolverInterface { 587583db5aSClemens Zeidler public: 597583db5aSClemens Zeidler ActiveSetSolver(LinearSpec* linearSpec); 607583db5aSClemens Zeidler ~ActiveSetSolver(); 617583db5aSClemens Zeidler 627583db5aSClemens Zeidler ResultType Solve(); 637583db5aSClemens Zeidler 647583db5aSClemens Zeidler bool VariableAdded(Variable* variable); 657583db5aSClemens Zeidler bool VariableRemoved(Variable* variable); 667583db5aSClemens Zeidler bool VariableRangeChanged(Variable* variable); 677583db5aSClemens Zeidler 687583db5aSClemens Zeidler bool ConstraintAdded(Constraint* constraint); 697583db5aSClemens Zeidler bool ConstraintRemoved(Constraint* constraint); 707583db5aSClemens Zeidler bool LeftSideChanged(Constraint* constraint); 717583db5aSClemens Zeidler bool RightSideChanged(Constraint* constraint); 727583db5aSClemens Zeidler bool OperatorChanged(Constraint* constraint); 73*82fbed25Sczeidler bool PenaltiesChanged(Constraint* constraint); 747583db5aSClemens Zeidler 757583db5aSClemens Zeidler bool SaveModel(const char* fileName); 767583db5aSClemens Zeidler 77419fe0b8SAlex Wilson ResultType FindMaxs(const VariableList* variables); 78419fe0b8SAlex Wilson ResultType FindMins(const VariableList* variables); 79419fe0b8SAlex Wilson 80c8b24e3eSAlex Wilson void GetRangeConstraints(const Variable* var, 81c8b24e3eSAlex Wilson const Constraint** _min, 82c8b24e3eSAlex Wilson const Constraint** _max) const; 83*82fbed25Sczeidler 84fbd2dfcfSAlex Wilson private: 85a0ad88e0SClemens Zeidler void _RemoveSoftConstraint(ConstraintList& list); 86a0ad88e0SClemens Zeidler void _AddSoftConstraint(const ConstraintList& list); 87a0ad88e0SClemens Zeidler 88fbd2dfcfSAlex Wilson typedef Constraint* (*AddConstraintFunc)(LinearSpec* spec, Variable* var); 89fbd2dfcfSAlex Wilson 90fbd2dfcfSAlex Wilson ResultType _FindWithConstraintsNoSoft( 91fbd2dfcfSAlex Wilson const VariableList* variables, 92fbd2dfcfSAlex Wilson AddConstraintFunc constraintFunc); 93fbd2dfcfSAlex Wilson 947583db5aSClemens Zeidler const VariableList& fVariables; 957583db5aSClemens Zeidler const ConstraintList& fConstraints; 967583db5aSClemens Zeidler 977583db5aSClemens Zeidler ConstraintList fVariableGEConstraints; 987583db5aSClemens Zeidler ConstraintList fVariableLEConstraints; 997583db5aSClemens Zeidler }; 1007583db5aSClemens Zeidler 1017583db5aSClemens Zeidler 1027583db5aSClemens Zeidler #endif // ACTICE_SET_SOLVER_H 103