xref: /haiku/headers/libs/linprog/LinearSpec.h (revision cad0c434c7585bd21548499b2fcdf347c44b8074)
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 <String.h>
15 #include <SupportDefs.h>
16 
17 #include "Constraint.h"
18 #include "OperatorType.h"
19 #include "OptimizationType.h"
20 #include "PenaltyFunction.h"
21 #include "ResultType.h"
22 #include "Summand.h"
23 #include "Variable.h"
24 
25 #include "lp_lib.h"
26 
27 
28 namespace LinearProgramming {
29 
30 /*!
31  * Specification of a linear programming problem.
32  */
33 class LinearSpec {
34 public:
35 								LinearSpec();
36 	virtual						~LinearSpec();
37 
38 			Variable*			AddVariable();
39 
40 			Constraint*			AddConstraint(SummandList* summands,
41 									OperatorType op, double rightSide);
42 			Constraint*			AddConstraint(double coeff1, Variable* var1,
43 									OperatorType op, double rightSide);
44 			Constraint*			AddConstraint(double coeff1, Variable* var1,
45 									double coeff2, Variable* var2,
46 									OperatorType op, double rightSide);
47 			Constraint*			AddConstraint(double coeff1, Variable* var1,
48 									double coeff2, Variable* var2,
49 									double coeff3, Variable* var3,
50 									OperatorType op, double rightSide);
51 			Constraint*			AddConstraint(double coeff1, Variable* var1,
52 									double coeff2, Variable* var2,
53 									double coeff3, Variable* var3,
54 									double coeff4, Variable* var4,
55 									OperatorType op, double rightSide);
56 
57 			Constraint*			AddConstraint(SummandList* summands,
58 									OperatorType op, double rightSide,
59 									double penaltyNeg, double penaltyPos);
60 			Constraint*			AddConstraint(double coeff1, Variable* var1,
61 									OperatorType op, double rightSide,
62 									double penaltyNeg, double penaltyPos);
63 			Constraint*			AddConstraint(double coeff1, Variable* var1,
64 									double coeff2, Variable* var2,
65 									OperatorType op, double rightSide,
66 									double penaltyNeg, double penaltyPos);
67 			Constraint*			AddConstraint(double coeff1, Variable* var1,
68 									double coeff2, Variable* var2,
69 									double coeff3, Variable* var3,
70 									OperatorType op, double rightSide,
71 									double penaltyNeg, double penaltyPos);
72 			Constraint*			AddConstraint(double coeff1, Variable* var1,
73 									double coeff2, Variable* var2,
74 									double coeff3, Variable* var3,
75 									double coeff4, Variable* var4,
76 									OperatorType op, double rightSide,
77 									double penaltyNeg, double penaltyPos);
78 
79 			PenaltyFunction*	AddPenaltyFunction(Variable* var, BList* xs,
80 									BList* gs);
81 
82 			SummandList*		ObjectiveFunction();
83 			//! Caller takes ownership of the Summand's and the SummandList.
84 			SummandList*		ReplaceObjectiveFunction(
85 									SummandList* objFunction);
86 			void				SetObjectiveFunction(SummandList* objFunction);
87 			void				UpdateObjectiveFunction();
88 
89 			ResultType			Presolve();
90 			void				RemovePresolved();
91 			ResultType			Solve();
92 			void				Save(const char* fileName);
93 
94 			int32				CountColumns() const;
95 			OptimizationType	Optimization() const;
96 			void				SetOptimization(OptimizationType value);
97 
98 			ResultType			Result() const;
99 			double				ObjectiveValue() const;
100 			double				SolvingTime() const;
101 
102 			operator BString() const;
103 			void				GetString(BString& string) const;
104 
105 protected:
106 			VariableList*		Variables() const;
107 			ConstraintList*		Constraints() const;
108 
109 			int32 				fCountColumns;
110 
111 private:
112 			lprec*				fLpPresolved;
113 			OptimizationType	fOptimization;
114 			lprec*				fLP;
115 			SummandList*		fObjFunction;
116 			VariableList		fVariables;
117 			ConstraintList		fConstraints;
118 			ResultType			fResult;
119 			double 				fObjectiveValue;
120 			double 				fSolvingTime;
121 
122 public:
123 	friend class		Constraint;
124 	friend class		Variable;
125 
126 };
127 
128 }	// namespace LinearProgramming
129 
130 using LinearProgramming::LinearSpec;
131 
132 #endif	// LINEAR_SPEC_H
133