diff --git a/src/toolkit/matl_sell_policy.cc b/src/toolkit/matl_sell_policy.cc index 2f9c82c499..1947a47b92 100644 --- a/src/toolkit/matl_sell_policy.cc +++ b/src/toolkit/matl_sell_policy.cc @@ -16,7 +16,8 @@ MatlSellPolicy::MatlSellPolicy() : name_(""), quantize_(0), throughput_(std::numeric_limits::max()), - ignore_comp_(false) { + ignore_comp_(false), + package_id_(Package::unpackaged_id()) { Warn( "MatlSellPolicy is experimental and its API may be subject to change"); } @@ -40,11 +41,23 @@ void MatlSellPolicy::set_ignore_comp(bool x) { ignore_comp_ = x; } +void MatlSellPolicy::set_package(int x) { + assert(x >= 1); + package_id_ = x; + if (manager() != NULL) { + package_ = manager()->context()->GetPackageById(package_id_); + } else { + // if no real context, only unpackaged can be used. + package_ = Package::unpackaged(); + } +} + MatlSellPolicy& MatlSellPolicy::Init(Agent* manager, ResBuf* buf, std::string name) { Trader::manager_ = manager; buf_ = buf; name_ = name; + package_ = Package::unpackaged(); return *this; } @@ -54,6 +67,7 @@ MatlSellPolicy& MatlSellPolicy::Init(Agent* manager, ResBuf* buf, buf_ = buf; name_ = name; set_throughput(throughput); + package_ = Package::unpackaged(); return *this; } @@ -63,6 +77,7 @@ MatlSellPolicy& MatlSellPolicy::Init(Agent* manager, ResBuf* buf, buf_ = buf; name_ = name; set_ignore_comp(ignore_comp); + package_ = Package::unpackaged(); return *this; } @@ -74,18 +89,21 @@ MatlSellPolicy& MatlSellPolicy::Init(Agent* manager, ResBuf* buf, name_ = name; set_throughput(throughput); set_ignore_comp(ignore_comp); + package_ = Package::unpackaged(); return *this; } MatlSellPolicy& MatlSellPolicy::Init(Agent* manager, ResBuf* buf, std::string name, double throughput, - bool ignore_comp, double quantize) { + bool ignore_comp, double quantize, + int package_id) { Trader::manager_ = manager; buf_ = buf; name_ = name; set_quantize(quantize); set_throughput(throughput); set_ignore_comp(ignore_comp); + set_package(package_id); return *this; } @@ -100,6 +118,12 @@ void MatlSellPolicy::Start() { ss << "No manager set on Sell Policy " << name_; throw ValueError(ss.str()); } + if (quantize_ < package_->fill_min() || quantize_ > package_->fill_max()) { + std::stringstream ss; + ss << "Quantize " << quantize_ << " is outside the package fill min/max values (" << package_->fill_min() << ", " + << package_->fill_max() << ")"; + throw ValueError(ss.str()); + } manager()->context()->RegisterTrader(this); } @@ -141,6 +165,7 @@ std::set::Ptr> MatlSellPolicy::GetMatlBids( Material::Ptr m, offer; double qty; int nbids; + double package_fill; std::set::iterator sit; std::vector*>::const_iterator rit; for (sit = commods_.begin(); sit != commods_.end(); ++sit) { @@ -153,8 +178,9 @@ std::set::Ptr> MatlSellPolicy::GetMatlBids( for (rit = requests.begin(); rit != requests.end(); ++rit) { req = *rit; qty = std::min(req->target()->quantity(), limit); - nbids = excl ? static_cast(std::floor(qty / quantize_)) : 1; - qty = excl ? quantize_ : qty; + package_fill = std::min(qty, package_->GetFillMass(qty)); + nbids = excl ? static_cast(std::floor(qty / quantize_)) : static_cast(std::floor(qty / package_fill)); + qty = excl ? quantize_ : package_fill; for (int i = 0; i < nbids; i++) { m = buf_->Pop(); buf_->Push(m); diff --git a/src/toolkit/matl_sell_policy.h b/src/toolkit/matl_sell_policy.h index 0983a02861..d93ec8fe5a 100644 --- a/src/toolkit/matl_sell_policy.h +++ b/src/toolkit/matl_sell_policy.h @@ -7,6 +7,7 @@ #include "material.h" #include "res_buf.h" #include "trader.h" +#include "package.h" namespace cyclus { namespace toolkit { @@ -83,7 +84,8 @@ class MatlSellPolicy : public Trader { double throughput, bool ignore_comp); MatlSellPolicy& Init(Agent* manager, ResBuf* buf, std::string name, double throughput, bool ignore_comp, - double quantize); + double quantize, + int package_id = Package::unpackaged_id()); /// @} /// Instructs the policy to empty its buffer with offers on the given @@ -124,6 +126,7 @@ class MatlSellPolicy : public Trader { void set_quantize(double x); void set_throughput(double x); void set_ignore_comp(bool x); + void set_package(int x); ResBuf* buf_; std::set commods_; @@ -131,6 +134,8 @@ class MatlSellPolicy : public Trader { double throughput_; std::string name_; bool ignore_comp_; + int package_id_; + Package::Ptr package_; }; } // namespace toolkit