CYCLUS
matl_buy_policy.cc
Go to the documentation of this file.
1 #include "matl_buy_policy.h"
2 
3 #include <sstream>
4 
5 #include "error.h"
6 
7 #define LG(X) LOG(LEV_##X, "buypol")
8 #define LGH(X) \
9  LOG(LEV_##X, "buypol") << "policy " << name_ << " (agent " \
10  << Trader::manager()->prototype() << "-" \
11  << Trader::manager()->id() << "): "
12 
13 namespace cyclus {
14 namespace toolkit {
15 
17  Trader(NULL),
18  name_(""),
19  throughput_(std::numeric_limits<double>::max()),
20  quantize_(-1),
21  fill_to_(1),
22  req_when_under_(1) {
23  Warn<EXPERIMENTAL_WARNING>(
24  "MatlBuyPolicy is experimental and its API may be subject to change");
25 }
26 
28  if (manager() != NULL)
29  manager()->context()->UnregisterTrader(this);
30 }
31 
32 void MatlBuyPolicy::set_fill_to(double x) {
33  if (x > 1)
34  x /= buf_->capacity();
35  assert(x > 0 && x <= 1.);
36  fill_to_ = x;
37 }
38 
39 void MatlBuyPolicy::set_req_when_under(double x) {
40  if (x > 1)
41  x /= buf_->capacity();
42  assert(x > 0 && x <= 1.);
43  req_when_under_ = x;
44 }
45 
46 void MatlBuyPolicy::set_quantize(double x) {
47  assert(x != 0);
48  quantize_ = x;
49 }
50 
51 void MatlBuyPolicy::set_throughput(double x) {
52  assert(x >= 0);
53  throughput_ = x;
54 }
55 
57  std::string name) {
59  buf_ = buf;
60  name_ = name;
61  return *this;
62 }
63 
65  std::string name, double throughput) {
67  buf_ = buf;
68  name_ = name;
69  set_throughput(throughput);
70  return *this;
71 }
72 
75  double fill_to, double req_when_under) {
77  buf_ = buf;
78  name_ = name;
79  set_fill_to(fill_to);
80  set_req_when_under(req_when_under);
81  return *this;
82 }
83 
85  std::string name, double throughput,
86  double fill_to, double req_when_under,
87  double quantize) {
89  buf_ = buf;
90  name_ = name;
91  set_fill_to(fill_to);
92  set_req_when_under(req_when_under);
93  set_quantize(quantize);
94  set_throughput(throughput);
95  return *this;
96 }
97 
99  CompMap c;
100  c[10010000] = 1e-100;
101  return Set(commod, Composition::CreateFromMass(c), 1.0);
102 }
103 
105  return Set(commod, c, 1.0);
106 }
107 
109  double pref) {
110  CommodDetail d;
111  d.comp = c;
112  d.pref = pref;
113  commod_details_[commod] = d;
114  return *this;
115 }
116 
118  if (manager() == NULL) {
119  std::stringstream ss;
120  ss << "No manager set on Buy Policy " << name_;
121  throw ValueError(ss.str());
122  }
123  manager()->context()->RegisterTrader(this);
124 }
125 
127  if (manager() == NULL) {
128  std::stringstream ss;
129  ss << "No manager set on Buy Policy " << name_;
130  throw ValueError(ss.str());
131  }
132  manager()->context()->UnregisterTrader(this);
133 }
134 
135 std::set<RequestPortfolio<Material>::Ptr> MatlBuyPolicy::GetMatlRequests() {
136  rsrc_commods_.clear();
137  std::set<RequestPortfolio<Material>::Ptr> ports;
138  bool make_req = buf_->quantity() < req_when_under_ * buf_->capacity();
139  double amt = TotalQty();
140  if (!make_req || amt < eps())
141  return ports;
142 
143  bool excl = Excl();
144  double req_amt = ReqQty();
145  int n_req = NReq();
146  LGH(INFO3) << "requesting " << amt << " kg via " << n_req << " request(s)";
147 
148  // one portfolio for each request
149  for (int i = 0; i != n_req; i++) {
151  std::vector<Request<Material>*> mreqs;
152  std::map<std::string, CommodDetail>::iterator it;
153  for (it = commod_details_.begin(); it != commod_details_.end(); ++it) {
154  std::string commod = it->first;
155  CommodDetail d = it->second;
156  LG(INFO3) << " - one " << amt << " kg request of " << commod;
157  Material::Ptr m = Material::CreateUntracked(req_amt, d.comp);
158  Request<Material>* r = port->AddRequest(m, this, commod, d.pref, excl);
159  mreqs.push_back(r);
160  }
161  port->AddMutualReqs(mreqs);
162  ports.insert(port);
163  }
164 
165  return ports;
166 }
167 
169  const std::vector<std::pair<Trade<Material>, Material::Ptr> >& resps) {
170  std::vector<std::pair<Trade<Material>, Material::Ptr> >::const_iterator it;
171  rsrc_commods_.clear();
172  for (it = resps.begin(); it != resps.end(); ++it) {
173  rsrc_commods_[it->second] = it->first.request->commodity();
174  LGH(INFO3) << "got " << it->second->quantity() << " kg of "
175  << it->first.request->commodity();
176  buf_->Push(it->second);
177  }
178 }
179 
180 } // namespace toolkit
181 } // namespace cyclus
void RegisterTrader(Trader *e)
Registers an agent as a participant in resource exchanges.
Definition: context.h:161
boost::shared_ptr< Composition > Ptr
Definition: composition.h:43
void Start()
Registers this policy as a trader in the current simulation.
MatlBuyPolicy()
Creates an uninitialized policy.
boost::shared_ptr< RequestPortfolio< T > > Ptr
For values that are too big, too small, etc.
Definition: error.h:41
boost::shared_ptr< Material > Ptr
Definition: material.h:75
std::string name(int nuc)
Definition: pyne.cc:2940
A simple API for agents that wish to exchange resources in the simulation.
Definition: trader.h:24
#define LGH(X)
Request< T > * AddRequest(boost::shared_ptr< T > target, Trader *requester, std::string commodity, double preference, bool exclusive, cost_function_t cost_function)
add a request to the portfolio
std::map< Nuc, double > CompMap
a raw definition of nuclides and corresponding (dimensionless quantities).
Definition: composition.h:17
int NReq() const
the number of requests made per each commodity
double ReqQty() const
the amount requested per each request
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
MatlBuyPolicy & Init(Agent *manager, ResBuf< Material > *buf, std::string name)
Configures the policy to keep buf filled to a certain fraction of its capacity every time step...
virtual Agent * manager()
Definition: trader.h:28
void UnregisterTrader(Trader *e)
Unregisters an agent as a participant in resource exchanges.
Definition: context.h:166
virtual std::set< RequestPortfolio< Material >::Ptr > GetMatlRequests()
Trader Methods.
Context * context() const
Returns this agent&#39;s simulation context.
Definition: agent.h:369
void AddMutualReqs(const std::vector< Request< T > *> &rs)
adds a collection of requests (already having been registered with this portfolio) as multicommodity ...
MatlBuyPolicy & Set(std::string commod)
Instructs the policy to fill its buffer with requests on the given commodity of composition c and the...
#define LG(X)
Agent * manager_
Definition: trader.h:88
void Stop()
Unregisters this policy as a trader in the current simulation.
double TotalQty() const
the total amount requested
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
virtual void AcceptMatlTrades(const std::vector< std::pair< Trade< Material >, Material::Ptr > > &resps)
default implementation for material trade acceptance
ResBuf is a helper class that provides semi-automated management of a collection of resources (e...
Definition: res_buf.h:62
MatlBuyPolicy performs semi-automatic inventory management of a material buffer by making requests an...
double eps()
a generic epsilon value
Definition: cyc_limits.h:12
static Ptr CreateFromMass(CompMap v)
Creates a new composition from v with its components having appropriate mass-based ratios...
Definition: composition.cc:29
A Request encapsulates all the information required to communicate the needs of an agent in the Dynam...
Definition: request.h:29
bool Excl() const
whether trades will be denoted as exclusive or not
A RequestPortfolio is a group of (possibly constrainted) requests for resources.
Definition: request.h:21