Skip to content

Commit

Permalink
fixed conflict in sgpworld setup
Browse files Browse the repository at this point in the history
  • Loading branch information
anyaevostinar committed Sep 26, 2024
2 parents 103db2b + cffa758 commit c43d775
Show file tree
Hide file tree
Showing 12 changed files with 19 additions and 186 deletions.
2 changes: 1 addition & 1 deletion source/ConfigSetup.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ EMP_BUILD_CONFIG(SymConfigBase,
VALUE(CYCLES_PER_UPDATE, size_t, 4, "Number of CPU cycles that organisms run every update"),
VALUE(THREAD_COUNT, size_t, 12, "Number of threads used to process organisms in parallel"),
VALUE(RANDOM_ANCESTOR, bool, false, "Randomize ancestor genomes instead of using the blank genome with just NOT and reproduction"),
VALUE(TASK_TYPE, bool, 0, "0 for squaring tasks, 1 for logic tasks"),
VALUE(TASK_TYPE, bool, 1, "If random ancestor off, 1 for NOT + repro starting genome, 0 for repro starting program"),
VALUE(DONATION_STEAL_INST, bool, 1, "1 if you want donate and steal instructions in the instruction set, 0 if not"),
VALUE(RANDOM_IO_INPUT, bool, true, "1 to give organisms random input when they IO, 0 to give them only ones"),

Expand Down
6 changes: 0 additions & 6 deletions source/catch/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,9 @@
#include "../test/pgg_mode_test/PGGDataNodes.test.cc"
#include "../test/pgg_mode_test/PGGWorld.test.cc"

#include "../test/sgp_mode_test/SGPModularity.test.cc"
#include "../test/sgp_mode_test/CPU.test.cc"
#include "../test/sgp_mode_test/AnalysisTools.test.cc"
#include "../test/sgp_mode_test/GenomeLibrary.test.cc"
#include "../test/sgp_mode_test/SymbiontImpact.test.cc"
#include "../test/sgp_mode_test/SquareFrequencyMap.test.cc"
#include "../test/sgp_mode_test/SGPDiversity.test.cc"
#include "../test/sgp_mode_test/SGPWorld.test.cc"
#include "../test/sgp_mode_test/SGPHost.test.cc"

#include "../test/integration_test/spatial_structure/vt.test.cc"
#include "../test/integration_test/lysogeny/plr.test.cc"
Expand Down
5 changes: 0 additions & 5 deletions source/native/symbulation_parasite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ int symbulation_main(int argc, char *argv[]) {
emp::Random random(config.SEED());

TaskSet task_set = LogicTasks;
if (config.TASK_TYPE() == 0) {
task_set = SquareTasks;
} else if (config.TASK_TYPE() == 1) {
task_set = LogicTasks;
}

SGPWorld world(random, &config, task_set);

Expand Down
6 changes: 3 additions & 3 deletions source/sgp_mode/GenomeLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ using Library = sgpl::OpLibrary<
// biological operations
// no copy or alloc
inst::Reproduce,
inst::PrivateIO,
//inst::SharedIO,
//inst::PrivateIO,
inst::SharedIO,
// double argument math
inst::Add, inst::Subtract, inst::Nand,
// Stack manipulation
Expand Down Expand Up @@ -308,7 +308,7 @@ sgpl::Program<Spec> CreateStartProgram(emp::Ptr<SymConfigBase> config) {
if (config->RANDOM_ANCESTOR()) {
return CreateRandomProgram(PROGRAM_LENGTH);
} else if (config->TASK_TYPE() == 1) {
return CreatePrivateNotProgram(PROGRAM_LENGTH);
return CreateNotProgram(PROGRAM_LENGTH);
} else {
return CreateReproProgram(PROGRAM_LENGTH);
}
Expand Down
48 changes: 3 additions & 45 deletions source/sgp_mode/SGPDataNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,9 @@ void SGPWorld::CreateDataFiles() {
SetupSymDonatedFile(my_config->FILE_PATH() + "SymDonated" +
my_config->FILE_NAME() + file_ending)
.SetTimingRepeat(my_config->DATA_INT());
if (my_config->TASK_TYPE() == 1) {
SetupTasksFile(my_config->FILE_PATH() + "Tasks" + my_config->FILE_NAME() +
file_ending)
.SetTimingRepeat(my_config->DATA_INT());
} else if (my_config->TASK_TYPE() == 0) {
SetupHostSquareFrequencyFile(my_config->FILE_PATH() + "Host_Square" +
my_config->FILE_NAME() + file_ending)
.SetTimingRepeat(my_config->DATA_INT());
SetupSymSquareFrequencyFile(my_config->FILE_PATH() + "Sym_Square" +
my_config->FILE_NAME() + file_ending)
.SetTimingRepeat(my_config->DATA_INT());
}
SetupTasksFile(my_config->FILE_PATH() + "Tasks" + my_config->FILE_NAME() +
file_ending)
.SetTimingRepeat(my_config->DATA_INT());
}


Expand Down Expand Up @@ -85,39 +76,6 @@ emp::DataFile &SGPWorld::SetupTasksFile(const std::string &filename) {
return file;
}

emp::DataFile &
SGPWorld::SetupHostSquareFrequencyFile(const std::string &filename) {
auto &file = SetupFile(filename);
file.AddVar(update, "update", "Update");
file.Add(
[&](std::ostream &os) {
for (auto [val, count] : data_node_host_squares) {
os << val << ": " << count << "; ";
}
data_node_host_squares.clear();
},
"host_square_frequencies", "Host number of repeats for each square");
file.PrintHeaderKeys();
return file;
}

emp::DataFile &
SGPWorld::SetupSymSquareFrequencyFile(const std::string &filename) {
auto &file = SetupFile(filename);
file.AddVar(update, "update", "Update");
file.Add(
[&](std::ostream &os) {
for (auto [val, count] : data_node_sym_squares) {
os << val << ": " << count << "; ";
}
data_node_sym_squares.clear();
},
"sym_square_frequencies", "Sym number of repeats for each square");
file.PrintHeaderKeys();

return file;
}

emp::DataFile &SGPWorld::SetupSymDonatedFile(const std::string &filename) {
auto &file = SetupFile(filename);
file.AddVar(update, "update", "Update");
Expand Down
6 changes: 0 additions & 6 deletions source/sgp_mode/SGPWorld.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ class SGPWorld : public SymWorld {
emp::vector<emp::DataMonitor<size_t>> data_node_sym_tasks;

public:
std::map<uint32_t, size_t> data_node_host_squares;
std::map<uint32_t, size_t> data_node_sym_squares;
std::mutex squares_mutex;

emp::vector<std::pair<emp::Ptr<Organism>, emp::WorldPosition>> to_reproduce;

SGPWorld(emp::Random &r, emp::Ptr<SymConfigBase> _config, TaskSet task_set)
Expand Down Expand Up @@ -154,8 +150,6 @@ class SGPWorld : public SymWorld {
emp::DataFile &SetUpOrgCountFile(const std::string &filename);
emp::DataFile &SetupSymDonatedFile(const std::string &filename);
emp::DataFile &SetupTasksFile(const std::string &filename);
emp::DataFile &SetupHostSquareFrequencyFile(const std::string &filename);
emp::DataFile &SetupSymSquareFrequencyFile(const std::string &filename);

void CreateDataFiles() override;
};
Expand Down
3 changes: 1 addition & 2 deletions source/sgp_mode/SGPWorldSetup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@

void SGPWorld::SetupHosts(unsigned long *POP_SIZE) {
for (size_t i = 0; i < *POP_SIZE; i++) {
//TODO: make a config option to decide which type of host to create?
emp::Ptr<HealthHost> new_org = emp::NewPtr<HealthHost>(
&GetRandom(), this, my_config, CreatePrivateNotProgram(100), my_config->HOST_INT());
&GetRandom(), this, my_config, CreateNotProgram(100), my_config->HOST_INT());
if(my_config->START_MOI()==1){
emp::Ptr<SGPSymbiont> new_sym = emp::NewPtr<SGPSymbiont>(
&GetRandom(), this, my_config, my_config->SYM_INT());
Expand Down
12 changes: 0 additions & 12 deletions source/sgp_mode/Tasks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,3 @@ void Task::MarkPerformed(CPUState &state, uint32_t output, size_t task_id,
state.available_dependencies[task_id]++;
}
}

void SquareTask::MarkPerformed(CPUState &state, uint32_t output, size_t task_id,
bool shared) {
OutputTask::MarkPerformed(state, output, task_id, shared);
state.internalEnvironment->insert(state.internalEnvironment->begin(),
sqrt(output));
auto &data_node = state.organism->IsHost()
? state.world->data_node_host_squares
: state.world->data_node_sym_squares;
std::lock_guard<std::mutex> lock(state.world->squares_mutex);
data_node[output]++;
}
51 changes: 2 additions & 49 deletions source/sgp_mode/Tasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Task {
return true;
}

virtual void MarkPerformed(CPUState &state, uint32_t output, size_t task_id,
void MarkPerformed(CPUState &state, uint32_t output, size_t task_id,
bool shared);

virtual float CheckOutput(CPUState &state, uint32_t output) = 0;
Expand All @@ -84,7 +84,7 @@ class InputTask : public Task {
size_t num_dep_completes = 1)
: Task(name, unlimited, dependencies, num_dep_completes),
n_inputs(n_inputs), task_fun(task_fun), value(value) {}

float CheckOutput(CPUState &state, uint32_t output) override {
for (size_t i = 0; i < state.input_buf.size(); i++) {
if (state.input_buf[i] == 0)
Expand All @@ -102,36 +102,6 @@ class InputTask : public Task {
}
};

/**
* An output task returns a reward based on the output the organism produced:
* `OutputTask is42() "IS42", [](uint32_t x) { return x == 42 ? 2.0 : 0.0; } );`
*/
class OutputTask : public Task {
std::function<float(uint32_t)> task_fun;

public:
OutputTask(std::string name, std::function<float(uint32_t)> task_fun,
bool unlimited = true, emp::vector<size_t> dependencies = {},
size_t num_dep_completes = 1)
: Task(name, unlimited, dependencies, num_dep_completes),
task_fun(task_fun) {}

float CheckOutput(CPUState &state, uint32_t output) override {
return task_fun(output);
}
};

class SquareTask : public OutputTask {
public:
SquareTask(std::string name, std::function<float(uint32_t)> task_fun,
bool unlimited = true, emp::vector<size_t> dependencies = {},
size_t num_dep_completes = 1)
: OutputTask(name, task_fun, unlimited, dependencies, num_dep_completes) {
}

void MarkPerformed(CPUState &state, uint32_t output, size_t task_id,
bool shared) override;
};

class TaskSet {
emp::vector<emp::Ptr<Task>> tasks;
Expand Down Expand Up @@ -290,21 +260,4 @@ const TaskSet LogicTasks{
emp::NewPtr<InputTask>(NOR), emp::NewPtr<InputTask>(XOR),
emp::NewPtr<InputTask>(EQU)};

const SquareTask SQU = {"SQU", [](uint32_t x) {
uint32_t largest_int = 4294967295;
if (sqrt(x) - floor(sqrt(x)) == 0) {
if (x > (largest_int / 2)) {
// Awards points based on a number's distance from
// 0 rather than absolute size
return 0.5 * (0 - x);
} else {
return (0.5 * x);
}
} else {
return 0.0;
}
return 0.0;
}};
const TaskSet SquareTasks{emp::NewPtr<SquareTask>(SQU)};

#endif
36 changes: 9 additions & 27 deletions source/test/sgp_mode_test/CPU.test.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "../../source/sgp_mode/Tasks.cc"
#include "../../sgp_mode/CPU.h"
#include "../../sgp_mode/SGPDataNodes.h"
#include "../../sgp_mode/SGPWorld.h"
Expand All @@ -8,9 +9,12 @@ TEST_CASE("Ancestor CPU can reproduce", "[sgp]") {
public:
int reproduce_count = 0;
double points = 0.0;
bool IsHost() override { return false; }
emp::vector<emp::Ptr<Organism>> empty_vector = emp::vector<emp::Ptr<Organism>>();
bool IsHost() override { return true; }
void AddPoints(double p) override { points += p; }
double GetPoints() override { return points; }
bool GetDead() override { return false; }
emp::vector<emp::Ptr<Organism>>& GetSymbionts() override { return empty_vector; }
emp::Ptr<Organism> Reproduce() override {
reproduce_count++;
return emp::NewPtr<TestOrg>();
Expand All @@ -20,40 +24,18 @@ TEST_CASE("Ancestor CPU can reproduce", "[sgp]") {
emp::Random random(61);
SymConfigBase config;
config.RANDOM_ANCESTOR(false);
config.SYM_HORIZ_TRANS_RES(1);
config.HOST_REPRO_RES(1);
WHEN("logic tasks are used") {
// Make the ancestor genome do NOT instead of SQUARE
// Make the ancestor genome do NOT
config.TASK_TYPE(1);

TaskSet task_set{emp::NewPtr<InputTask>(NOT)};
TaskSet task_set{ emp::NewPtr<InputTask>(NOT) };
SGPWorld world(random, &config, task_set);

TestOrg organism;
CPU cpu(&organism, &world);
cpu.state.shared_available_dependencies =
emp::NewPtr<emp::vector<size_t>>();
cpu.state.shared_available_dependencies->resize(1);

// It should reproduce at the end of its program, which has length 100
cpu.RunCPUStep(0, 100);
world.Update();

REQUIRE(organism.reproduce_count == 1);

cpu.state.shared_available_dependencies.Delete();
cpu.state.used_resources.Delete();
cpu.state.internalEnvironment.Delete();
}
WHEN("square task is used") {
config.TASK_TYPE(0);

TaskSet task_set{emp::NewPtr<SquareTask>(SQU)};
SGPWorld world(random, &config, task_set);

TestOrg organism;
CPU cpu(&organism, &world);
cpu.state.shared_available_dependencies =
emp::NewPtr<emp::vector<size_t>>();
emp::NewPtr<emp::vector<size_t>>();
cpu.state.shared_available_dependencies->resize(1);

// It should reproduce at the end of its program, which has length 100
Expand Down
4 changes: 0 additions & 4 deletions source/test/sgp_mode_test/GenomeLibrary.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ TEST_CASE("Generate NOT program", "[sgp]") {
TestGenome(emp::NewPtr<InputTask>(NOT), &ProgramBuilder::AddNot);
}

TEST_CASE("Generate SQUARE program", "[sgp]") {
TestGenome(emp::NewPtr<SquareTask>(SQU), &ProgramBuilder::AddSquare);
}

TEST_CASE("Generate NAND program", "[sgp]") {
TestGenome(emp::NewPtr<InputTask>(NAND), &ProgramBuilder::AddNand);
}
Expand Down
26 changes: 0 additions & 26 deletions source/test/sgp_mode_test/SGPHost.test.cc

This file was deleted.

0 comments on commit c43d775

Please sign in to comment.