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