private void CrossOver(ICollection <Creature> creatures, Bounds bounds) { var amountCrossOver = (int)(creatures.Count * CrossOverChance / 100); for (var i = 0; i < amountCrossOver; ++i) { var father = Selection(); var mother = Selection(); var fatherWeights = father.Brain.GetWeights(); var motherWeights = mother.Brain.GetWeights(); var childWeights = new double[fatherWeights.Length]; var crossOverPoint = Random.Range(0, fatherWeights.Length); for (var k = 0; k < childWeights.Length; ++k) { childWeights[k] = k < crossOverPoint ? fatherWeights[k] : motherWeights[k]; } var child = Prefabs.CreateCreature().SpawnIn(bounds, Generation); child.Brain.SetWeights(childWeights); NextGeneration.Add(child); } }
public List <Creature> SpawnInitialPopulation(int amount, Bounds bounds) { NextGeneration = new List <Creature>(amount); for (var i = 0; i < amount; ++i) { NextGeneration.Add(Prefabs.CreateCreature().SpawnIn(bounds, Generation)); } Generation++; return(NextGeneration); }
private void SpawnInitialPopulationAndFoodSupply() { // spawn initial population _creatures = GeneticAlgorythm.SpawnInitialPopulation(InitialPopulation, Bounds); // spawn initial food supply _foodSupply = new List <Food>(InitialFoodSupply); for (var i = 0; i < InitialFoodSupply; ++i) { var food = Prefabs.CreateFood(); food.SpawnIn(Bounds); _foodSupply.Add(food); } name = "World [" + _creatures.Count + "]"; }