Skip to content

Commit

Permalink
address @gonuke comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nuclearkatie committed Feb 4, 2024
1 parent b7b4707 commit ee81160
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 25 deletions.
18 changes: 9 additions & 9 deletions src/toolkit/matl_buy_policy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ MatlBuyPolicy::MatlBuyPolicy() :
quantize_(-1),
fill_to_(std::numeric_limits<double>::max()),
req_at_(std::numeric_limits<double>::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<EXPERIMENTAL_WARNING>(
"MatlBuyPolicy is experimental and its API may be subject to change");
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -280,14 +280,14 @@ std::set<RequestPortfolio<Material>::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_);
}

Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions src/toolkit/matl_buy_policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<RequestPortfolio<Material>::Ptr> GetMatlRequests();
Expand All @@ -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<double>::max());
/// requires buf_ already set
Expand Down
15 changes: 7 additions & 8 deletions src/toolkit/total_inv_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -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<Material>* buf) {return std::min(buf->space(), space());};
inline double constrained_buf_space(ResBuf<Material>* 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)
Expand Down
4 changes: 2 additions & 2 deletions tests/toolkit/matl_buy_policy_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class MatlBuyPolicyTests: public ::testing::Test {
};

TEST_F(MatlBuyPolicyTests, Init) {
double cap = 10;
double cap = 5;
ResBuf<Material> buff;
buff.capacity(cap);
TotalInvTracker buff_tracker({&buff});
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions tests/toolkit/total_inv_tracker_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit ee81160

Please sign in to comment.