xref: /haiku/headers/libs/linprog/LinearSpec.h (revision fc691d7de2182d23659b86d87c9c36b0feaa6b40)
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 			bool				AddVariable(Variable* variable);
40 			bool				RemoveVariable(Variable* variable,
41 									bool deleteVariable = true);
42 			int32				IndexOf(const Variable* variable) const;
43 			bool				SetRange(Variable* variable, double min,
44 									double max);
45 
46 			Constraint*			AddConstraint(SummandList* summands,
47 									OperatorType op, double rightSide);
48 			Constraint*			AddConstraint(double coeff1, Variable* var1,
49 									OperatorType op, double rightSide);
50 			Constraint*			AddConstraint(double coeff1, Variable* var1,
51 									double coeff2, Variable* var2,
52 									OperatorType op, double rightSide);
53 			Constraint*			AddConstraint(double coeff1, Variable* var1,
54 									double coeff2, Variable* var2,
55 									double coeff3, Variable* var3,
56 									OperatorType op, double rightSide);
57 			Constraint*			AddConstraint(double coeff1, Variable* var1,
58 									double coeff2, Variable* var2,
59 									double coeff3, Variable* var3,
60 									double coeff4, Variable* var4,
61 									OperatorType op, double rightSide);
62 
63 			Constraint*			AddConstraint(SummandList* summands,
64 									OperatorType op, double rightSide,
65 									double penaltyNeg, double penaltyPos);
66 			Constraint*			AddConstraint(double coeff1, Variable* var1,
67 									OperatorType op, double rightSide,
68 									double penaltyNeg, double penaltyPos);
69 			Constraint*			AddConstraint(double coeff1, Variable* var1,
70 									double coeff2, Variable* var2,
71 									OperatorType op, double rightSide,
72 									double penaltyNeg, double penaltyPos);
73 			Constraint*			AddConstraint(double coeff1, Variable* var1,
74 									double coeff2, Variable* var2,
75 									double coeff3, Variable* var3,
76 									OperatorType op, double rightSide,
77 									double penaltyNeg, double penaltyPos);
78 			Constraint*			AddConstraint(double coeff1, Variable* var1,
79 									double coeff2, Variable* var2,
80 									double coeff3, Variable* var3,
81 									double coeff4, Variable* var4,
82 									OperatorType op, double rightSide,
83 									double penaltyNeg, double penaltyPos);
84 
85 			PenaltyFunction*	AddPenaltyFunction(Variable* var, BList* xs,
86 									BList* gs);
87 
88 			SummandList*		ObjectiveFunction();
89 			//! Caller takes ownership of the Summand's and the SummandList.
90 			SummandList*		ReplaceObjectiveFunction(
91 									SummandList* objFunction);
92 			void				SetObjectiveFunction(SummandList* objFunction);
93 			void				UpdateObjectiveFunction();
94 
95 			ResultType			Solve();
96 			void				Save(const char* fileName);
97 
98 			int32				CountColumns() const;
99 			OptimizationType	Optimization() const;
100 			void				SetOptimization(OptimizationType value);
101 
102 			ResultType			Result() const;
103 			double				ObjectiveValue() const;
104 			double				SolvingTime() const;
105 
106 			operator BString() const;
107 			void				GetString(BString& string) const;
108 
109 	const	ConstraintList&		Constraints() const;
110 
111 private:
112 			ResultType			Presolve();
113 			void				RemovePresolved();
114 
115 			lprec*				fLpPresolved;
116 			OptimizationType	fOptimization;
117 			lprec*				fLP;
118 			SummandList*		fObjFunction;
119 			VariableList		fVariables;
120 			ConstraintList		fConstraints;
121 			ResultType			fResult;
122 			double 				fObjectiveValue;
123 			double 				fSolvingTime;
124 
125 public:
126 	friend class		Constraint;
127 
128 };
129 
130 }	// namespace LinearProgramming
131 
132 using LinearProgramming::LinearSpec;
133 
134 #endif	// LINEAR_SPEC_H
135