xref: /haiku/headers/libs/linprog/LinearSpec.h (revision f9cabdd00949d006252087a791c5d458fbfb5291)
1  /*
2   * Copyright 2007-2008, Christof Lutteroth, lutteroth@cs.auckland.ac.nz
3   * Copyright 2007-2008, James Kim, jkim202@ec.auckland.ac.nz
4   * Copyright 2010, Clemens Zeidler, haiku@clemens-zeidler.de
5   * Distributed under the terms of the MIT License.
6   */
7  #ifndef	LINEAR_SPEC_H
8  #define	LINEAR_SPEC_H
9  
10  #include <math.h>
11  
12  #include <List.h>
13  #include <OS.h>
14  #include <Referenceable.h>
15  #include <Size.h>
16  #include <String.h>
17  #include <SupportDefs.h>
18  
19  #include "Constraint.h"
20  #include "LinearProgrammingTypes.h"
21  #include "Summand.h"
22  #include "Variable.h"
23  
24  
25  namespace LinearProgramming {
26  
27  
28  class LinearSpec;
29  
30  
31  const BSize kMinSize(0, 0);
32  const BSize kMaxSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED);
33  
34  
35  class SolverInterface {
36  public:
37  								SolverInterface(LinearSpec* linSpec);
38  
39  	virtual						~SolverInterface() {}
40  
41  	virtual ResultType			Solve() = 0;
42  
43  	virtual	bool				VariableAdded(Variable* variable) = 0;
44  	virtual	bool				VariableRemoved(Variable* variable) = 0;
45  	virtual	bool				VariableRangeChanged(Variable* variable) = 0;
46  
47  	virtual	bool				ConstraintAdded(Constraint* constraint) = 0;
48  	virtual	bool				ConstraintRemoved(Constraint* constraint) = 0;
49  	virtual	bool				LeftSideChanged(Constraint* constraint) = 0;
50  	virtual	bool				RightSideChanged(Constraint* constraint) = 0;
51  	virtual	bool				OperatorChanged(Constraint* constraint) = 0;
52  
53  	virtual bool				SaveModel(const char* fileName) = 0;
54  
55  	virtual	BSize				MinSize(Variable* width, Variable* height) = 0;
56  	virtual	BSize				MaxSize(Variable* width, Variable* height) = 0;
57  
58  protected:
59  			LinearSpec*			fLinearSpec;
60  };
61  
62  
63  /*!
64   * Specification of a linear programming problem.
65   */
66  class LinearSpec : public BReferenceable {
67  public:
68  								LinearSpec();
69  	virtual						~LinearSpec();
70  
71  			Variable*			AddVariable();
72  			bool				AddVariable(Variable* variable);
73  			bool				RemoveVariable(Variable* variable,
74  									bool deleteVariable = true);
75  			int32				IndexOf(const Variable* variable) const;
76  			int32				GlobalIndexOf(const Variable* variable) const;
77  			bool				UpdateRange(Variable* variable);
78  
79  			bool				AddConstraint(Constraint* constraint);
80  			bool				RemoveConstraint(Constraint* constraint,
81  									bool deleteConstraint = true);
82  
83  			Constraint*			AddConstraint(SummandList* summands,
84  									OperatorType op, double rightSide);
85  			Constraint*			AddConstraint(double coeff1, Variable* var1,
86  									OperatorType op, double rightSide);
87  			Constraint*			AddConstraint(double coeff1, Variable* var1,
88  									double coeff2, Variable* var2,
89  									OperatorType op, double rightSide);
90  			Constraint*			AddConstraint(double coeff1, Variable* var1,
91  									double coeff2, Variable* var2,
92  									double coeff3, Variable* var3,
93  									OperatorType op, double rightSide);
94  			Constraint*			AddConstraint(double coeff1, Variable* var1,
95  									double coeff2, Variable* var2,
96  									double coeff3, Variable* var3,
97  									double coeff4, Variable* var4,
98  									OperatorType op, double rightSide);
99  
100  			Constraint*			AddConstraint(SummandList* summands,
101  									OperatorType op, double rightSide,
102  									double penaltyNeg, double penaltyPos);
103  			Constraint*			AddConstraint(double coeff1, Variable* var1,
104  									OperatorType op, double rightSide,
105  									double penaltyNeg, double penaltyPos);
106  			Constraint*			AddConstraint(double coeff1, Variable* var1,
107  									double coeff2, Variable* var2,
108  									OperatorType op, double rightSide,
109  									double penaltyNeg, double penaltyPos);
110  			Constraint*			AddConstraint(double coeff1, Variable* var1,
111  									double coeff2, Variable* var2,
112  									double coeff3, Variable* var3,
113  									OperatorType op, double rightSide,
114  									double penaltyNeg, double penaltyPos);
115  			Constraint*			AddConstraint(double coeff1, Variable* var1,
116  									double coeff2, Variable* var2,
117  									double coeff3, Variable* var3,
118  									double coeff4, Variable* var4,
119  									OperatorType op, double rightSide,
120  									double penaltyNeg, double penaltyPos);
121  
122  			BSize				MinSize(Variable* width, Variable* height);
123  			BSize				MaxSize(Variable* width, Variable* height);
124  
125  			ResultType			Solve();
126  			bool				Save(const char* fileName);
127  
128  			int32				CountColumns() const;
129  
130  			ResultType			Result() const;
131  			bigtime_t			SolvingTime() const;
132  
133  			BString				ToString() const;
134  
135  	const	ConstraintList&		Constraints() const;
136  	const	VariableList&		UsedVariables() const;
137  	const	VariableList&		AllVariables() const;
138  
139  protected:
140  	friend class Constraint;
141   			bool				UpdateLeftSide(Constraint* constraint);
142  			bool				UpdateRightSide(Constraint* constraint);
143  			bool				UpdateOperator(Constraint* constraint);
144  private:
145  			/*! Check if all entries != NULL otherwise delete the list and its
146  			entries. */
147  			bool				_CheckSummandList(SummandList* list);
148  			Constraint*			_AddConstraint(SummandList* leftSide,
149  									OperatorType op, double rightSide,
150  									double penaltyNeg, double penaltyPos);
151  
152  			VariableList		fVariables;
153  			VariableList		fUsedVariables;
154  			ConstraintList		fConstraints;
155  			ResultType			fResult;
156  			bigtime_t 			fSolvingTime;
157  
158  			SolverInterface*	fSolver;
159  };
160  
161  }	// namespace LinearProgramming
162  
163  using LinearProgramming::LinearSpec;
164  
165  #endif	// LINEAR_SPEC_H
166