CYCLUS
Loading...
Searching...
No Matches
exchange_context.h
Go to the documentation of this file.
1#ifndef CYCLUS_SRC_EXCHANGE_CONTEXT_H_
2#define CYCLUS_SRC_EXCHANGE_CONTEXT_H_
3
4#include <assert.h>
5#include <map>
6#include <cmath>
7#include <string>
8#include <utility>
9#include <vector>
10
11#include "bid.h"
12#include "bid_portfolio.h"
13#include "request.h"
14#include "request_portfolio.h"
15
16// Undefines isnan from pyne
17#ifdef isnan
18#undef isnan
19#endif
20
21namespace cyclus {
22
23template <class T> struct PrefMap {
24 typedef std::map<Request<T>*, std::map<Bid<T>*, double>> type;
26 typedef Bid<T>* bid_ptr;
27};
28
29template <class T> struct CommodMap {
30 typedef std::map<std::string, std::vector<Request<T>*>> type;
32};
33
34/// @class ExchangeContext
35///
36/// @brief The ExchangeContext is designed to provide an ease-of-use interface
37/// for querying and reaggregating information regarding requests and bids of a
38/// resource exchange.
39///
40/// The ExchangeContext is used by a ResourceExchange or related class to
41/// provide introspection into the requests and bids it collects. Specifically,
42/// this class is designed to assist in phases of the Dynamic Resource
43/// Exchange. The second phase, Response to Request for Bids, is assisted by
44/// grouping requests by commodity type. The third phase, preference adjustment,
45/// is assisted by grouping bids by the requester being responded to.
46template <class T> struct ExchangeContext {
47 public:
48 /// @brief adds a request to the context
50 requests.push_back(port);
51 const std::vector<Request<T>*>& vr = port->requests();
52 typename std::vector<Request<T>*>::const_iterator it;
53
54 for (it = vr.begin(); it != vr.end(); ++it) {
55 Request<T>* pr = *it;
56 AddRequest(*it);
57 }
58 }
59
60 /// @brief Adds an individual request
62 assert(pr->requester() != NULL);
63 requesters.insert(pr->requester());
64 commod_requests[pr->commodity()].push_back(pr);
65 }
66
67 /// @brief adds a bid to the context
68 void AddBidPortfolio(const typename BidPortfolio<T>::Ptr port) {
69 bids.push_back(port);
70 const std::set<Bid<T>*>& vr = port->bids();
71 typename std::set<Bid<T>*>::const_iterator it;
72
73 for (it = vr.begin(); it != vr.end(); ++it) {
74 Bid<T>* pb = *it;
75 AddBid(pb);
76 }
77 }
78
79 /// @brief adds a bid to the appropriate containers, default trade preference
80 /// between request and bid is set
81 /// @param pb the bid
82 void AddBid(Bid<T>* pb) {
83 assert(pb->bidder() != NULL);
84 bidders.insert(pb->bidder());
85
86 bids_by_request[pb->request()].push_back(pb);
87
88 double bid_pref = pb->preference();
89 trader_prefs[pb->request()->requester()][pb->request()].insert(
90 std::make_pair(
91 pb, std::isnan(bid_pref) ? pb->request()->preference() : bid_pref));
92 }
93
94 /// @brief a reference to an exchange's set of requests
95 std::vector<typename RequestPortfolio<T>::Ptr> requests;
96
97 /// @brief a reference to an exchange's set of bids
98 std::vector<typename BidPortfolio<T>::Ptr> bids;
99
100 /// @brief known requesters
101 std::set<Trader*> requesters;
102
103 /// @brief known bidders
104 std::set<Trader*> bidders;
105
106 /// @brief maps commodity name to requests for that commodity
108
109 /// @brief maps request to all bids for request
110 std::map<Request<T>*, std::vector<Bid<T>*>> bids_by_request;
111
112 /// @brief maps commodity name to requests for that commodity
113 std::map<Trader*, typename PrefMap<T>::type> trader_prefs;
114};
115
116} // namespace cyclus
117
118#endif // CYCLUS_SRC_EXCHANGE_CONTEXT_H_
const std::set< Bid< T > * > & bids() const
boost::shared_ptr< BidPortfolio< T > > Ptr
A Bid encapsulates all the information required to communicate a bid response to a request for a reso...
Definition bid.h:21
Request< T > * request() const
Definition bid.h:78
double preference() const
Definition bid.h:93
Trader * bidder() const
Definition bid.h:84
const std::vector< Request< T > * > & requests() const
boost::shared_ptr< RequestPortfolio< T > > Ptr
A Request encapsulates all the information required to communicate the needs of an agent in the Dynam...
Definition request.h:29
std::string commodity() const
Definition request.h:103
Trader * requester() const
Definition request.h:100
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
Request< T > * request_ptr
std::map< std::string, std::vector< Request< T > * > > type
The ExchangeContext is designed to provide an ease-of-use interface for querying and reaggregating in...
std::vector< typename BidPortfolio< T >::Ptr > bids
a reference to an exchange's set of bids
void AddBid(Bid< T > *pb)
adds a bid to the appropriate containers, default trade preference between request and bid is set
std::map< Request< T > *, std::vector< Bid< T > * > > bids_by_request
maps request to all bids for request
void AddRequest(Request< T > *pr)
Adds an individual request.
std::map< Trader *, typename PrefMap< T >::type > trader_prefs
maps commodity name to requests for that commodity
std::vector< typename RequestPortfolio< T >::Ptr > requests
a reference to an exchange's set of requests
void AddBidPortfolio(const typename BidPortfolio< T >::Ptr port)
adds a bid to the context
std::set< Trader * > requesters
known requesters
void AddRequestPortfolio(const typename RequestPortfolio< T >::Ptr port)
adds a request to the context
CommodMap< T >::type commod_requests
maps commodity name to requests for that commodity
std::set< Trader * > bidders
known bidders
std::map< Request< T > *, std::map< Bid< T > *, double > > type
Request< T > * request_ptr