public static PermutationChromosome CreateTestChromosome2() { var genes2 = new int[] { 4, 5, 9, 0, 3, 8, 2, 7, 6, 1 }; var chromosome2 = new PermutationChromosome(genes2); return(chromosome2); }
static void Main(string[] args) { int populationSize = 140; int queensCount = 50; var fitnessFunction = new MyProblemFitness(); var chromosome = new PermutationChromosome(queensCount); var selection = new EliteSelection(); Population population = new Population(populationSize, chromosome, fitnessFunction, selection); int i = 1; while (true) { population.RunEpoch(); ushort[] bestValue = ((PermutationChromosome)population.BestChromosome).Value; if (population.BestChromosome.Fitness == queensCount * (queensCount - 1) / 2) { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine(i + " - F = " + population.BestChromosome.Fitness + " " + string.Join(",", bestValue)); Console.ForegroundColor = ConsoleColor.White; break; } else { Console.WriteLine(i + " - F = " + population.BestChromosome.Fitness + " " + string.Join(",", bestValue)); } i++; } Console.ReadLine(); }
public static PermutationChromosome CreateTestChromosome1() { var genes1 = new int[] { 7, 2, 8, 4, 1, 6, 3, 5, 9, 0 }; var chromosome1 = new PermutationChromosome(genes1); return(chromosome1); }
public static void Main() { //RandomRegistry.SetRandom(new io.jenetics.prngine.LCG64ShiftRandom.ThreadLocal()); var engine = Engine.Engine .Builder( Length, PermutationChromosome.OfInteger(20)) .Optimize(Optimize.Minimum) .PopulationSize(1000) .OffspringFraction(0.9) .Alterers( new SwapMutator <EnumGene <int>, int>(0.01), new PartiallyMatchedCrossover <int, int>(0.3)) .Build(); var statistics = EvolutionStatistics.OfNumber <int>(); var result = engine.Stream() .TakeWhile(Limits.BySteadyFitness <EnumGene <int>, int>(100)) .Take(2500) .Peek(statistics.Accept) .ToBestEvolutionResult(); Console.WriteLine(statistics); Console.WriteLine(result.GetBestPhenotype()); }
private IList <Tuple <int, int> > FindIdenticalRegions(PermutationChromosome x, PermutationChromosome crossover) { var regions = new List <Tuple <int, int> >(); var same = new bool[GENE_COUNT]; for (var i = 0; i < GENE_COUNT; i++) { same[i] = crossover.Permutation[i] == x.Permutation[i]; } for (var i = 0; i < GENE_COUNT; i++) { if (same[i]) { var start = i; while (same[i]) { i++; } var end = i; regions.Add(new Tuple <int, int>(start, end)); } } return(regions); }
public static ICodec <int[], EnumGene <int> > OfPermutation(int length) { Positive(length); return(Codec.Of( () => Genotype.Of(PermutationChromosome.OfInteger(length)), gt => gt.GetChromosome().ToSeq() .Select(g => g.Allele) .ToArray() )); }
private void VerifyChangeOccured(PermutationChromosome x, PermutationChromosome y) { var diff = 0; for (var i = 0; i < GENE_COUNT; i++) { if (x.Permutation[i] != y.Permutation[i]) { diff++; } } Assert.IsTrue(diff > 0, "Mutation failed to change chromosome data."); }
public static ICodec <IImmutableSeq <T>, EnumGene <T> > OfSubSet <T>( IImmutableSeq <T> basicSet, int size ) { NonNull(basicSet); Base.CheckSubSet(basicSet.Length, size); return(Codec.Of( () => Genotype.Of(PermutationChromosome.Of(basicSet, size)), gt => gt.GetChromosome() .Select(g => g.Allele) .ToImmutableSeq())); }
private bool TestRegion(PermutationChromosome y, PermutationChromosome crossover, Tuple <int, int> region) { var seen = new bool[GENE_COUNT]; for (var i = region.Item1; i <= region.Item2; i++) { seen[crossover.Permutation[i]] = true; } for (var i = 1; i < GENE_COUNT; i++) { var index = (region.Item2 + 1) % GENE_COUNT; if (!seen[y.Permutation[index]] && (y.Permutation[index] != crossover.Permutation[index])) { return(false); } seen[y.Permutation[index]] = true; } return(true); }
public void Order1Crossover_Cross_ReturnChildren() { var position = 3; var length = 5; var random = new PredeterminedRandom(new double[] { position, length }); var parameterSet = new ParameterSet(); var crossover = new Order1Crossover(random, parameterSet, null); var parent1Genes = new int[] { 8, 4, 7, 3, 6, 2, 5, 1, 9, 0 }; var parent2Genes = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var parent1 = new PermutationChromosome(parent1Genes); var parent2 = new PermutationChromosome(parent2Genes); var children = crossover.Cross(parent1, parent2); children.Count.Should().Be(2); var expected = new int[] { 0, 4, 7, 3, 6, 2, 5, 1, 8, 9 }; ((PermutationChromosome)children[0]).Genes.Should().BeEquivalentTo(expected); }
private Tuple <int, int> GetDifferenceRange(PermutationChromosome chromosome, PermutationChromosome mutation) { var start = 0; var end = 0; for (var i = 0; i < GENE_COUNT; i++) { if (mutation.Permutation[i] != chromosome.Permutation[i]) { start = i; while (i < GENE_COUNT) { if (mutation.Permutation[i] != chromosome.Permutation[i]) { end = i; } i++; } } } return(new Tuple <int, int>(start, end)); }
public IChromosome Clone() { PermutationChromosome cpy = new PermutationChromosome(); cpy.length = this.length; cpy.x = new int[cpy.length]; for (int i = 0; i < cpy.length; ++i) cpy.x[i] = this.x[i]; IChromosome copy = cpy; return copy; }
private PermutationChromosome GetCrossover(PermutationChromosome x, PermutationChromosome y, PermutationCrossover crossover) { var rator = GeneticFactory.ConstructPermutationCrossoverOperators(crossover).First(); return(rator.Invoke(x, y) as PermutationChromosome); }
public IChromosome GetNewChromosome() { IChromosome instance = new PermutationChromosome(this.length); return instance; }
/// <summary> /// Initializes a new instance of the <see cref="PermutationChromosome"/> class. /// </summary> /// /// <param name="source">Source chromosome to copy.</param> /// /// <remarks><para>This is a copy constructor, which creates the exact copy /// of specified chromosome.</para></remarks> /// protected PermutationChromosome(PermutationChromosome source) : base(source) { }
private PermutationChromosome GetMutation(PermutationChromosome chromosome, PermutationMutation mutation) { var rator = GeneticFactory.ConstructPermutationMutationOperators(mutation).First(); return(rator.Invoke(chromosome) as PermutationChromosome); }