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");
38 throughput_ = std::numeric_limits<double>::max();
40 fill_to_ = std::numeric_limits<double>::max();
41 req_at_ = std::numeric_limits<double>::max();
50void MatlBuyPolicy::set_manager(
Agent* m) {
56 ss <<
"No manager set on Buy Policy " << name_;
61void MatlBuyPolicy::set_total_inv_tracker(TotalInvTracker* t) {
63 std::vector<ResBuf<Material>*> bufs = {buf_};
64 buf_tracker_->Init(bufs, buf_->capacity());
66 else if (!t->buf_in_tracker(buf_)) {
68 ss <<
"TotalInvTracker does not contain ResBuf used in buy policy";
69 throw ValueError(ss.str());
76void MatlBuyPolicy::set_inv_policy(std::string inv_policy,
double fill,
double req_at) {
78 std::transform(inv_policy.begin(), inv_policy.end(), inv_policy.begin(), ::tolower);
79 if ((inv_policy ==
"ss")) {
82 else if ((inv_policy ==
"rq") || (inv_policy ==
"qr")) {
85 set_fill_to(req_at + fill);
88 throw ValueError(
"Invalid inventory policy");
92void MatlBuyPolicy::set_fill_to(
double x) {
94 fill_to_ = std::min(x, buf_->capacity());
97void MatlBuyPolicy::set_req_at(
double x) {
99 req_at_ = std::min(x, buf_->capacity());
102void MatlBuyPolicy::set_cumulative_cap(
double x) {
104 cumulative_cap_ = std::min(x, buf_->capacity());
107void MatlBuyPolicy::set_quantize(
double x) {
112void MatlBuyPolicy::set_throughput(
double x) {
117void MatlBuyPolicy::init_active_dormant() {
118 if (active_dist_ == NULL) {
119 active_dist_ = boost::shared_ptr<FixedIntDist>(
new FixedIntDist(1));
121 if (dormant_dist_ == NULL) {
122 dormant_dist_ = boost::shared_ptr<FixedIntDist>(
new FixedIntDist(-1));
124 if (size_dist_ == NULL) {
125 size_dist_ = boost::shared_ptr<FixedDoubleDist>(
new FixedDoubleDist(1.0));
128 if (size_dist_->max() > 1) {
129 throw ValueError(
"Size distribution cannot have a max greater than 1.");
133 LGH(INFO4) <<
"first active time end: " << next_active_end_ << std::endl;
135 if (dormant_dist_->sample() < 0) {
136 next_dormant_end_ = -1;
137 LGH(INFO4) <<
"dormant length -1, always active" << std::endl;
140 next_dormant_end_ = -1;
141 LGH(INFO4) <<
"dormant length set at -1 for first active period of cumulative capacity cycle" << std::endl;
145 LGH(INFO4) <<
"first dormant time end: " << next_dormant_end_ << std::endl;
150 std::string name, TotalInvTracker* buf_tracker) {
154 set_total_inv_tracker(buf_tracker);
155 init_active_dormant();
160 std::string name, TotalInvTracker* buf_tracker,
161 double throughput, boost::shared_ptr<IntDistribution> active_dist,
162 boost::shared_ptr<IntDistribution> dormant_dist,
163 boost::shared_ptr<DoubleDistribution> size_dist) {
167 set_total_inv_tracker(buf_tracker);
168 set_throughput(throughput);
169 active_dist_ = active_dist;
170 dormant_dist_ = dormant_dist;
171 size_dist_ = size_dist;
172 init_active_dormant();
177 std::string name, TotalInvTracker* buf_tracker,
178 double throughput,
double quantize) {
182 set_total_inv_tracker(buf_tracker);
183 set_throughput(throughput);
184 set_quantize(quantize);
185 init_active_dormant();
191 TotalInvTracker* buf_tracker,
193 std::string inv_policy,
194 double fill_behav,
double req_at) {
198 set_total_inv_tracker(buf_tracker);
199 set_inv_policy(inv_policy, fill_behav, req_at);
200 set_throughput(throughput);
201 init_active_dormant();
207 TotalInvTracker* buf_tracker,
208 std::string inv_policy,
209 double fill_behav,
double req_at) {
213 set_total_inv_tracker(buf_tracker);
214 set_inv_policy(inv_policy, fill_behav, req_at);
215 init_active_dormant();
221 TotalInvTracker* buf_tracker,
222 double throughput,
double cumulative_cap,
223 boost::shared_ptr<IntDistribution> dormant_dist) {
227 set_total_inv_tracker(buf_tracker);
228 set_throughput(throughput);
229 set_cumulative_cap(cumulative_cap);
230 dormant_dist_ = dormant_dist;
231 init_active_dormant();
237 c[10010000] = 1e-100;
242 return Set(commod, c, 1.0);
250 commod_details_[commod] = d;
256 commod_details_.erase(commod);
262 std::stringstream ss;
263 ss <<
"No manager set on Buy Policy " << name_;
271 std::stringstream ss;
272 ss <<
"No manager set on Buy Policy " << name_;
279 rsrc_commods_.clear();
280 std::set<RequestPortfolio<Material>::Ptr> ports;
286 double max_request_amt =
use_cumulative_capacity() ? cumulative_cap_ - cycle_total_inv_ : std::numeric_limits<double>::max();
291 else {
LGH(INFO3) <<
"in dormant period, no request" << std::endl; }
299 double req_amt =
ReqQty(amt);
300 int n_req =
NReq(amt);
301 LGH(INFO3) <<
"requesting " << amt <<
" kg via " << n_req <<
" request(s)" << std::endl;
304 for (
int i = 0; i != n_req; i++) {
306 std::vector<Request<Material>*> mreqs;
307 std::map<std::string, CommodDetail>::iterator it;
308 for (it = commod_details_.begin(); it != commod_details_.end(); ++it) {
309 std::string commod = it->first;
310 CommodDetail d = it->second;
311 LG(INFO3) <<
" - one " << amt <<
" kg request of " << commod;
325 std::vector<std::pair<Trade<Material>,
Material::Ptr> >::const_iterator it;
326 rsrc_commods_.clear();
327 for (it = resps.begin(); it != resps.end(); ++it) {
328 rsrc_commods_[it->second] = it->first.request->commodity();
329 LGH(INFO3) <<
"got " << it->second->quantity() <<
" kg of "
330 << it->first.request->commodity() << std::endl;
331 buf_->Push(it->second);
334 cycle_total_inv_ += it->second->quantity();
340 (cumulative_cap_ - cycle_total_inv_) <
eps_rsrc())) {
342 LGH(INFO3) <<
"cycle cumulative inventory has been reached. Dormant period will end at " << next_dormant_end_ << std::endl;
343 cycle_total_inv_ = 0;
348 int active_length = active_dist_->sample();
368 dormant_length = dormant_dist_->sample();
371 else if (next_dormant_end_ >= 0) {
374 dormant_length = dormant_dist_->sample();
375 dormant_start = std::max(next_active_end_, 1);
379 next_dormant_end_ = dormant_length + dormant_start;
387 return size_dist_->sample();
391 if (
manager()->context()->time() == next_dormant_end_) {
397 LGH(INFO4) <<
"end of dormant period, next active time end: " << next_active_end_ <<
", and next dormant time end: " << next_dormant_end_ << std::endl;
408 ->
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.
boost::shared_ptr< Material > Ptr
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...
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...
A simple API for agents that wish to exchange resources in the simulation.
virtual Agent * manager()
For values that are too big, too small, etc.
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.