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");
32void MatlSellPolicy::set_quantize(
double x) {
37void MatlSellPolicy::set_throughput(
double x) {
42void MatlSellPolicy::set_ignore_comp(
bool x) {
46void MatlSellPolicy::set_package(std::string x) {
53 if (pkg->strategy() ==
"first" || pkg->strategy() ==
"equal") {
54 std::vector<double> fill = pkg->GetFillMass(quantize_);
55 if (fill.size() > 1 || std::fmod(fill.front(), quantize_) > 0) {
56 ss <<
"Quantize " << quantize_
57 <<
" is not fully packagable based on fill min/max values ("
58 << pkg->fill_min() <<
", " << pkg->fill_max() <<
")";
59 throw ValueError(ss.str());
62 ss <<
"Package strategy " << pkg->strategy()
63 <<
" is not allowed for sell policies with quantize.";
64 throw ValueError(ss.str());
71void MatlSellPolicy::set_transport_unit(std::string x) {
76 std::vector<double> fill = package_->GetFillMass(quantize_);
77 int num_pkgs = fill.size();
78 int max_shippable = tu->MaxShippablePackages(num_pkgs);
80 if (max_shippable != num_pkgs) {
82 ss <<
"Quantize " << quantize_
83 <<
" packages cannot be shipped according to transport unit fill "
85 << tu->fill_min() <<
", " << tu->fill_max() <<
")";
86 throw ValueError(ss.str());
102 std::string name,
double throughput) {
106 set_throughput(throughput);
111 std::string name,
bool ignore_comp) {
115 set_ignore_comp(ignore_comp);
120 std::string name,
double throughput,
125 set_throughput(throughput);
126 set_ignore_comp(ignore_comp);
131 std::string name,
double throughput,
132 bool ignore_comp,
double quantize,
133 std::string package_name,
134 std::string transport_unit_name) {
138 set_quantize(quantize);
139 set_throughput(throughput);
140 set_ignore_comp(ignore_comp);
141 set_package(package_name);
142 set_transport_unit(transport_unit_name);
147 commods_.insert(commod);
153 std::stringstream ss;
154 ss <<
"No manager set on Sell Policy " << name_;
162 std::stringstream ss;
163 ss <<
"No manager set on Sell Policy " << name_;
171 double bcap = buf_->quantity();
172 double limit =
Excl() ? \
173 quantize_ *
static_cast<int>(std::floor(bcap / quantize_)) : bcap;
174 return std::min(throughput_, limit);
179 std::set<BidPortfolio<Material>::Ptr> ports;
182 double limit =
Limit();
183 if (buf_->empty() || buf_->quantity() <
eps()|| limit <
eps())
190 LGH(INFO3) <<
"bidding out " << limit <<
" kg";
199 double remaining_qty;
200 std::vector<double> bids;
201 std::set<std::string>::iterator sit;
202 std::vector<Request<Material>*>::const_iterator rit;
203 for (sit = commods_.begin(); sit != commods_.end(); ++sit) {
205 if (commod_requests.count(commod) < 1)
208 const std::vector<Request<Material>*>& requests =
209 commod_requests.at(commod);
210 for (rit = requests.begin(); rit != requests.end(); ++rit) {
212 qty = std::min(req->
target()->quantity(), limit);
214 std::vector<double> bids;
216 int n_full_bids =
static_cast<int>(std::floor(qty / quantize_));
217 bids.assign(n_full_bids, quantize_);
219 bids = package_->GetFillMass(qty);
223 int shippable_pkgs = transport_unit_->MaxShippablePackages(bids.size());
224 if (shippable_pkgs < bids.size()) {
226 bids.erase(bids.begin() + shippable_pkgs, bids.end());
232 std::vector<double>::iterator bit;
233 for (bit = bids.begin(); bit != bids.end(); ++bit) {
234 offer = ignore_comp_ ? \
235 Material::CreateUntracked(*bit, req->
target()->comp()) : \
236 Material::CreateUntracked(*bit, m->comp());
237 port->
AddBid(req, offer,
this, excl);
238 LG(INFO3) <<
" - bid " << *bit <<
" kg on a request for " << commod;
249 std::vector<Trade<Material> >::const_iterator it;
252 int shippable_pkgs = transport_unit_->MaxShippablePackages(trades.size());
256 for (it = trades.begin(); it != trades.end(); ++it) {
257 if (shippable_pkgs > 0){
259 LGH(INFO3) <<
" sending " << qty <<
" kg of " << it->request->commodity();
266 if (package_->name() != mat->package_name()) {
267 std::vector<Material::Ptr> mat_pkgd = mat->Package<
Material>(package_);
274 if (mat_pkgd.size() > 0) {
276 trade_mat = mat_pkgd[0];
288 trade_mat->Transmute(it->request->target()->comp());
290 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...
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...
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
A simple API for agents that wish to exchange resources in the simulation.
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.