View Javadoc
1   package net.bmahe.genetics4j.core.spec;
2   
3   import java.util.Objects;
4   
5   import net.bmahe.genetics4j.core.selection.RouletteWheelSelectionPolicyHandler;
6   import net.bmahe.genetics4j.core.spec.ImmutableEAExecutionContext.Builder;
7   
8   /**
9    * Defines multiple factory and helper methods to create and manage EAExecutionContexts
10   */
11  public class EAExecutionContexts {
12  
13  	private EAExecutionContexts() {
14  	}
15  
16  	public static <T extends Comparable<T>> Builder<T> standard() {
17  		return ImmutableEAExecutionContext.builder();
18  	}
19  
20  	/**
21  	 * Enrich an EAExecutionContext builder based on the knowledge of the fitness function returning a scalar value.
22  	 * <p>This will enrich it with additional implementations for selecting, combining and mutating individuals.
23  	 * 
24  	 * @param <T>     Type of the fitness measurement
25  	 * @param builder
26  	 * @return Enriched EAExecutionContext builder
27  	 */
28  	public static <T extends Number & Comparable<T>> Builder<T> enrichForScalarFitness(final Builder<T> builder) {
29  		Objects.requireNonNull(builder);
30  
31  		builder.addSelectionPolicyHandlerFactories(
32  				gsd -> new RouletteWheelSelectionPolicyHandler<T>(gsd.randomGenerator()));
33  		return builder;
34  	}
35  
36  	/**
37  	 * Create an EAExecutionContext builder based on the knowledge of the fitness function returning a scalar value.
38  	 * <p>This will enrich it with additional implementations for selecting, combining and mutating individuals.
39  	 * 
40  	 * @param <T>
41  	 * @return
42  	 */
43  	public static <T extends Number & Comparable<T>> Builder<T> forScalarFitness() {
44  
45  		final Builder<T> builder = ImmutableEAExecutionContext.builder();
46  		return enrichForScalarFitness(builder);
47  	}
48  }