/** * Return a stream of all nodes of the whole underlying tree. This method * call is equivalent to * <pre>{@code * final Stream<FlatTreeNode<T>> nodes = getRoot().breadthFirstStream(); * }</pre> * * @return a stream of all nodes of the whole underlying tree */ public Stream<FlatTreeNode<T>> stream() { return IntStream.range(0, _nodes.size()).mapToObj(this::nodeAt); }
final int length = min(min(copy.size(), size), _n); for (int i = 0; i < length; ++i) { selection.set((count - size) + i, copy.get(i));
@Override public AltererResult<G, C> alter( final Seq<Phenotype<G, C>> population, final long generation ) { final double p = pow(_probability, 1.0/3.0); final IntRef alterations = new IntRef(0); final MSeq<Phenotype<G, C>> pop = MSeq.of(population); indexes(RandomRegistry.getRandom(), pop.size(), p).forEach(i -> { final Phenotype<G, C> pt = pop.get(i); final Genotype<G> gt = pt.getGenotype(); final Genotype<G> mgt = mutate(gt, p, alterations); final Phenotype<G, C> mpt = pt.newInstance(mgt, generation); pop.set(i, mpt); }); return AltererResult.of(pop.toISeq(), alterations.value); }
private FilterResult<G, C> filter( final Seq<Phenotype<G, C>> population, final long generation ) { int killCount = 0; int invalidCount = 0; final MSeq<Phenotype<G, C>> pop = MSeq.of(population); for (int i = 0, n = pop.size(); i < n; ++i) { final Phenotype<G, C> individual = pop.get(i); if (!_validator.test(individual)) { pop.set(i, newPhenotype(generation)); ++invalidCount; } else if (individual.getAge(generation) > _maximalPhenotypeAge) { pop.set(i, newPhenotype(generation)); ++killCount; } } return new FilterResult<>(pop.toISeq(), killCount, invalidCount); }
final MSeq<T> front = MSeq.of(set); int n = front.size(); int i = 0; while (i < n) {
@Test public void selectMinimum() { final MSeq<Phenotype<DoubleGene, Double>> population = MSeq.ofLength(100); for (int i = 0, n = population.size(); i < n; ++i) { final Genotype<DoubleGene> gt = Genotype.of(DoubleChromosome.of(DoubleGene.of(i, 0, n))); population.set(i, Phenotype.of(gt, 12, TestUtils.FF)); } final EliteSelector<DoubleGene, Double> selector = new EliteSelector<>(); final ISeq<Phenotype<DoubleGene, Double>> selected = selector.select(population, 10, Optimize.MINIMUM); Assert.assertEquals( population.get(0).getGenotype().getGene().doubleValue(), selected.get(0).getGenotype().getGene().doubleValue() ); }
@Test public void selectMaximum() { final MSeq<Phenotype<DoubleGene, Double>> population = MSeq.ofLength(100); for (int i = 0, n = population.size(); i < n; ++i) { final Genotype<DoubleGene> gt = Genotype.of(DoubleChromosome.of(DoubleGene.of(i, 0, n))); population.set(i, Phenotype.of(gt, 12, TestUtils.FF)); } final EliteSelector<DoubleGene, Double> selector = new EliteSelector<>(); final ISeq<Phenotype<DoubleGene, Double>> selected = selector.select(population, 10, Optimize.MAXIMUM); Assert.assertEquals( population.get(population.length() - 1).getGenotype().getGene().doubleValue(), selected.get(0).getGenotype().getGene().doubleValue() ); }
@Override public AltererResult<G, C> alter( final Seq<Phenotype<G, C>> population, final long generation ) { final AltererResult<G, C> result = _adoptee.alter(population, generation); final MSeq<Phenotype<G, C>> pop = MSeq.of(population); for (int i = 0, n = pop.size(); i < n; ++i) { if (!pop.get(i).isValid()) { pop.set(i, repair(pop.get(i))); } } return AltererResult.of(pop.toISeq(), result.getAlterations()); }