public Chromossome SelectIndividual(Chromossome p_differentFromThis) { float __totalFitness = 0; float __selectedFitness = 0; int __count = 0; Chromossome __selectedChromossome = p_differentFromThis; for (int i = 0; i < populationSize; i++) { __totalFitness += population[i].fitness; } while (__selectedChromossome == p_differentFromThis && __count < 10) { __selectedFitness = Random.value * __totalFitness; for (int i = 0; i < populationSize; i++) { if (population[i].fitness >= __selectedFitness) { __selectedChromossome = population[i]; break; } else { __selectedFitness -= population[i].fitness; } } __count++; } return(__selectedChromossome); }
public void Crossover(Chromossome c1, Chromossome c2) { for (int i = 0; i < m_Length; i++) { m_Genes[i] = Random.Range(0.0f, 1.0f) < 0.5f ? c1.m_Genes[i] : c2.m_Genes[i]; } }
public Chromossome SelectIndividual() { float __totalFitness = 0; float __selectedFitness = 0; Chromossome __selectedChromossome = new Chromossome(mapSize, mapSize); for (int i = 0; i < populationSize; i++) { __totalFitness += population[i].fitness; } __selectedFitness = Random.value * __totalFitness; for (int i = 0; i < populationSize; i++) { if (population[i].fitness >= __selectedFitness) { __selectedChromossome = population[i]; break; } else { __selectedFitness -= population[i].fitness; } } return(__selectedChromossome); }
private void NewPopulation() { Evaluate(); List <Brain> newPopulation = new List <Brain>(); for (int i = 0; i < m_PopulationSize; i++) { Chromossome parent1 = TournamentSelection().m_Chromosome; Chromossome parent2 = TournamentSelection().m_Chromosome; GameObject offspring = Instantiate(m_Prefab, m_Begin.position, m_Begin.rotation); Brain brain = offspring.GetComponent <Brain>(); brain.Initialize(); brain.m_Chromosome.Crossover(parent1, parent2); if (Random.Range(0.0f, 1.0f) < m_MutationRate) { brain.m_Chromosome.Mutate(); } newPopulation.Add(brain); } for (int i = 0; i < m_PopulationSize; i++) { Destroy(m_Population[i].gameObject); } m_Population = newPopulation; m_Generation++; }
public void Initialize() { // Gene 0 - Forward // Gene 1 - Angle m_Chromosome = new Chromossome(2, 90); m_StartPosition = transform.position; m_Alive = true; }
public void EvaluateFitness() { //DebugLogPopulation(); //testPop = new List<Chromossome>(); /** * for (int i = 0; i < population.Count; i++) * { * testPop.Add(population[i]); * } * /**/ for (int i = 0; i < population.Count; i++) { float __tempFitness = 0; Chromossome __tempChromossome = population[i]; int[][] __simulatedMap = CellularAutomata.SimulateMap(__tempChromossome.GetMap(), 5); __simulatedMap = CellularAutomata.FillExclaves(__simulatedMap); //by doing this we end up having only the largest cave area //first we calculate the fitness from Fill Ratio float __fill = CellularAutomata.GetFilledPercentage(__simulatedMap); __tempFitness += (1 - (Mathf.Abs(fillTarget - __fill))) * fitnessBaseValue; //now we calculate the fitness based on points for (int j = 0; j < fitnessPoints.Length; j++) { if (__simulatedMap[(int)fitnessPoints[j].y][(int)fitnessPoints[j].x] == 0) { __tempFitness += fitnessBaseValue * fitnessPointsWeight; } } __tempChromossome.fitness = __tempFitness; population[i] = __tempChromossome; } /** * for (int i = 0; i < population.Count; i++) * { * Debug.Log(testPop[i] == population[i]); * } * /**/ //DebugLogPopulation(); }
public void ChangeGeneration() { //DebugLogPopulation(); List <Chromossome> __newPopulation = new List <Chromossome>(); for (int i = 0; i < eliteSize; i++) { __newPopulation.Add(population[i]); //Debug.Log("Generation: " + generation + " Checking Elite " + i + ": " + __newPopulation[i].fitness); } for (int i = eliteSize; i < populationSize; i += 2) { Chromossome __parent1 = SelectIndividual(); Chromossome __parent2 = SelectIndividual(__parent1); //Debug.Log("Are parents equal? " + (__parent1 == __parent2)); Chromossome[] __tempChromossome = Crossover(__parent1, __parent2); __newPopulation.Add(__tempChromossome[0]); if (i < populationSize - 1) { __newPopulation.Add(__tempChromossome[1]); } } /** * for (int i = 0; i < population.Count ; i++) * { * Debug.Log("Are populations equal? " + (population[i] == __newPopulation[i])); * } * /**/ //Debug.Log(population.Count + " " + __newPopulation.Count); population = __newPopulation; generation++; //DebugLogPopulation(); }
public void Initialize() { population = new List <Chromossome>(); for (int i = 0; i < populationSize; i++) { Chromossome __temp = new Chromossome(mapSize, mapSize); __temp.InitializeRandom(); population.Add(__temp); } EvaluateFitness(); OrderPopulation(); //Debug.Log("Generation: " + generation + " Best Fitness: " + population[0].fitness); /** * for (int i = 0; i < population.Count; i++) * { * Debug.Log(population[i].fitness); * } * /**/ }
public Chromossome[] Crossover(Chromossome p_parent1, Chromossome p_parent2) { int __lenght = p_parent1.genes.Length; bool __swap = false; Chromossome[] __children = new Chromossome[2]; //initializing __children[0] = new Chromossome(mapSize, mapSize); __children[1] = new Chromossome(mapSize, mapSize); //Debug.Log("Are parents equal? " + (p_parent1 == p_parent2)); //We are using "Uniform Crossover" for (int i = 0; i < __lenght; i++) { if (Random.value < 0.5f) { __swap = !__swap; } __children[0].genes[i] = __swap ? p_parent2.genes[i] : p_parent1.genes[i]; __children[1].genes[i] = __swap ? p_parent1.genes[i] : p_parent2.genes[i]; if (Random.value <= mutationRate) { __children[0].genes[i] = __children[0].genes[i] == 0 ? 1 : 0; //Debug.Log("Mutation"); } if (Random.value <= mutationRate) { __children[1].genes[i] = __children[1].genes[i] == 0 ? 1 : 0; //Debug.Log("Mutation"); } } return(__children); }