xref: /haiku/src/libs/linprog/ActiveSetSolver.h (revision 1b6bc2675fe3691538c8764ab016593f3b06ca53)
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 "QPSolverInterface.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				GaussianElimination();
31 			bool				GaussJordan();
32 			/*! Gauss Jordan elimination just for one column, the diagonal
33 			element must be none zero. */
34 			void				GaussJordan(int32 column);
35 
36 			void				RemoveLinearlyDependentRows();
37 			void				RemoveUnusedVariables();
38 
39 			void				MoveColumnRight(int32 i, int32 target);
40 
41 			void				Print();
42 private:
43 	inline	void				_EliminateColumn(int32 column, int32 startRow,
44 									int32 endRow);
45 
46 			int32*				fRowIndices;
47 			int32*				fColumnIndices;
48 			double**			fMatrix;
49 			double*				fB;
50 			int32				fRows;
51 			int32				fColumns;
52 };
53 
54 
55 class ActiveSetSolver : public QPSolverInterface {
56 public:
57 								ActiveSetSolver(LinearSpec* linearSpec);
58 								~ActiveSetSolver();
59 
60 			ResultType			Solve();
61 
62 			bool				VariableAdded(Variable* variable);
63 			bool				VariableRemoved(Variable* variable);
64 			bool				VariableRangeChanged(Variable* variable);
65 
66 			bool				ConstraintAdded(Constraint* constraint);
67 			bool				ConstraintRemoved(Constraint* constraint);
68 			bool				LeftSideChanged(Constraint* constraint);
69 			bool				RightSideChanged(Constraint* constraint);
70 			bool				OperatorChanged(Constraint* constraint);
71 
72 			bool				SaveModel(const char* fileName);
73 
74 			BSize				MinSize(Variable* width, Variable* height);
75 			BSize				MaxSize(Variable* width, Variable* height);
76 
77 public:
78 			void				_RemoveSoftConstraint(ConstraintList& list);
79 			void				_AddSoftConstraint(const ConstraintList& list);
80 
81 	const	VariableList&		fVariables;
82 	const	ConstraintList&		fConstraints;
83 
84 			ConstraintList		fVariableGEConstraints;
85 			ConstraintList		fVariableLEConstraints;
86 };
87 
88 
89 #endif // ACTICE_SET_SOLVER_H
90