7#define LG(X) LOG(LEV_##X, "buypol")
9 LOG(LEV_##X, "buypol") << "policy " << name_ << " (agent " \
10 << Trader::manager()->prototype() << "-" \
11 << Trader::manager()->id() << "): "
29 "MatlBuyPolicy is experimental and its API may be subject to change");
37void MatlBuyPolicy::set_manager(
Agent* m) {
43 ss <<
"No manager set on Buy Policy " << name_;
50 std::vector<ResBuf<Material>*>
bufs = {buf_};
51 buf_tracker_->Init(
bufs, buf_->capacity());
53 else if (!t->buf_in_tracker(buf_)) {
55 ss <<
"TotalInvTracker does not contain ResBuf used in buy policy";
56 throw ValueError(
ss.str());
63void MatlBuyPolicy::set_inv_policy(std::string inv_policy,
double fill,
double req_at) {
65 std::transform(inv_policy.begin(), inv_policy.end(), inv_policy.begin(),
::tolower);
66 if ((inv_policy ==
"ss")) {
69 else if ((inv_policy ==
"rq") || (inv_policy ==
"qr")) {
75 throw ValueError(
"Invalid inventory policy");
79void MatlBuyPolicy::set_fill_to(
double x) {
81 fill_to_ = std::min(
x, buf_->capacity());
84void MatlBuyPolicy::set_req_at(
double x) {
86 req_at_ = std::min(
x, buf_->capacity());
89void MatlBuyPolicy::set_cumulative_cap(
double x) {
91 cumulative_cap_ = std::min(
x, buf_->capacity());
94void MatlBuyPolicy::set_quantize(
double x) {
99void MatlBuyPolicy::set_throughput(
double x) {
104void MatlBuyPolicy::init_active_dormant() {
105 if (active_dist_ ==
NULL) {
106 active_dist_ = boost::shared_ptr<FixedIntDist>(
new FixedIntDist(1));
108 if (dormant_dist_ ==
NULL) {
109 dormant_dist_ = boost::shared_ptr<FixedIntDist>(
new FixedIntDist(-1));
111 if (size_dist_ ==
NULL) {
112 size_dist_ = boost::shared_ptr<FixedDoubleDist>(
new FixedDoubleDist(1.0));
115 if (size_dist_->max() > 1) {
116 throw ValueError(
"Size distribution cannot have a max greater than 1.");
120 LGH(
INFO4) <<
"first active time end: " << next_active_end_ << std::endl;
122 if (dormant_dist_->sample() < 0) {
123 next_dormant_end_ = -1;
124 LGH(
INFO4) <<
"dormant length -1, always active" << std::endl;
127 next_dormant_end_ = -1;
128 LGH(
INFO4) <<
"dormant length set at -1 for first active period of cumulative capacity cycle" << std::endl;
132 LGH(
INFO4) <<
"first dormant time end: " << next_dormant_end_ << std::endl;
142 init_active_dormant();
150 boost::shared_ptr<DoubleDistribution>
size_dist) {
159 init_active_dormant();
172 init_active_dormant();
180 std::string inv_policy,
188 init_active_dormant();
195 std::string inv_policy,
202 init_active_dormant();
218 init_active_dormant();
224 c[10010000] = 1
e-100;
229 return Set(commod,
c, 1.0);
237 commod_details_[commod] =
d;
243 std::stringstream
ss;
244 ss <<
"No manager set on Buy Policy " << name_;
252 std::stringstream
ss;
253 ss <<
"No manager set on Buy Policy " << name_;
260 rsrc_commods_.clear();
261 std::set<RequestPortfolio<Material>::Ptr>
ports;
272 else {
LGH(
INFO3) <<
"in dormant period, no request" << std::endl; }
282 LGH(
INFO3) <<
"requesting " << amt <<
" kg via " <<
n_req <<
" request(s)" << std::endl;
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;
306 std::vector<std::pair<Trade<Material>,
Material::Ptr> >::const_iterator
it;
307 rsrc_commods_.clear();
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);
315 cycle_total_inv_ +=
it->second->quantity();
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;
336 next_dormant_end_ = (
339 else if (next_dormant_end_ >= 0) {
340 next_dormant_end_ = dormant_dist_->sample() +
341 std::max(next_active_end_, 1);
347 return size_dist_->sample();
351 if (
manager()->context()->time() == next_dormant_end_) {
357 LGH(
INFO4) <<
"end of dormant period, next active time end: " << next_active_end_ <<
", and next dormant time end: " << next_dormant_end_ << std::endl;
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.
void RegisterTrader(Trader *e)
Registers an agent as a participant in resource exchanges.
virtual int time()
Returns the current simulation timestep.
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.
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
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.