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