private EvolutionResult<BitGene, Double> run( final EvolutionResult<BitGene, Double> last, final AtomicBoolean proceed ) { System.out.println("Starting evolution with existing result."); return (last != null ? ENGINE.stream(last) : ENGINE.stream()) .limit(r -> proceed.get()) .collect(EvolutionResult.toBestEvolutionResult()); }
protected double[] fitness(final P param) { final Predicate<? super EvolutionResult<G, N>> terminator = _terminator.apply(param); final long start = System.currentTimeMillis(); final EvolutionResult<G, N> result = _engine.apply(param).stream() .limit(terminator) .collect(EvolutionResult.toBestEvolutionResult()); final long end = System.currentTimeMillis(); return new double[] { result.getTotalGenerations(), result.getBestFitness() != null ? result.getBestFitness().doubleValue() : Double.NEGATIVE_INFINITY, end - start }; }
public static void main(final String[] args) throws IOException { final EvolutionResult<DoubleGene, Double> rescue = ENGINE.stream() .limit(Limits.bySteadyFitness(10)) .collect(EvolutionResult.toBestEvolutionResult()); final Path path = Paths.get("result.bin"); IO.object.write(rescue, path); @SuppressWarnings("unchecked") final EvolutionResult<DoubleGene, Double> result = ENGINE .stream((EvolutionResult<DoubleGene, Double>)IO.object.read(path)) .limit(Limits.bySteadyFitness(20)) .collect(EvolutionResult.toBestEvolutionResult()); System.out.println(result); }
protected double[] fitness(final P param) { final Predicate<? super EvolutionResult<G, N>> terminator1 = _terminator1.apply(param); final long start1 = System.currentTimeMillis(); final EvolutionResult<G, N> result1 = _engine1.stream() .limit(terminator1) .collect(EvolutionResult.toBestEvolutionResult()); final long end1 = System.currentTimeMillis(); final Predicate<? super EvolutionResult<G, N>> terminator2 = _terminator2.apply(param); final long start2 = System.currentTimeMillis(); final EvolutionResult<G, N> result2 = _engine2.stream() .limit(terminator2) .collect(EvolutionResult.toBestEvolutionResult()); final long end2 = System.currentTimeMillis(); return new double[] { result1.getTotalGenerations(), result1.getBestFitness() != null ? result1.getBestFitness().doubleValue() : Double.NEGATIVE_INFINITY, end1 - start1, result2.getTotalGenerations(), result2.getBestFitness() != null ? result2.getBestFitness().doubleValue() : Double.NEGATIVE_INFINITY, end2 - start2 }; }
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); }
@Test // https://github.com/jenetics/jenetics/issues/420 public void bestFitnessResult() { final Genotype<IntegerGene> genotype = Genotype.of(IntegerChromosome.of(0, 10)); final AtomicInteger ai = new AtomicInteger(); final Function<Genotype<IntegerGene>, Integer> ff = x -> ai.incrementAndGet(); final int threshold = 100; final Integer result = Engine.builder(ff, genotype) .build() .stream() .limit(Limits.byFitnessThreshold(threshold)) .collect(EvolutionResult.toBestEvolutionResult()) .getBestFitness(); Assert.assertNotNull(result); Assert.assertTrue( result >= 100, format("Expected value >= %s, but got %s", threshold, result) ); }
public static void main(String[] args) throws IOException { final Knapsack knapsack = Knapsack.of(15, new Random(123)); final Engine<BitGene, Double> engine = Engine.builder(knapsack) .populationSize(500) .alterers( new Mutator<>(0.115), new SinglePointCrossover<>(0.16)) .evaluator(BatchEvalKnapsack::batchEval) .evaluator(pop -> { pop.forEach(Phenotype::evaluate); return pop.asISeq(); }) .build(); final Phenotype<BitGene, Double> best = engine.stream() .limit(bySteadyFitness(20)) .collect(toBestPhenotype()); System.out.println(best); }
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); }
@Test(dataProvider = "generations") public void generationLimit(final Long generations) { final Engine<DoubleGene, Double> engine = Engine .builder(a -> a.getGene().getAllele(), DoubleChromosome.of(0, 1)) .build(); final EvolutionResult<DoubleGene, Double> result = engine.stream() .limit(Limits.byFixedGeneration(generations)) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(generations.longValue(), result.getTotalGenerations()); }
@Test(dataProvider = "generations") public void engineGenerationLimit1(final Long generations) { final Engine<DoubleGene, Double> engine = Engine .builder(a -> a.getGene().getAllele(), DoubleChromosome.of(0, 1)) .build(); final EvolutionResult<DoubleGene, Double> result = engine .limit(() -> Limits.byFixedGeneration(generations)) .stream() .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(generations.longValue(), result.getTotalGenerations()); }
@Test(dataProvider = "generations") public void engineGenerationLimit2(final Long generations) { final Engine<DoubleGene, Double> engine = Engine .builder(a -> a.getGene().getAllele(), DoubleChromosome.of(0, 1)) .build(); final EvolutionResult<DoubleGene, Double> result = engine .limit(() -> Limits.byFixedGeneration(generations)) .limit(() -> Limits.byFixedGeneration(Math.min(generations, 5))) .stream() .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertEquals(Math.min(generations, 5), result.getTotalGenerations()); }
@Test public void streamWithSerializedPopulation() throws IOException { // Problem definition. final Problem<Double, DoubleGene, Double> problem = Problem.of( x -> cos(0.5 + sin(x))*cos(x), Codecs.ofScalar(DoubleRange.of(0.0, 2.0*PI)) ); // Define the GA engine. final Engine<DoubleGene, Double> engine = Engine.builder(problem) .optimize(Optimize.MINIMUM) .offspringSelector(new RouletteWheelSelector<>()) .build(); final EvolutionResult<DoubleGene, Double> interimResult = engine.stream() .limit(Limits.bySteadyFitness(10)) .collect(EvolutionResult.toBestEvolutionResult()); final ByteArrayOutputStream out = new ByteArrayOutputStream(); IO.object.write(interimResult, out); final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); @SuppressWarnings("unchecked") final EvolutionResult<DoubleGene, Double> loadedResult = (EvolutionResult<DoubleGene, Double>)IO.object.read(in); final EvolutionResult<DoubleGene, Double> result = engine .stream(loadedResult) .limit(10) .collect(EvolutionResult.toBestEvolutionResult()); }
public static void main(final String[] args) { final Problem<double[], DoubleGene, Double> problem = Problem.of( v -> Math.sin(v[0])*Math.cos(v[1]), Codecs.ofVector(DoubleRange.of(0, 2*Math.PI), 2) ); final Engine.Builder<DoubleGene, Double> builder = Engine .builder(problem) .minimizing(); final Genotype<DoubleGene> result = AdaptiveEngine.<DoubleGene, Double>of(er -> engine(er, builder)) .stream() .limit(Limits.bySteadyFitness(50)) .collect(EvolutionResult.toBestGenotype()); System.out.println(result + ": " + problem.fitness().apply(problem.codec().decode(result))); }
public static void main(final String[] args) { final RectFill problem = new RectFill(new Rect(0, 100, 0, 100)); final Engine<AnyGene<Rect>, Double> engine = Engine.builder(problem) .individualCreationRetries(10) .genotypeValidator(gt -> true) .offspringSelector(new RouletteWheelSelector<>()) .alterers( new SwapMutator<>(), new SinglePointCrossover<>()) .build(); final ISeq<Rect> best = problem.codec().decode( engine.stream() .limit(byFixedGeneration(10)) .collect(EvolutionResult.toBestGenotype()) ); System.out.println(best); }
@Test(invocationCount = 5) public void onesCountLimit() { final Problem<ISeq<BitGene>, BitGene, Integer> problem = Problem.of( genes -> (int)genes.stream().filter(BitGene::getBit).count(), Codec.of( Genotype.of(BitChromosome.of(20, 0.125)), gt -> gt.getChromosome().toSeq() ) ); final Engine<BitGene, Integer> engine = Engine.builder(problem) .build(); final EvolutionResult<BitGene, Integer> result = engine.stream() .limit(Limits.byFitnessConvergence(5, 10, 0.01)) .collect(EvolutionResult.toBestEvolutionResult()); Assert.assertTrue( result.getTotalGenerations() < 50, "Gen: " + result.getTotalGenerations() ); }
@Test(invocationCount = 5) public void onesCountLimit() { final Problem<ISeq<BitGene>, BitGene, Integer> problem = Problem.of( genes -> (int)genes.stream().filter(BitGene::getBit).count(), Codec.of( Genotype.of(BitChromosome.of(20, 0.125)), gt -> gt.getChromosome().toSeq() ) ); final Engine<BitGene, Integer> engine = Engine.builder(problem) .build(); final EvolutionResult<BitGene, Integer> result = engine.stream() .limit(Limits.byPopulationConvergence(0.015)) .collect(toBestEvolutionResult()); Assert.assertTrue( result.getTotalGenerations() < 2901, "Gen: " + result.getTotalGenerations() ); }
public static void main(final String[] args) { final Problem<int[][], IntegerGene, Integer> problem = Problem.of( Matrix::fitness, Codec.of( Genotype.of(IntegerChromosome.of(IntRange.of(0, 10), 3), 3), gt -> gt.stream() .map(ch -> ch.stream() .mapToInt(IntegerGene::intValue).toArray()) .toArray(int[][]::new) ) ); final Engine<IntegerGene, Integer> engine = Engine.builder(problem).build(); final Genotype<IntegerGene> gt = engine.stream() .limit(Limits.byFixedGeneration(20)) .collect(EvolutionResult.toBestGenotype()); final int[][] best = problem.codec().decode(gt); print(best); }
@Test public void initialResult() { // Problem definition. final Problem<Double, DoubleGene, Double> problem = Problem.of( x -> cos(0.5 + sin(x))*cos(x), Codecs.ofScalar(DoubleRange.of(0.0, 2.0*PI)) ); // Define the GA engine. final Engine<DoubleGene, Double> engine = Engine.builder(problem) .optimize(Optimize.MINIMUM) .offspringSelector(new RouletteWheelSelector<>()) .build(); final EvolutionResult<DoubleGene, Double> interimResult = engine.stream() .limit(Limits.bySteadyFitness(10)) .collect(EvolutionResult.toBestEvolutionResult()); engine.builder() .alterers(new Mutator<>()).build() .stream(interimResult); }
public static void main(final String[] args) { final Problem<double[], DoubleGene, Double> problem = Problem.of( v -> Math.sin(v[0])*Math.cos(v[1]), Codecs.ofVector(DoubleRange.of(0, 2*Math.PI), 2) ); final Engine<DoubleGene, Double> engine1 = Engine.builder(problem) .minimizing() .alterers(new Mutator<>(0.2)) .selector(new MonteCarloSelector<>()) .build(); final Engine<DoubleGene, Double> engine2 = Engine.builder(problem) .minimizing() .alterers( new Mutator<>(0.1), new MeanAlterer<>()) .selector(new RouletteWheelSelector<>()) .build(); final Genotype<DoubleGene> result = ConcatEngine.of( engine1.limit(50), engine2.limit(() -> Limits.bySteadyFitness(30))) .stream() .collect(EvolutionResult.toBestGenotype()); System.out.println(result + ": " + problem.fitness().apply(problem.codec().decode(result))); }
public static void main(final String[] args) { final Problem<double[], DoubleGene, Double> problem = Problem.of( v -> Math.sin(v[0])*Math.cos(v[1]), Codecs.ofVector(DoubleRange.of(0, 2*Math.PI), 2) ); final Engine<DoubleGene, Double> engine1 = Engine.builder(problem) .minimizing() .alterers(new Mutator<>(0.2)) .selector(new MonteCarloSelector<>()) .build(); final Engine<DoubleGene, Double> engine2 = Engine.builder(problem) .minimizing() .alterers( new Mutator<>(0.1), new MeanAlterer<>()) .selector(new RouletteWheelSelector<>()) .build(); final Genotype<DoubleGene> result = CyclicEngine.of( engine1.limit(50), engine2.limit(() -> Limits.bySteadyFitness(30))) .stream() .limit(Limits.bySteadyFitness(1000)) .collect(EvolutionResult.toBestGenotype()); System.out.println(result + ": " + problem.fitness().apply(problem.codec().decode(result))); }