From ee811609f97d2884536aec7508b4be13ee1a8d3c Mon Sep 17 00:00:00 2001 From: Katie Mummah Date: Sun, 4 Feb 2024 16:17:12 -0700 Subject: [PATCH] address @gonuke comments --- src/toolkit/matl_buy_policy.cc | 18 +++++++++--------- src/toolkit/matl_buy_policy.h | 7 ++++--- src/toolkit/total_inv_tracker.h | 15 +++++++-------- tests/toolkit/matl_buy_policy_tests.cc | 4 ++-- tests/toolkit/total_inv_tracker_tests.cc | 7 ++++--- 5 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/toolkit/matl_buy_policy.cc b/src/toolkit/matl_buy_policy.cc index 00005479c2..7deedb4bc4 100644 --- a/src/toolkit/matl_buy_policy.cc +++ b/src/toolkit/matl_buy_policy.cc @@ -20,11 +20,11 @@ MatlBuyPolicy::MatlBuyPolicy() : quantize_(-1), fill_to_(std::numeric_limits::max()), req_at_(std::numeric_limits::max()), + ccap_(-1), + cycle_total_inv_(0), active_dist_(NULL), dormant_dist_(NULL), - size_dist_(NULL), - ccap_(-1), - cycle_total_inv_(0){ + size_dist_(NULL){ Warn( "MatlBuyPolicy is experimental and its API may be subject to change"); } @@ -118,7 +118,7 @@ void MatlBuyPolicy::init_active_dormant() { next_dormant_end_ = -1; LGH(INFO4) << "dormant length -1, always active" << std::endl; } - else if (ccap_ > 0) { + else if (use_cumulative_capacity()) { next_dormant_end_ = -1; LGH(INFO4) << "dormant length set at -1 for first active period of ccap cycle" << std::endl; } @@ -280,14 +280,14 @@ std::set::Ptr> MatlBuyPolicy::GetMatlRequests() { // finished dormant. starting buying and sample/set length of active period amt = TotalAvailable() * SampleRequestSize(); SetNextActiveTime(); - if (ccap_ < 0) { + if (!use_cumulative_capacity()) { SetNextDormantTime(); LGH(INFO4) << "end of dormant period, next active time end: " << next_active_end_ << ", and next dormant time end: " << next_dormant_end_ << std::endl; } else {next_dormant_end_ = -1;} } - if ( ccap_ != -1 ) { + if (use_cumulative_capacity()) { amt = std::min(amt, ccap_ - cycle_total_inv_); } @@ -329,13 +329,13 @@ void MatlBuyPolicy::AcceptMatlTrades( << it->first.request->commodity() << std::endl; buf_->Push(it->second); // ccap handling - if (ccap_ > 0) { + if (use_cumulative_capacity()) { cycle_total_inv_ += it->second->quantity(); } } // check if cumulative cap has been reached. If yes, then sample for dormant // length and reset cycle_total_inv - if ((ccap_ > 0) && ((ccap_ - cycle_total_inv_) < eps())) { + if (use_cumulative_capacity() && ((ccap_ - cycle_total_inv_) < eps())) { SetNextDormantTime(); LGH(INFO3) << "cycle cumulative inventory has been reached. Dormant period will end at " << next_dormant_end_ << std::endl; cycle_total_inv_ = 0; @@ -348,7 +348,7 @@ void MatlBuyPolicy::SetNextActiveTime() { }; void MatlBuyPolicy::SetNextDormantTime() { - if (ccap_ > 0) { + if (use_cumulative_capacity()) { // need the +1 when not using next_active_end_ next_dormant_end_ = ( dormant_dist_->sample() + manager()->context()->time() + 1); diff --git a/src/toolkit/matl_buy_policy.h b/src/toolkit/matl_buy_policy.h index 50c7e54799..6d520557de 100644 --- a/src/toolkit/matl_buy_policy.h +++ b/src/toolkit/matl_buy_policy.h @@ -164,8 +164,7 @@ class MatlBuyPolicy : public Trader { inline bool MakeReq() const { return buf_tracker_->quantity() <= req_at_; } /// whether trades will be denoted as exclusive or not - inline bool Excl() const { - return quantize_ > 0; } + inline bool Excl() const { return quantize_ > 0; } /// the amount requested per each request @@ -189,6 +188,8 @@ class MatlBuyPolicy : public Trader { return (next_dormant_end_ < 0 || next_active_end_ < 0); }; + inline bool use_cumulative_capacity() {return ccap_ > 0;}; + /// Trader Methods /// @{ virtual std::set::Ptr> GetMatlRequests(); @@ -207,7 +208,7 @@ class MatlBuyPolicy : public Trader { }; void set_manager(Agent* m); - void set_total_inv_tracker(TotalInvTracker* t); + void set_total_inv_tracker(TotalInvTracker* t = NULL); void set_inv_policy(std::string p, double x, double y = std::numeric_limits::max()); /// requires buf_ already set diff --git a/src/toolkit/total_inv_tracker.h b/src/toolkit/total_inv_tracker.h index fdd7736e65..16da9fd7e8 100644 --- a/src/toolkit/total_inv_tracker.h +++ b/src/toolkit/total_inv_tracker.h @@ -67,9 +67,6 @@ class TotalInvTracker { /// @throws ValueError if the tracker has not been initialized (zero) inline double quantity() { int num = num_bufs(); - if (num == 0) { - throw ValueError("TotalInvTracker has not been initialized, no buffers to track"); - } qty_ = 0; for (int i = 0; i < num; i++) { qty_ += bufs_[i]->quantity(); @@ -82,7 +79,7 @@ class TotalInvTracker { /// whichever is lower. inline double capacity() { return std::min(total_capacity_bufs(), max_inv_size_); - }; + } // Returns the sum of the capacities of all buffers. Does not include the // capacity of the tracker @@ -96,17 +93,19 @@ class TotalInvTracker { } /// Returns the total capacity of the traker. Does not include ResBufs - inline double tracker_capacity() { return max_inv_size_;} + inline double tracker_capacity() { return max_inv_size_; } /// Returns the remaining facility-wide space across all tracked ResBufs. - inline double space() {return std::max(0.0, capacity() - quantity());}; + inline double space() { return std::max(0.0, capacity() - quantity()); } /// Returns the remaining space in the given ResBuf, considering the /// facility-wide limitations. - inline double buf_space(ResBuf* buf) {return std::min(buf->space(), space());}; + inline double constrained_buf_space(ResBuf* buf) { + return std::min(buf->space(), space()); + } /// Returns true if there are no resources in any buffer - inline bool empty() {return quantity() == 0;}; + inline bool empty() { return quantity() == 0; } /// Returns number of buffers being tracked /// @throws ValueError if the tracker has not been initialized (zero) diff --git a/tests/toolkit/matl_buy_policy_tests.cc b/tests/toolkit/matl_buy_policy_tests.cc index f105344243..3ae9e521fe 100644 --- a/tests/toolkit/matl_buy_policy_tests.cc +++ b/tests/toolkit/matl_buy_policy_tests.cc @@ -42,7 +42,7 @@ class MatlBuyPolicyTests: public ::testing::Test { }; TEST_F(MatlBuyPolicyTests, Init) { - double cap = 10; + double cap = 5; ResBuf buff; buff.capacity(cap); TotalInvTracker buff_tracker({&buff}); @@ -289,7 +289,7 @@ TEST_F(MatlBuyPolicyTests, TotalInvTracker) { EXPECT_EQ(buf_tracker.quantity(), 5); EXPECT_EQ(buf_tracker.space(), 0); EXPECT_EQ(inbuf.space(), inbuf.capacity() - inbuf.quantity()); - EXPECT_EQ(buf_tracker.buf_space(&inbuf), 0); + EXPECT_EQ(buf_tracker.constrained_buf_space(&inbuf), 0); } TEST_F(MatlBuyPolicyTests, DefaultFixedActiveDormant) { diff --git a/tests/toolkit/total_inv_tracker_tests.cc b/tests/toolkit/total_inv_tracker_tests.cc index d9c1ff1371..222e855963 100644 --- a/tests/toolkit/total_inv_tracker_tests.cc +++ b/tests/toolkit/total_inv_tracker_tests.cc @@ -31,9 +31,10 @@ TEST_F(TotalInvTrackerTest, space) { EXPECT_EQ(multi_tracker_.space(), max_inv_size_ - qty1_ - qty2_); } -TEST_F(TotalInvTrackerTest, buf_space) { - EXPECT_EQ(multi_tracker_.buf_space(&buf1_), buf1_.space()); - EXPECT_EQ(multi_tracker_.buf_space(&buf2_), max_inv_size_ - qty1_ - qty2_); +TEST_F(TotalInvTrackerTest, constrained_buf_space) { + EXPECT_EQ(multi_tracker_.constrained_buf_space(&buf1_), buf1_.space()); + EXPECT_EQ(multi_tracker_.constrained_buf_space(&buf2_), + max_inv_size_ - qty1_ - qty2_); } TEST_F(TotalInvTrackerTest, empty) {