/// <summary> /// /// </summary> public void CreateNewGeneration() { Generations[NbGeneration - 1] = Generations[NbGeneration - 1].OrderByDescending(x => x.Fitness).ToArray(); //Checker si shallow copy NetworkGenome[] generationTemp = new NetworkGenome[GenerationSize]; NetworkGenome[] elites = GetElites(NbGeneration); float totalFitness = GetTotalFitness(NbGeneration); int indexNewGen = 0; int indexPartner = -1; //garde lelite for (int i = 0; i < elites.Length && indexNewGen < GenerationSize; i++) { generationTemp[indexNewGen++] = elites[i]; } //rajoute random pour diversiter for (int i = 0; i < (int)(LoserPercent * GenerationSize) && indexNewGen < GenerationSize; i++) { generationTemp[indexNewGen++] = new NetworkGenome(NeuralNetwork.Layout, NeuralNetwork.NbNecessaryInputs, NeuralNetwork.NetworkSize, MutationRnd, CrossOverRnd, MutationChance); } //fill avec des enfants while (indexNewGen < GenerationSize) { indexPartner = ChooseMateIndex(NbGeneration); Genome[] enfants = Generations[NbGeneration - 1][indexPartner].ProduceOffspring(Generations[NbGeneration - 1][ChooseMateIndex(NbGeneration, indexPartner)]); generationTemp[indexNewGen++] = (enfants[0] as NetworkGenome); generationTemp[indexNewGen++] = (enfants[1] as NetworkGenome); } Generations.Add(generationTemp); }
public NetworkGenome(NetworkGenome genom) : base(genom.Dna, genom.MutationRnd, genom.CrossOverRnd) { Equation = genom.Equation; Layout = genom.Layout; NbInputs = genom.NbInputs; Fitness = genom.Fitness; }
/// <summary> /// /// </summary> public void CreateFirstGeneration() { NetworkGenome[] generationTemp = new NetworkGenome[GenerationSize]; for (int i = 0; i < GenerationSize; i++) { generationTemp[i] = new NetworkGenome(NeuralNetwork.Layout, NeuralNetwork.NbNecessaryInputs, NeuralNetwork.NetworkSize, MutationRnd, CrossOverRnd, MutationChance, NeuralNetwork.Layers[0].Neurons[0].MinValueRand, NeuralNetwork.Layers[0].Neurons[0].MaxValueRand); } Generations.Add(generationTemp); }
/// <summary> /// /// </summary> /// <param name="partner"></param> /// <returns></returns> public override Genome[] ProduceOffspring(Genome partner) { Genome[] r = new Genome[2]; if (Dna.Length != partner.Dna.Length) { //if (partner is NetworkGenome) // return ProduceOffspringNeat(partner as NetworkGenome); //else // throw new Exception("Les deux chromosones ne sont pas compatibles"); } NetworkGenome Children1 = new NetworkGenome(this); NetworkGenome Children2 = new NetworkGenome(this); int indexDna = 0; int crossPoint = 0;; for (int layer = 0; layer < this.Layout.Length; layer++) { for (int node = 0; node < this.Layout[layer]; node++) { //pour skip les biais A CHECKER indexDna++; crossPoint = CrossOverRnd.Next(0, ((layer == 0) ? NbInputs : Layout[layer - 1])); for (int weigths = 0; weigths < ((layer == 0) ? NbInputs:Layout[layer - 1]); weigths++) { if (weigths < crossPoint) { Children1.Dna[indexDna] = partner.Dna[indexDna]; } else { Children2.Dna[indexDna] = partner.Dna[indexDna]; } indexDna++; } } } Children1.Mutate(); Children2.Mutate(); Children1.Fitness = -1; Children2.Fitness = -1; r[0] = Children1; r[1] = Children2; return(r); }
public Tuple <Genome, Genome> ProduceOffspringNeat(NetworkGenome partner) { throw new NotImplementedException(); }