public void StreamWithInitialGenotypes() { var problem = Problem.Of( a => a, Codec.Of( () => Genotype.Of(IntegerChromosome.Of(0, 1000)), g => g.Gene.Allele ) ); const int genotypeCount = 10; const int max = 1000; var genotypes = IntRange.Of(1, genotypeCount) .Select(i => IntegerChromosome.Of(IntegerGene.Of(max, 0, max))) .Select(i => Genotype.Of(i)) .ToImmutableSeq(); var engine = Engine.Builder(problem).Build(); var result = engine.Stream(genotypes) .Take(1) .ToBestEvolutionResult(); long maxCount = result.GetPopulation().Count(pt => pt.GetFitness() == max); Assert.True(maxCount >= genotypeCount, $"{maxCount} >= {genotypeCount}"); }
/// <summary> /// Mutates a gene by bitwise mutation. /// </summary> /// <param name="gene"></param> /// <returns></returns> public static Gene Mutate(Gene gene) { if (gene is BinaryGene) { BinaryGene g = (BinaryGene)gene.Clone(); g.Value = !(BinaryGene)gene; return(g); } else if (gene is DoubleGene) { DoubleGene g = (DoubleGene)gene.Clone(); byte[] bytes = BitConverter.GetBytes(g.Value); BitArray ba = new BitArray(bytes); int p = Utils.Rand.Next(ba.Length); ba.Set(p, !ba[p]); ba.CopyTo(bytes, 0); g.Value = BitConverter.ToDouble(bytes, 0); return(g); } else if (gene is IntegerGene) { IntegerGene g = (IntegerGene)gene.Clone(); byte[] bytes = BitConverter.GetBytes(g.Value); BitArray ba = new BitArray(bytes); int p = Utils.Rand.Next(ba.Length); ba.Set(p, !ba[p]); ba.CopyTo(bytes, 0); g.Value = BitConverter.ToInt32(bytes, 0); return(g); } return((Gene)gene.Clone()); // default }
/// <summary> /// Mutates a gene by applying boundary mutation. /// </summary> /// <param name="gene">An IntegerGene or DoubleGene to be mutated.</param> /// <returns></returns> public static Gene Mutate(Gene gene) { if (gene is IntegerGene) { IntegerGene ig = (IntegerGene)gene; if (genX.Utils.Rand.NextDouble() < 0.5) { ig.Value = ig.Descriptor.MaxValue; } else { ig.Value = ig.Descriptor.MinValue; } } else if (gene is DoubleGene) { DoubleGene dg = (DoubleGene)gene; if (genX.Utils.Rand.NextDouble() < 0.5) { dg.Value = dg.Descriptor.MaxValue; } else { dg.Value = dg.Descriptor.MinValue; } } else { throw new ArgumentException( "Boundary mutation may only apply to IntegerGene or DoubleGene genes.", "gene" ); } return(gene); }
public double GetObjective(Chromosome c) { double distance = 0; int currentPosition = 0; int initialPosition = 0; for (int i = 0; i < c.Genes.Length; i++) { IntegerGene ig = (IntegerGene)c.Genes[i]; if (i == 0) { initialPosition = cities[ig.Label]; currentPosition = cities[ig.Label]; } else { distance += Math.Abs(cities[ig.Label] - currentPosition); currentPosition = cities[ig.Label]; } } IntegerGene lastGene = (IntegerGene)c.Genes[c.Genes.Length - 1]; distance += Math.Abs(cities[lastGene.Label] - initialPosition); return(distance); }
public void ItCanMutateRandomly() { var numberGene = new IntegerGene(_r); Assert.AreEqual(1, numberGene.GetNumberOfGeneAttributes()); numberGene.Mutate(MutationStrategy.Random); Assert.AreEqual(8, numberGene.Value); }
public void ItCanBoundaryMutate() { var numberGene = new IntegerGene(_r); Assert.AreEqual(1, numberGene.GetNumberOfGeneAttributes()); numberGene.Mutate(MutationStrategy.Boundary); Assert.AreEqual(9, numberGene.Value); }
public double LinearDiophantineObjective(Chromosome c) { double r = 0; for (int i = 0; i < Coefficients.Length; i++) { IntegerGene g = (IntegerGene)c.Genes[i]; r += ((double)Coefficients[i] * (double)g.Value); } return(System.Math.Abs((double)Result - r)); }
public TSPGenome(GeneticAlgorithm owner, City[] cities) { var organism = new int[cities.Length]; var 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; } } pathChromosome = new Chromosome(); Chromosomes.Add(pathChromosome); for (int i = 0; i < organism.Length; i++) { var gene = new IntegerGene(); gene.Value = organism[i]; pathChromosome.Genes.Add(gene); } Organism = organism; Encode(); }
/// <summary> /// 遺伝子を生成します /// </summary> /// <param name="chromosomesType"></param> /// <returns></returns> public static IntegerGene GenerateGene(ChromosomesType chromosomesType, Func <List <int>, double> calcFunc, int geneLength) { var gene = new IntegerGene { GenerationNum = 0, CalcFunc = calcFunc, Fittness = 0, }; switch (chromosomesType) { case ChromosomesType.Binary: gene.Chromosomes = GenerateBinaryChromosomes(geneLength); break; case ChromosomesType.Permutaion: gene.Chromosomes = GeneratePermutationChromosomes(geneLength); break; } return(gene); }
private static EvolutionResult <IntegerGene, int> NewResult(Optimize opt, int value) { const int length = 1000; Func <Genotype <IntegerGene>, int> F = delegate(Genotype <IntegerGene> gt) { return(gt.Gene.Allele); }; var pop = new Population <IntegerGene, int>(length); for (var i = 0; i < length; ++i) { var gt = Genotype.Of(IntegerChromosome.Of( IntegerGene.Of(value, 0, length) )); pop.Add(Phenotype.Of(gt, 1, F)); } Shuffle(pop, RandomRegistry.GetRandom()); return(EvolutionResult.Of(opt, pop, 0, 0, EvolutionDurations.Zero, 0, 0, 0)); }
public void BestWorstPhenotype() { const int length = 100; Func <Genotype <IntegerGene>, int> F = delegate(Genotype <IntegerGene> gt) { return(gt.Gene.Allele); }; var population = new Population <IntegerGene, int>(length); for (var i = 0; i < length; ++i) { var gt = Genotype.Of(IntegerChromosome.Of( IntegerGene.Of(i, 0, length) )); population.Add(Phenotype.Of(gt, 1, F)); } Shuffle(population, RandomRegistry.GetRandom()); var maxResult = EvolutionResult.Of( Optimize.Maximum, population, 0, 0, EvolutionDurations.Zero, 0, 0, 0 ); Assert.Equal(length - 1, maxResult.GetBestFitness()); Assert.Equal(0, maxResult.GetWorstFitness()); var minResult = EvolutionResult.Of( Optimize.Minimum, population, 0, 0, EvolutionDurations.Zero, 0, 0, 0 ); Assert.Equal(0, minResult.GetBestFitness()); Assert.Equal(length - 1, minResult.GetWorstFitness()); }
public void CompareTo() { const int length = 100; Func <Genotype <IntegerGene>, int> F = delegate(Genotype <IntegerGene> gt) { return(gt.Gene.Allele); }; var small = new Population <IntegerGene, int>(length); for (var i = 0; i < length; ++i) { var gt = Genotype.Of(IntegerChromosome.Of( IntegerGene.Of(i, 0, length) )); small.Add(Phenotype.Of(gt, 1, F)); } Shuffle(small, RandomRegistry.GetRandom()); var big = new Population <IntegerGene, int>(length); for (var i = 0; i < length; ++i) { var gt = Genotype.Of(IntegerChromosome.Of( IntegerGene.Of(i + length, 0, length) )); big.Add(Phenotype.Of(gt, 1, F)); } Shuffle(big, RandomRegistry.GetRandom()); var smallMaxResult = EvolutionResult.Of( Optimize.Maximum, small, 0, 0, EvolutionDurations.Zero, 0, 0, 0 ); var bigMaxResult = EvolutionResult.Of( Optimize.Maximum, big, 0, 0, EvolutionDurations.Zero, 0, 0, 0 ); Assert.True(smallMaxResult.CompareTo(bigMaxResult) < 0); Assert.True(bigMaxResult.CompareTo(smallMaxResult) > 0); Assert.True(smallMaxResult.CompareTo(smallMaxResult) == 0); Assert.True(bigMaxResult.CompareTo(bigMaxResult) == 0); var smallMinResult = EvolutionResult.Of( Optimize.Minimum, small, 0, 0, EvolutionDurations.Zero, 0, 0, 0 ); var bigMinResult = EvolutionResult.Of( Optimize.Minimum, big, 0, 0, EvolutionDurations.Zero, 0, 0, 0 ); Assert.True(smallMinResult.CompareTo(bigMinResult) > 0); Assert.True(bigMinResult.CompareTo(smallMinResult) < 0); Assert.True(smallMinResult.CompareTo(smallMinResult) == 0); Assert.True(bigMinResult.CompareTo(bigMinResult) == 0); }
public void ItCanCreateWithRandomValues() { var gene = new IntegerGene(_r); Assert.AreEqual(3, gene.Value); }
public void ItCanDetermineTheNumberOfGeneAttributesOnAGene() { var numberGene = new IntegerGene(_r); Assert.AreEqual(1, numberGene.GetNumberOfGeneAttributes()); }