From 2fa3a56f0399e5370fa367dedbc1e5f60f954fa9 Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 24 Jan 2016 19:40:55 -0500 Subject: [PATCH] [REFACTOR] Extract LP.h from LPSolver.cpp [REFACTOR] Extract InfeasibleInitialValus.h from LPSolver.cpp [REFACTOR] Extract InfeasibleOrUnboundedProblem.h from LPSolver.cpp --- .../linear/InfeasibleInitialValues.h | 34 ++++++++ .../linear/InfeasibleOrUnboundedProblem.h | 28 +++++++ gtsam_unstable/linear/LP.h | 46 +++++++++++ gtsam_unstable/linear/tests/testLPSolver.cpp | 78 ++----------------- 4 files changed, 113 insertions(+), 73 deletions(-) create mode 100644 gtsam_unstable/linear/InfeasibleInitialValues.h create mode 100644 gtsam_unstable/linear/InfeasibleOrUnboundedProblem.h create mode 100644 gtsam_unstable/linear/LP.h diff --git a/gtsam_unstable/linear/InfeasibleInitialValues.h b/gtsam_unstable/linear/InfeasibleInitialValues.h new file mode 100644 index 000000000..ff8606bf7 --- /dev/null +++ b/gtsam_unstable/linear/InfeasibleInitialValues.h @@ -0,0 +1,34 @@ +/** + * @file InfeasibleInitialValues.h + * @brief Exception thrown when given Infeasible Initial Values. + * @date jan 24, 2015 + * @author Duy-Nguyen Ta + */ + + + +#pragma once + +namespace gtsam { +/* ************************************************************************* */ +/** An exception indicating that the provided initial value is infeasible */ +class InfeasibleInitialValues: public ThreadsafeException< + InfeasibleInitialValues> { +public: + InfeasibleInitialValues() { + } + + virtual ~InfeasibleInitialValues() throw () { + } + + virtual const char *what() const throw () { + if (description_.empty()) + description_ = + "An infeasible initial value was provided for the solver.\n"; + return description_.c_str(); + } + +private: + mutable std::string description_; +}; +} diff --git a/gtsam_unstable/linear/InfeasibleOrUnboundedProblem.h b/gtsam_unstable/linear/InfeasibleOrUnboundedProblem.h new file mode 100644 index 000000000..0c362b0bc --- /dev/null +++ b/gtsam_unstable/linear/InfeasibleOrUnboundedProblem.h @@ -0,0 +1,28 @@ +/** + * @file InfeasibleOrUnboundedProblem.h + * @brief Throw when the problem is either infeasible or unbounded + * @author Ivan Dario Jimenez + * @date 1/24/16 + */ + + +namespace gtsam { + +class InfeasibleOrUnboundedProblem: public ThreadsafeException< + InfeasibleOrUnboundedProblem> { +public: + InfeasibleOrUnboundedProblem() { + } + virtual ~InfeasibleOrUnboundedProblem() throw () { + } + + virtual const char* what() const throw () { + if (description_.empty()) + description_ = "The problem is either infeasible or unbounded.\n"; + return description_.c_str(); + } + +private: + mutable std::string description_; +}; +} diff --git a/gtsam_unstable/linear/LP.h b/gtsam_unstable/linear/LP.h new file mode 100644 index 000000000..71826d972 --- /dev/null +++ b/gtsam_unstable/linear/LP.h @@ -0,0 +1,46 @@ +/** + * @file LP.h + * @brief Struct used to hold a Linear Programming Problem + * @author Ivan Dario Jimenez + * @date 1/24/16 + */ + +#pragma once + +#include + +namespace gtsam { + +using namespace std; + +struct LP { + LinearCost cost; //!< Linear cost factor + EqualityFactorGraph equalities; //!< Linear equality constraints: cE(x) = 0 + InequalityFactorGraph inequalities; //!< Linear inequality constraints: cI(x) <= 0 + + /// check feasibility + bool isFeasible(const VectorValues& x) const { + return (equalities.error(x) == 0 && inequalities.error(x) == 0); + } + + /// print + void print(const string& s = "") const { + std::cout << s << std::endl; + cost.print("Linear cost: "); + equalities.print("Linear equality factors: "); + inequalities.print("Linear inequality factors: "); + } + + /// equals + bool equals(const LP& other, double tol = 1e-9) const { + return cost.equals(other.cost) && equalities.equals(other.equalities) + && inequalities.equals(other.inequalities); + } + + typedef boost::shared_ptr shared_ptr; +}; + +/// traits +template<> struct traits : public Testable { +}; +} diff --git a/gtsam_unstable/linear/tests/testLPSolver.cpp b/gtsam_unstable/linear/tests/testLPSolver.cpp index 6d64c6314..156ba4195 100644 --- a/gtsam_unstable/linear/tests/testLPSolver.cpp +++ b/gtsam_unstable/linear/tests/testLPSolver.cpp @@ -24,89 +24,21 @@ #include #include #include - +#include #include - #include #include +#include +#include +#include + using namespace std; using namespace gtsam; using namespace gtsam::symbol_shorthand; namespace gtsam { -/* ************************************************************************* */ -/** An exception indicating that the provided initial value is infeasible */ -class InfeasibleInitialValues: public ThreadsafeException< - InfeasibleInitialValues> { -public: - InfeasibleInitialValues() { - } - virtual ~InfeasibleInitialValues() throw () { - } - - virtual const char* what() const throw () { - if (description_.empty()) description_ = - "An infeasible initial value was provided for the solver.\n"; - return description_.c_str(); - } - -private: - mutable std::string description_; -}; - -/// Throw when the problem is either infeasible or unbounded -class InfeasibleOrUnboundedProblem: public ThreadsafeException< -InfeasibleOrUnboundedProblem> { -public: - InfeasibleOrUnboundedProblem() { - } - virtual ~InfeasibleOrUnboundedProblem() throw () { - } - - virtual const char* what() const throw () { - if (description_.empty()) description_ = - "The problem is either infeasible or unbounded.\n"; - return description_.c_str(); - } - -private: - mutable std::string description_; -}; - - -struct LP { - LinearCost cost; //!< Linear cost factor - EqualityFactorGraph equalities; //!< Linear equality constraints: cE(x) = 0 - InequalityFactorGraph inequalities; //!< Linear inequality constraints: cI(x) <= 0 - - /// check feasibility - bool isFeasible(const VectorValues& x) const { - return (equalities.error(x) == 0 && inequalities.error(x) == 0); - } - - /// print - void print(const string& s = "") const { - std::cout << s << std::endl; - cost.print("Linear cost: "); - equalities.print("Linear equality factors: "); - inequalities.print("Linear inequality factors: "); - } - - /// equals - bool equals(const LP& other, double tol = 1e-9) const { - return cost.equals(other.cost) - && equalities.equals(other.equalities) - && inequalities.equals(other.inequalities); - } - - typedef boost::shared_ptr shared_ptr; -}; - -/// traits -template<> struct traits : public Testable {}; - /// This struct holds the state of QPSolver at each iteration struct LPState { VectorValues values;