Skip to content
Merged
11 changes: 11 additions & 0 deletions source/sgp_mode/SGPHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,20 @@ class SGPHost : public Host {

void DecPoints(double amt) {
points -= amt;

if (points < 0){
points = 0;
}
}

void AddPoints(double amt) {
points += amt;

if (points < 0){
points = 0;
}


}

size_t GetCountofMatchingSymsToInteractWith(){
Expand Down
16 changes: 12 additions & 4 deletions source/sgp_mode/SGPSymbiont.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,20 @@ class SGPSymbiont : public Symbiont {

}

void AddPoints(double _in) {
points += _in;
}

void DecPoints(double amt) {
points -= amt;

if (points < 0){
points = 0;
}
}

void AddPoints(double amt) {
points += amt;

if (points < 0){
points = 0;
}
}

/**
Expand Down
30 changes: 15 additions & 15 deletions source/sgp_mode/SGPWorldSetup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,15 @@ void SGPWorld::SetupChangingEnvironment() {
else if (task_env.GetTaskSet().HasTask("and_not")) {
andn_task_id = task_env.GetTaskSet().GetID("and_not");
}



size_t orn_task_id = task_env.GetTaskSet().GetSize();
if (task_env.GetTaskSet().HasTask("OR_NOT")) {
orn_task_id = task_env.GetTaskSet().GetID("OR_NOT");
}
else if (task_env.GetTaskSet().HasTask("or_not")) {
orn_task_id = task_env.GetTaskSet().GetID("or_not");
}



// grab task ids for NOT, AND, OR
size_t not_task_id = task_env.GetTaskSet().GetSize();
if (task_env.GetTaskSet().HasTask("NOT")) {
Expand All @@ -155,20 +153,21 @@ void SGPWorld::SetupChangingEnvironment() {
else if (task_env.GetTaskSet().HasTask("or")) {
or_task_id = task_env.GetTaskSet().GetID("or");
}

// update 0 will flip not-and-or to rewarded and nand-andn-orn to punished
GetTaskEnv().GetHostTaskReq(not_task_id).task_value = -1 * GetTaskEnv().GetHostTaskReq(not_task_id).task_value;
GetTaskEnv().GetSymTaskReq(not_task_id).task_value = -1 * GetTaskEnv().GetSymTaskReq(not_task_id).task_value;

GetTaskEnv().GetHostTaskReq(and_task_id).task_value = -1 * GetTaskEnv().GetHostTaskReq(and_task_id).task_value;
GetTaskEnv().GetSymTaskReq(and_task_id).task_value = -1 * GetTaskEnv().GetSymTaskReq(and_task_id).task_value;

GetTaskEnv().GetHostTaskReq(or_task_id).task_value = -1 * GetTaskEnv().GetHostTaskReq(or_task_id).task_value;
GetTaskEnv().GetSymTaskReq(or_task_id).task_value = -1 * GetTaskEnv().GetSymTaskReq(or_task_id).task_value;
// update 0 will flip nand-andn-orn to rewarded and not-and-or to punished
GetTaskEnv().GetHostTaskReq(nand_task_id).task_value = -1 * GetTaskEnv().GetHostTaskReq(nand_task_id).task_value;
GetTaskEnv().GetSymTaskReq(nand_task_id).task_value = -1 * GetTaskEnv().GetSymTaskReq(nand_task_id).task_value;
GetTaskEnv().GetHostTaskReq(andn_task_id).task_value = -1 * GetTaskEnv().GetHostTaskReq(andn_task_id).task_value;
GetTaskEnv().GetSymTaskReq(andn_task_id).task_value = -1 * GetTaskEnv().GetSymTaskReq(andn_task_id).task_value;
GetTaskEnv().GetHostTaskReq(orn_task_id).task_value = -1 * GetTaskEnv().GetHostTaskReq(orn_task_id).task_value;
GetTaskEnv().GetSymTaskReq(orn_task_id).task_value = -1 * GetTaskEnv().GetSymTaskReq(orn_task_id).task_value;

begin_update_sig.AddAction(
[this, nand_task_id, andn_task_id, orn_task_id, not_task_id, and_task_id, or_task_id]() {
if (GetUpdate() % sgp_config.TEMP_CHANGING_ENVIRONMENT_INTERVAL() == 0) {

GetTaskEnv().GetHostTaskReq(nand_task_id).task_value = -1 * GetTaskEnv().GetHostTaskReq(nand_task_id).task_value;
Comment thread
LaurenC267 marked this conversation as resolved.
GetTaskEnv().GetHostTaskReq(andn_task_id).task_value = -1 * GetTaskEnv().GetHostTaskReq(andn_task_id).task_value;
GetTaskEnv().GetHostTaskReq(orn_task_id).task_value = -1 * GetTaskEnv().GetHostTaskReq(orn_task_id).task_value;
Expand All @@ -177,14 +176,15 @@ void SGPWorld::SetupChangingEnvironment() {
GetTaskEnv().GetHostTaskReq(and_task_id).task_value = -1 * GetTaskEnv().GetHostTaskReq(and_task_id).task_value;
GetTaskEnv().GetHostTaskReq(or_task_id).task_value = -1 * GetTaskEnv().GetHostTaskReq(or_task_id).task_value;


GetTaskEnv().GetSymTaskReq(nand_task_id).task_value = -1 * GetTaskEnv().GetSymTaskReq(nand_task_id).task_value;
GetTaskEnv().GetSymTaskReq(andn_task_id).task_value = -1 * GetTaskEnv().GetSymTaskReq(andn_task_id).task_value;
GetTaskEnv().GetSymTaskReq(orn_task_id).task_value = -1 * GetTaskEnv().GetSymTaskReq(orn_task_id).task_value;

GetTaskEnv().GetSymTaskReq(not_task_id).task_value = -1 * GetTaskEnv().GetSymTaskReq(not_task_id).task_value;
GetTaskEnv().GetSymTaskReq(and_task_id).task_value = -1 * GetTaskEnv().GetSymTaskReq(and_task_id).task_value;
GetTaskEnv().GetSymTaskReq(or_task_id).task_value = -1 * GetTaskEnv().GetSymTaskReq(or_task_id).task_value;


}
}
);
Expand Down
58 changes: 30 additions & 28 deletions source/test/sgp_mode_test/functional_tests/SenseTask_Tasks.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,18 @@ TEST_CASE("Test host SenseTask instruction after a rewarded task", "[sgp]"){
host_hw.Reset();
host_hw.SetProgram(host_program);
world.AssignNewEnvIO(host_hw.GetCPUState());

// NOT is currently rewarded.
REQUIRE(world.GetTaskEnv().GetHostTaskReq(not_task_id).task_value > 0);

// NOT is currently not rewarded.
REQUIRE(world.GetTaskEnv().GetHostTaskReq(not_task_id).task_value < 0);
// Initial register values
host_hw.SetRegisters({3, 2, 5});

// Run host program
host_hw.RunCPUStep(4);

THEN("SenseTask puts a 1 into register 1"){
REQUIRE(host_hw.GetRegister(1) == 1);
THEN("SenseTask puts a 0 into register 1"){
Comment thread
anyaevostinar marked this conversation as resolved.
REQUIRE(host_hw.GetRegister(1) == 0);
}
}
}
Expand Down Expand Up @@ -110,22 +110,24 @@ TEST_CASE("Test host SenseTask instruction after a punished task", "[sgp]"){
host_hw.SetProgram(host_program);
world.AssignNewEnvIO(host_hw.GetCPUState());

// NAND is currently punished.
REQUIRE(world.GetTaskEnv().GetHostTaskReq(nand_task_id).task_value < 0);
// NAND is not currently punished.
REQUIRE(world.GetTaskEnv().GetHostTaskReq(nand_task_id).task_value > 0);



// Initial register values
host_hw.SetRegisters({7, 12, 9});

// Run host program
host_hw.RunCPUStep(5);

THEN("SenseTask puts a 0 into register 1"){
REQUIRE(host_hw.GetRegister(1) == 0);
THEN("SenseTask puts a 1 into register 1"){
REQUIRE(host_hw.GetRegister(1) == 1);
}
}
}

TEST_CASE("Test symbiont SenseTask instruction after a rewarded task", "[sgp]"){
TEST_CASE("Test symbiont SenseTask instruction after a punished task", "[sgp]"){
sgpmode::SymConfigSGP config;
config.CYCLES_PER_UPDATE(0);
config.SEED(61);
Expand All @@ -149,7 +151,7 @@ TEST_CASE("Test symbiont SenseTask instruction after a rewarded task", "[sgp]"){
// setup correct reward/punishment values for update 0
world.Update();

WHEN("A symbiont runs a task which is rewarded and then the SenseTask instruction"){
WHEN("A symbiont runs a task which is punished and then the SenseTask instruction"){
program_t sym_program;
prog_builder.AddStartAnchor(sym_program);
prog_builder.AddInst(sym_program, "IO", 0);
Expand All @@ -159,22 +161,22 @@ TEST_CASE("Test symbiont SenseTask instruction after a rewarded task", "[sgp]"){
sym_hw.SetProgram(sym_program);
world.AssignNewEnvIO(sym_hw.GetCPUState());

// NOT is currently rewarded.
REQUIRE(world.GetTaskEnv().GetHostTaskReq(not_task_id).task_value > 0);

// NOT is currently punished.
REQUIRE(world.GetTaskEnv().GetHostTaskReq(not_task_id).task_value < 0);
// Initial register values
sym_hw.SetRegisters({3, 2, 5});

// Run symbiont program
sym_hw.RunCPUStep(4);

THEN("SenseTask puts a 1 into register 1"){
REQUIRE(sym_hw.GetRegister(1) == 1);
sym_hw.RunCPUStep(4);
THEN("SenseTask puts a 0 into register 1"){
REQUIRE(sym_hw.GetRegister(1) == 0);
}
}
}

TEST_CASE("Test symbiont SenseTask instruction after a punished task", "[sgp]"){
TEST_CASE("Test symbiont SenseTask instruction after a rewarded task", "[sgp]"){
sgpmode::SymConfigSGP config;
config.CYCLES_PER_UPDATE(0);
config.SEED(61);
Expand All @@ -198,7 +200,7 @@ TEST_CASE("Test symbiont SenseTask instruction after a punished task", "[sgp]"){
// setup correct reward/punishment values for update 0
world.Update();

WHEN("A symbiont runs a task which is punished and then the SenseTask instruction") {
WHEN("A symbiont runs a task which is rewarded and then the SenseTask instruction") {
program_t sym_program;
prog_builder.AddStartAnchor(sym_program);
prog_builder.AddInst(sym_program, "IO", 0);
Expand All @@ -209,17 +211,17 @@ TEST_CASE("Test symbiont SenseTask instruction after a punished task", "[sgp]"){
sym_hw.SetProgram(sym_program);
world.AssignNewEnvIO(sym_hw.GetCPUState());

// NAND is currently punished.
REQUIRE(world.GetTaskEnv().GetHostTaskReq(nand_task_id).task_value < 0);

// NAND is currently rewarded.
REQUIRE(world.GetTaskEnv().GetHostTaskReq(nand_task_id).task_value > 0);
// Initial register values
sym_hw.SetRegisters({7, 12, 9});

// run symbiont program
sym_hw.RunCPUStep(5);

THEN("SenseTask puts a 0 into register 1"){
REQUIRE(sym_hw.GetRegister(1) == 0);
THEN("SenseTask puts a 1 into register 1"){
REQUIRE(sym_hw.GetRegister(1) == 1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,18 @@ TEST_CASE("Hosts start with a punished task in a temporally changing environment
world.Update();
size_t host_orn_count = world.GetHostTaskSuccesses().at(or_not_task_id);

THEN("The host initially loses points for completing its task") {
THEN("The host initially gains points for completing its task") {
REQUIRE(host_orn_count == 1);
REQUIRE(host_orn_only->GetPoints() == -5);
REQUIRE(host_orn_only->GetPoints() == 5);
}

// event update
world.Update();
host_orn_count += world.GetHostTaskSuccesses().at(or_not_task_id);

THEN("After the environment changes, the host gains points for completing its task") {
THEN("After the environment changes, the host loses points for completing its task") {
REQUIRE(host_orn_count == 3);
REQUIRE(host_orn_only->GetPoints() == 5);
REQUIRE(host_orn_only->GetPoints() == 0);
}
}
}
Expand Down Expand Up @@ -231,18 +231,18 @@ TEST_CASE("Symbionts start with a punished task in a temporally changing environ
world.Update();
size_t sym_nand_count = world.GetSymTaskSuccesses().at(nand_task_id);

THEN("The symbiont initially loses points for completing its task") {
THEN("The symbiont initially gains points for completing its task") {
REQUIRE(sym_nand_count == 1);
REQUIRE(symbiont_nand_only->GetPoints() == -5);
REQUIRE(symbiont_nand_only->GetPoints() == 5);
}

// event update
world.Update();
sym_nand_count += world.GetSymTaskSuccesses().at(nand_task_id);

THEN("After the environment changes, the symbiont gains points for completing its tasks") {
THEN("After the environment changes, the symbiont loses points for completing its tasks") {
REQUIRE(sym_nand_count == 3);
REQUIRE(symbiont_nand_only->GetPoints() == 5);
REQUIRE(symbiont_nand_only->GetPoints() == 0);
}
}
}
23 changes: 23 additions & 0 deletions source/test/sgp_mode_test/unit_tests/SGPHost.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,27 @@ TEST_CASE("ProcessOutputBuffer", "[sgp][sgp-unit]") {
}
}
}
}


TEST_CASE("debt", "[sgp][sgp-unit]"){

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Longer phrase please

using world_t = sgpmode::SGPWorld;
using cpu_state_t = sgpmode::CPUState<world_t>;
using hw_spec_t = sgpmode::SGPHardwareSpec<sgpmode::Library, cpu_state_t, world_t>;
using sgp_host_t = sgpmode::SGPHost<hw_spec_t>;
GIVEN("A zero point value"){

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"A host starting with zero points

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"that can do Not and world punishes not

emp::Random random(31);
sgpmode::SymConfigSGP config;
config.TASK_ENV_CFG_PATH("source/test/sgp_mode_test/hardware-test-env.json");
world_t world(random, &config);
auto& prog_builder = world.GetProgramBuilder();
emp::Ptr<sgp_host_t> host = emp::NewPtr<sgp_host_t>(&random, &world, &config, prog_builder.CreateNotProgram(100));

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CreateReproProgram instead


WHEN("points are added to make total points negative"){
host->AddPoints(-100);
THEN("point value should be set to zero"){
REQUIRE(host->GetPoints() == 0);
}
}
}
}
Loading