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 
74  std::string name,
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 
98 MatlBuyPolicy& MatlBuyPolicy::Set(std::string commod) {
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::map<int, std::vector<Request<Material>*> > grps;
152  // one request for each commodity
153  std::map<std::string, CommodDetail>::iterator it;
154  for (it = commod_details_.begin(); it != commod_details_.end(); ++it) {
155  std::string commod = it->first;
156  CommodDetail d = it->second;
157  LG(INFO3) << " - one " << amt << " kg request of " << commod;
158  Material::Ptr m = Material::CreateUntracked(req_amt, d.comp);
159  grps[i].push_back(port->AddRequest(m, this, commod, d.pref, excl));
160  }
161 
162  // if there's more than one commodity, then make them mutual
163  if (grps.size() > 1) {
164  std::map<int, std::vector<Request<Material>*> >::iterator grpit;
165  for (grpit = grps.begin(); grpit != grps.end(); ++grpit) {
166  port->AddMutualReqs(grpit->second);
167  }
168  }
169  ports.insert(port);
170  }
171 
172  return ports;
173 }
174 
176  const std::vector<std::pair<Trade<Material>, Material::Ptr> >& resps) {
177  std::vector<std::pair<Trade<Material>, Material::Ptr> >::const_iterator it;
178  rsrc_commods_.clear();
179  for (it = resps.begin(); it != resps.end(); ++it) {
180  rsrc_commods_[it->second] = it->first.request->commodity();
181  LGH(INFO3) << "got " << it->second->quantity() << " kg of "
182  << it->first.request->commodity();
183  buf_->Push(it->second);
184  }
185 }
186 
187 } // namespace toolkit
188 } // namespace cyclus
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
void Start()
Registers this policy as a trader in the current simulation.
bool Excl() const
whether trades will be denoted as exclusive or not
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
double ReqQty() const
the amount requested per each request
#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
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:164
virtual std::set< RequestPortfolio< Material >::Ptr > GetMatlRequests()
Trader Methods.
MatlBuyPolicy & Set(std::string commod)
Instructs the policy to fill its buffer with requests on the given commodity of composition c and the...
double TotalQty() const
the total amount requested
#define LG(X)
void AddMutualReqs(const std::vector< Request< T > * > &rs)
adds a collection of requests (already having been registered with this portfolio) as multicommodity ...
Agent * manager_
Definition: trader.h:88
void Stop()
Unregisters this policy as a trader in the current simulation.
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
int NReq() const
the number of requests made per each commodity
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
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:26
A RequestPortfolio is a group of (possibly constrainted) requests for resources.
Definition: request.h:21