Skip to content

Commit

Permalink
buy pol randomness in request size
Browse files Browse the repository at this point in the history
  • Loading branch information
nuclearkatie committed Dec 19, 2023
1 parent 402a592 commit b1483a2
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 17 deletions.
21 changes: 14 additions & 7 deletions src/random_number_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,36 +68,43 @@ namespace cyclus{
return UniformDoubleDist::dist(RandomNumberGenerator::gen_);
}

double UniformDoubleDist::max() {
return UniformDoubleDist::dist.max();
}

NormalDoubleDist::NormalDoubleDist(double mean, double std_dev, double min, double max) : dist(mean, std_dev) {
NormalDoubleDist::min = min;
NormalDoubleDist::max = max;
NormalDoubleDist::min_ = min;
NormalDoubleDist::max_ = max;
}

double NormalDoubleDist::sample() {
double val = NormalDoubleDist::dist(RandomNumberGenerator::gen_);
while (val < NormalDoubleDist::min || val > NormalDoubleDist::max){
while (val < NormalDoubleDist::min_ || val > NormalDoubleDist::max_){
val = NormalDoubleDist::dist(RandomNumberGenerator::gen_);
}
return val;
}

double NormalDoubleDist::max() {
return NormalDoubleDist::dist.max();
}

UniformIntDist::UniformIntDist(int min, int max) : dist(min, max) { }

int UniformIntDist::sample() {
return UniformIntDist::dist(RandomNumberGenerator::gen_);
}

NormalIntDist::NormalIntDist(double mean, double std_dev, int min, int max) : dist(mean, std_dev) {
NormalIntDist::min = std::min(min, 0);
NormalIntDist::max = max;
NormalIntDist::min_ = std::min(min, 0);
NormalIntDist::max_ = max;
}

int NormalIntDist::sample() {
double val = NormalIntDist::dist(RandomNumberGenerator::gen_);
while (val < NormalIntDist::min || val > NormalIntDist::max){
while (val < NormalIntDist::min_ || val > NormalIntDist::max_){
val = NormalIntDist::dist(RandomNumberGenerator::gen_);
}
return std::lrint(val);
}

}
12 changes: 8 additions & 4 deletions src/random_number_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class RandomNumberGenerator {
class DoubleDistribution {
public:
virtual double sample() = 0;
virtual double max() = 0;
};

class FixedDoubleDist : public DoubleDistribution {
Expand All @@ -95,6 +96,7 @@ class FixedDoubleDist : public DoubleDistribution {
public:
FixedDoubleDist(double value_) : value(value_) {};
virtual double sample() { return value; };
virtual double max() { return value; };
};

class UniformDoubleDist : public DoubleDistribution {
Expand All @@ -103,16 +105,18 @@ class UniformDoubleDist : public DoubleDistribution {
public:
UniformDoubleDist(double min = 0, double max=1);
virtual double sample();
virtual double max();
};

class NormalDoubleDist : public DoubleDistribution {
private:
boost::random::normal_distribution<> dist;
double min;
double max;
double min_;
double max_;
public:
NormalDoubleDist(double mean, double std_dev, double min=0, double max=std::numeric_limits<double>::max());
virtual double sample();
virtual double max();
};

class IntDistribution {
Expand All @@ -139,8 +143,8 @@ class UniformIntDist : public IntDistribution {
class NormalIntDist : public IntDistribution {
private:
boost::random::normal_distribution<> dist;
int min;
int max;
int min_;
int max_;
public:
NormalIntDist(double mean, double std_dev, int min=0, int max=std::numeric_limits<int>::max());
virtual int sample();
Expand Down
16 changes: 13 additions & 3 deletions src/toolkit/matl_buy_policy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ std::set<RequestPortfolio<Material>::Ptr> MatlBuyPolicy::GetMatlRequests() {

if (current_time_ < next_active_end_ || next_dormant_end_ < 0) {
// currently in the middle of active buying period
SetRequestSize();
amt = TotalQty();
}
else if (current_time_ < next_dormant_end_) {
Expand All @@ -180,12 +181,12 @@ std::set<RequestPortfolio<Material>::Ptr> MatlBuyPolicy::GetMatlRequests() {
// finished active. starting dormancy and sample/set length of dormant period
amt = 0;
SetNextDormantTime();

}
// the following is an if rather than if-else statement because if dormant
// length is zero, buy policy should return to active immediately
if (current_time_ == next_dormant_end_) {
// finished dormant. starting buying and sample/set length of active period
SetRequestSize();
amt = TotalQty();
SetNextActiveTime();
}
Expand All @@ -194,8 +195,8 @@ std::set<RequestPortfolio<Material>::Ptr> MatlBuyPolicy::GetMatlRequests() {
return ports;

bool excl = Excl();
double req_amt = ReqQty();
int n_req = NReq();
double req_amt = Excl() ? quantize_ : amt;
int n_req = Excl() ? static_cast<int>(amt / quantize_) : 1;
LGH(INFO3) << "requesting " << amt << " kg via " << n_req << " request(s)" << std::endl;

// one portfolio for each request
Expand Down Expand Up @@ -243,5 +244,14 @@ void MatlBuyPolicy::SetNextDormantTime() {
return;
}

void MatlBuyPolicy::SetRequestSize() {
sample_fraction_ = size_dist_->sample();
if (sample_fraction_ > 1) {
sample_fraction_ = sample_fraction_ / size_dist_->max();
}
return;
}


} // namespace toolkit
} // namespace cyclus
10 changes: 7 additions & 3 deletions src/toolkit/matl_buy_policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,18 @@ class MatlBuyPolicy : public Trader {
/// is idempotent.
void Stop();

double sample_fraction_ = 1.0;

/// the total amount requested
inline double TotalQty() const {
return std::min(throughput_,
fill_to_ * buf_->capacity() - buf_->quantity());
return std::min((throughput_* sample_fraction_),
((fill_to_ * buf_->capacity()) - buf_->quantity()) * sample_fraction_);
}

/// whether trades will be denoted as exclusive or not
inline bool Excl() const { return quantize_ > 0; }


/// the amount requested per each request
inline double ReqQty() const {
return Excl() ? quantize_ : TotalQty();
Expand All @@ -172,10 +175,11 @@ class MatlBuyPolicy : public Trader {
virtual std::set<RequestPortfolio<Material>::Ptr> GetMatlRequests();
virtual void AcceptMatlTrades(
const std::vector<std::pair<Trade<Material>, Material::Ptr> >& resps);
/// }@

void SetNextActiveTime();
void SetNextDormantTime();
/// }@
void SetRequestSize();

private:
struct CommodDetail {
Expand Down
100 changes: 100 additions & 0 deletions tests/toolkit/matl_buy_policy_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -405,5 +405,105 @@ TEST_F(MatlBuyPolicyTests, MixedActiveDormant) {
delete a;
}

TEST_F(MatlBuyPolicyTests, RandomSizeUniform) {
using cyclus::QueryResult;

FixedIntDist a_dist = FixedIntDist(1);
FixedIntDist d_dist = FixedIntDist(-1);
UniformDoubleDist size_dist = UniformDoubleDist(0.5, 1.0);

int dur = 2;
double throughput = 10;

cyclus::MockSim sim(dur);
cyclus::Agent* a = new TestFacility(sim.context());
sim.context()->AddPrototype(a->prototype(), a);
sim.agent = sim.context()->CreateAgent<cyclus::Agent>(a->prototype());
sim.AddSource("commod1").Finalize();

TestFacility* fac = dynamic_cast<TestFacility*>(sim.agent);

cyclus::toolkit::ResBuf<cyclus::Material> inbuf;
cyclus::toolkit::MatlBuyPolicy policy;
policy.Init(fac, &inbuf, "inbuf", throughput, &a_dist, &d_dist, &size_dist)
.Set("commod1").Start();

EXPECT_NO_THROW(sim.Run());

QueryResult qr = sim.db().Query("Resources", NULL);
EXPECT_NEAR(6.59845, qr.GetVal<double>("Quantity", 0), 0.00001);
EXPECT_NEAR(9.70636, qr.GetVal<double>("Quantity", 1), 0.00001);

delete a;
}

TEST_F(MatlBuyPolicyTests, RandomSizeNormal) {
using cyclus::QueryResult;

FixedIntDist a_dist = FixedIntDist(1);
FixedIntDist d_dist = FixedIntDist(-1);
NormalDoubleDist size_dist = NormalDoubleDist(0.5, 0.1);

int dur = 2;
double throughput = 10;

cyclus::MockSim sim(dur);
cyclus::Agent* a = new TestFacility(sim.context());
sim.context()->AddPrototype(a->prototype(), a);
sim.agent = sim.context()->CreateAgent<cyclus::Agent>(a->prototype());
sim.AddSource("commod1").Finalize();

TestFacility* fac = dynamic_cast<TestFacility*>(sim.agent);

cyclus::toolkit::ResBuf<cyclus::Material> inbuf;
cyclus::toolkit::MatlBuyPolicy policy;
policy.Init(fac, &inbuf, "inbuf", throughput, &a_dist, &d_dist, &size_dist)
.Set("commod1").Start();

EXPECT_NO_THROW(sim.Run());

QueryResult qr = sim.db().Query("Resources", NULL);
EXPECT_NEAR(6.40838, qr.GetVal<double>("Quantity", 0), 0.00001);
EXPECT_NEAR(3.26489, qr.GetVal<double>("Quantity", 1), 0.00001);

delete a;
}

TEST_F(MatlBuyPolicyTests, RandomSizeAndFrequency) {
using cyclus::QueryResult;

UniformIntDist a_dist = UniformIntDist(1, 2);
UniformIntDist d_dist = UniformIntDist(1, 2);
NormalDoubleDist size_dist = NormalDoubleDist(0.5, 0.25);

int dur = 8;
double throughput = 10;

cyclus::MockSim sim(dur);
cyclus::Agent* a = new TestFacility(sim.context());
sim.context()->AddPrototype(a->prototype(), a);
sim.agent = sim.context()->CreateAgent<cyclus::Agent>(a->prototype());
sim.AddSource("commod1").Finalize();

TestFacility* fac = dynamic_cast<TestFacility*>(sim.agent);

cyclus::toolkit::ResBuf<cyclus::Material> inbuf;
cyclus::toolkit::MatlBuyPolicy policy;
policy.Init(fac, &inbuf, "inbuf", throughput, &a_dist, &d_dist, &size_dist)
.Set("commod1").Start();

EXPECT_NO_THROW(sim.Run());

QueryResult qr = sim.db().Query("Resources", NULL);
EXPECT_NEAR(0.66224, qr.GetVal<double>("Quantity", 0), 0.00001);
EXPECT_NEAR(4.02323, qr.GetVal<double>("Quantity", 1), 0.00001);

QueryResult qr2 = sim.db().Query("Transactions", NULL);
EXPECT_EQ(0, qr2.GetVal<int>("Time", 0));
EXPECT_EQ(4, qr2.GetVal<int>("Time", 2));

delete a;
}

}
}

0 comments on commit b1483a2

Please sign in to comment.