diff options
| -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 | ||||
| -rw-r--r-- | native/src/linprog/io.cpp | 22 |
4 files changed, 166 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 | ||
diff --git a/native/src/linprog/io.cpp b/native/src/linprog/io.cpp new file mode 100644 index 0000000..1b5d503 --- /dev/null +++ b/native/src/linprog/io.cpp | |||
| @@ -0,0 +1,22 @@ | |||
| 1 | #include <iostream> | ||
| 2 | |||
| 3 | #include "linprog/io.h" | ||
| 4 | |||
| 5 | std::ostream& operator<<(std::ostream &os, const LinearExpression &exp) | ||
| 6 | { | ||
| 7 | bool first = true; | ||
| 8 | foreach (exp.get_terms(), term) | ||
| 9 | { | ||
| 10 | if (term->first < 0) | ||
| 11 | os << "- " << -term->first; | ||
| 12 | else if (!first) | ||
| 13 | os << "+ " << term->first; | ||
| 14 | else | ||
| 15 | os << term->first; | ||
| 16 | |||
| 17 | os << " X" << term->second << " "; | ||
| 18 | first = false; | ||
| 19 | } | ||
| 20 | |||
| 21 | return os; | ||
| 22 | } | ||
