xref: /haiku/src/libs/linprog/ActiveSetSolver.h (revision b46615c55ad2c8fe6de54412055a0713da3d610a)
1 /*
2  * Copyright 2010, Clemens Zeidler <haiku@clemens-zeidler.de>
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef ACTICE_SET_SOLVER_H
6 #define ACTICE_SET_SOLVER_H
7 
8 
9 #include "LinearSpec.h"
10 
11 
12 class EquationSystem {
13 public:
14 								EquationSystem(int32 rows, int32 columns);
15 								~EquationSystem();
16 
17 			void				SetRows(int32 rows);
18 			int32				Rows();
19 			int32				Columns();
20 
21 	inline	double&				A(int32 row, int32 column);
22 	inline	double&				B(int32 row);
23 			/*! Copy the solved variables into results, keeping the original
24 			variable order. */
25 	inline	void				Results(double* results, int32 size);
26 
27 	inline	void				SwapColumn(int32 i, int32 j);
28 	inline	void				SwapRow(int32 i, int32 j);
29 
30 			bool				GaussJordan();
31 			/*! Gauss Jordan elimination just for one column, the diagonal
32 			element must be none zero. */
33 			void				GaussJordan(int32 column);
34 
35 			void				RemoveLinearlyDependentRows();
36 			void				RemoveUnusedVariables();
37 
38 			void				MoveColumnRight(int32 i, int32 target);
39 
40 			void				Print();
41 private:
42 			int32*				fRowIndices;
43 			int32*				fColumnIndices;
44 			double**			fMatrix;
45 			double*				fB;
46 			int32				fRows;
47 			int32				fColumns;
48 };
49 
50 
51 class ActiveSetSolver : public LinearProgramming::SolverInterface {
52 public:
53 								ActiveSetSolver(LinearSpec* linearSpec);
54 								~ActiveSetSolver();
55 
56 			ResultType			Solve();
57 
58 			bool				VariableAdded(Variable* variable);
59 			bool				VariableRemoved(Variable* variable);
60 			bool				VariableRangeChanged(Variable* variable);
61 
62 			bool				ConstraintAdded(Constraint* constraint);
63 			bool				ConstraintRemoved(Constraint* constraint);
64 			bool				LeftSideChanged(Constraint* constraint);
65 			bool				RightSideChanged(Constraint* constraint);
66 			bool				OperatorChanged(Constraint* constraint);
67 
68 			bool				SaveModel(const char* fileName);
69 
70 			BSize				MinSize(Variable* width, Variable* height);
71 			BSize				MaxSize(Variable* width, Variable* height);
72 
73 public:
74 			void				_RemoveSoftConstraint(ConstraintList& list);
75 			void				_AddSoftConstraint(const ConstraintList& list);
76 
77 	const	VariableList&		fVariables;
78 	const	ConstraintList&		fConstraints;
79 
80 			ConstraintList		fVariableGEConstraints;
81 			ConstraintList		fVariableLEConstraints;
82 };
83 
84 
85 #endif // ACTICE_SET_SOLVER_H
86