Skip to content

Commit cabd2e9

Browse files
Merge pull request #308 from anyaevostinar/nand-switch
Nand switch
2 parents 425ba97 + 3291530 commit cabd2e9

7 files changed

Lines changed: 53 additions & 52 deletions

File tree

source/sgp_mode/ProgramBuilder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,10 @@ class ProgramBuilder {
362362
"Global Anchor",
363363
start_tag
364364
);
365-
// Add not instruction
365+
// Add nand instruction
366366
AddInst(program, io_op);
367367
AddInst(program, io_op, 1);
368+
AddInst(program, io_op);
368369
AddTask_Nand(program);
369370
// Nop filler is length minus current size + repro instructions
370371
// const size_t nop_filler = length - (program.size() + 1);

source/sgp_mode/SGPWorldSetup.cc

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -383,19 +383,19 @@ void SGPWorld::SetupHosts(long unsigned int* POP_SIZE) {
383383
);
384384

385385
// TODO - inject initial population at fixed positions (unless configured otherwise)
386-
size_t not_task_id = task_env.GetTaskSet().GetSize();
387-
if (task_env.GetTaskSet().HasTask("NOT")) {
388-
not_task_id = task_env.GetTaskSet().GetID("NOT");
389-
} else if (task_env.GetTaskSet().HasTask("not")) {
390-
not_task_id = task_env.GetTaskSet().GetID("not");
386+
size_t nand_task_id = task_env.GetTaskSet().GetSize();
387+
if (task_env.GetTaskSet().HasTask("NAND")) {
388+
nand_task_id = task_env.GetTaskSet().GetID("NAND");
389+
} else if (task_env.GetTaskSet().HasTask("nand")) {
390+
nand_task_id = task_env.GetTaskSet().GetID("nand");
391391
}
392392

393393

394394
const size_t init_pop_size = *POP_SIZE;
395395
for (size_t i = 0; i < init_pop_size; ++i) {
396396
emp::Ptr<sgp_host_t> new_host;
397397
sgp_prog_t init_prog(
398-
prog_builder.CreateNotProgram(PROGRAM_LENGTH)
398+
prog_builder.CreateNandProgram(PROGRAM_LENGTH)
399399
);
400400
switch (sgp_org_type) {
401401
case org_mode_t::DEFAULT:
@@ -419,7 +419,7 @@ void SGPWorld::SetupHosts(long unsigned int* POP_SIZE) {
419419
// - these endosymbionts have empty programs?
420420
if (sgp_config.START_MOI() == 1) {
421421
sgp_prog_t sym_prog(
422-
prog_builder.CreateNotProgram(PROGRAM_LENGTH)
422+
prog_builder.CreateNandProgram(PROGRAM_LENGTH)
423423
);
424424
emp::Ptr<sgp_sym_t> new_sym = emp::NewPtr<sgp_sym_t>(
425425
random_ptr,
@@ -432,19 +432,19 @@ void SGPWorld::SetupHosts(long unsigned int* POP_SIZE) {
432432
// NOTE - Move env io assignment to different signal that is triggered on inject?
433433
// AssignNewEnvIO(new_sym->GetHardware().GetCPUState()); // Add to AddSymbiont
434434
// Set sym's parent task
435-
if (task_env.IsSymTask(not_task_id)) {
436-
new_sym->GetHardware().GetCPUState().SetParentTaskPerformed(not_task_id, true);
437-
new_sym->GetHardware().GetCPUState().SetParentFirstTaskPerformed(not_task_id, true);
438-
new_sym->GetHardware().GetCPUState().MarkTaskPerformed(not_task_id);
435+
if (task_env.IsSymTask(nand_task_id)) {
436+
new_sym->GetHardware().GetCPUState().SetParentTaskPerformed(nand_task_id, true);
437+
new_sym->GetHardware().GetCPUState().SetParentFirstTaskPerformed(nand_task_id, true);
438+
new_sym->GetHardware().GetCPUState().MarkTaskPerformed(nand_task_id);
439439
}
440440
// NOTE - Do we need to set location in cpu state here?
441441
new_host->AddSymbiont(new_sym);
442442
}
443443
// TODO - Add SGPWorld function to wrap inject host function
444444
// AssignNewEnvIO(new_host->GetHardware().GetCPUState()); // This is in OnPlacement now, so should be fine
445-
if (task_env.IsHostTask(not_task_id)) {
446-
new_host->GetHardware().GetCPUState().SetParentTaskPerformed(not_task_id, true);
447-
new_host->GetHardware().GetCPUState().SetParentFirstTaskPerformed(not_task_id, true);
445+
if (task_env.IsHostTask(nand_task_id)) {
446+
new_host->GetHardware().GetCPUState().SetParentTaskPerformed(nand_task_id, true);
447+
new_host->GetHardware().GetCPUState().SetParentFirstTaskPerformed(nand_task_id, true);
448448
}
449449
InjectHost(new_host);
450450
}

source/test/sgp_mode_test/functional_tests/HealthMode.test.cc

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -599,45 +599,44 @@ TEST_CASE("Health hosts evolve", "[sgp][sgp-functional]") {
599599
emp::Random random(config.SEED());
600600
world_t world(random, &config);
601601

602-
size_t no_mut_NOT_rate_per_100 = 3400; // 3400 NOTs per 100 updates from empirical runs.
602+
size_t no_mut_NAND_rate_per_100 = 3700; // Approx 3700 NANDs per 100 updates from empirical runs.
603603
size_t run_updates = 3000; // The mutation population initially dips and then recovers around 2k updates.
604604

605605
WHEN("Mutation size is 0") {
606606
config.SGP_MUT_PER_BIT_RATE(0);
607607
world.Setup();
608-
size_t not_task_id = world.GetTaskEnv().GetTaskSet().GetID("NOT");
609-
size_t total_NOTs = 0;
608+
size_t nand_task_id = world.GetTaskEnv().GetTaskSet().GetID("NAND");
609+
size_t total_NANDs = 0;
610610
for (size_t i = 0; i < run_updates; i++) {
611611
world.Update();
612612
if(i >= (run_updates-100)) {
613613
//Only considering last 100 updates to make it easier to compare reasonable numbers
614-
total_NOTs += world.GetHostTaskSuccesses().at(not_task_id);
614+
total_NANDs += world.GetHostTaskSuccesses().at(nand_task_id);
615615
}
616616
}
617617
THEN("Health hosts do not accrue mutations late in an experiment") {
618618
REQUIRE(world.GetNumOrgs() == world_size);
619-
REQUIRE(total_NOTs > no_mut_NOT_rate_per_100 - no_mut_NOT_rate_per_100*0.25);
620-
REQUIRE(total_NOTs < no_mut_NOT_rate_per_100 + no_mut_NOT_rate_per_100*0.25);
619+
REQUIRE(total_NANDs > no_mut_NAND_rate_per_100 - no_mut_NAND_rate_per_100*0.25);
620+
REQUIRE(total_NANDs < no_mut_NAND_rate_per_100 + no_mut_NAND_rate_per_100*0.25);
621621
}
622622
}
623623

624624
WHEN("Mutation size is greater than 0") {
625625
config.SGP_MUT_PER_BIT_RATE(0.001);
626626
world.Setup();
627-
size_t not_task_id = world.GetTaskEnv().GetTaskSet().GetID("NOT");
628-
size_t total_NOTs = 0;
627+
size_t nand_task_id = world.GetTaskEnv().GetTaskSet().GetID("NAND");
629628
size_t total_NANDs = 0;
630629

631630
for (size_t i = 0; i < run_updates; i++) {
632631
world.Update();
633632
if(i >= (run_updates-100)) {
634633
//Only considering last 100 updates to make it easier to compare reasonable numbers
635-
total_NOTs += world.GetHostTaskSuccesses().at(not_task_id);
634+
total_NANDs += world.GetHostTaskSuccesses().at(nand_task_id);
636635
}
637636
}
638637
THEN("Health hosts accrue more mutations late in an experiment") {
639638
REQUIRE(world.GetNumOrgs() == world_size);
640-
REQUIRE(total_NOTs > no_mut_NOT_rate_per_100 * 3);
639+
REQUIRE(total_NANDs > no_mut_NAND_rate_per_100);
641640
}
642641
}
643642
}

source/test/sgp_mode_test/functional_tests/SGPHardware_Tasks.test.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,16 @@ TEST_CASE("Ancestor hardware can attempt reproduction and do NOT", "[sgp]") {
5858

5959
REQUIRE(sgp_host.GetPoints() == 0);
6060
sgp_host.ProcessOutputBuffer();
61-
// Hardware should also have completed NOT task seven times, which is also checking that they
61+
// Hardware should also have completed NAND task seven times, which is also checking that they
6262
// are able to fully cycle through all possible inputs (they don't do the last one because they need to go back to their first IO to get it)
63-
const size_t not_task_id = world.GetTaskEnv().GetTaskSet().GetID("NOT");
64-
REQUIRE(hw.GetCPUState().GetTaskPerformed(not_task_id));
63+
const size_t start_task_id = world.GetTaskEnv().GetTaskSet().GetID("NAND");
64+
REQUIRE(hw.GetCPUState().GetTaskPerformed(start_task_id));
6565
REQUIRE(output_buffer.size() == 0);
66-
REQUIRE(sgp_host.GetPoints() == 5); // should have done NOT 1 time for 5 points
66+
REQUIRE(sgp_host.GetPoints() == 5); // should have done NAND 1 time for 5 points
6767

6868
// Reset should clear CPU state, etc
6969
hw.Reset();
70-
REQUIRE(!hw.GetCPUState().GetTaskPerformed(not_task_id));
70+
REQUIRE(!hw.GetCPUState().GetTaskPerformed(start_task_id));
7171
}
7272

7373

source/test/sgp_mode_test/functional_tests/SGPHost_Reproduce.test.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ TEST_CASE("SGPHost Reproduce function results in correct parental task tracking"
143143
// Assert that added host is what we expect
144144
REQUIRE(hw.GetCPUState().GetNumTasks() == 9);
145145

146-
REQUIRE(world.GetTaskEnv().GetTaskSet().HasTask("NOT"));
147-
const size_t not_task_id = world.GetTaskEnv().GetTaskSet().GetID("NOT");
146+
REQUIRE(world.GetTaskEnv().GetTaskSet().HasTask("NAND"));
147+
const size_t start_task_id = world.GetTaskEnv().GetTaskSet().GetID("NAND");
148148

149-
WHEN("A host can only perform NOT") {
149+
WHEN("A host can only perform NAND") {
150150
WHEN("It is one of the first generation (does not have parents)") {
151151

152152
auto& host_parent_tasks = hw.GetCPUState().GetParentTasksPerformed();
@@ -157,18 +157,18 @@ TEST_CASE("SGPHost Reproduce function results in correct parental task tracking"
157157
}
158158

159159
THEN("Its parent's tasks are marked with initial not task") {
160-
REQUIRE(host_parent_tasks.Get(not_task_id));
160+
REQUIRE(host_parent_tasks.Get(start_task_id));
161161
}
162162

163163
// Run world for enough updates to evaluate full genome
164164
for (size_t i = 0; i < 26; ++i) {
165165
world.Update();
166166
}
167167

168-
THEN("After running for 25 updates, host tasks should show NOT completed") {
169-
REQUIRE(host_tasks.Get(not_task_id));
168+
THEN("After running for 25 updates, host tasks should show NAND completed") {
169+
REQUIRE(host_tasks.Get(start_task_id));
170170
REQUIRE(host_tasks.CountOnes() == 1);
171-
// REQUIRE(hw.GetCPUState().GetTaskPerformanceCount(not_task_id) == 1);
171+
// REQUIRE(hw.GetCPUState().GetTaskPerformanceCount(start_task_id) == 1);
172172
}
173173

174174
THEN("Offspring should have correct parent tasks marked as completed") {
@@ -178,7 +178,7 @@ TEST_CASE("SGPHost Reproduce function results in correct parental task tracking"
178178
auto& offspring_tasks = offspring_hw.GetCPUState().GetTasksPerformed();
179179
REQUIRE(offspring_tasks.None());
180180
REQUIRE(offspring_parent_tasks.CountOnes() == 1);
181-
REQUIRE(offspring_parent_tasks.Get(not_task_id));
181+
REQUIRE(offspring_parent_tasks.Get(start_task_id));
182182
offspring.Delete();
183183
}
184184
}

source/test/sgp_mode_test/functional_tests/StressMode.test.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,37 +139,37 @@ TEST_CASE("Stress hosts evolve", "[sgp][sgp-functional]") {
139139
emp::Random random(config.SEED());
140140
world_t world(random, &config);
141141

142-
size_t no_mut_NOT_rate = 40000;
142+
size_t no_mut_NAND_rate = 40000;
143143
size_t run_updates = 1000;
144144

145145
WHEN("Mutation size is 0") {
146146
config.SGP_MUT_PER_BIT_RATE(0);
147147
world.Setup();
148-
size_t not_task_id = world.GetTaskEnv().GetTaskSet().GetID("NOT");
149-
size_t total_NOTs = 0;
148+
size_t nand_task_id = world.GetTaskEnv().GetTaskSet().GetID("NAND");
149+
size_t total_NANDs = 0;
150150
for (size_t i = 0; i < run_updates; i++) {
151151
world.Update();
152-
total_NOTs += world.GetHostTaskSuccesses().at(not_task_id);
152+
total_NANDs += world.GetHostTaskSuccesses().at(nand_task_id);
153153
}
154154
THEN("Stress hosts do not accrue mutations late in an experiment") {
155155
REQUIRE(world.GetNumOrgs() == world_size);
156-
REQUIRE(total_NOTs > no_mut_NOT_rate - no_mut_NOT_rate*0.25);
157-
REQUIRE(total_NOTs < no_mut_NOT_rate + no_mut_NOT_rate*0.25);
156+
REQUIRE(total_NANDs > no_mut_NAND_rate - no_mut_NAND_rate*0.25);
157+
REQUIRE(total_NANDs < no_mut_NAND_rate + no_mut_NAND_rate*0.25);
158158
}
159159
}
160160

161161
WHEN("Mutation size is greater than 0") {
162162
config.SGP_MUT_PER_BIT_RATE(0.01);
163163
world.Setup();
164-
size_t not_task_id = world.GetTaskEnv().GetTaskSet().GetID("NOT");
165-
size_t total_NOTs = 0;
164+
size_t nand_task_id = world.GetTaskEnv().GetTaskSet().GetID("NAND");
165+
size_t total_NANDs = 0;
166166
for (size_t i = 0; i < run_updates; i++) {
167167
world.Update();
168-
total_NOTs += world.GetHostTaskSuccesses().at(not_task_id);
168+
total_NANDs += world.GetHostTaskSuccesses().at(nand_task_id);
169169
}
170170
THEN("Stress hosts accrue more mutations late in an experiment") {
171171
REQUIRE(world.GetNumOrgs() == world_size);
172-
REQUIRE(total_NOTs > no_mut_NOT_rate * 3);
172+
REQUIRE(total_NANDs > no_mut_NAND_rate);
173173
}
174174
}
175175
}

source/test/sgp_mode_test/unit_tests/ProgramBuilder.test.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -489,14 +489,15 @@ TEST_CASE("CreateNandProgram()", "[sgp][sgp-unit]"){
489489
REQUIRE(program[0].op_code == sgpmode::Library::GetOpCode("Global Anchor"));
490490
}
491491

492-
THEN("The program contains IO, IO, Nand"){
492+
THEN("The program contains IO, IO, IO, Nand"){
493493
REQUIRE(program[1].op_code == sgpmode::Library::GetOpCode("IO"));
494494
REQUIRE(program[2].op_code == sgpmode::Library::GetOpCode("IO"));
495-
REQUIRE(program[3].op_code == sgpmode::Library::GetOpCode("Nand"));
495+
REQUIRE(program[3].op_code == sgpmode::Library::GetOpCode("IO"));
496+
REQUIRE(program[4].op_code == sgpmode::Library::GetOpCode("Nand"));
496497
}
497498

498-
THEN("The program contains 94 nop instructions in a row"){
499-
for (size_t i = 4; i < (program.size() - 1); ++i) {
499+
THEN("The program contains 93 nop instructions in a row"){
500+
for (size_t i = 5; i < (program.size() - 1); ++i) {
500501
REQUIRE(program[i].op_code == 0);
501502
}
502503
}

0 commit comments

Comments
 (0)