public void Crossover(ref Genome genome2, out Genome child1, out Genome child2) { int pos = (int)(random.NextDouble() * (double)Length); child1 = new Genome(Length, false); child2 = new Genome(Length, false); for (int i = 0; i < Length; i++) { if (i < pos) { child1.Genes[i] = Genes[i]; child2.Genes[i] = genome2.Genes[i]; } else { child1.Genes[i] = genome2.Genes[i]; child2.Genes[i] = Genes[i]; } } }
public abstract void CopyGeneInfo(Genome g);
public abstract void CopyGeneFrom(Genome src);
private void Mutate(Genome aGene) { if (EquationGenome.TheSeed.Next(100) < (int)(kMutationFrequency * 100.0)) { aGene.Mutate(); } }
public void CheckForUndefinedFitness(Genome g) { if (double.IsNaN(g.CurrentFitness)) g.CurrentFitness = 0.01f; }
public Genome <T> Evolve(List <Genome <T> > items) { Solutions.AddRange(items); for (int i = 0; i < GenerationCount; i++) { for (int k = 0; k < PopulationSize; k++) { _firstGeneration.Invoke(Solutions[k]); _fitnessValue.Invoke(Solutions[k]); } Solutions.OrderByDescending(t => t.Fitness); double minimalFitness = Solutions.Where(x => !x.Fitness.Equals(0)).Sum(t => t.Fitness) / PopulationSize - Solutions.Count(x => !x.Fitness.Equals(0)); while (NextGeneration.Count < PopulationSize) { for (int m = 0; m < Solutions.Count; m++) { if (NextGeneration.Count < PopulationSize) { if (Solutions[m].Fitness >= minimalFitness && rnd.NextDouble() <= CrossoverProbability) { if (_crossoverPartner == null) { _crossoverPartner = Solutions[m]; } else { _crossedGenomes = _crossover.Invoke(_crossoverPartner, Solutions[m]); NextGeneration.Add(new Genome <T>(_crossedGenomes[0])); NextGeneration.Add(new Genome <T>(_crossedGenomes[1])); _crossoverPartner = null; } } if (rnd.NextDouble() <= MutationProbability) { NextGeneration.Add(new Genome <T>(_mutation.Invoke(Solutions[m]))); } } else { break; } } } while (NextGeneration.Count > PopulationSize) { NextGeneration.RemoveAt(NextGeneration.Count - 1); } if (_bestFitness != null) { if (_bestFitness.Fitness <= Solutions[0].Fitness) { _bestFitness = Solutions[0]; } } else { _bestFitness = Solutions[0]; } Solutions.RemoveAll(t => t.Parameter != null); Solutions.AddRange(NextGeneration); NextGeneration.RemoveAll(t => t.Parameter != null); } return(_bestFitness); }
public void Start() { //Setup vars int popSize = 200; int survivors = 70; //Init network, population Network n = new Network(2, 3, 3, 1); List <(double fitness, Genome genome)> population = new List <(double, Genome)>(); for (int i = 0; i < popSize; i++) { population.Add((0, new Genome(2, 3, 3, 1))); } //Fitness func 0-4 Func <Network, double> fitness = (Network net) => { double score = 0; score -= Util.MeanSquaredLoss(new double[] { 0 }, n.Eval(new double[] { 0, 0 })) * 5000; score -= Util.MeanSquaredLoss(new double[] { 1 }, n.Eval(new double[] { 0, 1 })) * 5000; score -= Util.MeanSquaredLoss(new double[] { 1 }, n.Eval(new double[] { 1, 0 })) * 5000; score -= Util.MeanSquaredLoss(new double[] { 0 }, n.Eval(new double[] { 1, 1 })) * 5000; return(score); }; //Start training int epoch = 0; double bestScore = double.MinValue; while (bestScore < -0.05) { for (int i = 0; i < population.Count; i++) { //Get fitness of each n.InsertGenome(population[i].genome); population[i] = (fitness(n), population[i].genome); } //Order by fitness, kill off population = population.OrderByDescending(x => x.fitness).Take(survivors).ToList(); bestScore = population[0].fitness; Console.WriteLine("Gen {0}, best fit {1}", epoch, bestScore); //Parent selection List <(double, Genome)> offspring = new List <(double, Genome)>(); while (offspring.Count < popSize - survivors) { Genome[] parents = new Genome[2]; do { //Tournament for (int i = 0; i < 2; i++) { parents[i] = population.OrderBy(x => Util.Rand.Next()). Take(7) .OrderByDescending(x => x.fitness) .First().genome; } }while (parents[0] == parents[1]); //Breed and mutate var children = parents[0].CrossoverUniform(parents[1]); var childA = children.childA; var childB = children.childB; childA = Util.Rand.NextDouble() <= 0.1 ? childA.MutationDelta(3, 0.3, 0.3) : childA; offspring.Add((0, childA)); childB = Util.Rand.NextDouble() <= 0.1 ? childB.MutationDelta(3, 0.3, 0.3) : childB; offspring.Add((0, childB)); } //Add offspring population.AddRange(offspring); epoch++; } TestXOR(n); Console.ReadKey(); }
public abstract Genome UniformCrossover(Genome g);
public override Genome Crossover(Genome g) { EquationGenome aGene1 = new EquationGenome(); EquationGenome aGene2 = new EquationGenome(); g.CopyGeneInfo(aGene1); g.CopyGeneInfo(aGene2); // Pick a random crossover point CrossoverPoint = TheSeed.Next(1, (int)Length); EquationGenome CrossingGene = (EquationGenome)g; for (int i = 0; i < CrossoverPoint; i++) { aGene1.TheArray.Add(CrossingGene.TheArray[i]); aGene2.TheArray.Add(TheArray[i]); } for (int j = CrossoverPoint; j < Length; j++) { aGene1.TheArray.Add(TheArray[j]); aGene2.TheArray.Add(CrossingGene.TheArray[j]); } // 50/50 chance of returning gene1 or gene2 EquationGenome aGene = null; if (TheSeed.Next(2) == 1) { aGene = aGene1; } else { aGene = aGene2; } return aGene; }
public override void CopyGeneInfo(Genome dest) { EquationGenome theGene = (EquationGenome)dest; theGene.Length = Length; theGene.TheMin = TheMin; theGene.TheMax = TheMax; }
public void AddHuman(Genome genome) { this.Citizens.Add(genome); }
public Genome(Genome copy) { this.values = new double[2][]; this.Weights = copy.Weights; this.Biases = copy.Biases; }
public Genome DeepCopy() { Genome g = new Genome(Length, false); Array.Copy(Genes, g.Genes, Length); return g; }
public List <double> GetWeights() => Genome.Select(value => value.Data / 1000000.0).ToList();
public abstract Genome Crossover(Genome g);
public override Genome Crossover2Point(Genome g) { EquationGenome aGene1 = new EquationGenome(); EquationGenome aGene2 = new EquationGenome(); g.CopyGeneInfo(aGene1); g.CopyGeneInfo(aGene2); // Pick a random crossover point int CrossoverPoint1 = TheSeed.Next(1, (int)Length); int CrossoverPoint2 = TheSeed.Next(CrossoverPoint1, (int)Length); // normalize if (CrossoverPoint1 > CrossoverPoint2) { int temp = CrossoverPoint1; CrossoverPoint1 = CrossoverPoint2; CrossoverPoint2 = temp; } EquationGenome CrossingGene = (EquationGenome)g; for (int j = 0; j < CrossoverPoint1; j++) { aGene1.TheArray.Add(TheArray[j]); aGene2.TheArray.Add(CrossingGene.TheArray[j]); } for (int j = CrossoverPoint1; j < CrossoverPoint2; j++) { aGene1.TheArray.Add(CrossingGene.TheArray[j]); aGene2.TheArray.Add(TheArray[j]); } for (int j = CrossoverPoint2; j < Length; j++) { aGene1.TheArray.Add(TheArray[j]); aGene2.TheArray.Add(CrossingGene.TheArray[j]); } // 50/50 chance of returning gene1 or gene2 EquationGenome aGene = null; if (TheSeed.Next(2) == 1) { aGene = aGene1; } else { aGene = aGene2; } return aGene; }
public abstract Genome Crossover2Point(Genome g);
public override Genome UniformCrossover(Genome g) { EquationGenome aGene1 = new EquationGenome(); EquationGenome aGene2 = new EquationGenome(); g.CopyGeneInfo(aGene1); g.CopyGeneInfo(aGene2); // swap genes randomly EquationGenome CrossingGene = (EquationGenome)g; for (int i = 0; i < Length; i++) { if (TheSeed.Next(100) % 2 == 0) { aGene1.TheArray.Add(CrossingGene.TheArray[i]); aGene2.TheArray.Add(TheArray[i]); } else { aGene1.TheArray.Add(TheArray[i]); aGene2.TheArray.Add(CrossingGene.TheArray[i]); } } // 50/50 chance of returning gene1 or gene2 EquationGenome aGene = null; if (TheSeed.Next(2) == 1) { aGene = aGene1; } else { aGene = aGene2; } return aGene; }
/// <summary> /// Create the *initial* genomes by repeated calling the supplied fitness function /// </summary> private void CreateGenomes() { for (int i = 0; i < PopulationSize; i++) { Genome g = new Genome(GenomeSize); CurrentGeneration.Add(g); } }
public override void CopyGeneFrom(Genome src) { EquationGenome theGene = (EquationGenome)src; for (int i = 0; i < TheArray.Count; i++) { TheArray[i] = (Gene)theGene.TheArray[i]; } Length = theGene.Length; TheMin = theGene.TheMin; TheMax = theGene.TheMax; CurrentFitness = theGene.CurrentFitness; }