xref: /haiku/src/libs/linprog/LayoutOptimizer.h (revision f2b4344867e97c3f4e742a1b4a15e6879644601a)
1 /*
2  * Copyright 2007, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 #ifndef LAYOUT_OPTIMIZER_H
6 #define LAYOUT_OPTIMIZER_H
7 
8 #include <List.h>
9 #include <math.h>
10 
11 #include "LinearSpec.h"
12 
13 
14 static const double kEqualsEpsilon = 0.000001;
15 
16 
17 double** allocate_matrix(int m, int n);
18 void free_matrix(double** matrix);
19 void copy_matrix(const double* const* A, double** B, int m, int n);
20 void zero_matrix(double** A, int m, int n);
21 int compute_dependencies(double** a, int m, int n, bool* independent);
22 
23 
24 class LayoutOptimizer {
25 public:
26 								LayoutOptimizer(const ConstraintList& list,
27 									int32 variableCount);
28 								~LayoutOptimizer();
29 
30 			bool				SetConstraints(const ConstraintList& list,
31 									int32 variableCount);
32 
33 			status_t			InitCheck() const;
34 
35 			bool				Solve(double* initialSolution);
36 
37 private:
38 			double				_ActualValue(Constraint* constraint,
39 									double* values) const;
40 			double				_RightSide(Constraint* constraint);
41 
42 			void				_MakeEmpty();
43 			void				_Init(int32 variableCount, int32 nConstraints);
44 
45 			bool				_Solve(double* values);
46 			bool				_SolveSubProblem(const double* d, int am,
47 									double* p);
48 			void				_SetResult(const double* x, double* values);
49 
50 
51 			int32				fVariableCount;
52 			ConstraintList*		fConstraints;
53 
54 			double**			fTemp1;
55 			double**			fTemp2;
56 			double**			fZtrans;
57 			double**			fQ;
58 			double**			fActiveMatrix;
59 			double**			fActiveMatrixTemp;
60 			double**			fSoftConstraints;
61 			double**			fG;
62 			double*				fDesired;
63 };
64 
65 
66 inline bool
67 fuzzy_equals(double a, double b)
68 {
69 	return fabs(a - b) < kEqualsEpsilon;
70 }
71 
72 
73 #endif	// LAYOUT_OPTIMIZER_H
74