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