7#define LG(X) LOG(LEV_##X, "buypol")
9 LOG(LEV_##X, "buypol") << "policy " << name_ << " (agent " \
10 << Trader::manager()->prototype() << "-" \
11 << Trader::manager()->id() << "): "
19 throughput_(
std::numeric_limits<double>::max()),
21 fill_to_(
std::numeric_limits<double>::max()),
22 req_at_(
std::numeric_limits<double>::max()),
29 "MatlBuyPolicy is experimental and its API may be subject to change");
37 throughput_ = std::numeric_limits<double>::max();
39 fill_to_ = std::numeric_limits<double>::max();
40 req_at_ = std::numeric_limits<double>::max();
49void MatlBuyPolicy::set_manager(
Agent* m) {
54 ss <<
"No manager set on Buy Policy " << name_;
59void MatlBuyPolicy::set_total_inv_tracker(TotalInvTracker* t) {
61 std::vector<ResBuf<Material>*> bufs = {buf_};
62 buf_tracker_->Init(bufs, buf_->capacity());
63 }
else if (!t->buf_in_tracker(buf_)) {
65 ss <<
"TotalInvTracker does not contain ResBuf used in buy policy";
66 throw ValueError(ss.str());
72void MatlBuyPolicy::set_inv_policy(std::string inv_policy,
double fill,
75 std::transform(inv_policy.begin(), inv_policy.end(), inv_policy.begin(),
77 if ((inv_policy ==
"ss")) {
79 }
else if ((inv_policy ==
"rq") || (inv_policy ==
"qr")) {
82 set_fill_to(req_at + fill);
84 throw ValueError(
"Invalid inventory policy");
88void MatlBuyPolicy::set_fill_to(
double x) {
90 fill_to_ = std::min(x, buf_->capacity());
93void MatlBuyPolicy::set_req_at(
double x) {
95 req_at_ = std::min(x, buf_->capacity());
98void MatlBuyPolicy::set_cumulative_cap(
double x) {
100 cumulative_cap_ = std::min(x, buf_->capacity());
103void MatlBuyPolicy::set_quantize(
double x) {
108void MatlBuyPolicy::set_throughput(
double x) {
113void MatlBuyPolicy::init_active_dormant() {
114 if (active_dist_ == NULL) {
115 active_dist_ = boost::shared_ptr<FixedIntDist>(
new FixedIntDist(1));
117 if (dormant_dist_ == NULL) {
118 dormant_dist_ = boost::shared_ptr<FixedIntDist>(
new FixedIntDist(-1));
120 if (size_dist_ == NULL) {
121 size_dist_ = boost::shared_ptr<FixedDoubleDist>(
new FixedDoubleDist(1.0));
124 if (size_dist_->max() > 1) {
125 throw ValueError(
"Size distribution cannot have a max greater than 1.");
129 LGH(INFO4) <<
"first active time end: " << next_active_end_ << std::endl;
131 if (dormant_dist_->sample() < 0) {
132 next_dormant_end_ = -1;
133 LGH(INFO4) <<
"dormant length -1, always active" << std::endl;
135 next_dormant_end_ = -1;
136 LGH(INFO4) <<
"dormant length set at -1 for first active period of "
137 "cumulative capacity cycle"
141 LGH(INFO4) <<
"first dormant time end: " << next_dormant_end_ << std::endl;
147 TotalInvTracker* buf_tracker) {
151 set_total_inv_tracker(buf_tracker);
152 init_active_dormant();
158 TotalInvTracker* buf_tracker,
double throughput,
159 boost::shared_ptr<IntDistribution> active_dist,
160 boost::shared_ptr<IntDistribution> dormant_dist,
161 boost::shared_ptr<DoubleDistribution> size_dist) {
165 set_total_inv_tracker(buf_tracker);
166 set_throughput(throughput);
170 init_active_dormant();
176 TotalInvTracker* buf_tracker,
177 double throughput,
double quantize) {
181 set_total_inv_tracker(buf_tracker);
182 set_throughput(throughput);
183 set_quantize(quantize);
184 init_active_dormant();
190 TotalInvTracker* buf_tracker,
191 double throughput, std::string inv_policy,
192 double fill_behav,
double req_at) {
196 set_total_inv_tracker(buf_tracker);
197 set_inv_policy(inv_policy, fill_behav, req_at);
198 set_throughput(throughput);
199 init_active_dormant();
205 TotalInvTracker* buf_tracker,
206 std::string inv_policy,
double fill_behav,
211 set_total_inv_tracker(buf_tracker);
212 set_inv_policy(inv_policy, fill_behav, req_at);
213 init_active_dormant();
219 TotalInvTracker* buf_tracker,
double throughput,
double cumulative_cap,
220 boost::shared_ptr<IntDistribution> dormant_dist) {
224 set_total_inv_tracker(buf_tracker);
225 set_throughput(throughput);
228 init_active_dormant();
234 c[10010000] = 1e-100;
239 return Set(commod, c, 1.0);
247 commod_details_[commod] = d;
252 commod_details_.erase(commod);
258 std::stringstream ss;
259 ss <<
"No manager set on Buy Policy " << name_;
267 std::stringstream ss;
268 ss <<
"No manager set on Buy Policy " << name_;
275 rsrc_commods_.clear();
276 std::set<RequestPortfolio<Material>::Ptr> ports;
283 ? cumulative_cap_ - cycle_total_inv_
284 : std::numeric_limits<double>::max();
289 LGH(INFO3) <<
"in dormant period, no request" << std::endl;
294 if (amt <
eps())
return ports;
297 double req_amt =
ReqQty(amt);
298 int n_req =
NReq(amt);
299 LGH(INFO3) <<
"requesting " << amt <<
" kg via " << n_req <<
" request(s)"
303 for (
int i = 0; i != n_req; i++) {
305 std::vector<Request<Material>*> mreqs;
306 std::map<std::string, CommodDetail>::iterator it;
307 for (it = commod_details_.begin(); it != commod_details_.end(); ++it) {
308 std::string commod = it->first;
309 CommodDetail d = it->second;
310 LG(INFO3) <<
" - one " << amt <<
" kg request of " << commod;
324 std::vector<std::pair<Trade<Material>,
Material::Ptr>>::const_iterator it;
325 rsrc_commods_.clear();
326 for (it = resps.begin(); it != resps.end(); ++it) {
327 rsrc_commods_[it->second] = it->first.request->commodity();
328 LGH(INFO3) <<
"got " << it->second->quantity() <<
" kg of "
329 << it->first.request->commodity() << std::endl;
330 buf_->Push(it->second);
333 cycle_total_inv_ += it->second->quantity();
339 ((cumulative_cap_ - cycle_total_inv_) <
eps_rsrc())) {
341 LGH(INFO3) <<
"cycle cumulative inventory has been reached. Dormant period "
343 << next_dormant_end_ << std::endl;
344 cycle_total_inv_ = 0;
349 int active_length = active_dist_->sample();
371 dormant_length = dormant_dist_->sample();
373 }
else if (next_dormant_end_ >= 0) {
376 dormant_length = dormant_dist_->sample();
377 dormant_start = std::max(next_active_end_, 1);
382 next_dormant_end_ = dormant_length + dormant_start;
390 return size_dist_->sample();
394 if (
manager()->context()->time() == next_dormant_end_) {
396 next_dormant_end_ = -1;
401 LGH(INFO4) <<
"end of dormant period, next active time end: "
403 <<
", and next dormant time end: " << next_dormant_end_
418 ->
AddVal(
"Length", length)
The abstract base class used by all types of agents that live and interact in a simulation.
Context * context() const
Returns this agent's simulation context.
static Ptr CreateFromMass(CompMap v)
Creates a new composition from v with its components having appropriate mass-based ratios.
boost::shared_ptr< Composition > Ptr
void UnregisterTrader(Trader *e)
Unregisters an agent as a participant in resource exchanges.
Datum * NewDatum(std::string title)
See Recorder::NewDatum documentation.
void RegisterTrader(Trader *e)
Registers an agent as a participant in resource exchanges.
virtual int time()
Returns the current simulation timestep.
Datum * AddVal(const char *field, boost::spirit::hold_any val, std::vector< int > *shape=NULL)
Add an arbitrary field-value pair to the datum.
void Record()
Record this datum to its Recorder.
static Ptr CreateUntracked(double quantity, Composition::Ptr c, double unit_value=kUnsetUnitValue)
Creates a new material resource that does not actually exist as part of the simulation and is untrack...
boost::shared_ptr< Material > Ptr
A RequestPortfolio is a group of (possibly constrained) requests for resources.
void AddMutualReqs(const std::vector< Request< T > * > &rs)
adds a collection of requests (already having been registered with this portfolio) as multicommodity ...
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
boost::shared_ptr< RequestPortfolio< T > > Ptr
A Request encapsulates all the information required to communicate the needs of an agent in the Dynam...
virtual Agent * manager()
For values that are too big, too small, etc.
cyclus::IntDistribution::Ptr dormant_dist_
cyclus::IntDistribution::Ptr active_dist_
cyclus::DoubleDistribution::Ptr size_dist_
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
double eps_rsrc()
an epsilon value to be used by resources
std::map< Nuc, double > CompMap
a raw definition of nuclides and corresponding (dimensionless quantities).
double eps()
a generic epsilon value
void Warn(const std::string &msg)
Issue a warning with the approriate message, accoring to the current warning settings.
A Trade is a simple container that associates a request for a resource with a bid for that resource.