Skip to content

Commit

Permalink
Rework a little Simulation::progressByOneYear method.
Browse files Browse the repository at this point in the history
  • Loading branch information
przemek83 committed Aug 9, 2024
1 parent 4c2a633 commit 23727e5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/Simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,13 @@ SimulationData::BasicMetrics<int> Simulation::progressByOneYear(
{
++yearMetrics.deaths_;
it = individuals_.erase(it);
continue;
}

if (shouldHaveOffspring(individual))
else
{
for (int i{0}; i < params_.offspring_.count_; ++i)
individuals_.emplace_front(individual.offspring(
*generator_, params_.mutations_.added_));
yearMetrics.births_ += params_.offspring_.count_;
handleOffspring(individual, yearMetrics);
individual.ageByOneYear();
++it;
}

individual.ageByOneYear();
++it;
}

return yearMetrics;
Expand Down Expand Up @@ -231,3 +225,16 @@ int Simulation::getLivesOnYearStart(

return basicMetrics[static_cast<std::size_t>(year - 1)].getLivingAtEnd();
}

void Simulation::handleOffspring(const Individual& individual,
BasicMetrics& yearMetrics)
{
if (!shouldHaveOffspring(individual))
return;

for (int i = 0; i < params_.offspring_.count_; ++i)
individuals_.emplace_front(
individual.offspring(*generator_, params_.mutations_.added_));

yearMetrics.births_ += params_.offspring_.count_;
}
3 changes: 3 additions & 0 deletions src/Simulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class Simulation
int getLivesOnYearStart(
int year, const std::vector<BasicMetrics>& basicMetrics) const;

void handleOffspring(const Individual& individual,
BasicMetrics& yearMetrics);

std::list<Individual> individuals_;

config::Params params_;
Expand Down

0 comments on commit 23727e5

Please sign in to comment.