CYCLUS
Loading...
Searching...
No Matches
resource_exchange.h
Go to the documentation of this file.
1#ifndef CYCLUS_SRC_RESOURCE_EXCHANGE_H_
2#define CYCLUS_SRC_RESOURCE_EXCHANGE_H_
3
4#include <algorithm>
5#include <functional>
6#include <set>
7
8#include "bid_portfolio.h"
9#include "context.h"
10#include "exchange_context.h"
11#include "product.h"
12#include "material.h"
13#include "request_portfolio.h"
14#include "trader.h"
15#include "trader_management.h"
16
17namespace cyclus {
18
19/// @brief Preference adjustment method helpers to convert from templates to the
20/// Agent inheritance hierarchy
21template<class T>
22inline static void AdjustPrefs(Agent* m, typename PrefMap<T>::type& prefs) {}
23inline static void AdjustPrefs(Agent* m, PrefMap<Material>::type& prefs) {
24 m->AdjustMatlPrefs(prefs);
25}
26inline static void AdjustPrefs(Agent* m, PrefMap<Product>::type& prefs) {
27 m->AdjustProductPrefs(prefs);
28}
29inline static void AdjustPrefs(Trader* t, PrefMap<Material>::type& prefs) {
30 t->AdjustMatlPrefs(prefs);
31}
32inline static void AdjustPrefs(Trader* t, PrefMap<Product>::type& prefs) {
33 t->AdjustProductPrefs(prefs);
34}
35
36/// @class ResourceExchange
37///
38/// The ResourceExchange class manages the communication for the supply and
39/// demand of resources in a simulation. At any given timestep, there are three
40/// main phases involved:
41/// -# Request for Bids
42/// Agents that demand resources of a given type post their\n
43/// demands to the exchange
44/// -# Response to Request for Bids
45/// Agents that supply resources of a given type respond to\n
46/// those requests
47/// -# Preference Adjustment
48/// Preferences for each request-bid pair are set, informing\n
49/// the evenutal soluation mechanism
50///
51/// For example, assuming a simulation Context, ctx, and resource type,
52/// ResourceType:
53///
54/// @code
55/// ResourceExchange<ResourceType> exchng(ctx);
56/// exchng.AddAllRequests();
57/// exchng.AddAllBids();
58/// exchng.AdjustAll();
59/// @endcode
60template <class T>
62 public:
63 /// @brief default constructor
64 ///
65 /// @param ctx the simulation context
67 sim_ctx_ = ctx;
68 }
69
71 return ex_ctx_;
72 }
73
74 /// @brief queries traders and collects all requests for bids
76 InitTraders();
77 std::for_each(
78 traders_.begin(),
79 traders_.end(),
81 this,
82 std::placeholders::_1));
83 }
84
85 /// @brief queries traders and collects all responses to requests for bids
86 void AddAllBids() {
87 InitTraders();
88 std::for_each(
89 traders_.begin(),
90 traders_.end(),
92 this,
93 std::placeholders::_1));
94 }
95
96 /// @brief adjust preferences for requests given bid responses
97 void AdjustAll() {
98 InitTraders();
99 std::set<Trader*> traders = ex_ctx_.requesters;
100 std::for_each(
101 traders.begin(),
102 traders.end(),
103 std::bind(
105 this,
106 std::placeholders::_1));
107 }
108
109 /// return true if this is an empty exchange (i.e., no requests exist,
110 /// therefore no bids)
111 inline bool Empty() { return ex_ctx_.bids_by_request.empty(); }
112
113 private:
114 void InitTraders() {
115 if (traders_.size() == 0) {
116 std::set<Trader*> orig = sim_ctx_->traders();
117 std::set<Trader*>::iterator it;
118 for (it = orig.begin(); it != orig.end(); ++it) {
119 traders_.insert(*it);
120 }
121 }
122 }
123
124 /// @brief queries a given facility agent for
125 void AddRequests_(Trader* t) {
126 std::set<typename RequestPortfolio<T>::Ptr> rp = QueryRequests<T>(t);
127 typename std::set<typename RequestPortfolio<T>::Ptr>::iterator it;
128 for (it = rp.begin(); it != rp.end(); ++it) {
129 ex_ctx_.AddRequestPortfolio(*it);
130 }
131 }
132
133 /// @brief queries a given facility agent for
134 void AddBids_(Trader* t) {
135 std::set<typename BidPortfolio<T>::Ptr> bp =
136 QueryBids<T>(t, ex_ctx_.commod_requests);
137 typename std::set<typename BidPortfolio<T>::Ptr>::iterator it;
138 for (it = bp.begin(); it != bp.end(); ++it) {
139 ex_ctx_.AddBidPortfolio(*it);
140 }
141 }
142
143 /// @brief allows a trader and its parents to adjust any preferences in the
144 /// system
145 void AdjustPrefs_(Trader* t) {
146 typename PrefMap<T>::type& prefs = ex_ctx_.trader_prefs[t];
147 AdjustPrefs(t, prefs);
148 Agent* m = t->manager()->parent();
149 while (m != NULL) {
150 AdjustPrefs(m, prefs);
151 m = m->parent();
152 }
153 }
154
155 struct trader_compare {
156 bool operator()(Trader* lhs, Trader* rhs) const {
157 int left = lhs->manager()->id();
158 int right = rhs->manager()->id();
159 if (left != right) {
160 return left < right;
161 } else {
162 return lhs < rhs;
163 }
164 }
165 };
166
167 // this sorts traders (and results in iteration...) based on traders'
168 // manager id. Iterating over traders in this order helps increase the
169 // determinism of Cyclus overall. This allows all traders' resource
170 // exchange functions are called in a much closer to deterministic order.
171 std::set<Trader*, trader_compare> traders_;
172
173 Context* sim_ctx_;
174 ExchangeContext<T> ex_ctx_;
175};
176
177} // namespace cyclus
178
179#endif // CYCLUS_SRC_RESOURCE_EXCHANGE_H_
a holding class for information related to a TradeExecutor
The abstract base class used by all types of agents that live and interact in a simulation.
Definition agent.h:49
virtual void AdjustMatlPrefs(PrefMap< Material >::type &prefs)
default implementation for material preferences.
Definition agent.h:325
virtual void AdjustProductPrefs(PrefMap< Product >::type &prefs)
default implementation for material preferences.
Definition agent.h:328
A simulation context provides access to necessary simulation-global functions and state.
Definition context.h:145
const std::set< Trader * > & traders() const
Definition context.h:186
The ResourceExchange class manages the communication for the supply and demand of resources in a simu...
void AdjustAll()
adjust preferences for requests given bid responses
void AddAllBids()
queries traders and collects all responses to requests for bids
bool Empty()
return true if this is an empty exchange (i.e., no requests exist, therefore no bids)
ResourceExchange(Context *ctx)
default constructor
void AddAllRequests()
queries traders and collects all requests for bids
ExchangeContext< T > & ex_ctx()
A simple API for agents that wish to exchange resources in the simulation.
Definition trader.h:24
virtual void AdjustMatlPrefs(PrefMap< Material >::type &prefs)
default implementation for material preferences.
Definition trader.h:57
virtual void AdjustProductPrefs(PrefMap< Product >::type &prefs)
default implementation for material preferences.
Definition trader.h:60
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
static void AdjustPrefs(Agent *m, typename PrefMap< T >::type &prefs)
Preference adjustment method helpers to convert from templates to the Agent inheritance hierarchy.
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters
The ExchangeContext is designed to provide an ease-of-use interface for querying and reaggregating in...
std::map< Request< T > *, std::map< Bid< T > *, double > > type