All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
constraints.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
16 #ifndef PAAL_CONSTRAINTS_HPP
17 #define PAAL_CONSTRAINTS_HPP
18 
19 #include "paal/lp/expressions.hpp"
20 
21 #include <type_traits>
22 
23 namespace paal {
24 namespace lp {
25 
26 // bound types
27 struct LowerBoundTag {};
28 struct UpperBoundTag {};
29 
30 // bound directions
31 struct RightBoundTag {};
32 struct LeftBoundTag {};
33 
34 namespace {
35 struct lp_traits {
36  static const double PLUS_INF;
37  static const double MINUS_INF;
38 };
39 
40 const double lp_traits::PLUS_INF = std::numeric_limits<double>::infinity();
41 const double lp_traits::MINUS_INF = -std::numeric_limits<double>::infinity();
42 }
43 
47 template <typename BoundTypeTag, typename BoundDirectionTag>
49  public:
52  : m_lb(lb), m_expr(std::move(expr)) {
53  assert_bound_type_tag<LowerBoundTag>();
54  }
55 
58  : m_ub(ub), m_expr(std::move(expr)) {
59  assert_bound_type_tag<UpperBoundTag>();
60  }
61 
63  linear_expression get_expression() const { return m_expr; }
64 
66  double get_lower_bound() const {
67  assert_bound_type_tag<LowerBoundTag>();
68  return m_lb;
69  }
70 
72  double get_upper_bound() const {
73  assert_bound_type_tag<UpperBoundTag>();
74  return m_ub;
75  }
76 
77  private:
78  static_assert(std::is_same<BoundDirectionTag, LeftBoundTag>::value ||
79  std::is_same<BoundDirectionTag, RightBoundTag>::value,
80  "Incorrect BoundDirectionTag");
81 
82  template <typename CorrectTag> void assert_bound_type_tag() const {
83  static_assert(std::is_same<BoundTypeTag, CorrectTag>::value,
84  "Incorrect BoundTypeTag");
85  }
86 
87  double m_lb;
88  double m_ub;
89  linear_expression m_expr;
90 };
91 
93 template <typename Stream, typename BoundDirection>
94 Stream &operator<<(
95  Stream &o,
97  o << expr.get_lower_bound() << " <= " << expr.get_expression();
98  return o;
99 }
100 
102 template <typename Stream, typename BoundDirection>
103 Stream &operator<<(
104  Stream &o,
106  o << expr.get_expression() << " <= " << expr.get_upper_bound();
107  return o;
108 }
109 
114  public:
117  : m_lb(lp_traits::MINUS_INF), m_ub(lp_traits::PLUS_INF) {}
118 
121  : m_lb(lp_traits::MINUS_INF), m_ub(lp_traits::PLUS_INF), m_expr(expr) {}
122 
125  double lb = lp_traits::MINUS_INF,
126  double ub = lp_traits::PLUS_INF)
127  : m_lb(lb), m_ub(ub), m_expr(expr) {}
128 
130  template <typename BoundDirection>
133  double ub = lp_traits::PLUS_INF)
134  : m_lb(expr.get_lower_bound()), m_ub(ub),
135  m_expr(expr.get_expression()) {}
136 
138  template <typename BoundDirection>
141  double lb = lp_traits::MINUS_INF)
142  : m_lb(lb), m_ub(expr.get_upper_bound()),
143  m_expr(expr.get_expression()) {}
144 
146  linear_expression get_expression() const { return m_expr; }
147 
149  double get_lower_bound() const { return m_lb; }
150 
152  double get_upper_bound() const { return m_ub; }
153 
154  private:
155  double m_lb;
156  double m_ub;
157  linear_expression m_expr;
158 };
159 
160 namespace detail {
161 template <typename Stream, typename PrintCol>
162 void print_double_bounded_expression(Stream &o,
163  const double_bounded_expression &expr,
164  PrintCol print_col) {
165  o << expr.get_lower_bound() << " <= ";
166  print_expression(o, expr.get_expression(), print_col);
167  o << " <= " << expr.get_upper_bound();
168 }
169 }
170 
172 template <typename Stream>
173 Stream &operator<<(Stream &o, const double_bounded_expression &expr) {
174  detail::print_double_bounded_expression(o, expr, detail::col_id_to_string);
175  return o;
176 }
177 
180  double val, const linear_expression &expr) {
182 }
183 
186  const linear_expression &expr, double val) {
188 }
189 
192  double val, const linear_expression &expr) {
194 }
195 
198  const linear_expression &expr, double val) {
200 }
201 
204  double val,
206  return double_bounded_expression(expr, val);
207 }
208 
212  double val) {
213  return double_bounded_expression(expr, val);
214 }
215 
218  double val,
220  return double_bounded_expression(expr, val);
221 }
222 
226  double val) {
227  return double_bounded_expression(expr, val);
228 }
229 
232  const linear_expression &expr) {
233  return double_bounded_expression(expr, val, val);
234 }
235 
238  double val) {
239  return double_bounded_expression(expr, val, val);
240 }
241 
242 } // lp
243 } // paal
244 
245 #endif // PAAL_CONSTRAINTS_HPP
single_bounded_expression< LowerBoundTag, RightBoundTag > operator>=(const linear_expression &expr, double val)
linear_expression &gt;= double operator.
double get_upper_bound() const
Upper bound getter.
Definition: constraints.hpp:72
double_bounded_expression(const linear_expression &expr, double lb=lp_traits::MINUS_INF, double ub=lp_traits::PLUS_INF)
Constructor from expression, lower and upper bound.
double get_upper_bound() const
Upper bound getter.
linear_expression get_expression() const
Expression getter.
double_bounded_expression(const single_bounded_expression< UpperBoundTag, BoundDirection > &expr, double lb=lp_traits::MINUS_INF)
Constructor from upper bound expression and a lower bound.
single_bounded_expression(double lb, linear_expression expr)
Constructor from expression and lower bound.
Definition: constraints.hpp:51
double_bounded_expression(const single_bounded_expression< LowerBoundTag, BoundDirection > &expr, double ub=lp_traits::PLUS_INF)
Constructor from lower bound expression and an upper bound.
double get_lower_bound() const
Lower bound getter.
Definition: constraints.hpp:66
double_bounded_expression operator==(double val, const linear_expression &expr)
double == linear_expression operator.
double_bounded_expression(const col_id &expr)
Constructor from a single column expression.
linear_expression get_expression() const
Expression getter.
Definition: constraints.hpp:63
Stream & operator<<(Stream &o, const single_bounded_expression< LowerBoundTag, BoundDirection > &expr)
operator&lt;&lt; for Lower Bound
Definition: constraints.hpp:94
single_bounded_expression< LowerBoundTag, LeftBoundTag > operator<=(double val, const linear_expression &expr)
double &lt;= linear_expression operator.
single_bounded_expression(linear_expression expr, double ub)
Constructor from expression and upper bound.
Definition: constraints.hpp:57
double get_lower_bound() const
Lower bound getter.