/// <summary> /// Assuming this chromosome is the "mother" mate with the passed in /// "father". /// </summary> /// /// <param name="mother">The mother.</param> /// <param name="father">The father.</param> /// <param name="offspring1">Returns the first offspring</param> /// <param name="offspring2">Returns the second offspring.</param> public void Mate(Chromosome mother, Chromosome father, Chromosome offspring1, Chromosome offspring2) { int geneLength = mother.Genes.Count; // the chromosome must be cut at two positions, determine them var cutpoint1 = (int)(ThreadSafeRandom.NextDouble()*(geneLength - _cutLength)); int cutpoint2 = cutpoint1 + _cutLength; // handle cut section for (int i = 0; i < geneLength; i++) { if (!((i < cutpoint1) || (i > cutpoint2))) { offspring1.GetGene(i).Copy(father.GetGene(i)); offspring2.GetGene(i).Copy(mother.GetGene(i)); } } // handle outer sections for (int i = 0; i < geneLength; i++) { if ((i < cutpoint1) || (i > cutpoint2)) { offspring1.GetGene(i).Copy(mother.GetGene(i)); offspring2.GetGene(i).Copy(father.GetGene(i)); } } }
public void Mate(Chromosome mother, Chromosome father, Chromosome offspring1, Chromosome offspring2) { int num2; int num3; int num4; int count = mother.Genes.Count; goto Label_0116; Label_0097: num4++; Label_009B: if (num4 >= count) { int gene = 0; if ((((uint) count) & 0) != 0) { if (((uint) num3) >= 0) { goto Label_00BB; } goto Label_0097; } while (true) { if (gene >= count) { if ((((uint) num2) + ((uint) count)) < 0) { break; } if ((((uint) num2) - ((uint) num2)) >= 0) { return; } goto Label_0116; } if ((gene < num2) || (gene > num3)) { offspring1.GetGene(gene).Copy(mother.GetGene(gene)); offspring2.GetGene(gene).Copy(father.GetGene(gene)); } gene++; } } Label_00BB: if ((num4 >= num2) && (num4 <= num3)) { offspring1.GetGene(num4).Copy(father.GetGene(num4)); offspring2.GetGene(num4).Copy(mother.GetGene(num4)); if ((((uint) count) & 0) != 0) { goto Label_0116; } } goto Label_0097; Label_0116: num2 = (int) (ThreadSafeRandom.NextDouble() * (count - this._x8bd2fc977ef263b3)); num3 = num2 + this._x8bd2fc977ef263b3; num4 = 0; goto Label_009B; }
/// <summary> /// Construct an innovation list. /// </summary> /// /// <param name="population_0">The population.</param> /// <param name="links">The links.</param> /// <param name="neurons">THe neurons.</param> public NEATInnovationList(IPopulation population_0, Chromosome links, Chromosome neurons) { nextNeuronID = 0; population = population_0; foreach (IGene gene in neurons.Genes) { var neuronGene = (NEATNeuronGene) gene; var innovation = new NEATInnovation(neuronGene, population_0.AssignInnovationID(), AssignNeuronID()); Add(innovation); } foreach (IGene gene_1 in links.Genes) { var linkGene = (NEATLinkGene) gene_1; var innovation_2 = new NEATInnovation( linkGene.FromNeuronID, linkGene.ToNeuronID, NEATInnovationType.NewLink, population.AssignInnovationID()); Add(innovation_2); } }
public void PerformMutation(Chromosome chromosome) { using (List<IGene>.Enumerator enumerator = chromosome.Genes.GetEnumerator()) { IGene gene; DoubleGene gene2; double num; goto Label_0031; Label_000E: if (0 != 0) { goto Label_003C; } if (0 != 0) { goto Label_009D; } goto Label_0031; Label_0019: if ((((uint) num) - ((uint) num)) < 0) { goto Label_000E; } Label_0031: if (enumerator.MoveNext()) { goto Label_008C; } goto Label_0046; Label_003C: if (gene is DoubleGene) { goto Label_0096; } goto Label_0019; Label_0046: if ((((uint) num) + ((uint) num)) >= 0) { return; } goto Label_008C; Label_0060: num += this._x0133fea5e9a09a63 - ((ThreadSafeRandom.NextDouble() * this._x0133fea5e9a09a63) * 2.0); gene2.Value = num; goto Label_000E; Label_008C: gene = enumerator.Current; goto Label_003C; Label_0096: gene2 = (DoubleGene) gene; Label_009D: num = gene2.Value; goto Label_0060; } }
/// <summary> /// Perform a perturb mutation on the specified chromosome. /// </summary> /// /// <param name="chromosome">The chromosome to mutate.</param> public void PerformMutation(Chromosome chromosome) { foreach (IGene gene in chromosome.Genes) { if (gene is DoubleGene) { var doubleGene = (DoubleGene) gene; double v = doubleGene.Value; v += (_perturbAmount - (ThreadSafeRandom.NextDouble()*_perturbAmount*2)); doubleGene.Value = v; } } }
public NEATGenome(NEATGenome other) { goto Label_0182; Label_0017: this.inputCount = other.inputCount; this.outputCount = other.outputCount; Label_002F: this.speciesID = other.speciesID; foreach (IGene gene in other.Neurons.Genes) { NEATNeuronGene gene2 = (NEATNeuronGene) gene; if (0xff != 0) { NEATNeuronGene gene3 = new NEATNeuronGene(gene2.NeuronType, gene2.Id, gene2.SplitY, gene2.SplitX, gene2.Recurrent, gene2.ActivationResponse); this.Neurons.Add(gene3); } } foreach (IGene gene4 in other.Links.Genes) { NEATLinkGene gene5 = (NEATLinkGene) gene4; NEATLinkGene gene6 = new NEATLinkGene((long) gene5.FromNeuronID, (long) gene5.ToNeuronID, gene5.Enabled, gene5.InnovationId, gene5.Weight, gene5.Recurrent); this.Links.Add(gene6); } return; Label_0182: this.neuronsChromosome = new Chromosome(); this.linksChromosome = new Chromosome(); base.GA = other.GA; base.Chromosomes.Add(this.neuronsChromosome); if (0 != 0) { goto Label_002F; } base.Chromosomes.Add(this.linksChromosome); base.GenomeID = other.GenomeID; this.networkDepth = other.networkDepth; base.Population = other.Population; base.Score = other.Score; if (0x7fffffff == 0) { goto Label_0017; } base.AdjustedScore = other.AdjustedScore; if (0 == 0) { base.AmountToSpawn = other.AmountToSpawn; goto Label_0017; } goto Label_0182; }
/// <summary> /// Construct a neural genome. /// </summary> /// /// <param name="network">The network to use.</param> public NeuralGenome(BasicNetwork network) { Organism = network; _networkChromosome = new Chromosome(); // create an array of "double genes" int size = network.Structure.CalculateSize(); for (int i = 0; i < size; i++) { IGene gene = new DoubleGene(); _networkChromosome.Genes.Add(gene); } Chromosomes.Add(_networkChromosome); Encode(); }
public TSPGenome(GeneticAlgorithm owner, City[] cities) { int[] organism = new int[cities.Length]; bool[] taken = new bool[cities.Length]; for (int i = 0; i < organism.Length; i++) { taken[i] = false; } for (int i = 0; i < organism.Length - 1; i++) { int icandidate; do { icandidate = (int)(ThreadSafeRandom.NextDouble() * organism.Length); } while (taken[icandidate]); organism[i] = icandidate; taken[icandidate] = true; if (i == organism.Length - 2) { icandidate = 0; while (taken[icandidate]) { icandidate++; } organism[i + 1] = icandidate; } } this.pathChromosome = new Chromosome(); this.Chromosomes.Add(this.pathChromosome); for (int i = 0; i < organism.Length; i++) { IntegerGene gene = new IntegerGene(); gene.Value = organism[i]; this.pathChromosome.Genes.Add(gene); } Organism = organism; Encode(); }
public NEATInnovationList(IPopulation population_0, Chromosome links, Chromosome neurons) { this.nextNeuronID = 0L; this.population = population_0; foreach (IGene gene in neurons.Genes) { NEATNeuronGene neuronGene = (NEATNeuronGene) gene; NEATInnovation innovation = new NEATInnovation(neuronGene, population_0.AssignInnovationID(), this.AssignNeuronID()); if (15 == 0) { break; } base.Add(innovation); } foreach (IGene gene3 in links.Genes) { NEATLinkGene gene4 = (NEATLinkGene) gene3; NEATInnovation innovation2 = new NEATInnovation((long) gene4.FromNeuronID, (long) gene4.ToNeuronID, NEATInnovationType.NewLink, this.population.AssignInnovationID()); base.Add(innovation2); } }
/// <summary> /// Perform a shuffle mutation. /// </summary> /// /// <param name="chromosome">The chromosome to mutate.</param> public void PerformMutation(Chromosome chromosome) { int length = chromosome.Genes.Count; var iswap1 = (int)(ThreadSafeRandom.NextDouble()*length); var iswap2 = (int)(ThreadSafeRandom.NextDouble() * length); // can't be equal if (iswap1 == iswap2) { // move to the next, but // don't go out of bounds if (iswap1 > 0) { iswap1--; } else { iswap1++; } } // make sure they are in the right order if (iswap1 > iswap2) { int temp = iswap1; iswap1 = iswap2; iswap2 = temp; } IGene gene1 = chromosome.Genes[iswap1]; IGene gene2 = chromosome.Genes[iswap2]; // remove the two genes chromosome.Genes.Remove(gene1); chromosome.Genes.Remove(gene2); // insert them back in, reverse order chromosome.Genes.Insert(iswap1, gene2); chromosome.Genes.Insert(iswap2, gene1); }
/// <summary> /// Assuming this chromosome is the "mother" mate with the passed in /// "father". /// </summary> /// <param name="mother">The mother.</param> /// <param name="father">The father.</param> /// <param name="offspring1">The first offspring.</param> /// <param name="offspring2">The second offspring.</param> public void Mate(Chromosome mother, Chromosome father, Chromosome offspring1, Chromosome offspring2) { int geneLength = father.Genes.Count; // the chromosome must be cut at two positions, determine them var cutpoint1 = (int) (ThreadSafeRandom.NextDouble()*(geneLength - _cutLength)); int cutpoint2 = cutpoint1 + _cutLength; // keep track of which genes have been taken in each of the two // offspring, defaults to false. IList<IGene> taken1 = new List<IGene>(); IList<IGene> taken2 = new List<IGene>(); // handle cut section for (int i = 0; i < geneLength; i++) { if (!((i < cutpoint1) || (i > cutpoint2))) { offspring1.Genes[i].Copy(father.Genes[i]); offspring2.Genes[i].Copy(mother.Genes[i]); taken1.Add(offspring1.Genes[i]); taken2.Add(offspring2.Genes[i]); } } // handle outer sections for (int i = 0; i < geneLength; i++) { if ((i < cutpoint1) || (i > cutpoint2)) { offspring1.Genes[i].Copy( GetNotTaken(mother, taken1)); offspring2.Genes[i].Copy( GetNotTaken(father, taken2)); } } }
public NeuralGenome(BasicNetwork network) { int num; int num2; goto Label_0058; Label_0008: if (num2 >= num) { if ((((uint) num2) & 0) == 0) { base.Chromosomes.Add(this._x8b1a2d051765aa13); this.Encode(); return; } goto Label_0058; } IGene item = new DoubleGene(); this._x8b1a2d051765aa13.Genes.Add(item); Label_0052: num2++; if ((((uint) num) + ((uint) num)) <= uint.MaxValue) { goto Label_0008; } return; Label_0058: base.Organism = network; this._x8b1a2d051765aa13 = new Chromosome(); num = network.Structure.CalculateSize(); num2 = 0; if (0 != 0) { goto Label_0052; } goto Label_0008; }
public NEATGenome(long id, int inputCount_0, int outputCount_1) { double num3; int num4; int num5; int num6; base.GenomeID = id; base.AdjustedScore = 0.0; if (((uint) num5) < 0) { goto Label_00A7; } this.inputCount = inputCount_0; if ((((uint) num4) & 0) != 0) { goto Label_00AC; } this.outputCount = outputCount_1; base.AmountToSpawn = 0.0; this.speciesID = 0L; double num = 0.8 / ((double) inputCount_0); this.neuronsChromosome = new Chromosome(); this.linksChromosome = new Chromosome(); if (2 != 0) { base.Chromosomes.Add(this.neuronsChromosome); base.Chromosomes.Add(this.linksChromosome); for (int i = 0; i < inputCount_0; i++) { this.neuronsChromosome.Add(new NEATNeuronGene(NEATNeuronType.Input, (long) i, 0.0, 0.1 + (i * num))); } this.neuronsChromosome.Add(new NEATNeuronGene(NEATNeuronType.Bias, (long) inputCount_0, 0.0, 0.9)); num3 = 1.0 / ((double) (outputCount_1 + 1)); num4 = 0; goto Label_00C8; } return; Label_0079: while (num6 < outputCount_1) { this.linksChromosome.Add(new NEATLinkGene(((NEATNeuronGene) this.neuronsChromosome.Get(num5)).Id, ((NEATNeuronGene) this.Neurons.Get((inputCount_0 + num6) + 1)).Id, true, (long) (((inputCount_0 + outputCount_1) + 1) + this.NumGenes), RangeRandomizer.Randomize(-1.0, 1.0), false)); num6++; } num5++; Label_0084: if (num5 >= (inputCount_0 + 1)) { if (((uint) outputCount_1) >= 0) { return; } goto Label_0079; } Label_00A7: num6 = 0; goto Label_0079; Label_00AC: num4++; if ((((uint) id) + ((uint) num5)) < 0) { goto Label_00A7; } Label_00C8: if (num4 < outputCount_1) { this.neuronsChromosome.Add(new NEATNeuronGene(NEATNeuronType.Output, (long) ((num4 + inputCount_0) + 1), 1.0, (num4 + 1) * num3)); goto Label_00AC; } if (((uint) inputCount_0) >= 0) { num5 = 0; } goto Label_0084; }
public void PerformMutation(Chromosome chromosome) { int num2; int num3; int num4; IGene gene; IGene gene2; int count = chromosome.Genes.Count; if ((((uint) count) - ((uint) num3)) <= uint.MaxValue) { goto Label_010A; } Label_0027: gene2 = chromosome.Genes[num3]; chromosome.Genes.Remove(gene); chromosome.Genes.Remove(gene2); chromosome.Genes.Insert(num2, gene2); Label_005F: chromosome.Genes.Insert(num3, gene); if ((((uint) count) + ((uint) num4)) >= 0) { return; } goto Label_010A; Label_008F: if (num2 > num3) { num4 = num2; num2 = num3; num3 = num4; if (((uint) num4) < 0) { goto Label_0027; } } Label_0093: gene = chromosome.Genes[num2]; goto Label_0027; Label_00B9: num2++; if ((((uint) num4) + ((uint) num3)) >= 0) { goto Label_008F; } Label_010A: num2 = (int) (ThreadSafeRandom.NextDouble() * count); num3 = (int) (ThreadSafeRandom.NextDouble() * count); if (num2 == num3) { if (num2 > 0) { num2--; if ((((uint) num2) | 0x7fffffff) != 0) { if ((((uint) num4) & 0) != 0) { goto Label_005F; } goto Label_008F; } } else { goto Label_00B9; } if (((uint) num4) > uint.MaxValue) { goto Label_0093; } if ((((uint) num3) + ((uint) num2)) <= uint.MaxValue) { goto Label_00B9; } } goto Label_008F; }
/// <summary> /// Construct a genome, do not provide links and neurons. /// </summary> /// /// <param name="id">The genome id.</param> /// <param name="inputCount_0">The input count.</param> /// <param name="outputCount_1">The output count.</param> public NEATGenome(long id, int inputCount_0, int outputCount_1) { GenomeID = id; AdjustedScore = 0; inputCount = inputCount_0; outputCount = outputCount_1; AmountToSpawn = 0; speciesID = 0; double inputRowSlice = 0.8d/(inputCount_0); neuronsChromosome = new Chromosome(); linksChromosome = new Chromosome(); Chromosomes.Add(neuronsChromosome); Chromosomes.Add(linksChromosome); for (int i = 0; i < inputCount_0; i++) { neuronsChromosome.Add(new NEATNeuronGene(NEATNeuronType.Input, i, 0, 0.1d + i*inputRowSlice)); } neuronsChromosome.Add(new NEATNeuronGene(NEATNeuronType.Bias, inputCount_0, 0, 0.9d)); double outputRowSlice = 1/(double) (outputCount_1 + 1); for (int i_2 = 0; i_2 < outputCount_1; i_2++) { neuronsChromosome.Add(new NEATNeuronGene( NEATNeuronType.Output, i_2 + inputCount_0 + 1, 1, (i_2 + 1) *outputRowSlice)); } for (int i_3 = 0; i_3 < inputCount_0 + 1; i_3++) { for (int j = 0; j < outputCount_1; j++) { linksChromosome.Add(new NEATLinkGene( ((NEATNeuronGene) neuronsChromosome.Get(i_3)).Id, ((NEATNeuronGene) Neurons.Get( inputCount_0 + j + 1)).Id, true, inputCount_0 + outputCount_1 + 1 + NumGenes, RangeRandomizer.Randomize(-1, 1), false)); } } }
/// <summary> /// Get a list of the genes that have not been taken before. This is useful /// if you do not wish the same gene to appear more than once in a /// chromosome. /// </summary> /// <param name="source">The pool of genes to select from.</param> /// <param name="taken">An array of the taken genes.</param> /// <returns>Those genes in source that are not taken.</returns> private static IGene GetNotTaken(Chromosome source, IList<IGene> taken) { int geneLength = source.Genes.Count; for (int i = 0; i < geneLength; i++) { IGene trial = source.Genes[i]; bool found = Enumerable.Contains(taken, trial); if (!found) { taken.Add(trial); return trial; } } return null; }
/// <summary> /// Construct a genome by copying another. /// </summary> /// /// <param name="other">The other genome.</param> public NEATGenome(NEATGenome other) { neuronsChromosome = new Chromosome(); linksChromosome = new Chromosome(); GA = other.GA; Chromosomes.Add(neuronsChromosome); Chromosomes.Add(linksChromosome); GenomeID = other.GenomeID; networkDepth = other.networkDepth; Population = other.Population; Score = other.Score; AdjustedScore = other.AdjustedScore; AmountToSpawn = other.AmountToSpawn; inputCount = other.inputCount; outputCount = other.outputCount; speciesID = other.speciesID; // copy neurons foreach (IGene gene in other.Neurons.Genes) { var oldGene = (NEATNeuronGene) gene; var newGene = new NEATNeuronGene( oldGene.NeuronType, oldGene.Id, oldGene.SplitY, oldGene.SplitX, oldGene.Recurrent, oldGene.ActivationResponse); Neurons.Add(newGene); } // copy links foreach (IGene gene_0 in other.Links.Genes) { var oldGene_1 = (NEATLinkGene) gene_0; var newGene_2 = new NEATLinkGene( oldGene_1.FromNeuronID, oldGene_1.ToNeuronID, oldGene_1.Enabled, oldGene_1.InnovationId, oldGene_1.Weight, oldGene_1.Recurrent); Links.Add(newGene_2); } }
public NEATGenome Crossover(NEATGenome mom, NEATGenome dad) { x47361738465134f2 xf; Chromosome chromosome; Chromosome chromosome2; List<long> list; int num; int num2; NEATLinkGene gene; NEATLinkGene gene2; NEATLinkGene gene3; int num3; NEATGenome genome; if (mom.Score != dad.Score) { while (base.Comparator.IsBetterThan(mom.Score, dad.Score)) { xf = x47361738465134f2.x431cb293a9d996e8; if (((uint) num3) >= 0) { if ((((uint) num3) & 0) == 0) { goto Label_03BA; } goto Label_0399; } if ((((uint) num) & 0) == 0) { goto Label_0478; } } xf = x47361738465134f2.x86f3d3d96a9a0f93; goto Label_03BA; } goto Label_0478; Label_002E: genome = new NEATGenome(base.Population.AssignGenomeID(), chromosome, chromosome2, mom.InputCount, mom.OutputCount); genome.GA = this; if (15 != 0) { if (0 != 0) { goto Label_031C; } genome.Population = base.Population; if ((((uint) num3) & 0) == 0) { return genome; } goto Label_02C3; } goto Label_00B2; Label_0071: num3++; Label_0077: if (num3 < list.Count) { chromosome.Add(this.Innovations.CreateNeuronFromID(list[num3])); goto Label_0071; } goto Label_002E; Label_00B2: this.AddNeuronID((long) gene3.FromNeuronID, list); this.AddNeuronID((long) gene3.ToNeuronID, list); Label_00D0: if ((num < mom.NumGenes) || (num2 < dad.NumGenes)) { if (num >= mom.NumGenes) { gene = null; } else { gene = (NEATLinkGene) mom.Links.Get(num); } goto Label_034F; } list.Sort(); num3 = 0; if ((((uint) num2) + ((uint) num)) > uint.MaxValue) { goto Label_0071; } if (0 != 0) { goto Label_034F; } goto Label_0077; Label_0160: chromosome2.Add(gene3); goto Label_00B2; Label_016D: if (chromosome2.Size() == 0) { goto Label_0160; } if ((0 != 0) || (((NEATLinkGene) chromosome2.Get(chromosome2.Size() - 1)).InnovationId != gene3.InnovationId)) { chromosome2.Add(gene3); } goto Label_00B2; Label_01C8: num++; if (((uint) num) <= uint.MaxValue) { if (((uint) num3) > uint.MaxValue) { goto Label_002E; } if ((((uint) num2) | 4) == 0) { goto Label_025D; } num2++; goto Label_016D; } Label_01E0: if ((((uint) num2) | 2) != 0) { if ((((uint) num3) + ((uint) num3)) <= uint.MaxValue) { goto Label_016D; } goto Label_0160; } if (-2147483648 != 0) { goto Label_0234; } if ((((uint) num) + ((uint) num3)) < 0) { goto Label_024D; } Label_0222: while (ThreadSafeRandom.NextDouble() < 0.5) { gene3 = gene; goto Label_01C8; } gene3 = gene2; goto Label_01C8; Label_0234: gene3 = gene2; Label_0238: num2++; goto Label_016D; Label_024D: if (gene.InnovationId < gene2.InnovationId) { if (xf == x47361738465134f2.x431cb293a9d996e8) { gene3 = gene; } num++; goto Label_016D; } Label_025D: if (gene2.InnovationId >= gene.InnovationId) { if (gene2.InnovationId == gene.InnovationId) { goto Label_0222; } goto Label_01E0; } if ((((uint) num2) + ((uint) num)) > uint.MaxValue) { goto Label_024D; } if (xf != x47361738465134f2.x86f3d3d96a9a0f93) { goto Label_0238; } goto Label_0234; Label_02C3: if (gene2 == null) { if (gene != null) { if (xf == x47361738465134f2.x431cb293a9d996e8) { gene3 = gene; if ((((uint) num2) + ((uint) num)) > uint.MaxValue) { goto Label_03BA; } } num++; goto Label_016D; } if ((((uint) num3) | 0xfffffffe) != 0) { } } goto Label_024D; Label_031C: if (gene2 == null) { goto Label_02C3; } if (xf == x47361738465134f2.x86f3d3d96a9a0f93) { if (1 == 0) { goto Label_01C8; } gene3 = gene2; } num2++; goto Label_016D; Label_034F: if (num2 < dad.NumGenes) { gene2 = (NEATLinkGene) dad.Links.Get(num2); } else { gene2 = null; } if (gene != null) { goto Label_02C3; } goto Label_031C; Label_0399: if ((((uint) num) & 0) != 0) { goto Label_00B2; } gene3 = null; goto Label_00D0; Label_03BA: chromosome = new Chromosome(); chromosome2 = new Chromosome(); list = new List<long>(); num = 0; num2 = 0; if (0 == 0) { goto Label_0399; } Label_0478: if (mom.NumGenes != dad.NumGenes) { if (mom.NumGenes < dad.NumGenes) { xf = x47361738465134f2.x431cb293a9d996e8; } else { xf = x47361738465134f2.x86f3d3d96a9a0f93; } } else if (((((uint) num) - ((uint) num3)) >= 0) && (ThreadSafeRandom.NextDouble() <= 0.0)) { if ((((uint) num3) + ((uint) num2)) <= uint.MaxValue) { xf = x47361738465134f2.x86f3d3d96a9a0f93; } } else { xf = x47361738465134f2.x431cb293a9d996e8; } goto Label_03BA; }
public void Mate(Chromosome mother, Chromosome father, Chromosome offspring1, Chromosome offspring2) { int num2; int num3; IList<IGene> list; IList<IGene> list2; int num4; int num5; int count = father.Genes.Count; goto Label_0148; Label_002C: num5++; Label_0032: if (num5 < count) { if (num5 < num2) { goto Label_0047; } if ((((uint) num2) + ((uint) num5)) > uint.MaxValue) { goto Label_0162; } } else { if (0 == 0) { return; } goto Label_0148; } Label_0042: if (num5 <= num3) { goto Label_002C; } Label_0047: offspring1.Genes[num5].Copy(x1a4a8d31470092e5(mother, list)); if (-1 != 0) { offspring2.Genes[num5].Copy(x1a4a8d31470092e5(father, list2)); goto Label_002C; } return; Label_00A6: if (num4 >= count) { if (((uint) num3) > uint.MaxValue) { goto Label_00CE; } num5 = 0; if ((((uint) num3) + ((uint) count)) < 0) { goto Label_0042; } goto Label_0032; } if ((num4 >= num2) && (num4 <= num3)) { offspring1.Genes[num4].Copy(father.Genes[num4]); offspring2.Genes[num4].Copy(mother.Genes[num4]); list.Add(offspring1.Genes[num4]); list2.Add(offspring2.Genes[num4]); } Label_00CE: num4++; goto Label_00A6; Label_0148: num2 = (int) (ThreadSafeRandom.NextDouble() * (count - this._x8bd2fc977ef263b3)); num3 = num2 + this._x8bd2fc977ef263b3; Label_0162: list = new List<IGene>(); list2 = new List<IGene>(); num4 = 0; goto Label_00A6; }
private static IGene x1a4a8d31470092e5(Chromosome x337e217cb3ba0627, IList<IGene> xa41924b42e357ba2) { IGene gene; int count = x337e217cb3ba0627.Genes.Count; int num2 = 0; Label_0030: if (num2 < count) { gene = x337e217cb3ba0627.Genes[num2]; } else if ((((uint) num2) | 1) != 0) { goto Label_0081; } bool flag = xa41924b42e357ba2.Contains<IGene>(gene); if ((((uint) flag) + ((uint) count)) <= uint.MaxValue) { if (((uint) count) >= 0) { } if (!flag) { xa41924b42e357ba2.Add(gene); return gene; } num2++; goto Label_0030; } Label_0081: return null; }
/// <summary> /// Perform a cross over. /// </summary> /// <param name="mom">The mother genome.</param> /// <param name="dad">The father genome.</param> /// <returns></returns> public new NEATGenome Crossover(NEATGenome mom, NEATGenome dad) { NEATParent best; // first determine who is more fit, the mother or the father? if (mom.Score == dad.Score) { if (mom.NumGenes == dad.NumGenes) { if (ThreadSafeRandom.NextDouble() > 0) { best = NEATParent.Mom; } else { best = NEATParent.Dad; } } else { if (mom.NumGenes < dad.NumGenes) { best = NEATParent.Mom; } else { best = NEATParent.Dad; } } } else { if (Comparator.IsBetterThan(mom.Score, dad.Score)) { best = NEATParent.Mom; } else { best = NEATParent.Dad; } } var babyNeurons = new Chromosome(); var babyGenes = new Chromosome(); var vecNeurons = new List<long>(); int curMom = 0; int curDad = 0; NEATLinkGene momGene; NEATLinkGene dadGene; NEATLinkGene selectedGene = null; while ((curMom < mom.NumGenes) || (curDad < dad.NumGenes)) { if (curMom < mom.NumGenes) { momGene = (NEATLinkGene) mom.Links.Get(curMom); } else { momGene = null; } if (curDad < dad.NumGenes) { dadGene = (NEATLinkGene) dad.Links.Get(curDad); } else { dadGene = null; } if ((momGene == null) && (dadGene != null)) { if (best == NEATParent.Dad) { selectedGene = dadGene; } curDad++; } else if ((dadGene == null) && (momGene != null)) { if (best == NEATParent.Mom) { selectedGene = momGene; } curMom++; } else if (momGene.InnovationId < dadGene.InnovationId) { if (best == NEATParent.Mom) { selectedGene = momGene; } curMom++; } else if (dadGene.InnovationId < momGene.InnovationId) { if (best == NEATParent.Dad) { selectedGene = dadGene; } curDad++; } else if (dadGene.InnovationId == momGene.InnovationId) { if (ThreadSafeRandom.NextDouble() < 0.5f) { selectedGene = momGene; } else { selectedGene = dadGene; } curMom++; curDad++; } if (babyGenes.Size() == 0) { babyGenes.Add(selectedGene); } else { if (((NEATLinkGene) babyGenes.Get(babyGenes.Size() - 1)) .InnovationId != selectedGene.InnovationId) { babyGenes.Add(selectedGene); } } // Check if we already have the nodes referred to in SelectedGene. // If not, they need to be added. AddNeuronID(selectedGene.FromNeuronID, vecNeurons); AddNeuronID(selectedGene.ToNeuronID, vecNeurons); } // end while // now create the required nodes. First sort them into order vecNeurons.Sort(); for (int i = 0; i < vecNeurons.Count; i++) { babyNeurons.Add(Innovations.CreateNeuronFromID( vecNeurons[i])); } // finally, create the genome var babyGenome = new NEATGenome(Population .AssignGenomeID(), babyNeurons, babyGenes, mom.InputCount, mom.OutputCount); babyGenome.GA = this; babyGenome.Population = Population; return babyGenome; }
public NEATGenome(long genomeID, Chromosome neurons, Chromosome links, int inputCount_0, int outputCount_1) { base.GenomeID = genomeID; while (true) { this.linksChromosome = links; this.neuronsChromosome = neurons; base.AmountToSpawn = 0.0; base.AdjustedScore = 0.0; this.inputCount = inputCount_0; this.outputCount = outputCount_1; base.Chromosomes.Add(this.neuronsChromosome); if (0 == 0) { base.Chromosomes.Add(this.linksChromosome); return; } } }
/// <summary> /// Create a NEAT gnome. /// </summary> /// /// <param name="genomeID">The genome id.</param> /// <param name="neurons">The neurons.</param> /// <param name="links">The links.</param> /// <param name="inputCount_0">The input count.</param> /// <param name="outputCount_1">The output count.</param> public NEATGenome(long genomeID, Chromosome neurons, Chromosome links, int inputCount_0, int outputCount_1) { GenomeID = genomeID; linksChromosome = links; neuronsChromosome = neurons; AmountToSpawn = 0; AdjustedScore = 0; inputCount = inputCount_0; outputCount = outputCount_1; Chromosomes.Add(neuronsChromosome); Chromosomes.Add(linksChromosome); }