CYCLUS
matl_sell_policy.cc
Go to the documentation of this file.
1 #include "matl_sell_policy.h"
2 
3 #include "error.h"
4 
5 #define LG(X) LOG(LEV_##X, "selpol")
6 #define LGH(X) \
7  LOG(LEV_##X, "selpol") << "policy " << name_ << " (agent " \
8  << Trader::manager()->prototype() << "-" \
9  << Trader::manager()->id() << "): "
10 
11 namespace cyclus {
12 namespace toolkit {
13 
15  Trader(NULL),
16  name_(""),
17  quantize_(-1),
18  throughput_(std::numeric_limits<double>::max()),
19  ignore_comp_(false) {
20  Warn<EXPERIMENTAL_WARNING>(
21  "MatlSellPolicy is experimental and its API may be subject to change");
22 }
23 
25  if (manager() != NULL)
26  manager()->context()->UnregisterTrader(this);
27 }
28 
29 void MatlSellPolicy::set_quantize(double x) {
30  assert(x != 0);
31  quantize_ = x;
32 }
33 
34 void MatlSellPolicy::set_throughput(double x) {
35  assert(x >= 0);
36  throughput_ = x;
37 }
38 
39 void MatlSellPolicy::set_ignore_comp(bool x) {
40  ignore_comp_ = x;
41 }
42 
44  std::string name) {
46  buf_ = buf;
47  name_ = name;
48  return *this;
49 }
50 
52  std::string name, double throughput) {
54  buf_ = buf;
55  name_ = name;
56  set_throughput(throughput);
57  return *this;
58 }
59 
61  std::string name, bool ignore_comp) {
63  buf_ = buf;
64  name_ = name;
65  set_ignore_comp(ignore_comp);
66  return *this;
67 }
68 
70  std::string name, double throughput,
71  bool ignore_comp) {
73  buf_ = buf;
74  name_ = name;
75  set_throughput(throughput);
76  set_ignore_comp(ignore_comp);
77  return *this;
78 }
79 
81  std::string name, double throughput,
82  bool ignore_comp, double quantize) {
84  buf_ = buf;
85  name_ = name;
86  set_quantize(quantize);
87  set_throughput(throughput);
88  set_ignore_comp(ignore_comp);
89  return *this;
90 }
91 
92 MatlSellPolicy& MatlSellPolicy::Set(std::string commod) {
93  commods_.insert(commod);
94  return *this;
95 }
96 
98  if (manager() == NULL) {
99  std::stringstream ss;
100  ss << "No manager set on Sell Policy " << name_;
101  throw ValueError(ss.str());
102  }
103  manager()->context()->RegisterTrader(this);
104 }
105 
107  if (manager() == NULL) {
108  std::stringstream ss;
109  ss << "No manager set on Sell Policy " << name_;
110  throw ValueError(ss.str());
111  }
112  manager()->context()->UnregisterTrader(this);
113 }
114 
115 
116 double MatlSellPolicy::Limit() const {
117  double bcap = buf_->quantity();
118  double limit = Excl() ? \
119  quantize_ * static_cast<int>(std::floor(bcap / quantize_)) : bcap;
120  return std::min(throughput_, limit);
121 }
122 
123 std::set<BidPortfolio<Material>::Ptr> MatlSellPolicy::GetMatlBids(
124  CommodMap<Material>::type& commod_requests) {
125  std::set<BidPortfolio<Material>::Ptr> ports;
126  if (buf_->empty() || buf_->quantity() < eps())
127  return ports;
128 
130 
131  double limit = Limit();
133  port->AddConstraint(cc);
134  ports.insert(port);
135  LGH(INFO3) << "bidding out " << limit << " kg";
136 
137  bool excl = Excl();
138  std::string commod;
139  Request<Material>* req;
140  Material::Ptr m, offer;
141  double qty;
142  int nbids;
143  std::set<std::string>::iterator sit;
144  std::vector<Request<Material>*>::const_iterator rit;
145  for (sit = commods_.begin(); sit != commods_.end(); ++sit) {
146  commod = *sit;
147  if (commod_requests.count(commod) < 1)
148  continue;
149 
150  const std::vector<Request<Material>*>& requests =
151  commod_requests.at(commod);
152  for (rit = requests.begin(); rit != requests.end(); ++rit) {
153  req = *rit;
154  qty = std::min(req->target()->quantity(), limit);
155  nbids = excl ? static_cast<int>(std::floor(qty / quantize_)) : 1;
156  qty = excl ? quantize_ : qty;
157  for (int i = 0; i < nbids; i++) {
158  m = buf_->Pop();
159  buf_->Push(m);
160  offer = ignore_comp_ ? \
161  Material::CreateUntracked(qty, req->target()->comp()) : \
162  Material::CreateUntracked(qty, m->comp());
163  port->AddBid(req, offer, this, excl);
164  LG(INFO3) << " - bid " << qty << " kg on a request for " << commod;
165  }
166  }
167  }
168  return ports;
169 }
170 
172  const std::vector<Trade<Material> >& trades,
173  std::vector<std::pair<Trade<Material>, Material::Ptr> >& responses) {
175  std::vector<Trade<Material> >::const_iterator it;
176  for (it = trades.begin(); it != trades.end(); ++it) {
177  double qty = it->amt;
178  LGH(INFO3) << " sending " << qty << " kg of " << it->request->commodity();
179  Material::Ptr mat = buf_->Pop(qty, cyclus::eps_rsrc());
180  if (ignore_comp_)
181  mat->Transmute(it->request->target()->comp());
182  responses.push_back(std::make_pair(*it, mat));
183  }
184 }
185 
186 } // namespace toolkit
187 } // namespace cyclus
MatlSellPolicy & Set(std::string commod)
Instructs the policy to empty its buffer with offers on the given commodity.
std::map< std::string, std::vector< Request< T > * > > type
boost::shared_ptr< BidPortfolio< T > > Ptr
Definition: bid_portfolio.h:34
bool Excl() const
whether trades will be denoted as exclusive or not
void Start()
Registers this policy as a trader in the current simulation.
void RegisterTrader(Trader *e)
Registers an agent as a participant in resource exchanges.
Definition: context.h:159
boost::shared_ptr< Composition > Ptr
Definition: composition.h:43
#define LG(X)
For values that are too big, too small, etc.
Definition: error.h:41
boost::shared_ptr< Material > Ptr
Definition: material.h:75
boost::shared_ptr< T > target() const
Definition: request.h:97
MatlSellPolicy & Init(Agent *manager, ResBuf< Material > *buf, std::string name)
Configures the policy to keep buf empty every time step by offering away as much material as possible...
std::string name(int nuc)
Definition: pyne.cc:2940
A CapacityConstraint provides an ability to determine an agent&#39;s constraints on resource allocation g...
A simple API for agents that wish to exchange resources in the simulation.
Definition: trader.h:24
#define LGH(X)
MatlSellPolicy performs semi-automatic inventory management of a material buffer by making offers and...
virtual void GetMatlTrades(const std::vector< Trade< Material > > &trades, std::vector< std::pair< Trade< Material >, Material::Ptr > > &responses)
default implementation for responding to material trades
virtual std::set< BidPortfolio< Material >::Ptr > GetMatlBids(CommodMap< Material >::type &commod_requests)
Trader Methods.
static Ptr CreateUntracked(double quantity, Composition::Ptr c)
Creates a new material resource that does not actually exist as part of the simulation and is untrack...
Definition: material.cc:24
virtual Agent * manager()
Definition: trader.h:28
void UnregisterTrader(Trader *e)
Unregisters an agent as a participant in resource exchanges.
Definition: context.h:164
double eps_rsrc()
an epsilon value to be used by resources
Definition: cyc_limits.h:19
A BidPortfolio is a collection of bids as responses to requests for resources and associated constrai...
Definition: bid.h:13
Agent * manager_
Definition: trader.h:88
Bid< T > * AddBid(Request< T > *request, boost::shared_ptr< T > offer, Trader *bidder, bool exclusive, double preference)
add a bid to the portfolio
Definition: bid_portfolio.h:56
The abstract base class used by all types of agents that live and interact in a simulation.
Definition: agent.h:51
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
A Trade is a simple container that associates a request for a resource with a bid for that resource...
Definition: trade.h:16
Context * context() const
Returns this agent&#39;s simulation context.
Definition: agent.h:369
double Limit() const
the current (total) limit on transactions, i.e., the quantity of resources that can be transacted in ...
ResBuf is a helper class that provides semi-automated management of a collection of resources (e...
Definition: res_buf.h:62
void Stop()
Unregisters this policy as a trader in the current simulation.
MatlSellPolicy()
Creates an uninitialized policy.
double eps()
a generic epsilon value
Definition: cyc_limits.h:12
void AddConstraint(const CapacityConstraint< T > &c)
add a capacity constraint associated with the portfolio
Definition: bid_portfolio.h:87
A Request encapsulates all the information required to communicate the needs of an agent in the Dynam...
Definition: request.h:29