private void CreateFirstPopulation() { population = new Parameters[population_size]; int loaded = 0; if (fitnesses.Any()) { var myList = fitnesses.ToList(); myList.Sort((pair1, pair2) => - pair1.Value.average.CompareTo(pair2.Value.average)); while (loaded < population_size && loaded < myList.Count) { population[loaded] = myList[loaded].Key; loaded++; } } for (int i = loaded; i < population_size; i++) { population[i] = new Parameters(); //completely randomize some of the values for (int j = 0; j < i * 10; j++) { population[i] = MutationManager.Mutate(population[i]); } } }
private void CreateNewGeneration() { var myList = population.ToList(); myList.Sort((pair1, pair2) => - fitnesses[pair1].average.CompareTo(fitnesses[pair2].average)); Parameters[] nextGeneration = new Parameters[population_size]; nextGeneration[0] = myList[0]; int pos = 1; while (pos < population_size) { Parameters p1 = SelectCandidateByRank(myList); Parameters p2 = SelectCandidateByRank(myList); nextGeneration[pos] = MutationManager.Mutate(MutationManager.Crossover(p1, p2)); pos++; } population = nextGeneration; }
public void Update(uint tick, float _gameSpeed) { Random r = new Random(); for (int i = 0; i < Compatible.Count; i++) { var parentEntity = Compatible[i]; var reproduction = parentEntity.GetComponent <ReproductionComponent>(); if (reproduction.Reproduce > reproduction.ReproductionThreshold) { var energy = parentEntity.GetComponent <EnergyComponent>(); // check if we have the energy if (!energy.CanAfford(reproduction.ReproductionEnergyCost)) { continue; } if (energy.Energy - reproduction.ReproductionEnergyCost < reproduction.RequiredRemainingEnergy) { continue; } // charge teh parent energy. Each critter has an efficency rating, so some energy gets wasted an the rest gets sent to the child. float charged = energy.ChargeEnergy(reproduction.ReproductionEnergyCost); float toChild = reproduction.Efficency * charged; float wasted = charged - toChild; // Let the energy manager know about the wasted energy. _energyManager.DepositEnergy(wasted); // Some set up for the new child, setting positions/giving energy. var babyEntity = Pool.AddEntityFromDefinition(reproduction.ChildDefinitionId, _simulation.JsonSettings, parentEntity.Tag); babyEntity.GetComponent <EnergyComponent>().Energy = toChild; var parentPosition = parentEntity.GetComponent <TransformComponent>().LocalPosition; babyEntity.GetComponent <TransformComponent>().LocalPosition = parentPosition; // The new child will need a mutated brain var originalBrainComponent = parentEntity.GetComponent <BrainComponent>(); var originalBrain = originalBrainComponent.Brain; AbstractBrainGenotype parentGenotype = ((NeatBrainPhenotype)originalBrain).Genotype; NeatBrainGenotype childGenotype = (NeatBrainGenotype)parentGenotype.Clone(); _muationManager.Mutate(childGenotype, _innovationIdManager); var newBrain = babyEntity.GetComponent <BrainComponent>(); newBrain.SetGenotype(childGenotype); // Do not need to pass it type as we have already set the brain, therefore it does not need initializing. newBrain.SetUpLinks(null, _simulation.Settings); newBrain.ParentSpecies = originalBrainComponent.Species; BirthEventInfo e = new BirthEventInfo(parentPosition, tick * _gameSpeed, parentEntity.Id, parentEntity.Tag, babyEntity.Id, babyEntity.Tag, childGenotype, originalBrainComponent.Species.Id); BirthEvent?.Invoke(e); } } }