CYCLUS
Loading...
Searching...
No Matches
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
13namespace cyclus {
14namespace toolkit {
15
17 Trader(NULL),
18 name_(""),
19 throughput_(std::numeric_limits<double>::max()),
20 quantize_(-1),
21 fill_to_(std::numeric_limits<double>::max()),
22 req_at_(std::numeric_limits<double>::max()),
23 cumulative_cap_(-1),
24 cycle_total_inv_(0),
25 active_dist_(NULL),
26 dormant_dist_(NULL),
27 size_dist_(NULL){
29 "MatlBuyPolicy is experimental and its API may be subject to change");
30}
31
36
37void MatlBuyPolicy::set_manager(Agent* m) {
38 if (m != NULL) {
40 }
41 else {
42 std::stringstream ss;
43 ss << "No manager set on Buy Policy " << name_;
44 throw ValueError(ss.str());
45 }
46}
47
48void MatlBuyPolicy::set_total_inv_tracker(TotalInvTracker* t) {
49 if (t == NULL){
50 std::vector<ResBuf<Material>*> bufs = {buf_};
51 buf_tracker_->Init(bufs, buf_->capacity());
52 }
53 else if (!t->buf_in_tracker(buf_)) {
54 std::stringstream ss;
55 ss << "TotalInvTracker does not contain ResBuf used in buy policy";
56 throw ValueError(ss.str());
57 }
58 else {
59 buf_tracker_ = t;
60 }
61}
62
63void MatlBuyPolicy::set_inv_policy(std::string inv_policy, double fill, double req_at) {
64 set_req_at(req_at);
65 std::transform(inv_policy.begin(), inv_policy.end(), inv_policy.begin(), ::tolower);
66 if ((inv_policy == "ss")) {
67 set_fill_to(fill);
68 }
69 else if ((inv_policy == "rq") || (inv_policy == "qr")) {
70 set_quantize(fill);
71 // maximum amount that an RQ policy could achieve is req_at + fill
72 set_fill_to(req_at + fill);
73 }
74 else {
75 throw ValueError("Invalid inventory policy");
76 }
77}
78
79void MatlBuyPolicy::set_fill_to(double x) {
80 assert(x > 0);
81 fill_to_ = std::min(x, buf_->capacity());
82}
83
84void MatlBuyPolicy::set_req_at(double x) {
85 assert(x >= 0);
86 req_at_ = std::min(x, buf_->capacity());
87}
88
89void MatlBuyPolicy::set_cumulative_cap(double x) {
90 assert(x > 0);
91 cumulative_cap_ = std::min(x, buf_->capacity());
92}
93
94void MatlBuyPolicy::set_quantize(double x) {
95 assert(x != 0);
96 quantize_ = x;
97}
98
99void MatlBuyPolicy::set_throughput(double x) {
100 assert(x >= 0);
101 throughput_ = x;
102}
103
104void MatlBuyPolicy::init_active_dormant() {
105 if (active_dist_ == NULL) {
106 active_dist_ = boost::shared_ptr<FixedIntDist>(new FixedIntDist(1));
107 }
108 if (dormant_dist_ == NULL) {
109 dormant_dist_ = boost::shared_ptr<FixedIntDist>(new FixedIntDist(-1));
110 }
111 if (size_dist_ == NULL) {
112 size_dist_ = boost::shared_ptr<FixedDoubleDist>(new FixedDoubleDist(1.0));
113 }
114
115 if (size_dist_->max() > 1) {
116 throw ValueError("Size distribution cannot have a max greater than 1.");
117 }
118
120 LGH(INFO4) << "first active time end: " << next_active_end_ << std::endl;
121
122 if (dormant_dist_->sample() < 0) {
123 next_dormant_end_ = -1;
124 LGH(INFO4) << "dormant length -1, always active" << std::endl;
125 }
126 else if (use_cumulative_capacity()) {
127 next_dormant_end_ = -1;
128 LGH(INFO4) << "dormant length set at -1 for first active period of cumulative capacity cycle" << std::endl;
129 }
130 else {
132 LGH(INFO4) << "first dormant time end: " << next_dormant_end_ << std::endl;
133 }
134}
135
137 std::string name, TotalInvTracker* buf_tracker) {
138 set_manager(manager);
139 buf_ = buf;
140 name_ = name;
141 set_total_inv_tracker(buf_tracker);
142 init_active_dormant();
143 return *this;
144}
145
147 std::string name, TotalInvTracker* buf_tracker,
148 double throughput, boost::shared_ptr<IntDistribution> active_dist,
149 boost::shared_ptr<IntDistribution> dormant_dist,
150 boost::shared_ptr<DoubleDistribution> size_dist) {
151 set_manager(manager);
152 buf_ = buf;
153 name_ = name;
154 set_total_inv_tracker(buf_tracker);
155 set_throughput(throughput);
156 active_dist_ = active_dist;
157 dormant_dist_ = dormant_dist;
158 size_dist_ = size_dist;
159 init_active_dormant();
160 return *this;
161}
162
164 std::string name, TotalInvTracker* buf_tracker,
165 double throughput, double quantize) {
166 set_manager(manager);
167 buf_ = buf;
168 name_ = name;
169 set_total_inv_tracker(buf_tracker);
170 set_throughput(throughput);
171 set_quantize(quantize);
172 init_active_dormant();
173 return *this;
174}
175
177 std::string name,
179 double throughput,
180 std::string inv_policy,
181 double fill_behav, double req_at) {
182 set_manager(manager);
183 buf_ = buf;
184 name_ = name;
185 set_total_inv_tracker(buf_tracker);
186 set_inv_policy(inv_policy, fill_behav, req_at);
187 set_throughput(throughput);
188 init_active_dormant();
189 return *this;
190}
191
193 std::string name,
195 std::string inv_policy,
196 double fill_behav, double req_at) {
197 set_manager(manager);
198 buf_ = buf;
199 name_ = name;
200 set_total_inv_tracker(buf_tracker);
201 set_inv_policy(inv_policy, fill_behav, req_at);
202 init_active_dormant();
203 return *this;
204}
205
207 std::string name,
209 double throughput, double cumulative_cap,
210 boost::shared_ptr<IntDistribution> dormant_dist) {
211 set_manager(manager);
212 buf_ = buf;
213 name_ = name;
214 set_total_inv_tracker(buf_tracker);
215 set_throughput(throughput);
216 set_cumulative_cap(cumulative_cap);
217 dormant_dist_ = dormant_dist;
218 init_active_dormant();
219 return *this;
220}
221
222MatlBuyPolicy& MatlBuyPolicy::Set(std::string commod) {
223 CompMap c;
224 c[10010000] = 1e-100;
225 return Set(commod, Composition::CreateFromMass(c), 1.0);
226}
227
229 return Set(commod, c, 1.0);
230}
231
233 double pref) {
234 CommodDetail d;
235 d.comp = c;
236 d.pref = pref;
237 commod_details_[commod] = d;
238 return *this;
239}
240
242 if (manager() == NULL) {
243 std::stringstream ss;
244 ss << "No manager set on Buy Policy " << name_;
245 throw ValueError(ss.str());
246 }
247 manager()->context()->RegisterTrader(this);
248}
249
251 if (manager() == NULL) {
252 std::stringstream ss;
253 ss << "No manager set on Buy Policy " << name_;
254 throw ValueError(ss.str());
255 }
256 manager()->context()->UnregisterTrader(this);
257}
258
259std::set<RequestPortfolio<Material>::Ptr> MatlBuyPolicy::GetMatlRequests() {
260 rsrc_commods_.clear();
261 std::set<RequestPortfolio<Material>::Ptr> ports;
262
263 double amt = 0;
264
265 int current_time_ = manager()->context()->time();
266
267 double max_request_amt = use_cumulative_capacity() ? cumulative_cap_ - cycle_total_inv_ : std::numeric_limits<double>::max();
268
269 if (MakeReq() && !dormant(current_time_)) {
270 amt = std::min(TotalAvailable() * SampleRequestSize(), max_request_amt);
271 }
272 else { LGH(INFO3) << "in dormant period, no request" << std::endl; }
273
275
276 if (amt < eps())
277 return ports;
278
279 bool excl = Excl();
280 double req_amt = ReqQty(amt);
281 int n_req = NReq(amt);
282 LGH(INFO3) << "requesting " << amt << " kg via " << n_req << " request(s)" << std::endl;
283
284 // one portfolio for each request
285 for (int i = 0; i != n_req; i++) {
287 std::vector<Request<Material>*> mreqs;
288 std::map<std::string, CommodDetail>::iterator it;
289 for (it = commod_details_.begin(); it != commod_details_.end(); ++it) {
290 std::string commod = it->first;
291 CommodDetail d = it->second;
292 LG(INFO3) << " - one " << amt << " kg request of " << commod;
294 Request<Material>* r = port->AddRequest(m, this, commod, d.pref, excl);
295 mreqs.push_back(r);
296 }
297 port->AddMutualReqs(mreqs);
298 ports.insert(port);
299 }
300
301 return ports;
302}
303
305 const std::vector<std::pair<Trade<Material>, Material::Ptr> >& resps) {
306 std::vector<std::pair<Trade<Material>, Material::Ptr> >::const_iterator it;
307 rsrc_commods_.clear();
308 for (it = resps.begin(); it != resps.end(); ++it) {
309 rsrc_commods_[it->second] = it->first.request->commodity();
310 LGH(INFO3) << "got " << it->second->quantity() << " kg of "
311 << it->first.request->commodity() << std::endl;
312 buf_->Push(it->second);
313 // cumulative capacity handling
315 cycle_total_inv_ += it->second->quantity();
316 }
317 }
318 // check if cumulative cap has been reached. If yes, then sample for dormant
319 // length and reset cycle_total_inv
320 if (use_cumulative_capacity() && (
321 (cumulative_cap_ - cycle_total_inv_) < eps_rsrc())) {
323 LGH(INFO3) << "cycle cumulative inventory has been reached. Dormant period will end at " << next_dormant_end_ << std::endl;
324 cycle_total_inv_ = 0;
325 }
326}
327
329 next_active_end_ = active_dist_->sample() + manager()->context()->time();
330 return;
331};
332
335 // need the +1 when not using next_active_end_
336 next_dormant_end_ = (
337 dormant_dist_->sample() + manager()->context()->time() + 1);
338 }
339 else if (next_dormant_end_ >= 0) {
340 next_dormant_end_ = dormant_dist_->sample() +
341 std::max(next_active_end_, 1);
342 }
343 return;
344}
345
347 return size_dist_->sample();
348}
349
351 if (manager()->context()->time() == next_dormant_end_) {
352 if (use_cumulative_capacity()) { next_dormant_end_ = -1; }
353 else {
355
357 LGH(INFO4) << "end of dormant period, next active time end: " << next_active_end_ << ", and next dormant time end: " << next_dormant_end_ << std::endl;
358 }
359 }
360 return;
361}
362
363} // namespace toolkit
364} // namespace cyclus
The abstract base class used by all types of agents that live and interact in a simulation.
Definition agent.h:49
Context * context() const
Returns this agent's simulation context.
Definition agent.h:367
static Ptr CreateFromMass(CompMap v)
Creates a new composition from v with its components having appropriate mass-based ratios.
boost::shared_ptr< Composition > Ptr
Definition composition.h:43
void UnregisterTrader(Trader *e)
Unregisters an agent as a participant in resource exchanges.
Definition context.h:181
void RegisterTrader(Trader *e)
Registers an agent as a participant in resource exchanges.
Definition context.h:176
virtual int time()
Returns the current simulation timestep.
Definition context.cc:313
boost::shared_ptr< Material > Ptr
Definition material.h:75
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
A RequestPortfolio is a group of (possibly constrained) requests for resources.
boost::shared_ptr< RequestPortfolio< T > > Ptr
A Request encapsulates all the information required to communicate the needs of an agent in the Dynam...
Definition request.h:29
A simple API for agents that wish to exchange resources in the simulation.
Definition trader.h:24
Agent * manager_
Definition trader.h:88
virtual Agent * manager()
Definition trader.h:28
For values that are too big, too small, etc.
Definition error.h:41
MatlBuyPolicy performs semi-automatic inventory management of a material buffer by making requests an...
void Stop()
Unregisters this policy as a trader in the current simulation.
int NReq(double amt) const
the number of requests made per each commodity
MatlBuyPolicy & Set(std::string commod)
Instructs the policy to fill its buffer with requests on the given commodity of composition c and the...
virtual std::set< RequestPortfolio< Material >::Ptr > GetMatlRequests()
Trader Methods.
void Start()
Registers this policy as a trader in the current simulation.
virtual void AcceptMatlTrades(const std::vector< std::pair< Trade< Material >, Material::Ptr > > &resps)
default implementation for material trade acceptance
bool Excl() const
whether trades will be denoted as exclusive or not
MatlBuyPolicy & Init(Agent *manager, ResBuf< Material > *buf, std::string name, TotalInvTracker *buf_tracker)
Configures the policy to keep buf filled to a certain fraction of its capacity every time step.
double TotalAvailable() const
the total amount available to request
bool MakeReq() const
whether a request can be made
MatlBuyPolicy()
Creates an uninitialized policy.
double ReqQty(double amt) const
the amount requested per each request
ResBuf is a helper class that provides semi-automated management of a collection of resources (e....
Definition res_buf.h:62
#define LG(X)
#define LGH(X)
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
double eps_rsrc()
an epsilon value to be used by resources
Definition cyc_limits.h:19
std::map< Nuc, double > CompMap
a raw definition of nuclides and corresponding (dimensionless quantities).
Definition composition.h:17
double eps()
a generic epsilon value
Definition cyc_limits.h:12
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters
A Trade is a simple container that associates a request for a resource with a bid for that resource.
Definition trade.h:16