CYCLUS
capacity_constraint.h
Go to the documentation of this file.
1 #ifndef CYCLUS_SRC_CAPACITY_CONSTRAINT_H_
2 #define CYCLUS_SRC_CAPACITY_CONSTRAINT_H_
3 
4 #include <boost/shared_ptr.hpp>
5 
6 #include "error.h"
7 #include "exchange_graph.h"
9 
10 namespace cyclus {
11 
12 /// @class Converter
13 ///
14 /// @brief a simple interface for converting resource objects to unit capacities
15 template<class T>
16 struct Converter {
17  typedef boost::shared_ptr< Converter<T> > Ptr;
18 
19  /// @brief convert a capacitated quantity for an offer in its exchange context
20  /// @param offer the resource being offered
21  /// @param a the associated arc for the potential offer
22  /// @param ctx the exchange context in which the offer is being made
23  ///
24  /// @warning it is up to the user to inherit default parameters
25  virtual double convert(
26  boost::shared_ptr<T> offer,
27  Arc const * a = NULL,
28  ExchangeTranslationContext<T> const * ctx = NULL) const = 0;
29 
30  /// @brief operator== is available for subclassing, see
31  /// cyclus::TrivialConverter for an example
32  virtual bool operator==(Converter& other) const {
33  return false;
34  }
35  bool operator!=(Converter& other) const {
36  return !operator==(other);
37  }
38 };
39 
40 /// @class TrivialConverter
41 ///
42 /// @brief The default converter returns the resource's quantity
43 template<class T>
44 struct TrivialConverter : public Converter<T> {
45  /// @returns the quantity of resource offer
46  inline virtual double convert(
47  boost::shared_ptr<T> offer,
48  Arc const * a = NULL,
49  ExchangeTranslationContext<T> const * ctx = NULL) const {
50  return offer->quantity();
51  }
52 
53  /// @returns true if a dynamic cast succeeds
54  virtual bool operator==(Converter<T>& other) const {
55  return dynamic_cast<TrivialConverter<T>*>(&other) != NULL;
56  }
57 };
58 
59 /// @class CapacityConstraint
60 ///
61 /// @brief A CapacityConstraint provides an ability to determine an agent's
62 /// constraints on resource allocation given a capacity.
63 template <class T>
65  public:
66  /// @brief constructor for a constraint with a non-trivial converter
67  CapacityConstraint(double capacity, typename Converter<T>::Ptr converter)
68  : capacity_(capacity),
69  converter_(converter),
70  id_(next_id_++) {
71  if (capacity_ <= 0)
72  throw ValueError("Capacity is not positive, no trades will be executed");
73  }
74 
75  /// @brief constructor for a constraint with a trivial converter (i.e., one
76  /// that simply returns 1)
77  explicit CapacityConstraint(double capacity)
78  : capacity_(capacity),
79  id_(next_id_++) {
80  if (capacity_ <= 0)
81  throw ValueError("Capacity is not positive, no trades will be executed");
82  converter_ = typename Converter<T>::Ptr(new TrivialConverter<T>());
83  }
84 
85  /// @brief constructor for a constraint with a non-trivial converter
87  : capacity_(other.capacity_),
88  converter_(other.converter_),
89  id_(next_id_++) {}
90 
91  /// @return the constraints capacity
92  inline double capacity() const {
93  return capacity_;
94  }
95 
96  /// @return the converter
97  inline typename Converter<T>::Ptr converter() const {
98  return converter_;
99  }
100 
101  inline double convert(
102  boost::shared_ptr<T> offer,
103  Arc const * a = NULL,
104  ExchangeTranslationContext<T> const * ctx = NULL) const {
105  return converter_->convert(offer, a, ctx);
106  }
107 
108  /// @return a unique id for the constraint
109  inline int id() const {
110  return id_;
111  }
112 
113  private:
114  double capacity_;
115  typename Converter<T>::Ptr converter_;
116  int id_;
117  static int next_id_;
118 };
119 
120 template<class T> int CapacityConstraint<T>::next_id_ = 0;
121 
122 /// @brief CapacityConstraint-CapacityConstraint equality operator
123 template<class T>
124 inline bool operator==(const CapacityConstraint<T>& lhs,
125  const CapacityConstraint<T>& rhs) {
126  return ((lhs.capacity() == rhs.capacity()) &&
127  (*lhs.converter() == *rhs.converter()));
128 }
129 
130 /// @brief CapacityConstraint-CapacityConstraint comparison operator, allows
131 /// usage in ordered containers
132 template<class T>
133 inline bool operator<(const CapacityConstraint<T>& lhs,
134  const CapacityConstraint<T>& rhs) {
135  return (lhs.id() < rhs.id());
136 }
137 
138 } // namespace cyclus
139 
140 #endif // CYCLUS_SRC_CAPACITY_CONSTRAINT_H_
The default converter returns the resource&#39;s quantity.
a simple interface for converting resource objects to unit capacities
An arc represents a possible connection between two nodes in the bipartite resource exchange graph...
For values that are too big, too small, etc.
Definition: error.h:41
Converter< T >::Ptr converter() const
A CapacityConstraint provides an ability to determine an agent&#39;s constraints on resource allocation g...
virtual double convert(boost::shared_ptr< T > offer, Arc const *a=NULL, ExchangeTranslationContext< T > const *ctx=NULL) const =0
convert a capacitated quantity for an offer in its exchange context
virtual bool operator==(Converter &other) const
operator== is available for subclassing, see cyclus::TrivialConverter for an example ...
CapacityConstraint(const CapacityConstraint &other)
constructor for a constraint with a non-trivial converter
double convert(boost::shared_ptr< T > offer, Arc const *a=NULL, ExchangeTranslationContext< T > const *ctx=NULL) const
CapacityConstraint(double capacity, typename Converter< T >::Ptr converter)
constructor for a constraint with a non-trivial converter
bool operator!=(Converter &other) const
An ExchangeTranslationContext is a simple holder class for any information needed to translate a Reso...
boost::shared_ptr< Converter< T > > Ptr
virtual bool operator==(Converter< T > &other) const
virtual double convert(boost::shared_ptr< T > offer, Arc const *a=NULL, ExchangeTranslationContext< T > const *ctx=NULL) const
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
CapacityConstraint(double capacity)
constructor for a constraint with a trivial converter (i.e., one that simply returns 1) ...