All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jenetics.engine.EvolutionStream Maven / Gradle / Ivy

There is a newer version: 3.6.0
Show newest version
/*
 * Java Genetic Algorithm Library (jenetics-3.1.0).
 * Copyright (c) 2007-2015 Franz Wilhelmstötter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author:
 *    Franz Wilhelmstötter ([email protected])
 */
package org.jenetics.engine;

import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;

import org.jenetics.Gene;

/**
 * The {@code EvolutionStream} class extends the Java {@link Stream} and adds a
 * method for limiting the evolution by a given predicate.
 *
 * @see java.util.stream.Stream
 * @see Engine
 *
 * @author Franz Wilhelmstötter
 * @since 3.0
 * @version 3.1
 */
public interface EvolutionStream<
	G extends Gene,
	C extends Comparable
>
	extends Stream>
{

	/**
	 * Returns a stream consisting of the elements of this stream, truncated
	 * when the given {@code proceed} predicate returns {@code false}.
	 * 

* General usage example: *

{@code
	 * final Phenotype result = engine.stream()
	 *      // Truncate the evolution stream after 5 "steady" generations.
	 *     .limit(bySteadyFitness(5))
	 *      // The evolution will stop after maximal 100 generations.
	 *     .limit(100)
	 *     .collect(toBestPhenotype());
	 * }
* * @see limit * * @param proceed the predicate which determines whether the stream is * truncated or not. If the predicate returns {@code false}, the * evolution stream is truncated. * @return the new stream * @throws NullPointerException if the given predicate is {@code null}. */ public EvolutionStream limit(final Predicate> proceed); /** * Create a new {@code EvolutionStream} from the given {@code start} * population and {@code evolution} function. The main purpose of this * factory method is to simplify the creation of an {@code EvolutionStream} * from an own evolution (GA) engine. * *
{@code
	 * final Supplier> start = ...
	 * final EvolutionStream stream =
	 *     EvolutionStream.of(start, new MySpecialEngine());
	 * }
* * A more complete example for would look like as: * *
{@code
	 * public final class SpecialEngine {
	 *
	 *     // The fitness function.
	 *     private static Double fitness(final Genotype gt) {
	 *         return gt.getGene().getAllele();
	 *     }
	 *
	 *     // Create new evolution start object.
	 *     private static EvolutionStart
	 *     start(final int populationSize, final long generation) {
	 *         final Population population =
	 *             Genotype.of(DoubleChromosome.of(0, 1)).instances()
	 *                 .map(gt -> Phenotype.of(gt, generation, SpecialEngine::fitness))
	 *                 .limit(populationSize)
	 *                 .collect(Population.toPopulation());
	 *
	 *         return EvolutionStart.of(population, generation);
	 *     }
	 *
	 *     // The special evolution function.
	 *     private static EvolutionResult
	 *     evolve(final EvolutionStart start) {
	 *         // Your special evolution implementation comes here!
	 *         return null;
	 *     }
	 *
	 *     public static void main(final String[] args) {
	 *         final Genotype best = EvolutionStream
	 *             .of(() -> start(50, 0), SpecialEngine::evolve)
	 *             .limit(limit.bySteadyFitness(10))
	 *             .limit(1000)
	 *             .collect(EvolutionResult.toBestGenotype());
	 *
	 *         System.out.println(String.format("Best Genotype: %s", best));
	 *     }
	 * }
	 * }
* * * @since 3.1 * * @param the gene type * @param the fitness type * @param start the evolution start * @param evolution the evolution function * @return a new {@code EvolutionStream} with the given {@code start} and * {@code evolution} function * @throws java.lang.NullPointerException if one of the arguments is * {@code null} */ public static , C extends Comparable> EvolutionStream of( final Supplier> start, final Function, EvolutionResult> evolution ) { return new EvolutionStreamImpl<>(start, evolution); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy