From 66d44c7e3f0d5d3e1a6dd4263187666461e1f85c Mon Sep 17 00:00:00 2001 From: Katie Mummah Date: Mon, 13 May 2024 14:32:20 -0500 Subject: [PATCH] sell pol return responses as vector of packaged materials --- src/toolkit/matl_sell_policy.cc | 42 ++++++++++++++----------- src/toolkit/matl_sell_policy.h | 2 +- tests/toolkit/matl_sell_policy_tests.cc | 6 ++-- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/toolkit/matl_sell_policy.cc b/src/toolkit/matl_sell_policy.cc index 1947a47b92..6e67288417 100644 --- a/src/toolkit/matl_sell_policy.cc +++ b/src/toolkit/matl_sell_policy.cc @@ -17,7 +17,8 @@ MatlSellPolicy::MatlSellPolicy() : quantize_(0), throughput_(std::numeric_limits::max()), ignore_comp_(false), - package_id_(Package::unpackaged_id()) { + package_id_(Package::unpackaged_id()), + package_(Package::unpackaged()) { Warn( "MatlSellPolicy is experimental and its API may be subject to change"); } @@ -43,12 +44,10 @@ void MatlSellPolicy::set_ignore_comp(bool x) { void MatlSellPolicy::set_package(int x) { assert(x >= 1); - package_id_ = x; + // if no real context, only unpackaged can be used (keep default) if (manager() != NULL) { - package_ = manager()->context()->GetPackageById(package_id_); - } else { - // if no real context, only unpackaged can be used. - package_ = Package::unpackaged(); + package_id_ = x; + package_ = manager()->context()->GetPackageById(x); } } @@ -57,7 +56,6 @@ MatlSellPolicy& MatlSellPolicy::Init(Agent* manager, ResBuf* buf, Trader::manager_ = manager; buf_ = buf; name_ = name; - package_ = Package::unpackaged(); return *this; } @@ -67,7 +65,6 @@ MatlSellPolicy& MatlSellPolicy::Init(Agent* manager, ResBuf* buf, buf_ = buf; name_ = name; set_throughput(throughput); - package_ = Package::unpackaged(); return *this; } @@ -77,7 +74,6 @@ MatlSellPolicy& MatlSellPolicy::Init(Agent* manager, ResBuf* buf, buf_ = buf; name_ = name; set_ignore_comp(ignore_comp); - package_ = Package::unpackaged(); return *this; } @@ -89,7 +85,6 @@ MatlSellPolicy& MatlSellPolicy::Init(Agent* manager, ResBuf* buf, name_ = name; set_throughput(throughput); set_ignore_comp(ignore_comp); - package_ = Package::unpackaged(); return *this; } @@ -103,7 +98,6 @@ MatlSellPolicy& MatlSellPolicy::Init(Agent* manager, ResBuf* buf, set_quantize(quantize); set_throughput(throughput); set_ignore_comp(ignore_comp); - set_package(package_id); return *this; } @@ -118,7 +112,8 @@ void MatlSellPolicy::Start() { ss << "No manager set on Sell Policy " << name_; throw ValueError(ss.str()); } - if (quantize_ < package_->fill_min() || quantize_ > package_->fill_max()) { + 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() << ")"; @@ -178,8 +173,12 @@ std::set::Ptr> MatlSellPolicy::GetMatlBids( for (rit = requests.begin(); rit != requests.end(); ++rit) { req = *rit; qty = std::min(req->target()->quantity(), limit); - package_fill = std::min(qty, package_->GetFillMass(qty)); - nbids = excl ? static_cast(std::floor(qty / quantize_)) : static_cast(std::floor(qty / package_fill)); + package_fill = package_->GetFillMass(qty); + if (package_fill == 0) { + nbids = 0; + } else { + 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(); @@ -197,16 +196,23 @@ std::set::Ptr> MatlSellPolicy::GetMatlBids( void MatlSellPolicy::GetMatlTrades( const std::vector >& trades, - std::vector, Material::Ptr> >& responses) { + std::vector, std::vector > >& responses) { Composition::Ptr c; std::vector >::const_iterator it; for (it = trades.begin(); it != trades.end(); ++it) { double qty = it->amt; LGH(INFO3) << " sending " << qty << " kg of " << it->request->commodity(); Material::Ptr mat = buf_->Pop(qty, cyclus::eps_rsrc()); - if (ignore_comp_) - mat->Transmute(it->request->target()->comp()); - responses.push_back(std::make_pair(*it, mat)); + std::vector mat_pkgd = mat->Package(package_); + // push any extra material that couldn't be packaged back onto buffer + buf_->Push(mat); + if (ignore_comp_) { + std::vector::iterator pit; + for (pit = mat_pkgd.begin(); pit != mat_pkgd.end(); ++pit) { + (*pit)->Transmute(it->request->target()->comp()); + } + } + responses.push_back(std::make_pair(*it, mat_pkgd)); } } diff --git a/src/toolkit/matl_sell_policy.h b/src/toolkit/matl_sell_policy.h index d93ec8fe5a..8bb2a456cf 100644 --- a/src/toolkit/matl_sell_policy.h +++ b/src/toolkit/matl_sell_policy.h @@ -119,7 +119,7 @@ class MatlSellPolicy : public Trader { CommodMap::type& commod_requests); virtual void GetMatlTrades( const std::vector >& trades, - std::vector, Material::Ptr> >& responses); + std::vector, std::vector > >& responses); /// }@ private: diff --git a/tests/toolkit/matl_sell_policy_tests.cc b/tests/toolkit/matl_sell_policy_tests.cc index e1f9f05446..b505fddacd 100644 --- a/tests/toolkit/matl_sell_policy_tests.cc +++ b/tests/toolkit/matl_sell_policy_tests.cc @@ -127,7 +127,7 @@ TEST_F(MatlSellPolicyTests, Trades) { MatlSellPolicy p; std::string commod("commod"); std::vector > trades; - std::vector, Material::Ptr> > obs; + std::vector, std::vector > > obs; Request* req = Request::Create(mat1, fac1, commod); Bid* bid = Bid::Create(req, mat, fac1); @@ -138,14 +138,14 @@ TEST_F(MatlSellPolicyTests, Trades) { p.Init(NULL, &buff, "").Set(commod); p.GetMatlTrades(trades, obs); ASSERT_EQ(obs.size(), 1); - ASSERT_EQ(obs.begin()->second->comp(), comp); + ASSERT_EQ(obs.begin()->second[0]->comp(), comp); // ignore comp obs.clear(); p.Init(NULL, &buff, "", qty, true).Set(commod); p.GetMatlTrades(trades, obs); ASSERT_EQ(obs.size(), 1); - ASSERT_EQ(obs.begin()->second->comp(), comp1); + ASSERT_EQ(obs.begin()->second[0]->comp(), comp1); delete bid; delete req;