Skip to content

Commit

Permalink
sell pol return responses as vector of packaged materials
Browse files Browse the repository at this point in the history
  • Loading branch information
nuclearkatie committed May 13, 2024
1 parent 3b14f57 commit 66d44c7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
42 changes: 24 additions & 18 deletions src/toolkit/matl_sell_policy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ MatlSellPolicy::MatlSellPolicy() :
quantize_(0),
throughput_(std::numeric_limits<double>::max()),
ignore_comp_(false),
package_id_(Package::unpackaged_id()) {
package_id_(Package::unpackaged_id()),
package_(Package::unpackaged()) {
Warn<EXPERIMENTAL_WARNING>(
"MatlSellPolicy is experimental and its API may be subject to change");
}
Expand All @@ -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);
}
}

Expand All @@ -57,7 +56,6 @@ MatlSellPolicy& MatlSellPolicy::Init(Agent* manager, ResBuf<Material>* buf,
Trader::manager_ = manager;
buf_ = buf;
name_ = name;
package_ = Package::unpackaged();
return *this;
}

Expand All @@ -67,7 +65,6 @@ MatlSellPolicy& MatlSellPolicy::Init(Agent* manager, ResBuf<Material>* buf,
buf_ = buf;
name_ = name;
set_throughput(throughput);
package_ = Package::unpackaged();
return *this;
}

Expand All @@ -77,7 +74,6 @@ MatlSellPolicy& MatlSellPolicy::Init(Agent* manager, ResBuf<Material>* buf,
buf_ = buf;
name_ = name;
set_ignore_comp(ignore_comp);
package_ = Package::unpackaged();
return *this;
}

Expand All @@ -89,7 +85,6 @@ MatlSellPolicy& MatlSellPolicy::Init(Agent* manager, ResBuf<Material>* buf,
name_ = name;
set_throughput(throughput);
set_ignore_comp(ignore_comp);
package_ = Package::unpackaged();
return *this;
}

Expand All @@ -103,7 +98,6 @@ MatlSellPolicy& MatlSellPolicy::Init(Agent* manager, ResBuf<Material>* buf,
set_quantize(quantize);
set_throughput(throughput);
set_ignore_comp(ignore_comp);
set_package(package_id);
return *this;
}

Expand All @@ -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() << ")";
Expand Down Expand Up @@ -178,8 +173,12 @@ std::set<BidPortfolio<Material>::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<int>(std::floor(qty / quantize_)) : static_cast<int>(std::floor(qty / package_fill));
package_fill = package_->GetFillMass(qty);
if (package_fill == 0) {
nbids = 0;
} else {
nbids = excl ? static_cast<int>(std::floor(qty / quantize_)) : static_cast<int>(std::floor(qty / package_fill));
}
qty = excl ? quantize_ : package_fill;
for (int i = 0; i < nbids; i++) {
m = buf_->Pop();
Expand All @@ -197,16 +196,23 @@ std::set<BidPortfolio<Material>::Ptr> MatlSellPolicy::GetMatlBids(

void MatlSellPolicy::GetMatlTrades(
const std::vector<Trade<Material> >& trades,
std::vector<std::pair<Trade<Material>, Material::Ptr> >& responses) {
std::vector<std::pair<Trade<Material>, std::vector<Material::Ptr> > >& responses) {
Composition::Ptr c;
std::vector<Trade<Material> >::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<Material::Ptr> mat_pkgd = mat->Package<Material>(package_);
// push any extra material that couldn't be packaged back onto buffer
buf_->Push(mat);
if (ignore_comp_) {
std::vector<Material::Ptr>::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));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/toolkit/matl_sell_policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class MatlSellPolicy : public Trader {
CommodMap<Material>::type& commod_requests);
virtual void GetMatlTrades(
const std::vector<Trade<Material> >& trades,
std::vector<std::pair<Trade<Material>, Material::Ptr> >& responses);
std::vector<std::pair<Trade<Material>, std::vector<Material::Ptr> > >& responses);
/// }@

private:
Expand Down
6 changes: 3 additions & 3 deletions tests/toolkit/matl_sell_policy_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ TEST_F(MatlSellPolicyTests, Trades) {
MatlSellPolicy p;
std::string commod("commod");
std::vector<Trade<Material> > trades;
std::vector<std::pair<Trade<Material>, Material::Ptr> > obs;
std::vector<std::pair<Trade<Material>, std::vector<Material::Ptr> > > obs;

Request<Material>* req = Request<Material>::Create(mat1, fac1, commod);
Bid<Material>* bid = Bid<Material>::Create(req, mat, fac1);
Expand All @@ -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;
Expand Down

0 comments on commit 66d44c7

Please sign in to comment.