Engine<EnumGene<double[]>, Double> engine = Engine .builder(tsm) .optimize(Optimize.MINIMUM) .maximalPhenotypeAge(11) .populationSize(500)
public static void main(final String[] args) { RandomRegistry.setRandom(new Random()); final Engine<EnumGene<Integer>, Integer> engine = Engine .builder( Sorting::length, PermutationChromosome.ofInteger(20)) .optimize(Optimize.MINIMUM) .populationSize(1000) //.survivorsSelector(new RouletteWheelSelector<>()) //.offspringSelector(new TruncationSelector<>()) .offspringFraction(0.9) .alterers( new SwapMutator<>(0.01), new PartiallyMatchedCrossover<>(0.3)) .build(); final EvolutionStatistics<Integer, DoubleMomentStatistics> statistics = EvolutionStatistics.ofNumber(); final EvolutionResult<EnumGene<Integer>, Integer> result = engine.stream() .limit(Limits.bySteadyFitness(100)) .limit(2500) .peek(statistics) .collect(EvolutionResult.toBestEvolutionResult()); System.out.println(statistics); System.out.println(result.getBestPhenotype()); }
Codecs.ofScalar(DoubleRange.of(0.0, 2.0*PI))) .populationSize(500) .optimize(Optimize.MINIMUM) .alterers( new Mutator<>(0.03),
.optimize(Optimize.MAXIMUM) .maximalPhenotypeAge(50) .survivorsSelector(new TruncationSelector<>())
Codecs.ofScalar(DoubleRange.of(0.0, 2.0*PI))) .populationSize(500) .optimize(Optimize.MINIMUM) .alterers( new Mutator<>(0.03),
.survivorsSelector(survivorsSelector) .alterers(alterer) .optimize(optimize) .offspringFraction(offspringFraction) .populationSize(populationSize)
@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()); }
/** * Create a new evolution {@code Engine.Builder} initialized with the values * of the current evolution {@code Engine}. With this method, the evolution * engine can serve as a template for a new one. * * @return a new engine builder */ public Builder<G, C> builder() { return new Builder<G, C>(_genotypeFactory, _fitnessFunction) .alterers(_alterer) .clock(_clock) .evaluator(_evaluator) .executor(_executor.get()) .fitnessScaler(_fitnessScaler) .maximalPhenotypeAge(_maximalPhenotypeAge) .offspringFraction((double)_offspringCount/(double)getPopulationSize()) .offspringSelector(_offspringSelector) .optimize(_optimize) .phenotypeValidator(_validator) .populationSize(getPopulationSize()) .survivorsSelector(_survivorsSelector) .individualCreationRetries(_individualCreationRetries) .mapping(_mapper); }
/** * Create a new builder, with the current configuration. * * @since 3.1 * * @return a new builder, with the current configuration */ @Override public Builder<G, C> copy() { return new Builder<G, C>(_genotypeFactory, _fitnessFunction) .alterers(_alterer) .clock(_clock) .executor(_executor) .evaluator(_evaluator) .fitnessScaler(_fitnessScaler) .maximalPhenotypeAge(_maximalPhenotypeAge) .offspringFraction(_offspringFraction) .offspringSelector(_offspringSelector) .phenotypeValidator(_validator) .optimize(_optimize) .populationSize(_populationSize) .survivorsSelector(_survivorsSelector) .individualCreationRetries(_individualCreationRetries) .mapping(_mapper); }
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); } }
@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); }
/** * Set to a fitness maximizing strategy. * * @since 3.4 * * @return {@code this} builder, for command chaining */ public Builder<G, C> maximizing() { return optimize(Optimize.MAXIMUM); }
static Engine<EnumGene<Integer>, Integer> buildEngine(Codec<ISeq<Integer>, EnumGene<Integer>> codec) { return Engine.builder(E305::fitness, codec) .offspringSelector(new RouletteWheelSelector<>()) .alterers( new SwapMutator<>(), new PartiallyMatchedCrossover<>(0.9) ) .populationSize(20) .optimize(Optimize.MINIMUM) .build(); }
/** * Set to a fitness minimizing strategy. * * @since 3.4 * * @return {@code this} builder, for command chaining */ public Builder<G, C> minimizing() { return optimize(Optimize.MINIMUM); }