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> class ResourceExchange {
61 public:
62 /// @brief default constructor
63 ///
64 /// @param ctx the simulation context
65 ResourceExchange(Context* ctx) { sim_ctx_ = ctx; }
66
67 inline ExchangeContext<T>& ex_ctx() { return ex_ctx_; }
68
69 /// @brief queries traders and collects all requests for bids
71 InitTraders();
72 std::for_each(traders_.begin(),
73 traders_.end(),
74 std::bind(&cyclus::ResourceExchange<T>::AddRequests_,
75 this,
76 std::placeholders::_1));
77 }
78
79 /// @brief queries traders and collects all responses to requests for bids
80 void AddAllBids() {
81 InitTraders();
82 std::for_each(traders_.begin(),
83 traders_.end(),
84 std::bind(&cyclus::ResourceExchange<T>::AddBids_,
85 this,
86 std::placeholders::_1));
87 }
88
89 /// @brief adjust preferences for requests given bid responses
90 void AdjustAll() {
91 InitTraders();
92 std::set<Trader*> traders = ex_ctx_.requesters;
93 std::for_each(traders.begin(),
94 traders.end(),
95 std::bind(&cyclus::ResourceExchange<T>::AdjustPrefs_,
96 this,
97 std::placeholders::_1));
98 }
99
100 /// return true if this is an empty exchange (i.e., no requests exist,
101 /// therefore no bids)
102 inline bool Empty() { return ex_ctx_.bids_by_request.empty(); }
103
104 private:
105 void InitTraders() {
106 if (traders_.size() == 0) {
107 std::set<Trader*> orig = sim_ctx_->traders();
108 std::set<Trader*>::iterator it;
109 for (it = orig.begin(); it != orig.end(); ++it) {
110 traders_.insert(*it);
111 }
112 }
113 }
114
115 /// @brief queries a given facility agent for
116 void AddRequests_(Trader* t) {
117 std::set<typename RequestPortfolio<T>::Ptr> rp = QueryRequests<T>(t);
118 typename std::set<typename RequestPortfolio<T>::Ptr>::iterator it;
119 for (it = rp.begin(); it != rp.end(); ++it) {
120 ex_ctx_.AddRequestPortfolio(*it);
121 }
122 }
123
124 /// @brief queries a given facility agent for
125 void AddBids_(Trader* t) {
126 std::set<typename BidPortfolio<T>::Ptr> bp =
127 QueryBids<T>(t, ex_ctx_.commod_requests);
128 typename std::set<typename BidPortfolio<T>::Ptr>::iterator it;
129 for (it = bp.begin(); it != bp.end(); ++it) {
130 ex_ctx_.AddBidPortfolio(*it);
131 }
132 }
133
134 /// @brief allows a trader and its parents to adjust any preferences in the
135 /// system
136 void AdjustPrefs_(Trader* t) {
137 typename PrefMap<T>::type& prefs = ex_ctx_.trader_prefs[t];
138 AdjustPrefs(t, prefs);
139 Agent* m = t->manager()->parent();
140 while (m != NULL) {
141 AdjustPrefs(m, prefs);
142 m = m->parent();
143 }
144 }
145
146 struct trader_compare {
147 bool operator()(Trader* lhs, Trader* rhs) const {
148 int left = lhs->manager()->id();
149 int right = rhs->manager()->id();
150 if (left != right) {
151 return left < right;
152 } else {
153 return lhs < rhs;
154 }
155 }
156 };
157
158 // this sorts traders (and results in iteration...) based on traders'
159 // manager id. Iterating over traders in this order helps increase the
160 // determinism of Cyclus overall. This allows all traders' resource
161 // exchange functions are called in a much closer to deterministic order.
162 std::set<Trader*, trader_compare> traders_;
163
164 Context* sim_ctx_;
165 ExchangeContext<T> ex_ctx_;
166};
167
168} // namespace cyclus
169
170#endif // CYCLUS_SRC_RESOURCE_EXCHANGE_H_
The abstract base class used by all types of agents that live and interact in a simulation.
Definition agent.h:50
virtual void AdjustMatlPrefs(PrefMap< Material >::type &prefs)
default implementation for material preferences.
Definition agent.h:326
virtual void AdjustProductPrefs(PrefMap< Product >::type &prefs)
default implementation for material preferences.
Definition agent.h:329
A simulation context provides access to necessary simulation-global functions and state.
Definition context.h:146
const std::set< Trader * > & traders() const
Definition context.h:184
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:53
virtual void AdjustProductPrefs(PrefMap< Product >::type &prefs)
default implementation for material preferences.
Definition trader.h:56
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
static std::set< typename BidPortfolio< T >::Ptr > QueryBids(Trader *t, typename CommodMap< T >::type &map)
static void AdjustPrefs(Agent *m, typename PrefMap< T >::type &prefs)
Preference adjustment method helpers to convert from templates to the Agent inheritance hierarchy.
static std::set< typename RequestPortfolio< T >::Ptr > QueryRequests(Trader *t)
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