diff options
| author | Bjoern Brandenburg <bbb@mpi-sws.org> | 2012-08-07 14:10:06 -0400 |
|---|---|---|
| committer | Bjoern Brandenburg <bbb@mpi-sws.org> | 2013-02-12 06:49:39 -0500 |
| commit | 673f367fc5835ebce3357dfebee9c0e414e5c09b (patch) | |
| tree | ca95cb1afe91af4845627ec5a8370b364ec7193d /native/include | |
| parent | 52c28dbda3f4ef2241182e32002b710b1a9eae0b (diff) | |
Add simple C++ linear program representation
- Constraints are expressed as lists of terms.
- Linear programs consist of only equalities and less-than-or-equal constraints.
- Solvers hide behind an abstract solution class that can evaluate
linear expressions.
Diffstat (limited to 'native/include')
| -rw-r--r-- | native/include/linprog/io.h | 10 | ||||
| -rw-r--r-- | native/include/linprog/model.h | 105 | ||||
| -rw-r--r-- | native/include/linprog/solver.h | 29 |
3 files changed, 144 insertions, 0 deletions
diff --git a/native/include/linprog/io.h b/native/include/linprog/io.h new file mode 100644 index 0000000..1e9a3cc --- /dev/null +++ b/native/include/linprog/io.h | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | #ifndef LINPROG_IO_H | ||
| 2 | #define LINPROG_IO_H | ||
| 3 | |||
| 4 | #include <ostream> | ||
| 5 | |||
| 6 | #include "linprog/model.h" | ||
| 7 | |||
| 8 | std::ostream& operator<<(std::ostream &os, const LinearExpression &exp); | ||
| 9 | |||
| 10 | #endif | ||
diff --git a/native/include/linprog/model.h b/native/include/linprog/model.h new file mode 100644 index 0000000..27729dd --- /dev/null +++ b/native/include/linprog/model.h | |||
| @@ -0,0 +1,105 @@ | |||
| 1 | #ifndef LINPROG_MODEL_H | ||
| 2 | #define LINPROG_MODEL_H | ||
| 3 | |||
| 4 | #include <vector> | ||
| 5 | #include <utility> | ||
| 6 | |||
| 7 | #include "stl-helper.h" | ||
| 8 | |||
| 9 | typedef std::pair<double, unsigned int> Term; | ||
| 10 | typedef std::vector<Term> Terms; | ||
| 11 | |||
| 12 | class LinearExpression | ||
| 13 | { | ||
| 14 | private: | ||
| 15 | Terms terms; | ||
| 16 | |||
| 17 | public: | ||
| 18 | void add_term(double coefficient, unsigned int variable_index) | ||
| 19 | { | ||
| 20 | terms.push_back(Term(coefficient, variable_index)); | ||
| 21 | } | ||
| 22 | |||
| 23 | // by default, assumes coefficient == 1 | ||
| 24 | void add_var(unsigned int variable_index) | ||
| 25 | { | ||
| 26 | add_term(1.0, variable_index); | ||
| 27 | } | ||
| 28 | |||
| 29 | const Terms& get_terms(void) const | ||
| 30 | { | ||
| 31 | return terms; | ||
| 32 | } | ||
| 33 | |||
| 34 | bool has_terms(void) const | ||
| 35 | { | ||
| 36 | return !terms.empty(); | ||
| 37 | } | ||
| 38 | }; | ||
| 39 | |||
| 40 | typedef std::pair<LinearExpression *, double> Constraint; | ||
| 41 | typedef std::vector<Constraint> Constraints; | ||
| 42 | |||
| 43 | // builds a maximization problem piece-wise | ||
| 44 | class LinearProgram | ||
| 45 | { | ||
| 46 | // the function to be maximized | ||
| 47 | LinearExpression *objective; | ||
| 48 | |||
| 49 | // linear expressions constrained to an exact value | ||
| 50 | Constraints equalities; | ||
| 51 | |||
| 52 | // linear expressions constrained by an upper bound (exp <= bound) | ||
| 53 | Constraints inequalities; | ||
| 54 | |||
| 55 | public: | ||
| 56 | LinearProgram() : objective(0) {}; | ||
| 57 | |||
| 58 | ~LinearProgram() | ||
| 59 | { | ||
| 60 | delete objective; | ||
| 61 | foreach(equalities, eq) | ||
| 62 | delete eq->first; | ||
| 63 | foreach(inequalities, ineq) | ||
| 64 | delete ineq->first; | ||
| 65 | } | ||
| 66 | |||
| 67 | void set_objective(LinearExpression *exp) | ||
| 68 | { | ||
| 69 | delete objective; | ||
| 70 | objective = exp; | ||
| 71 | } | ||
| 72 | |||
| 73 | void add_inequality(LinearExpression *exp, double upper_bound) | ||
| 74 | { | ||
| 75 | if (exp->has_terms()) | ||
| 76 | inequalities.push_back(Constraint(exp, upper_bound)); | ||
| 77 | else | ||
| 78 | delete exp; | ||
| 79 | } | ||
| 80 | |||
| 81 | void add_equality(LinearExpression *exp, double equal_to) | ||
| 82 | { | ||
| 83 | if (exp->has_terms()) | ||
| 84 | inequalities.push_back(Constraint(exp, equal_to)); | ||
| 85 | else | ||
| 86 | delete exp; | ||
| 87 | } | ||
| 88 | |||
| 89 | const LinearExpression *get_objective() const | ||
| 90 | { | ||
| 91 | return objective; | ||
| 92 | } | ||
| 93 | |||
| 94 | const Constraints& get_equalities() const | ||
| 95 | { | ||
| 96 | return equalities; | ||
| 97 | } | ||
| 98 | |||
| 99 | const Constraints& get_inequalities() const | ||
| 100 | { | ||
| 101 | return inequalities; | ||
| 102 | } | ||
| 103 | }; | ||
| 104 | |||
| 105 | #endif | ||
diff --git a/native/include/linprog/solver.h b/native/include/linprog/solver.h new file mode 100644 index 0000000..15e37e6 --- /dev/null +++ b/native/include/linprog/solver.h | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | #ifndef LINPROG_SOLVER_H | ||
| 2 | #define LINPROG_SOLVER_H | ||
| 3 | |||
| 4 | #include "linprog/model.h" | ||
| 5 | |||
| 6 | class Solution | ||
| 7 | { | ||
| 8 | |||
| 9 | public: | ||
| 10 | virtual ~Solution() {}; | ||
| 11 | |||
| 12 | virtual double get_value(unsigned int variable_index) const = 0; | ||
| 13 | |||
| 14 | virtual double evaluate(const LinearExpression &exp) const | ||
| 15 | { | ||
| 16 | double sum = 0; | ||
| 17 | foreach(exp.get_terms(), term) | ||
| 18 | { | ||
| 19 | double coeff = term->first; | ||
| 20 | double var = term->second; | ||
| 21 | sum += coeff * get_value(var); | ||
| 22 | } | ||
| 23 | return sum; | ||
| 24 | } | ||
| 25 | }; | ||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | #endif | ||
