Skip to content

Commit

Permalink
Merge branch 'main' into coveralls
Browse files Browse the repository at this point in the history
Signed-off-by: bennibbelink <[email protected]>
  • Loading branch information
bennibbelink authored Nov 29, 2023
2 parents c54ed5c + 940062a commit a9f7029
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ on:
- '.github/workflows/changelog_test.yml'
- 'docker/**'
- 'doc/**'
- 'CHANGELOG.rst'
push:
paths-ignore:
- '.github/workflows/build_test_publish.yml'
- '.github/workflows/changelog_test.yml'
- 'docker/**'
- 'doc/**'
- 'CHANGELOG.rst'

jobs:
build-and-test:
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/build_test_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ on:
push:
branches:
- main
paths:
- '.github/workflows/build_test_publish.yml'
- 'docker/**'

jobs:
build-dependency-and-test-img:
Expand Down
5 changes: 4 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ Since last release
will fail unless update-alternatives has been used to point python at the
correct python3 version (#1558)
* build and test are now fown on githubAction in place or CircleCI (#1569)
* Have separate workflows for testing, publishing dependency images, and publishing release images (#1597, #1602, #1606, #1609)
* Have separate workflows for testing, publishing dependency images, and publishing release images (#1597, #1602, #1606, #1609, #1629)
* Add Ubuntu 20.04 to the list of supported platforms (#1605, #1608)
* Add random number generator (Mersenne Twister 19937, from boost) and the ability to set the seed in the simulation control block (#1599)
* Added code coverage reporting to GitHub workflows (#1616)
* Adds active and dormant buying cycles in buy policy (#1596)
* Add random number generator (Mersenne Twister 19937, from boost) and the ability to set the seed in the simulation control block (#1599)

**Changed:**

* Moved to unified CHANGELOG Entry and check them with GithubAction (#1571)
* Major update and modernization of build (#1587)
* Changed Json formatting for compatibility with current python standards (#1587)
* Changed README.rst installation instructions, tested on fresh Ubuntu-22.04 system with Python 3.11 (#1617)

**Removed:**

Expand Down
11 changes: 7 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,13 @@ To install Cyclus and its dependencies onto a clean Ubuntu machine (tested on 18

- ``conda config --add channels conda-forge``

- ``conda install -y openssh gxx_linux-64 gcc_linux-64 cmake make docker-pycreds git xo
python-json-logger python=3.6 glibmm glib=2.56 libxml2 libxmlpp libblas libcblas
liblapack pkg-config coincbc=2.9 boost-cpp hdf5 sqlite pcre gettext bzip2 xz
setuptools nose pytables pandas jinja2 cython==0.26 websockets pprintpp``
- ``conda create -n cyclus python=3.11``

- ``conda activate cyclus``

- ``conda install -y openssh gxx_linux-64 gcc_linux-64 cmake make docker-pycreds git xo python-json-logger glib glibmm libxml2 libxmlpp libxmlpp-4.0 libblas libcblas liblapack pkg-config coincbc boost-cpp hdf5 sqlite pcre gettext bzip2 xz setuptools pytest pytables pandas jinja2 "cython<3" websockets pprintpp``

- ``conda install -y --force-reinstall libsqlite``

- Use ``sudo apt install`` to install and configure git

Expand Down
33 changes: 30 additions & 3 deletions src/toolkit/matl_buy_policy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ MatlBuyPolicy::MatlBuyPolicy() :
throughput_(std::numeric_limits<double>::max()),
quantize_(-1),
fill_to_(1),
req_when_under_(1) {
req_when_under_(1),
active_(1),
dormant_(0){
Warn<EXPERIMENTAL_WARNING>(
"MatlBuyPolicy is experimental and its API may be subject to change");
}
Expand Down Expand Up @@ -53,6 +55,16 @@ void MatlBuyPolicy::set_throughput(double x) {
throughput_ = x;
}

void MatlBuyPolicy::set_active(int x) {
assert(x > 0);
active_ = x;
}

void MatlBuyPolicy::set_dormant(int x) {
assert(x >= 0);
dormant_ = x;
}

MatlBuyPolicy& MatlBuyPolicy::Init(Agent* manager, ResBuf<Material>* buf,
std::string name) {
Trader::manager_ = manager;
Expand All @@ -62,11 +74,15 @@ MatlBuyPolicy& MatlBuyPolicy::Init(Agent* manager, ResBuf<Material>* buf,
}

MatlBuyPolicy& MatlBuyPolicy::Init(Agent* manager, ResBuf<Material>* buf,
std::string name, double throughput) {
std::string name, double throughput, int active, int dormant) {
Trader::manager_ = manager;
buf_ = buf;
name_ = name;
set_throughput(throughput);
set_active(active);
set_dormant(dormant);
LGH(INFO3) << "has buy policy with active = " << active_ \
<< "time steps and dormant = " << dormant_ << " time steps." ;
return *this;
}

Expand Down Expand Up @@ -136,7 +152,18 @@ std::set<RequestPortfolio<Material>::Ptr> MatlBuyPolicy::GetMatlRequests() {
rsrc_commods_.clear();
std::set<RequestPortfolio<Material>::Ptr> ports;
bool make_req = buf_->quantity() < req_when_under_ * buf_->capacity();
double amt = TotalQty();
double amt;

if (dormant_ > 0 && manager()->context()->time() % (active_ + dormant_) < active_) {
amt = TotalQty();
}
else if (dormant_ == 0)
amt = TotalQty();
else {
// in dormant part of cycle, return empty portfolio
amt = 0;
LGH(INFO3) << "in dormant period, no request";
}
if (!make_req || amt < eps())
return ports;

Expand Down
19 changes: 17 additions & 2 deletions src/toolkit/matl_buy_policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,22 @@ class MatlBuyPolicy : public Trader {
/// be sent to fill the buffer's empty space.
/// @warning, (s, S) policy values are ambiguous for buffers with a capacity
/// in (0, 1]. However that is a rare case.
/// The following features, if used, will set the policy to so that agents
/// cycle through "on" phases, where the buf will attemp to be filled, and
/// "off" or dormant phases, where no requests will be made regardless of
/// capacity. The "on" and "off" phases are sampled and rounded to the
/// nearest integer number of time steps from a truncated normal
/// distribution from a mean, standard deviation, min, and max value.
/// Note that the (s, S) policy is not currently compatible with active and
/// dormant buying perionds
/// @param active the length of the on, actively buying period
/// @param dormant the length of the dormant period
/// Note that active and dormant periods are note currently compatible with
/// (s, S) inventory management
/// @{
MatlBuyPolicy& Init(Agent* manager, ResBuf<Material>* buf, std::string name);
MatlBuyPolicy& Init(Agent* manager, ResBuf<Material>* buf, std::string name,
double throughput);
double throughput, int active = 1, int dormant = 0);
MatlBuyPolicy& Init(Agent* manager, ResBuf<Material>* buf, std::string name,
double fill_to, double req_when_under);
MatlBuyPolicy& Init(Agent* manager, ResBuf<Material>* buf, std::string name,
Expand Down Expand Up @@ -161,14 +173,17 @@ class MatlBuyPolicy : public Trader {
void set_req_when_under(double x);
void set_quantize(double x);
void set_throughput(double x);
void set_active(int x);
void set_dormant(int x);

ResBuf<Material>* buf_;
std::string name_;
double fill_to_, req_when_under_, quantize_, throughput_;
int active_, dormant_;
std::map<Material::Ptr, std::string> rsrc_commods_;
std::map<std::string, CommodDetail> commod_details_;
};

} // namespace toolkit
} // namespace cyclus

Expand Down
128 changes: 124 additions & 4 deletions tests/toolkit/matl_buy_policy_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "test_context.h"
#include "test_agents/test_facility.h"
#include "cyclus.h"

namespace cyclus {
namespace toolkit {
Expand All @@ -24,6 +25,9 @@ class MatlBuyPolicyTests: public ::testing::Test {
protected:
TestContext tc;
TestFacility* fac1;
cyclus::Timer ti;
cyclus::Recorder rec;
cyclus::Context* ctx;

virtual void SetUp() {
fac1 = new TestFacility(tc.get());
Expand Down Expand Up @@ -88,7 +92,25 @@ TEST_F(MatlBuyPolicyTests, StartStop) {
ASSERT_THROW(p.Stop(), ValueError);
}

TEST_F(MatlBuyPolicyTests, Reqs) {
// Tests that matlbuypolicy sends out a request properly
TEST_F(MatlBuyPolicyTests, OneReq) {
double cap = 5;
ResBuf<Material> buff;
buff.capacity(cap);
cyclus::Composition::Ptr c1 = cyclus::Composition::Ptr(new TestComp());
MatlBuyPolicy p;

p.Init(fac1, &buff, "").Set("commod1", c1);
std::set<RequestPortfolio<Material>::Ptr> obs = p.GetMatlRequests();
ASSERT_EQ(obs.size(), 1);
ASSERT_EQ((*obs.begin())->requests().size(), 1);
Request<Material>* req = (*obs.begin())->requests().at(0);
ASSERT_EQ(req->commodity(), "commod1");
ASSERT_FLOAT_EQ(req->target()->quantity(), cap);
}

// Tests that matlbuypolicy can send out requests for multiple commodities
TEST_F(MatlBuyPolicyTests, MultipleReqs) {
double cap = 5;
ResBuf<Material> buff;
buff.capacity(cap);
Expand All @@ -113,18 +135,116 @@ TEST_F(MatlBuyPolicyTests, Reqs) {
}
ASSERT_FALSE(req->exclusive());
ASSERT_FLOAT_EQ(req->target()->quantity(), cap);
}

// Test single quantized request
TEST_F(MatlBuyPolicyTests, Quantize) {
double cap = 5;
ResBuf<Material> buff;
buff.capacity(cap);
double p2 = 2.5;
cyclus::Composition::Ptr c1 = cyclus::Composition::Ptr(new TestComp());
MatlBuyPolicy p;

double quantize = 2.5;
p.Init(fac1, &buff, "", std::numeric_limits<double>::max(), 1, 1, quantize).Set("commod1", c1);
std::set<RequestPortfolio<Material>::Ptr> obs = p.GetMatlRequests();
ASSERT_EQ(obs.size(), 2);
ASSERT_EQ((*obs.begin())->requests().size(), 1);
Request<Material>* req = (*obs.begin())->requests().at(0);
ASSERT_TRUE(req->exclusive());
ASSERT_FLOAT_EQ(req->target()->quantity(), quantize);
}

// Test two quantized requests
TEST_F(MatlBuyPolicyTests, MultiReqQuantize) {
double cap = 5;
ResBuf<Material> buff;
buff.capacity(cap);
cyclus::Composition::Ptr c1 = cyclus::Composition::Ptr(new TestComp());
cyclus::Composition::Ptr c2 = cyclus::Composition::Ptr(new TestComp());
MatlBuyPolicy p;

// two portfolios with quantize
double quantize = 2.5;
p.Init(fac1, &buff, "", std::numeric_limits<double>::max(), 1, 1, quantize);
obs = p.GetMatlRequests();
p.Init(fac1, &buff, "", std::numeric_limits<double>::max(), 1, 1, quantize).Set("commod1", c1).Set("commod2", c2);
std::set<RequestPortfolio<Material>::Ptr> obs = p.GetMatlRequests();
ASSERT_EQ(obs.size(), 2);
ASSERT_EQ((*obs.begin())->requests().size(), 2);
ASSERT_EQ((*(obs.begin()++))->requests().size(), 2);
req = (*obs.begin())->requests().at(0);
Request<Material>* req = (*obs.begin())->requests().at(0);
ASSERT_TRUE(req->exclusive());
ASSERT_FLOAT_EQ(req->target()->quantity(), quantize);

req = (*(obs.begin()++))->requests().at(0);
ASSERT_TRUE(req->exclusive());
ASSERT_FLOAT_EQ(req->target()->quantity(), quantize);
}

// Tests that agent cycles between active and dormant cycles during a mock
// sim, only buying when active.
TEST_F(MatlBuyPolicyTests, ActiveDormant) {
int active = 2;
int dormant = 3;
int dur = 4;
double throughput = 1;

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, active, dormant)
.Set("commod1").Start();

EXPECT_NO_THROW(sim.Run());
EXPECT_DOUBLE_EQ(2, inbuf.quantity());

delete a;
}

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

int active = 2;
int dormant = 3;
int dur = 16;
double throughput = 1;

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, active, dormant)
.Set("commod1").Start();

EXPECT_NO_THROW(sim.Run());

QueryResult qr = sim.db().Query("Transactions", NULL);
// confirm that transactions are only occurring during active periods
int first_cycle = qr.GetVal<int>("Time", 0);
EXPECT_EQ(0, first_cycle);
int second_cycle = qr.GetVal<int>("Time", 2);
EXPECT_EQ(5, second_cycle);
int third_cycle = qr.GetVal<int>("Time", 5);
EXPECT_EQ(11, third_cycle);

// confirm total inbuf recieved material
EXPECT_DOUBLE_EQ(7, inbuf.quantity());

delete a;
}

}
}

0 comments on commit a9f7029

Please sign in to comment.