xref: /haiku/src/libs/linprog/LayoutOptimizer.h (revision 7583db5a1ec81636829331e5fc7cae81ca7a7ba7)
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 namespace BPrivate {
18 namespace Layout {
19 
20 
21 double** allocate_matrix(int m, int n);
22 void free_matrix(double** matrix);
23 void copy_matrix(const double* const* A, double** B, int m, int n);
24 void zero_matrix(double** A, int m, int n);
25 int compute_dependencies(double** a, int m, int n, bool* independent);
26 int remove_linearly_dependent_rows(double** A, double** temp,
27 	bool* independentRows, int m, int n);
28 bool solve(double** a, int n, double* b);
29 
30 
31 class LayoutOptimizer {
32 public:
33 								LayoutOptimizer(const ConstraintList& list,
34 									int32 variableCount);
35 								~LayoutOptimizer();
36 
37 			bool				SetConstraints(const ConstraintList& list,
38 									int32 variableCount);
39 
40 			status_t			InitCheck() const;
41 
42 			bool				Solve(double* initialSolution);
43 
44 private:
45 			double				_ActualValue(Constraint* constraint,
46 									double* values) const;
47 			double				_RightSide(Constraint* constraint);
48 
49 			void				_MakeEmpty();
50 			void				_Init(int32 variableCount, int32 nConstraints);
51 
52 			bool				_Solve(double* values);
53 			bool				_SolveSubProblem(const double* d, int am,
54 									double* p);
55 			void				_SetResult(const double* x, double* values);
56 
57 
58 			int32				fVariableCount;
59 			ConstraintList		fConstraints;
60 
61 			double**			fTemp1;
62 			double**			fTemp2;
63 			double**			fZtrans;
64 			double**			fQ;
65 			double**			fActiveMatrix;
66 			double**			fActiveMatrixTemp;
67 			double**			fSoftConstraints;
68 			double**			fG;
69 			double*				fDesired;
70 };
71 
72 }	// namespace Layout
73 }	// namespace BPrivate
74 
75 using BPrivate::Layout::LayoutOptimizer;
76 
77 
78 inline bool
79 fuzzy_equals(double a, double b)
80 {
81 	return fabs(a - b) < kEqualsEpsilon;
82 }
83 
84 
85 #endif	// LAYOUT_OPTIMIZER_H
86