CYCLUS
exchange_manager.h
Go to the documentation of this file.
1 #ifndef CYCLUS_SRC_EXCHANGE_MANAGER_H_
2 #define CYCLUS_SRC_EXCHANGE_MANAGER_H_
3 
4 #include <algorithm>
5 
6 #include "exchange_graph.h"
7 #include "exchange_solver.h"
8 #include "exchange_translator.h"
9 #include "resource_exchange.h"
10 #include "trade_executor.h"
11 #include "trader_management.h"
12 #include "env.h"
13 
14 namespace cyclus {
15 
16 /// @class ExchangeManager
17 ///
18 /// @brief The ExchangeManager is designed to house all of the internals
19 /// involved in executing a resource exchange. At a given timestep, assuming a
20 /// simulation context, ctx, and resource type, ResourceType, it can be invoked
21 /// by:
22 ///
23 /// @code
24 /// ExchangeManager<ResourceType> manager(ctx);
25 /// manager.Execute();
26 /// @endcode
27 template <class T>
29  public:
30  ExchangeManager(Context* ctx) : ctx_(ctx), debug_(false) {
31  debug_ = Env::GetEnv("CYCLUS_DEBUG_DRE").size() > 0;
32  }
33 
34  /// @brief execute the full resource sequence
35  void Execute() {
36  // collect resource exchange information
37  ResourceExchange<T> exchng(ctx_);
38  exchng.AddAllRequests();
39  exchng.AddAllBids();
40  exchng.AdjustAll();
41  CLOG(LEV_DEBUG1) << "done with info gathering";
42 
43  if (debug_)
44  RecordDebugInfo(exchng.ex_ctx());
45 
46  if (exchng.Empty())
47  return; // empty exchange, move on
48 
49  // translate graph
50  ExchangeTranslator<T> xlator(&exchng.ex_ctx());
51  CLOG(LEV_DEBUG1) << "translating graph...";
52  ExchangeGraph::Ptr graph = xlator.Translate();
53  CLOG(LEV_DEBUG1) << "graph translated!";
54 
55  // solve graph
56  CLOG(LEV_DEBUG1) << "solving graph...";
57  ctx_->solver()->Solve(graph.get());
58  CLOG(LEV_DEBUG1) << "graph solved!";
59 
60  // get trades
61  std::vector< Trade<T> > trades;
62  xlator.BackTranslateSolution(graph->matches(), trades);
63  CLOG(LEV_DEBUG1) << "trades translated!";
64 
65  // execute trades!
66  TradeExecutor<T> exec(trades);
67  exec.ExecuteTrades(ctx_);
68  }
69 
70  private:
71  void RecordDebugInfo(ExchangeContext<T>& exctx) {
72  typename std::vector<typename RequestPortfolio<T>::Ptr>::iterator it;
73  for (it = exctx.requests.begin(); it != exctx.requests.end(); ++it) {
74  std::vector<Request<T>*> reqs = (*it)->requests();
75  typename std::vector<Request<T>*>::iterator it2;
76  for (it2 = reqs.begin(); it2 != reqs.end(); ++it2) {
77  Request<T>* r = *it2;
78  std::stringstream ss;
79  ss << ctx_->time() << "_" << r;
80  ctx_->NewDatum("DebugRequests")
81  ->AddVal("Time", ctx_->time())
82  ->AddVal("ReqId", ss.str())
83  ->AddVal("RequesterID", r->requester()->manager()->id())
84  ->AddVal("Commodity", r->commodity())
85  ->AddVal("Preference", r->preference())
86  ->AddVal("Exclusive", r->exclusive())
87  ->AddVal("ResType", r->target()->type())
88  ->AddVal("Quantity", r->target()->quantity())
89  ->AddVal("ResUnits", r->target()->units())
90  ->Record();
91  }
92  }
93 
94  typename std::vector<typename BidPortfolio<T>::Ptr>::iterator it3;
95  for (it3 = exctx.bids.begin(); it3 != exctx.bids.end(); ++it3) {
96  std::set<Bid<T>*> bids = (*it3)->bids();
97  typename std::set<Bid<T>*>::iterator it4;
98  for (it4 = bids.begin(); it4 != bids.end(); ++it4) {
99  Bid<T>* b = *it4;
100  Request<T>* r = b->request();
101  double pref = exctx.trader_prefs[r->requester()][r][b];
102  std::stringstream ss;
103  ss << ctx_->time() << "_" << b->request();
104  ctx_->NewDatum("DebugBids")
105  ->AddVal("ReqId", ss.str())
106  ->AddVal("BidderId", b->bidder()->manager()->id())
107  ->AddVal("BidQuantity", b->offer()->quantity())
108  ->AddVal("Exclusive", b->exclusive())
109  ->AddVal("Preference", pref)
110  ->Record();
111  }
112  }
113  }
114 
115  bool debug_;
116  Context* ctx_;
117 };
118 
119 } // namespace cyclus
120 
121 #endif // CYCLUS_SRC_EXCHANGE_MANAGER_H_
std::map< Trader *, typename PrefMap< T >::type > trader_prefs
maps commodity name to requests for that commodity
std::string commodity() const
Definition: request.h:103
The ResourceExchange class manages the communication for the supply and demand of resources in a simu...
std::vector< typename RequestPortfolio< T >::Ptr > requests
a reference to an exchange&#39;s set of requests
double b(int nuc)
Computes the scattering length [cm] from the coherent and incoherent components.
Definition: pyne.cc:11180
ExchangeContext< T > & ex_ctx()
The TradeExecutor is an object whose task is to execute a collection of Trades.
boost::shared_ptr< T > target() const
Definition: request.h:97
bool exclusive() const
Definition: request.h:114
bool Empty()
return true if this is an empty exchange (i.e., no requests exist, therefore no bids) ...
The ExchangeManager is designed to house all of the internals involved in executing a resource exchan...
void ExecuteTrades()
execute all trades, collecting responders from bidders and sending responses to requesters ...
The ExchangeContext is designed to provide an ease-of-use interface for querying and reaggregating in...
boost::shared_ptr< T > offer() const
Definition: bid.h:77
double Solve(ExchangeGraph *graph=NULL)
interface for solving a given exchange graph
#define CLOG(level)
Definition: logger.h:39
void AddAllRequests()
queries traders and collects all requests for bids
std::vector< typename BidPortfolio< T >::Ptr > bids
a reference to an exchange&#39;s set of bids
boost::shared_ptr< ExchangeGraph > Ptr
virtual int time()
Returns the current simulation timestep.
Definition: context.cc:227
void AddAllBids()
queries traders and collects all responses to requests for 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
static std::string GetEnv(std::string var)
Method to check the existence of and return an environment variable.
Definition: env.cc:71
void AdjustAll()
adjust preferences for requests given bid responses
bool exclusive() const
Definition: bid.h:86
double preference() const
Definition: request.h:106
Datum * AddVal(const char *field, boost::spirit::hold_any val, std::vector< int > *shape=NULL)
Add an arbitrary field-value pair to the datum.
Definition: datum.cc:22
Trader * requester() const
Definition: request.h:100
ExchangeManager(Context *ctx)
void Execute()
execute the full resource sequence
A simulation context provides access to necessary simulation-global functions and state...
Definition: context.h:130
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
debugging information - least verbose
Definition: logger.h:58
void Record()
Record this datum to its Recorder.
Definition: datum.cc:35
An ExchangeTranslator facilitates translation from a resource specific exchange to a resource-neutral...
ExchangeSolver * solver()
Returns the exchange solver associated with this context.
Definition: context.h:261
Request< T > * request() const
Definition: bid.h:74
Datum * NewDatum(std::string title)
See Recorder::NewDatum documentation.
Definition: context.cc:239
A Request encapsulates all the information required to communicate the needs of an agent in the Dynam...
Definition: request.h:29