xref: /haiku/headers/libs/linprog/LinearSpec.h (revision 25a7b01d15612846f332751841da3579db313082)
103069455SIngo Weinhold /*
203069455SIngo Weinhold  * Copyright 2007-2008, Christof Lutteroth, lutteroth@cs.auckland.ac.nz
303069455SIngo Weinhold  * Copyright 2007-2008, James Kim, jkim202@ec.auckland.ac.nz
444032741SClemens Zeidler  * Copyright 2010, Clemens Zeidler, haiku@clemens-zeidler.de
503069455SIngo Weinhold  * Distributed under the terms of the MIT License.
603069455SIngo Weinhold  */
7a101e99aSIngo Weinhold #ifndef	LINEAR_SPEC_H
8a101e99aSIngo Weinhold #define	LINEAR_SPEC_H
9a101e99aSIngo Weinhold 
10*cf5eb5ddSczeidler #include <ObjectList.h>
1144032741SClemens Zeidler #include <OS.h>
127c380007SAlex Wilson #include <Referenceable.h>
137583db5aSClemens Zeidler #include <Size.h>
1444032741SClemens Zeidler #include <String.h>
1544032741SClemens Zeidler #include <SupportDefs.h>
1644032741SClemens Zeidler 
1744032741SClemens Zeidler #include "Constraint.h"
18f108cdbfSClemens Zeidler #include "LinearProgrammingTypes.h"
1944032741SClemens Zeidler #include "Summand.h"
2044032741SClemens Zeidler #include "Variable.h"
2144032741SClemens Zeidler 
22cd88ee00SClemens Zeidler 
23a101e99aSIngo Weinhold namespace LinearProgramming {
24a101e99aSIngo Weinhold 
257583db5aSClemens Zeidler 
267583db5aSClemens Zeidler class LinearSpec;
277583db5aSClemens Zeidler 
287583db5aSClemens Zeidler 
297583db5aSClemens Zeidler const BSize kMinSize(0, 0);
307583db5aSClemens Zeidler const BSize kMaxSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED);
317583db5aSClemens Zeidler 
327583db5aSClemens Zeidler 
335440f6feSClemens Zeidler class SolverInterface {
345440f6feSClemens Zeidler public:
357583db5aSClemens Zeidler 								SolverInterface(LinearSpec* linSpec);
367583db5aSClemens Zeidler 
37*cf5eb5ddSczeidler 	virtual						~SolverInterface();
385440f6feSClemens Zeidler 
397583db5aSClemens Zeidler 	virtual ResultType			Solve() = 0;
405440f6feSClemens Zeidler 
417583db5aSClemens Zeidler 	virtual	bool				VariableAdded(Variable* variable) = 0;
427583db5aSClemens Zeidler 	virtual	bool				VariableRemoved(Variable* variable) = 0;
437583db5aSClemens Zeidler 	virtual	bool				VariableRangeChanged(Variable* variable) = 0;
445440f6feSClemens Zeidler 
457583db5aSClemens Zeidler 	virtual	bool				ConstraintAdded(Constraint* constraint) = 0;
467583db5aSClemens Zeidler 	virtual	bool				ConstraintRemoved(Constraint* constraint) = 0;
477583db5aSClemens Zeidler 	virtual	bool				LeftSideChanged(Constraint* constraint) = 0;
487583db5aSClemens Zeidler 	virtual	bool				RightSideChanged(Constraint* constraint) = 0;
497583db5aSClemens Zeidler 	virtual	bool				OperatorChanged(Constraint* constraint) = 0;
50*cf5eb5ddSczeidler 	virtual	bool				PenaltiesChanged(Constraint* constraint) = 0;
515440f6feSClemens Zeidler 
525440f6feSClemens Zeidler 	virtual bool				SaveModel(const char* fileName) = 0;
537583db5aSClemens Zeidler 
54419fe0b8SAlex Wilson 	virtual	ResultType			FindMins(const VariableList* variables) = 0;
55419fe0b8SAlex Wilson 	virtual	ResultType			FindMaxs(const VariableList* variables) = 0;
56419fe0b8SAlex Wilson 
57c8b24e3eSAlex Wilson 	virtual	void				GetRangeConstraints(const Variable* var,
58c8b24e3eSAlex Wilson 									const Constraint** _min,
59c8b24e3eSAlex Wilson 									const Constraint** _max) const = 0;
60c8b24e3eSAlex Wilson 
617583db5aSClemens Zeidler protected:
62*cf5eb5ddSczeidler 			bool				AddConstraint(Constraint* constraint,
63*cf5eb5ddSczeidler 									bool notifyListener);
64*cf5eb5ddSczeidler 			bool				RemoveConstraint(Constraint* constraint,
65*cf5eb5ddSczeidler 									bool deleteConstraint, bool notifyListener);
66*cf5eb5ddSczeidler 
677583db5aSClemens Zeidler 			LinearSpec*			fLinearSpec;
685440f6feSClemens Zeidler };
695440f6feSClemens Zeidler 
705440f6feSClemens Zeidler 
71*cf5eb5ddSczeidler class SpecificationListener {
72*cf5eb5ddSczeidler public:
73*cf5eb5ddSczeidler 	virtual						~SpecificationListener();
74*cf5eb5ddSczeidler 
75*cf5eb5ddSczeidler 	virtual	void				VariableAdded(Variable* variable);
76*cf5eb5ddSczeidler 	virtual	void				VariableRemoved(Variable* variable);
77*cf5eb5ddSczeidler 	virtual void				ConstraintAdded(Constraint* constraint);
78*cf5eb5ddSczeidler 	virtual void				ConstraintRemoved(Constraint* constraint);
79*cf5eb5ddSczeidler };
80*cf5eb5ddSczeidler 
81*cf5eb5ddSczeidler 
8244032741SClemens Zeidler /*!
83a101e99aSIngo Weinhold  * Specification of a linear programming problem.
84a101e99aSIngo Weinhold  */
857c380007SAlex Wilson class LinearSpec : public BReferenceable {
86a101e99aSIngo Weinhold public:
87a101e99aSIngo Weinhold 								LinearSpec();
88676ef01bSAxel Dörfler 	virtual						~LinearSpec();
895bced18eSIngo Weinhold 
90*cf5eb5ddSczeidler 			/*! Does not takes ownership of the listener. */
91*cf5eb5ddSczeidler 			bool				AddListener(SpecificationListener* listener);
92*cf5eb5ddSczeidler 			bool				RemoveListener(SpecificationListener* listener);
93*cf5eb5ddSczeidler 
94a101e99aSIngo Weinhold 			Variable*			AddVariable();
95fc691d7dSClemens Zeidler 			bool				AddVariable(Variable* variable);
96fc691d7dSClemens Zeidler 			bool				RemoveVariable(Variable* variable,
97fc691d7dSClemens Zeidler 									bool deleteVariable = true);
98fc691d7dSClemens Zeidler 			int32				IndexOf(const Variable* variable) const;
9982867791SClemens Zeidler 			int32				GlobalIndexOf(const Variable* variable) const;
1005440f6feSClemens Zeidler 			bool				UpdateRange(Variable* variable);
101a101e99aSIngo Weinhold 
102067f47a3SClemens Zeidler 			bool				AddConstraint(Constraint* constraint);
103067f47a3SClemens Zeidler 			bool				RemoveConstraint(Constraint* constraint,
104067f47a3SClemens Zeidler 									bool deleteConstraint = true);
105067f47a3SClemens Zeidler 
106601eded9SClemens Zeidler 			Constraint*			AddConstraint(SummandList* summands,
107a101e99aSIngo Weinhold 									OperatorType op, double rightSide,
108*cf5eb5ddSczeidler 									double penaltyNeg = -1,
109*cf5eb5ddSczeidler 									double penaltyPos = -1);
11003069455SIngo Weinhold 			Constraint*			AddConstraint(double coeff1, Variable* var1,
111a101e99aSIngo Weinhold 									OperatorType op, double rightSide,
112*cf5eb5ddSczeidler 									double penaltyNeg = -1,
113*cf5eb5ddSczeidler 									double penaltyPos = -1);
11403069455SIngo Weinhold 			Constraint*			AddConstraint(double coeff1, Variable* var1,
115a101e99aSIngo Weinhold 									double coeff2, Variable* var2,
116a101e99aSIngo Weinhold 									OperatorType op, double rightSide,
117*cf5eb5ddSczeidler 									double penaltyNeg = -1,
118*cf5eb5ddSczeidler 									double penaltyPos = -1);
11903069455SIngo Weinhold 			Constraint*			AddConstraint(double coeff1, Variable* var1,
120a101e99aSIngo Weinhold 									double coeff2, Variable* var2,
121a101e99aSIngo Weinhold 									double coeff3, Variable* var3,
122a101e99aSIngo Weinhold 									OperatorType op, double rightSide,
123*cf5eb5ddSczeidler 									double penaltyNeg = -1,
124*cf5eb5ddSczeidler 									double penaltyPos = -1);
12503069455SIngo Weinhold 			Constraint*			AddConstraint(double coeff1, Variable* var1,
126a101e99aSIngo Weinhold 									double coeff2, Variable* var2,
127a101e99aSIngo Weinhold 									double coeff3, Variable* var3,
128a101e99aSIngo Weinhold 									double coeff4, Variable* var4,
129a101e99aSIngo Weinhold 									OperatorType op, double rightSide,
130*cf5eb5ddSczeidler 									double penaltyNeg = -1,
131*cf5eb5ddSczeidler 									double penaltyPos = -1);
1325bced18eSIngo Weinhold 
133419fe0b8SAlex Wilson 			ResultType			FindMins(const VariableList* variables);
134419fe0b8SAlex Wilson 			ResultType			FindMaxs(const VariableList* variables);
135419fe0b8SAlex Wilson 
136a101e99aSIngo Weinhold 			ResultType			Solve();
1375440f6feSClemens Zeidler 			bool				Save(const char* fileName);
138a101e99aSIngo Weinhold 
1395bced18eSIngo Weinhold 			int32				CountColumns() const;
14044032741SClemens Zeidler 
141a101e99aSIngo Weinhold 			ResultType			Result() const;
1427583db5aSClemens Zeidler 			bigtime_t			SolvingTime() const;
143a101e99aSIngo Weinhold 
144ef93b55dSClemens Zeidler 			BString				ToString() const;
145676ef01bSAxel Dörfler 
146c8b24e3eSAlex Wilson 			void				GetRangeConstraints(const Variable*,
147c8b24e3eSAlex Wilson 									const Constraint** _min,
148c8b24e3eSAlex Wilson 								   	const Constraint** _max) const;
149fc691d7dSClemens Zeidler 	const	ConstraintList&		Constraints() const;
15082867791SClemens Zeidler 	const	VariableList&		UsedVariables() const;
15182867791SClemens Zeidler 	const	VariableList&		AllVariables() const;
152a101e99aSIngo Weinhold 
153067f47a3SClemens Zeidler protected:
154067f47a3SClemens Zeidler 	friend class Constraint;
155*cf5eb5ddSczeidler 	friend class SolverInterface;
156*cf5eb5ddSczeidler 
157*cf5eb5ddSczeidler  			bool				UpdateLeftSide(Constraint* constraint,
158*cf5eb5ddSczeidler  									const SummandList* oldSummands);
159067f47a3SClemens Zeidler 			bool				UpdateRightSide(Constraint* constraint);
160067f47a3SClemens Zeidler 			bool				UpdateOperator(Constraint* constraint);
161*cf5eb5ddSczeidler 			bool				UpdatePenalties(Constraint* constraint);
162*cf5eb5ddSczeidler 
163*cf5eb5ddSczeidler 			bool				AddConstraint(Constraint* constraint,
164*cf5eb5ddSczeidler 									bool notifyListener);
165*cf5eb5ddSczeidler 			bool				RemoveConstraint(Constraint* constraint,
166*cf5eb5ddSczeidler 									bool deleteConstraint, bool notifyListener);
167a101e99aSIngo Weinhold private:
168067f47a3SClemens Zeidler 			/*! Check if all entries != NULL otherwise delete the list and its
169067f47a3SClemens Zeidler 			entries. */
170067f47a3SClemens Zeidler 			bool				_CheckSummandList(SummandList* list);
171067f47a3SClemens Zeidler 			Constraint*			_AddConstraint(SummandList* leftSide,
172067f47a3SClemens Zeidler 									OperatorType op, double rightSide,
173067f47a3SClemens Zeidler 									double penaltyNeg, double penaltyPos);
174067f47a3SClemens Zeidler 
175*cf5eb5ddSczeidler 			void				_AddConstraintRef(Variable* var);
176*cf5eb5ddSczeidler 			void				_RemoveConstraintRef(Variable* var);
177*cf5eb5ddSczeidler 
178cd88ee00SClemens Zeidler 			VariableList		fVariables;
179a0ad88e0SClemens Zeidler 			VariableList		fUsedVariables;
180cd88ee00SClemens Zeidler 			ConstraintList		fConstraints;
181a101e99aSIngo Weinhold 			ResultType			fResult;
1827583db5aSClemens Zeidler 			bigtime_t 			fSolvingTime;
183a101e99aSIngo Weinhold 
1845440f6feSClemens Zeidler 			SolverInterface*	fSolver;
185*cf5eb5ddSczeidler 
186*cf5eb5ddSczeidler 			BObjectList<SpecificationListener>	fListeners;
187a101e99aSIngo Weinhold };
188a101e99aSIngo Weinhold 
189a101e99aSIngo Weinhold }	// namespace LinearProgramming
190a101e99aSIngo Weinhold 
191a101e99aSIngo Weinhold using LinearProgramming::LinearSpec;
192a101e99aSIngo Weinhold 
193a101e99aSIngo Weinhold #endif	// LINEAR_SPEC_H
194