Skip to content

Commit

Permalink
Impl cActionKillDemesHighestParasiteLoad action
Browse files Browse the repository at this point in the history
  • Loading branch information
mmore500 committed Nov 14, 2023
1 parent 28442e3 commit e85c722
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
83 changes: 83 additions & 0 deletions avida-core/source/actions/PopulationActions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5168,6 +5168,88 @@ class cActionKillDemePercent : public cAction
};


/*
Kill deme(s) with the highest parasite load
Parameters:
- The percent of living organisms to kill (default: 0)
*/

class cActionKillDemesHighestParasiteLoad : public cAction
{
private:
double m_killprob;
public:
cActionKillDemesHighestParasiteLoad(cWorld *world, const cString &args, Feedback &) : cAction(world, args), m_killprob(0.01)
{
cString largs(args);
if (largs.GetSize()) m_killprob = largs.PopWord().AsDouble();

assert(m_killprob >= 0);
}

static const cString GetDescription() { return "Arguments: [int prob=1]"; }

void Process(cAvidaContext& ctx)
{

int target_cell;
cPopulation& pop = m_world->GetPopulation();

long cells_scanned = 0;
long orgs_killed = 0;
long cells_empty = 0;

const int num_demes = pop.GetNumDemes();
const int kill_quota = ctx.GetRandom().GetRandBinomial(
num_demes,
m_killprob
);
if (kill_quota == 0) return;

double kill_thresh = 1.0;
int init_countdown = kill_quota;
int d;
int _num_eligible = 0;
for (int d = 0; d < pop.GetNumDemes(); d++) {
cDeme &deme = pop.GetDeme(d);
if (deme.IsTreatableNow() && not deme.IsEmpty()) {
{
_num_eligible++;
if (init_countdown > 0) {
kill_thresh = std::min(
kill_thresh,
deme.GetParasiteLoad()
);
init_countdown--;
} else {
kill_thresh = std::max(
deme.GetParasiteLoad(), kill_thresh
);
}
} // End if deme is treatable
} //End iterating through all demes

// go through and kill cells
int _num_killed = 0;
for (int d = 0; d < pop.GetNumDemes(); d++)
{
cDeme &deme = pop.GetDeme(d);
if (
deme.IsTreatableNow() && not deme.IsEmpty() && (deme.GetParasiteLoad() >= kill_thresh))
{
_num_killed += 1;
deme.KillAll(ctx);
} // End if deme is eligible
} //End iterating through all demes
}

const auto _expected_killed = std::min(_num_eligible, kill_quota);
assert(_num_killed == _expected_killed);
} // End Process()
};


/*
Set the ages at which treatable demes can be treated
Expand Down Expand Up @@ -5834,6 +5916,7 @@ void RegisterPopulationActions(cActionLibrary* action_lib)
action_lib->Register<cActionKillWithinRadiusBelowResourceThreshold>("KillWithinRadiusBelowResourceThreshold");
action_lib->Register<cActionKillWithinRadiusMeanBelowResourceThreshold>("KillWithinRadiusMeanBelowResourceThreshold");
action_lib->Register<cActionKillWithinRadiusBelowResourceThresholdTestAll>("KillWithinRadiusBelowResourceThresholdTestAll");
action_lib->Register<cActionKillDemesHighestParasiteLoad>("KillDemesHighestParasiteLoad");
action_lib->Register<cActionKillMeanBelowThresholdPaintable>("KillMeanBelowThresholdPaintable");

action_lib->Register<cActionDiffuseHGTGenomeFragments>("DiffuseHGTGenomeFragments");
Expand Down
14 changes: 14 additions & 0 deletions avida-core/source/main/cDeme.cc
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,20 @@ int cDeme::GetNumOrgsWithOpinion() const
return count;
}

int cDeme::GetNumParasites() const
{
const int demeSize = GetSize();
int count = 0;

for (int pos = 0; pos < demeSize; ++pos)
{
const cPopulationCell &cell = GetCell(pos);
if (cell.IsOccupied()) count += cell.GetOrganism()->GetNumParasites();
}

return count;
}

void cDeme::ProcessPreUpdate()
{
deme_resource_count.SetSpatialUpdate(m_world->GetStats().GetUpdate());
Expand Down
5 changes: 5 additions & 0 deletions avida-core/source/main/cDeme.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ class cDeme

double GetDensity() const { return static_cast<double>(cur_org_count) / static_cast<double>(GetSize()); }
int GetNumOrgsWithOpinion() const;

int GetNumParasites() const;
double GetParasiteLoad() const {
return static_cast<double>(GetNumParasites()) / GetOrgCount();
}

void IncOrgCount() { cur_org_count++; }
void DecOrgCount() { cur_org_count--; }
Expand Down

0 comments on commit e85c722

Please sign in to comment.