GA/Assets/Scripts/World.cs
/// <summary>
/// Evaluates the population and performs crossover (one generation).
/// </summary>
Evaluates the population and performs crossover (one generation).
Evaluates the population and performs crossover ( one generation ) .
void Update() {
        #region Calculate fitness
        fitnessSum = 0f;
        for (int i = 0; i < population.Count; i++) {
            population[i].CalculateFitness();
            fitnessSum += population[i].fitness;
        }
        #endregion
        #region Selection
        population = population.OrderByDescending(x => x.fitness).ToList();
        best = population[0];
        best.Draw();
        var matingPool = new List<Chromosome>();
        for (int j = 0; j < population.Count; j++) {
            matingPool.Add(Select());
        }
        var newPopulation = new List<Chromosome>();
        while (newPopulation.Count < population.Count - 1 - elitism) { 
            var parentA = matingPool[Random.Range(0, matingPool.Count)];
            var parentB = matingPool[Random.Range(0, matingPool.Count)];
            #region Crossover
            var child1 = parentA.Crossover(parentB);
            var child2 = parentA.Crossover(parentB);
            #endregion
            #region Mutation
            child1.Mutate(mutationRate);
            child2.Mutate(mutationRate);
            #endregion
            newPopulation.Add(child1);
            newPopulation.Add(child2);
            if (population.Count - newPopulation.Count - elitism == 1) {
                var child = parentA.Crossover(parentB);
                child.Mutate(mutationRate);
                newPopulation.Add(child);
            }
        }
        #region Elitism
        for (int i = 0; i < elitism; i++) {
            var dna = new List<City>();
            for (int j = 0; j < population[i].dna.Count; j++) {
                dna.Add(cities.Where(x => x.index == population[i].dna[j].index).FirstOrDefault());
            }
            var elite = new Chromosome(this);
            elite.dna = dna;
            if (mutateElites)
                elite.Mutate(eliteMutationRate);
            newPopulation.Add(elite);
        }
        #endregion
        population = newPopulation;
        generation++;
        #endregion
        #region Visualization
        var distA = System.Math.Truncate(best.GetDistance() * 1000) / 1000;
        currentDist.text = "Distance: " + distA;
        currentGen.text = "Generation: " + generation;
        if (allTimeBest == null || best.fitness > allTimeBest.fitness) {
            allTimeBest = best;
            allTimeBest.SetGeneration(generation);
            DrawBest();
            ShowChange();
            bestGen.text = "Generation: " + allTimeBest.generation;
        }
        if (generation == 2)
            startDist = best.GetDistance();
        #endregion
    }
void Update ( ) { region  Calculate fitness fitnessSum = 0f ; for ( int i = 0 ; i < population . Count ; i ++ ) { population [ i ] . CalculateFitness ( ) ; fitnessSum += population [ i ] . fitness ; } endregion region  Selection population = population . OrderByDescending ( x => x . fitness ) . ToList ( ) ; best = population [ 0 ] ; best . Draw ( ) ; var matingPool = new List < Chromosome > ( ) ; for ( int j = 0 ; j < population . Count ; j ++ ) { matingPool . Add ( Select ( ) ) ; } var newPopulation = new List < Chromosome > ( ) ; while ( newPopulation . Count < population . Count - 1 - elitism ) { var parentA = matingPool [ Random . Range ( 0 , matingPool . Count ) ] ; var parentB = matingPool [ Random . Range ( 0 , matingPool . Count ) ] ; region  Crossover var child1 = parentA . Crossover ( parentB ) ; var child2 = parentA . Crossover ( parentB ) ; endregion region  Mutation child1 . Mutate ( mutationRate ) ; child2 . Mutate ( mutationRate ) ; endregion newPopulation . Add ( child1 ) ; newPopulation . Add ( child2 ) ; if ( population . Count - newPopulation . Count - elitism == 1 ) { var child = parentA . Crossover ( parentB ) ; child . Mutate ( mutationRate ) ; newPopulation . Add ( child ) ; } } region  Elitism for ( int i = 0 ; i < elitism ; i ++ ) { var dna = new List < City > ( ) ; for ( int j = 0 ; j < population [ i ] . dna . Count ; j ++ ) { dna . Add ( cities . Where ( x => x . index == population [ i ] . dna [ j ] . index ) . FirstOrDefault ( ) ) ; } var elite = new Chromosome ( this ) ; elite . dna = dna ; if ( mutateElites ) elite . Mutate ( eliteMutationRate ) ; newPopulation . Add ( elite ) ; } endregion population = newPopulation ; generation ++ ; endregion region  Visualization var distA = System . Math . Truncate ( best . GetDistance ( ) * 1000 ) / 1000 ; currentDist . text = " " + distA ; currentGen . text = " " + generation ; if ( allTimeBest == null || best . fitness > allTimeBest . fitness ) { allTimeBest = best ; allTimeBest . SetGeneration ( generation ) ; DrawBest ( ) ; ShowChange ( ) ; bestGen . text = " " + allTimeBest . generation ; } if ( generation == 2 ) startDist = best . GetDistance ( ) ; endregion }
Evaluates the population and performs crossover (one generation).
Evaluates the population and performs crossover ( one generation ) .
// sort elements // draw best element of generation // add elements to matingPool according to their fitness // select two parents // update text // draw best route