Skip to content

Commit cea2d57

Browse files
committed
fixing bad vector access due to stress symbiont being removed before symdobirth
1 parent cd7da29 commit cea2d57

5 files changed

Lines changed: 36 additions & 5 deletions

File tree

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ test-debug-sgp:
163163
test-executable:
164164
$(CXX_nat) $(CFLAGS_nat) $(TEST_DIR)/main.cc -o symbulation.test
165165

166+
test-executable-debug:
167+
$(CXX_nat) $(CFLAGS_nat_debug) $(TEST_DIR)/main.cc -o symbulation.test
168+
166169
test-all:
167170
$(CXX_nat) $(CFLAGS_nat) $(TEST_DIR)/main.cc -o symbulation.test
168171
./symbulation.test || { gdb ./symbulation.test --ex="catch throw" --ex="set confirm off" --ex="run" --ex="backtrace" --ex="quit"; exit 1; }

source/sgp_mode/SGPWorld.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ class SGPWorld : public SymWorld {
141141
int GetNeighborHost(size_t id, emp::Ptr<Organism> symbiont);
142142
bool TaskMatchCheck(emp::Ptr<Organism> sym_parent, emp::Ptr<Organism> host_parent);
143143

144+
// Prototypes for sym transfering
145+
emp::WorldPosition SymFindHost(emp::Ptr<Organism> symbiont, emp::WorldPosition cur_pos);
146+
147+
144148
// Prototype for graveyard handling method
145149
void SendToGraveyard(emp::Ptr<Organism> org) override;
146150

source/sgp_mode/SGPWorldSetup.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,31 @@ emp::WorldPosition SGPWorld::SymDoBirth(emp::Ptr<Organism> sym_baby, emp::WorldP
177177
return emp::WorldPosition();
178178
}
179179
}
180+
181+
182+
/**
183+
* Input: Pointers to a symbiont and the position of the symbiont.
184+
* Note that the position of the symbiont is a WorldPosition with index as 1-index position
185+
* in host's syms list and pop_id as host's location in the world
186+
*
187+
* Output: Returns a WorldPosition pointer, a valid one for succesful
188+
* infection and an invalid for a failed infection
189+
*
190+
* Purpose: To place a symbiont in a new location or host in the world.
191+
*/
192+
emp::WorldPosition SGPWorld::SymFindHost(emp::Ptr<Organism> symbiont, emp::WorldPosition cur_pos) {
193+
size_t i = cur_pos.GetPopID();
194+
int new_host_pos = GetNeighborHost(i, symbiont);
195+
if (new_host_pos > -1) { //-1 means no living neighbors
196+
int new_index = pop[new_host_pos]->AddSymbiont(symbiont);
197+
if(new_index > 0){ //sym successfully infected
198+
return emp::WorldPosition(new_index, new_host_pos);
199+
} else { //sym got killed trying to infect
200+
return emp::WorldPosition();
201+
}
202+
} else {
203+
symbiont.Delete();
204+
return emp::WorldPosition();
205+
}
206+
}
180207
#endif

source/sgp_mode/StressHost.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class StressHost : public SGPHost {
9292
for (size_t j = 0; j < syms.size(); j++) {
9393
emp::Ptr<Organism> cur_sym = syms[j];
9494
RemoveSymbiont(j+1); //RemoveSymbiont uses 1-indexed value
95-
my_world->SymDoBirth(cur_sym, emp::WorldPosition(j+1, pos.GetIndex()));
95+
my_world->SymFindHost(cur_sym, emp::WorldPosition(j+1, pos.GetIndex()));
9696
}
9797
SetDead();
9898
}

source/test/sgp_mode_test/StressHost.test.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ TEST_CASE("Parasites transfer during stress event", "[sgp]") {
120120
size_t world_size = config.GRID_X() * config.GRID_Y();
121121
SGPWorld world(random, &config, LogicTasks);
122122

123+
123124
WHEN("There are stress parasites with 100% kill rate in the world"){
124125
config.START_MOI(1);
125126
double parasite_death_chance = 1.0;
@@ -145,13 +146,9 @@ TEST_CASE("Parasites transfer during stress event", "[sgp]") {
145146
world.AddOrgAt(host,i);
146147
}
147148

148-
149-
150149
for (size_t i = 0; i < config.EXTINCTION_FREQUENCY() - 1; i++) world.Update();
151150
REQUIRE(world.GetNumOrgs() == world_size);
152-
153151
world.Update();
154-
155152
THEN("Half the hosts died but the remaining got infected by fleeing parasites") {
156153
REQUIRE(world.GetNumOrgs() == 50);
157154
int surviving_syms = 0;

0 commit comments

Comments
 (0)