@Override public String toString() { return _genotype + " --> " + getFitness(); }
/** * Evaluates the (raw) fitness values and caches it so the fitness calculation * is performed only once. * * @return this phenotype, for method chaining. */ public Phenotype<G, C> evaluate() { getFitness(); return this; }
private int dominance(final Phenotype<G, C> a, final Phenotype<G, C> b) { return _optimize == Optimize.MAXIMUM ? _dominance.compare(a.getFitness(), b.getFitness()) : _dominance.compare(b.getFitness(), a.getFitness()); }
@Override public int compareTo(final Phenotype<G, C> pt) { return getFitness().compareTo(pt.getFitness()); }
private int compare( final Phenotype<G, C> a, final Phenotype<G, C> b, final int i ) { return _optimize == Optimize.MAXIMUM ? _comparator.compare(a.getFitness(), b.getFitness(), i) : _comparator.compare(b.getFitness(), a.getFitness(), i); }
@Override public boolean equals(final Object obj) { return obj == this || obj instanceof Phenotype && Objects.equals(getFitness(), ((Phenotype)obj).getFitness()) && Objects.equals(getRawFitness(), ((Phenotype)obj).getRawFitness()) && Objects.equals(_genotype, ((Phenotype)obj)._genotype) && _generation == ((Phenotype)obj)._generation; }
@Override void accept(final Phenotype<?, N> pt, final long generation) { super.accept(pt, generation); _fitness.accept(pt.getFitness().doubleValue()); }
private void writeObject(final ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeLong(getGeneration()); out.writeObject(getGenotype()); out.writeObject(getFitness()); out.writeObject(getRawFitness()); }
@Override void accept(final Phenotype<?, C> pt, final long generation) { super.accept(pt, generation); _fitness.accept(pt.getFitness()); }
/** * Return the worst population fitness. * * @return The worst population fitness. */ public C getWorstFitness() { return _worst.get() != null ? _worst.get().getFitness() : null; }
/** * Return the best population fitness. * * @return The best population fitness. */ public C getBestFitness() { return _best.get() != null ? _best.get().getFitness() : null; }
@Override public boolean test(final EvolutionResult<?, N> result) { final DoubleMomentStatistics fitness = new DoubleMomentStatistics(); result.getPopulation() .forEach(p -> fitness.accept(p.getFitness().doubleValue())); return result.getTotalGenerations() <= 1 || _proceed.test( result.getBestFitness() != null ? result.getBestFitness().doubleValue() : Double.NaN, DoubleMoments.of(fitness) ); }
@Override public int hashCode() { return hash(_generation, hash(getFitness(), hash(getRawFitness(), hash(_genotype, hash(getClass()))))); }
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); }
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); }
@Override public boolean test(final EvolutionResult<?, N> result) { final DoubleMoments moments = result.getPopulation().stream() .collect(toDoubleMoments(pt -> pt.getFitness().doubleValue())); boolean proceed = true; if (_fitness == null) { _fitness = result.getBestFitness(); _stableGenerations = 1; } else { final Optimize opt = result.getOptimize(); if (opt.compare(_fitness, result.getBestFitness()) >= 0) { proceed = ++_stableGenerations <= _generations; } else { _fitness = result.getBestFitness(); _stableGenerations = 1; } } return proceed; } }
private void trim() { assert _front != null; assert _optimize != null; if (_front.size() > _size.getMax() - 1) { _front.trim( _size.getMin(), this::compare, _distance.map(Phenotype::getFitness), v -> _dimension.applyAsInt(v.getFitness()) ); } }
@Override protected double[] probabilities( final Seq<Phenotype<G, N>> population, final int count ) { assert population != null : "Population must not be null. "; assert population.nonEmpty() : "Population is empty."; assert count > 0 : "Population to select must be greater than zero. "; // Copy the fitness values to probabilities arrays. final double[] fitness = new double[population.size()]; for (int i = population.size(); --i >= 0;) { fitness[i] = population.get(i).getFitness().doubleValue(); } final double worst = Math.min(min(fitness), 0.0); final double sum = DoubleAdder.sum(fitness) - worst*population.size(); if (eq(sum, 0.0)) { Arrays.fill(fitness, 1.0/population.size()); } else { for (int i = population.size(); --i >= 0;) { fitness[i] = (fitness[i] - worst)/sum; } } return fitness; }
@Test public void streamWithInitialGenotypes() { final Problem<Integer, IntegerGene, Integer> problem = Problem.of( a -> a, Codec.of( Genotype.of(IntegerChromosome.of(0, 1000)), g -> g.getGene().getAllele() ) ); final int genotypeCount = 10; final int max = 1000; final ISeq<Genotype<IntegerGene>> genotypes = IntRange.of(1, genotypeCount) .stream() .mapToObj(i -> IntegerChromosome.of(IntegerGene.of(max, 0, max))) .map(Genotype::of) .collect(ISeq.toISeq()); final Engine<IntegerGene, Integer> engine = Engine.builder(problem) .build(); final EvolutionResult<IntegerGene, Integer> result = engine.stream(genotypes) .limit(1) .collect(EvolutionResult.toBestEvolutionResult()); final long maxCount = result.getPopulation().stream() .filter(pt -> pt.getFitness() == max) .count(); Assert.assertTrue(maxCount >= genotypeCount); }
@Test public void worstIndividual() { final int size = 20; final MSeq<Phenotype<DoubleGene, Integer>> population = MSeq.ofLength(size); for (int i = 0; i < size; ++i) { final DoubleGene gene = DoubleGene.of(i, 0, size + 10); final DoubleChromosome ch = DoubleChromosome.of(gene); final Genotype<DoubleGene> gt = Genotype.of(ch); final Phenotype<DoubleGene, Integer> pt = Phenotype.of( gt, 1, g -> g.getGene().intValue() ); population.set(i, pt); } final TruncationSelector<DoubleGene, Integer> selector = new TruncationSelector<>(5); final ISeq<Phenotype<DoubleGene, Integer>> selected = selector.select(population.toISeq(), 10, Optimize.MINIMUM); for (Phenotype<DoubleGene, Integer> pt : selected) { Assert.assertTrue(pt.getFitness() < 5); } }