示例#1
0
 public EA(int population, int numInputs, LayerParams[] networkStructure)
 {
     this.Population = new IndividualStruct[population];
     for (int i = 0; i < population; i++)
     {
         Population[i] = new IndividualStruct(numInputs);
         for (int j = 0; j < networkStructure.Length; j++)
         {
             this.Population[i].Individual.AddLayer(networkStructure[j].F, networkStructure[j].G, networkStructure[j].NumNeurons);
         }
         Population[i].Individual.BuildNetwork();
     }
     this.RNG = new Random();
 }
示例#2
0
        private Network TournamentSelection(IndividualStruct[] population, int k, double chanceWin)
        {
            IndividualStruct[] tournament = new IndividualStruct[k];
            for (int i = 0; i < k; i++)
            {
                tournament[i] = population[this.RNG.Next(population.Length)];
            }

            if (chanceWin > this.RNG.NextDouble())
            {
                return(tournament[this.RNG.Next(k)].Individual);
            }
            else
            {
                return(tournament.OrderBy(f => f.Fitness).ElementAt(0).Individual);
            }
        }
示例#3
0
        public void EvolutionaryLoop(DataStruct[] dataset, int generations, int numChildren, double crossoverRate, double mutationRate, int kTournament, double chanceWin)
        {
            for (int i = 0; i < generations; i++)
            {
                double             bestFitness    = int.MaxValue;
                double             averageFitness = 0;
                IndividualStruct[] newPopulation  = new IndividualStruct[this.Population.Length];
                for (int j = 0; j < this.Population.Length; j++)
                {
                    this.Population[j].Fitness = Fitness(dataset, this.Population[j]);
                    if (this.Population[j].Fitness < bestFitness)
                    {
                        bestFitness = this.Population[j].Fitness;
                    }
                    averageFitness += this.Population[j].Fitness;
                }
                if (i != generations - 1)
                {
                    var survivors = this.Population.OrderBy(x => x.Fitness).ThenBy(x => x.FromGeneration);
                    for (int c = 0; c < numChildren; c++)
                    {
                        newPopulation[c] = new IndividualStruct(this.Crossover(this.TournamentSelection(this.Population, kTournament, chanceWin), this.TournamentSelection(this.Population, kTournament, chanceWin), crossoverRate), i + 1);
                    }
                    for (int a = numChildren; a < this.Population.Length; a++)
                    {
                        newPopulation[a] = survivors.ElementAt(a - numChildren);
                    }

                    for (int ind = 0; ind < newPopulation.Length; ind++)
                    {
                        if (this.RNG.NextDouble() < mutationRate)
                        {
                            this.Mutation(newPopulation[ind].Individual);
                            newPopulation[ind].NumMutations += 1;
                        }
                    }
                    this.Population = newPopulation;
                }
                Console.WriteLine($"Generation: {i + 1}");
                Console.WriteLine($"Best fitness: {bestFitness}");
                Console.WriteLine($"Average fitness: {averageFitness / this.Population.Length}");
                Console.WriteLine("-----------------------------------");
            }
        }
示例#4
0
        public double Fitness(DataStruct[] dataset, IndividualStruct individual)
        {
            //TODO avoid duplicate code from the supervised learning implementation in network (Train())
            double accumulatedError = 0;

            foreach (var example in dataset)
            {
                double[] result = individual.Individual.Inference(example.Input);
                double[] delta  = individual.Individual.Compare(example.Output, result);

                double[] MSE = new double[delta.Length];
                for (int iDelta = 0; iDelta < delta.Length; iDelta++)
                {
                    MSE[iDelta] = Math.Pow(delta[iDelta], 2);
                }
                accumulatedError += MSE.Sum() / MSE.Length;
            }
            return(accumulatedError);
        }