private void Selection(double crysisPower, Random random, Settings settings) { List <Tuple <double, double> > survivabilityByAge = new List <Tuple <double, double> >(); List <Species> survivors = new List <Species>(this.m_speciesList.Count); List <Species> victims = new List <Species>(this.m_speciesList.Count); int maxAge = 0; foreach (Species species in this.m_speciesList) { double survivability; double survivalProbability = species.GetSurvivalProbability(crysisPower, settings.SurvivabilityToSurvivalProbabilityTransfrom, out survivability); survivabilityByAge.Add(Tuple.Create((double)species.age, survivability)); if (random.ProbCheck(survivalProbability)) { survivors.Add(species); } else { victims.Add(species); } if (species.age > maxAge) { maxAge = species.age; } } if (m_speciesList.Count > 0) { this.MortalityRate = 1.0 * (m_speciesList.Count - survivors.Count) / m_speciesList.Count; } else { this.MortalityRate = 0; } this.m_speciesList = survivors; this.SurvivabilityStats = new Stats(survivabilityByAge.Select(item => item.Item2)); this.AgeAtDeathStats = new Stats(victims.Select(item => (double)item.age)); this.SurvivabilityByAgeStats = new Stats2D(0, maxAge, 1, survivabilityByAge); }
private void Breeding(Random random, Settings settings, double crysisPower) { List <Species> parents = new List <Species>(this.m_speciesList.Count); double matingProbability = settings.PopulationSizeToMatingProbabilityTransform(this.m_speciesList.Count); foreach (Species species in this.m_speciesList) { if (random.ProbCheck(matingProbability)) { parents.Add(species); } } List <Species> offsprings = new List <Species>(); if (settings.SexualReproduction) { if (parents.Count > 1) { switch (settings.MatingStrategy) { case Settings.MatingStrategyType.Random: random.Shuffle(parents); for (int i = 0; i < parents.Count; i += 2) { Species parent1 = parents[i]; int j = i + 1; if (j >= parents.Count) { j = 0; } Species parent2 = parents[j]; Species offspring = Species.CreateOffspringSex(parent1, parent2, random, settings); offsprings.Add(offspring); } break; case Settings.MatingStrategyType.PositiveAssortion: parents = parents.OrderByDescending(item => item.GetSurvivability(crysisPower)).ToList(); //descending because we'll be taking last from list as it's much faster { double maxCunning = parents.Max(item => item.cunning); double maxAgeingSpeed = parents.Max(item => item.ageingSpeed); while (parents.Count > 2) { Species parent1 = parents[parents.Count - 1]; parents.RemoveAt(parents.Count - 1); int bestPartnerIndex = 0; double bestPartnerDistance = SimilarityDistanceSquared(parent1, parents[bestPartnerIndex], maxCunning, maxAgeingSpeed); for (int i = 1; i < parents.Count; ++i) { double distance = SimilarityDistanceSquared(parent1, parents[i], maxCunning, maxAgeingSpeed); if (distance < bestPartnerDistance) { bestPartnerDistance = distance; bestPartnerIndex = i; } } Species parent2 = parents[bestPartnerIndex]; parents.RemoveAt(bestPartnerIndex); Species offspring = Species.CreateOffspringSex(parent1, parent2, random, settings); offsprings.Add(offspring); } } break; default: throw new ApplicationException("Unexpected strategy " + settings.MatingStrategy); } } } else { foreach (Species parent in parents) { Species offspring = Species.CreateOffspringReplication(parent, random, settings); offsprings.Add(offspring); } } if (m_speciesList.Count > 0) { this.RateOfOrigination = 1.0 * (offsprings.Count) / m_speciesList.Count; } else { this.RateOfOrigination = 0; } this.m_speciesList.AddRange(offsprings); this.CunningHistogramm = Stats2D.CreateForCounts(0, settings.MaximalCunning, 0.25, m_speciesList.Select(item => item.cunning)); }