/** * Returns the fitness value for the given argument. * * @since 4.1 * * @param arg the argument of the fitness function * @return the fitness value */ public default C fitness(final T arg) { return fitness().apply(arg); }
/** * Returns the fitness value for the given argument. * * @since 4.1 * * @param genotype the argument of the fitness function * @return the fitness value */ public default C fitness(final Genotype<G> genotype) { return fitness(codec().decode(genotype)); }
/** * Create a new evolution {@code Engine.Builder} for the given * {@link Problem}. * * @since 3.4 * * @param problem the problem to be solved by the evolution {@code Engine} * @param <T> the (<i>native</i>) argument type of the problem fitness function * @param <G> the gene type the evolution engine is working with * @param <C> the result type of the fitness function * @return Create a new evolution {@code Engine.Builder} */ public static <T, G extends Gene<?, G>, C extends Comparable<? super C>> Builder<G, C> builder(final Problem<T, G, C> problem) { return builder(problem.fitness(), problem.codec()); }
private Phenotype<DoubleGene, Vec<double[]>> phenotype() { return Phenotype.of( PROBLEM.codec().encoding().newInstance(), 1L, gt -> PROBLEM.fitness().apply(PROBLEM.codec().decode(gt)) ); }
private Phenotype<DoubleGene, Vec<double[]>> phenotype() { return Phenotype.of( PROBLEM.codec().encoding().newInstance(), 1L, gt -> PROBLEM.fitness().apply(PROBLEM.codec().decode(gt)) ); }
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 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))); }