6#define LG(X) LOG(LEV_##X, "selpol")
8 LOG(LEV_##X, "selpol") << "policy " << name_ << " (agent " \
9 << Trader::manager()->prototype() << "-" \
10 << Trader::manager()->id() << "): "
19 throughput_(
std::numeric_limits<double>::max()),
21 package_(
Package::unpackaged()),
24 "MatlSellPolicy is experimental and its API may be subject to change");
31void MatlSellPolicy::set_quantize(
double x) {
36void MatlSellPolicy::set_throughput(
double x) {
41void MatlSellPolicy::set_ignore_comp(
bool x) {
45void MatlSellPolicy::set_package(std::string x) {
52 if (pkg->strategy() ==
"first" || pkg->strategy() ==
"equal") {
53 std::vector<double> fill = pkg->GetFillMass(quantize_);
54 if (fill.size() > 1 || std::fmod(fill.front(), quantize_) > 0) {
55 ss <<
"Quantize " << quantize_
56 <<
" is not fully packagable based on fill min/max values ("
57 << pkg->fill_min() <<
", " << pkg->fill_max() <<
")";
58 throw ValueError(ss.str());
61 ss <<
"Package strategy " << pkg->strategy()
62 <<
" is not allowed for sell policies with quantize.";
63 throw ValueError(ss.str());
70void MatlSellPolicy::set_transport_unit(std::string x) {
75 std::vector<double> fill = package_->GetFillMass(quantize_);
76 int num_pkgs = fill.size();
77 int max_shippable = tu->MaxShippablePackages(num_pkgs);
79 if (max_shippable != num_pkgs) {
81 ss <<
"Quantize " << quantize_
82 <<
" packages cannot be shipped according to transport unit fill "
83 <<
"min/max values (" << tu->fill_min() <<
", " << tu->fill_max()
85 throw ValueError(ss.str());
101 std::string name,
double throughput) {
105 set_throughput(throughput);
110 std::string name,
bool ignore_comp) {
114 set_ignore_comp(ignore_comp);
119 std::string name,
double throughput,
124 set_throughput(throughput);
125 set_ignore_comp(ignore_comp);
130 std::string name,
double throughput,
131 bool ignore_comp,
double quantize,
132 std::string package_name,
133 std::string transport_unit_name) {
137 set_quantize(quantize);
138 set_throughput(throughput);
139 set_ignore_comp(ignore_comp);
140 set_package(package_name);
141 set_transport_unit(transport_unit_name);
146 commods_.insert(commod);
152 std::stringstream ss;
153 ss <<
"No manager set on Sell Policy " << name_;
161 std::stringstream ss;
162 ss <<
"No manager set on Sell Policy " << name_;
169 double bcap = buf_->quantity();
171 Excl() ? quantize_ *
static_cast<int>(std::floor(bcap / quantize_))
173 return std::min(throughput_, limit);
178 std::set<BidPortfolio<Material>::Ptr> ports;
180 double limit =
Limit();
181 if (buf_->empty() || buf_->quantity() <
eps() || limit <
eps())
return ports;
187 LGH(INFO3) <<
"bidding out " << limit <<
" kg";
196 double remaining_qty;
197 std::vector<double> bids;
198 std::set<std::string>::iterator sit;
199 std::vector<Request<Material>*>::const_iterator rit;
200 for (sit = commods_.begin(); sit != commods_.end(); ++sit) {
202 if (commod_requests.count(commod) < 1)
continue;
204 const std::vector<Request<Material>*>& requests =
205 commod_requests.at(commod);
206 for (rit = requests.begin(); rit != requests.end(); ++rit) {
208 qty = std::min(req->
target()->quantity(), limit);
210 std::vector<double> bids;
212 int n_full_bids =
static_cast<int>(std::floor(qty / quantize_));
213 bids.assign(n_full_bids, quantize_);
215 bids = package_->GetFillMass(qty);
219 int shippable_pkgs = transport_unit_->MaxShippablePackages(bids.size());
220 if (shippable_pkgs < bids.size()) {
222 bids.erase(bids.begin() + shippable_pkgs, bids.end());
228 std::vector<double>::iterator bit;
229 for (bit = bids.begin(); bit != bids.end(); ++bit) {
233 port->
AddBid(req, offer,
this, excl);
234 LG(INFO3) <<
" - bid " << *bit <<
" kg on a request for " << commod;
245 std::vector<Trade<Material>>::const_iterator it;
248 int shippable_pkgs = transport_unit_->MaxShippablePackages(trades.size());
252 for (it = trades.begin(); it != trades.end(); ++it) {
253 if (shippable_pkgs > 0) {
255 LGH(INFO3) <<
" sending " << qty <<
" kg of " << it->request->commodity();
262 if (package_->name() != mat->package_name()) {
263 std::vector<Material::Ptr> mat_pkgd = mat->Package<
Material>(package_);
270 if (mat_pkgd.size() > 0) {
272 trade_mat = mat_pkgd[0];
285 trade_mat->comp()->mass(),
eps_rsrc())) {
286 trade_mat->Transmute(it->request->target()->comp());
288 responses.push_back(std::make_pair(*it, trade_mat));
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.
A BidPortfolio is a collection of bids as responses to requests for resources and associated constrai...
void AddConstraint(const CapacityConstraint< T > &c)
add a capacity constraint associated with the portfolio
Bid< T > * AddBid(Request< T > *request, boost::shared_ptr< T > offer, Trader *bidder, bool exclusive, double preference)
add a bid to the portfolio
boost::shared_ptr< BidPortfolio< T > > Ptr
A CapacityConstraint provides an ability to determine an agent's constraints on resource allocation g...
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.
Package::Ptr GetPackage(std::string name)
Retrieve a registered package.
TransportUnit::Ptr GetTransportUnit(std::string name)
Retrieve a registered transport unit.
The material class is primarily responsible for enabling basic material manipulation while helping en...
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
Package is a class that packages materials into discrete items in ways.
boost::shared_ptr< Package > Ptr
static std::string unpackaged_name()
A Request encapsulates all the information required to communicate the needs of an agent in the Dynam...
boost::shared_ptr< T > target() const
virtual Agent * manager()
TransportUnit is a class that can be used in conjunction with packages to restrict the amount of mate...
static std::string unrestricted_name()
boost::shared_ptr< TransportUnit > Ptr
For values that are too big, too small, etc.
bool AlmostEq(const CompMap &v1, const CompMap &v2, double threshold)
Returns true if all nuclides of v1 and v2 are the same within threshold.
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
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.
std::map< std::string, std::vector< Request< T > * > > type
A Trade is a simple container that associates a request for a resource with a bid for that resource.