@Override public Alterer<DoubleGene, Double> newAlterer(final double p) { return new Mutator<>(p); }
/** * Mutates the given phenotype. * * @see #mutate(Genotype, double, Random) * @see #mutate(Chromosome, double, Random) * @see #mutate(Gene, Random) * * @param phenotype the phenotype to mutate * @param generation the actual generation * @param p the mutation probability for the underlying genetic objects * @param random the random engine used for the phenotype mutation * @return the mutation result */ protected MutatorResult<Phenotype<G, C>> mutate( final Phenotype<G, C> phenotype, final long generation, final double p, final Random random ) { return mutate(phenotype.getGenotype(), p, random) .map(gt -> phenotype.newInstance(gt, generation)); }
/** * Mutates the given chromosome. * * @see #mutate(Gene, Random) * * @param chromosome the chromosome to mutate * @param p the mutation probability for the underlying genetic objects * @param random the random engine used for the genotype mutation * @return the mutation result */ protected MutatorResult<Chromosome<G>> mutate( final Chromosome<G> chromosome, final double p, final Random random ) { final int P = probability.toInt(p); final ISeq<MutatorResult<G>> result = chromosome.stream() .map(gene -> random.nextInt() < P ? MutatorResult.of(mutate(gene, random), 1) : MutatorResult.of(gene)) .collect(ISeq.toISeq()); return MutatorResult.of( chromosome.newInstance(result.map(MutatorResult::getResult)), result.stream().mapToInt(MutatorResult::getMutations).sum() ); }
public static void main(final String[] args) throws IOException { final String base = "/home/fwilhelm/Workspace/Development/Projects/" + "Jenetics/jenetics.tool/src/main/resources/io/jenetics/tool/moea"; final Path data = Paths.get(base, "circle_max_front.dat"); final Path output = Paths.get(base, "circle_max_front.svg"); final Engine<DoubleGene, Vec<double[]>> engine = Engine.builder(PROBLEM) .alterers( new Mutator<>(0.1), new MeanAlterer<>()) .offspringSelector(new TournamentSelector<>(3)) .survivorsSelector(UFTournamentSelector.ofVec()) .build(); final ISeq<Phenotype<DoubleGene, Vec<double[]>>> front = engine.stream() .limit(Limits.byFixedGeneration(100)) .collect(MOEA.toParetoSet(IntRange.of(100, 150))); final StringBuilder out = new StringBuilder(); out.append("#x y\n"); front.forEach(p -> { out.append(p.getFitness().data()[0]); out.append(" "); out.append(p.getFitness().data()[1]); out.append("\n"); }); Files.write(data, out.toString().getBytes()); final Gnuplot gnuplot = new Gnuplot(Paths.get(base, "circle_points.gp")); gnuplot.create(data, output); }
/** * Concrete implementation of the alter method. It uses the following * mutation methods: {@link #mutate(Phenotype, long, double, Random)}, * {@link #mutate(Genotype, double, Random)}, * {@link #mutate(Chromosome, double, Random)}, {@link #mutate(Gene, Random)}, * in this specific order. * * @see #mutate(Phenotype, long, double, Random) * @see #mutate(Genotype, double, Random) * @see #mutate(Chromosome, double, Random) * @see #mutate(Gene, Random) */ @Override public AltererResult<G, C> alter( final Seq<Phenotype<G, C>> population, final long generation ) { assert population != null : "Not null is guaranteed from base class."; final Random random = RandomRegistry.getRandom(); final double p = pow(_probability, 1.0/3.0); final int P = probability.toInt(p); final Seq<MutatorResult<Phenotype<G, C>>> result = population .map(pt -> random.nextInt() < P ? mutate(pt, generation, p, random) : MutatorResult.of(pt)); return AltererResult.of( result.map(MutatorResult::getResult).asISeq(), result.stream().mapToInt(MutatorResult::getMutations).sum() ); }
public static void main(final String[] args) { final Engine<ProgramGene<Double>, Double> engine = Engine .builder(SymbolicRegression::error, CODEC) .minimizing() .alterers( new SingleNodeCrossover<>(), new Mutator<>()) .build(); final ProgramGene<Double> program = engine.stream() .limit(100) .collect(EvolutionResult.toBestGenotype()) .getGene(); System.out.println(program.toParenthesesString()); }
/** * Mutates the given genotype. * * @see #mutate(Chromosome, double, Random) * @see #mutate(Gene, Random) * * @param genotype the genotype to mutate * @param p the mutation probability for the underlying genetic objects * @param random the random engine used for the genotype mutation * @return the mutation result */ protected MutatorResult<Genotype<G>> mutate( final Genotype<G> genotype, final double p, final Random random ) { final int P = probability.toInt(p); final ISeq<MutatorResult<Chromosome<G>>> result = genotype.toSeq() .map(gt -> random.nextInt() < P ? mutate(gt, p, random) : MutatorResult.of(gt)); return MutatorResult.of( Genotype.of(result.map(MutatorResult::getResult)), result.stream().mapToInt(MutatorResult::getMutations).sum() ); }
public static void main(final String[] args) throws IOException { final String base = "/home/fwilhelm/Workspace/Development/Projects/" + "Jenetics/jenetics.tool/src/main/resources/io/jenetics/tool/moea"; final Path data = Paths.get(base, "circle_min_front.dat"); final Path output = Paths.get(base, "circle_min_front.svg"); final Engine<DoubleGene, Vec<double[]>> engine = Engine.builder(PROBLEM) .alterers( new Mutator<>(0.1), new MeanAlterer<>()) .offspringSelector(new TournamentSelector<>(3)) .survivorsSelector(UFTournamentSelector.ofVec()) .minimizing() .build(); final ISeq<Phenotype<DoubleGene, Vec<double[]>>> front = engine.stream() .limit(Limits.byFixedGeneration(100)) .collect(MOEA.toParetoSet(IntRange.of(100, 150))); final StringBuilder out = new StringBuilder(); out.append("#x y\n"); front.forEach(p -> { out.append(p.getFitness().data()[0]); out.append(" "); out.append(p.getFitness().data()[1]); out.append("\n"); }); Files.write(data, out.toString().getBytes()); final Gnuplot gnuplot = new Gnuplot(Paths.get(base, "circle_points.gp")); gnuplot.create(data, output); }
public static void main(final String[] args) { final Engine<CharacterGene, Integer> engine = Engine.builder(PROBLEM) .populationSize(500) .survivorsSelector(new StochasticUniversalSelector<>()) .offspringSelector(new TournamentSelector<>(5)) .alterers( new Mutator<>(0.1), new SinglePointCrossover<>(0.5)) .build(); final Phenotype<CharacterGene, Integer> result = engine.stream() .limit(100) .collect(toBestPhenotype()); System.out.println(result); }
public static void main(final String[] args) { final Knapsack knapsack = Knapsack.of(15, new Random(123)); // Configure and build the evolution engine. final Engine<BitGene, Double> engine = Engine.builder(knapsack) .populationSize(500) .survivorsSelector(new TournamentSelector<>(5)) .offspringSelector(new RouletteWheelSelector<>()) .alterers( new Mutator<>(0.115), new SinglePointCrossover<>(0.16)) .build(); // Create evolution statistics consumer. final EvolutionStatistics<Double, ?> statistics = EvolutionStatistics.ofNumber(); final Phenotype<BitGene, Double> best = engine.stream() // Truncate the evolution stream after 7 "steady" // generations. .limit(bySteadyFitness(7)) // The evolution will stop after maximal 100 // generations. .limit(100) // Update the evaluation statistics after // each generation .peek(statistics) // Collect (reduce) the evolution stream to // its best phenotype. .collect(toBestPhenotype()); System.out.println(statistics); System.out.println(best); }
public static void main(final String[] args) { final Engine<DoubleGene, Double> engine = Engine .builder( RastriginFunction::fitness, // Codec for 'x' vector. Codecs.ofVector(DoubleRange.of(-R, R), N)) .populationSize(500) .optimize(Optimize.MINIMUM) .alterers( new Mutator<>(0.03), new MeanAlterer<>(0.6)) .build(); final EvolutionStatistics<Double, ?> statistics = EvolutionStatistics.ofNumber(); final Phenotype<DoubleGene, Double> best = engine.stream() .limit(bySteadyFitness(7)) .peek(statistics) .collect(toBestPhenotype()); System.out.println(statistics); System.out.println(best); } }
.selector(new RouletteWheelSelector<>()) .alterers( new Mutator<>(0.55), new SinglePointCrossover<>(0.06)) .build();
.optimize(Optimize.MINIMUM) .alterers( new Mutator<>(0.03), new MeanAlterer<>(0.6))
.optimize(Optimize.MINIMUM) .alterers( new Mutator<>(0.03), new MeanAlterer<>(0.6))
.offspringSelector(new RouletteWheelSelector<>()) .alterers( new Mutator<>(0.115), new SinglePointCrossover<>(0.16)) .build();
public static void main(final String[] args) { final int μ = 5; final int λ = 20; final double p = 0.2; final Codec<Double, DoubleGene> codec = Codecs .ofScalar(DoubleRange.of(0, 1)); final Engine<DoubleGene, Double> engine = Engine .builder(MLStrategy::fitness, codec) .populationSize(λ) .survivorsSize(0) .offspringSelector(new TruncationSelector<>(μ)) .alterers(new Mutator<>(p)) .build(); System.out.println( codec.decode( engine.stream() .limit(100) .collect(EvolutionResult.toBestGenotype()) ) ); }
public static void main(final String[] args) { final int μ = 5; final int λ = 20; final double p = 0.2; final Codec<Double, DoubleGene> codec = Codecs .ofScalar(DoubleRange.of(0, 1)); final Engine<DoubleGene, Double> engine = Engine .builder(MpLStrategy::fitness, codec) .populationSize(λ) .survivorsSize(μ) .selector(new TruncationSelector<>(μ)) .alterers(new Mutator<>(p)) .build(); System.out.println( codec.decode( engine.stream() .limit(100) .collect(EvolutionResult.toBestGenotype()) ) ); }
public static void main(final String[] args) { final SubsetSum problem = of(500, 15, new LCG64ShiftRandom(101010)); final Engine<EnumGene<Integer>, Integer> engine = Engine.builder(problem) .minimizing() .maximalPhenotypeAge(5) .alterers( new PartiallyMatchedCrossover<>(0.4), new Mutator<>(0.3)) .build(); final Phenotype<EnumGene<Integer>, Integer> result = engine.stream() .limit(Limits.bySteadyFitness(55)) .collect(EvolutionResult.toBestPhenotype()); System.out.print(result); }
public static void main(final String[] args) { final Knapsack knapsack = Knapsack.of(15, new Random(123)); // The base engine tries to approximate to good solution in current // environment. final Engine<BitGene, Double> baseEngine = Engine.builder(knapsack) .populationSize(500) .alterers( new Mutator<>(0.115), new SinglePointCrossover<>(0.16)) .build(); // The 'diversity' engine tries to broaden the search space again. final Engine<BitGene, Double> diversityEngine = baseEngine.builder() .alterers(new Mutator<>(0.5)) .build(); // Concatenates the two engines into one cyclic engine. final EvolutionStreamable<BitGene, Double> engine = CyclicEngine.of( // This engine stops the evolution after 10 non-improving // generations and hands over to the diversity engine. baseEngine.limit(() -> Limits.bySteadyFitness(10)), // The higher mutation rate of this engine broadens the search // space for 15 generations and hands over to the base engine. diversityEngine.limit(15) ); final EvolutionResult<BitGene, Double> best = engine.stream() // The evolution is stopped after 50 non-improving generations. .limit(bySteadyFitness(50)) .collect(toBestEvolutionResult()); System.out.println(best.getTotalGenerations()); System.out.println(best.getBestPhenotype()); }