View Javadoc
1   package net.bmahe.genetics4j.core.spec.replacement;
2   
3   import org.apache.commons.lang3.Validate;
4   import org.immutables.value.Value;
5   
6   import net.bmahe.genetics4j.core.spec.selection.SelectionPolicy;
7   
8   /**
9    * Specify an elitism based replacement strategy
10   * <p>Elitism will retain the best individuals of both offsprings and survivors of the previous generation.
11   */
12  @Value.Immutable
13  public interface Elitism extends ReplacementStrategy {
14  	static final double DEFAULT_OFFSPRING_RATIO = 0.99;
15  
16  	static final int DEFAULT_AT_LEAST_NUM_OFFSPRINGS = 0;
17  	static final int DEFAULT_AT_LEAST_NUM_SURVIVORS = 1;
18  
19  	/**
20  	 * {@return the policy used to select offsprings for the next generation}
21  	 */
22  	SelectionPolicy offspringSelectionPolicy();
23  
24  	/**
25  	 * {@return how many offsprings that elitism will always select}
26  	 */
27  	@Value.Default
28  	default int atLeastNumOffsprings() {
29  		return DEFAULT_AT_LEAST_NUM_OFFSPRINGS;
30  	}
31  
32  	/**
33  	 * {@return the policy used to select survivors for the next generation}
34  	 */
35  	SelectionPolicy survivorSelectionPolicy();
36  
37  	/**
38  	 * {@return how many survivors that elitism will always select}
39  	 */
40  	@Value.Default
41  	default int atLeastNumSurvivors() {
42  		return DEFAULT_AT_LEAST_NUM_SURVIVORS;
43  	}
44  
45  	/**
46  	 * {@return how many children will be generated at each iteration. Value must be between 0 and 1 (inclusive)
47  	 * <p>The number of survivor will be the complement of it, or 1 - offspringRatio()}
48  	 */
49  	@Value.Default
50  	default double offspringRatio() {
51  		return DEFAULT_OFFSPRING_RATIO;
52  	}
53  
54  	@Value.Check
55  	default void check() {
56  		Validate.inclusiveBetween(0.0, 1.0, offspringRatio());
57  		Validate.isTrue(atLeastNumOffsprings() >= 0);
58  		Validate.isTrue(atLeastNumSurvivors() >= 0);
59  	}
60  
61  	class Builder extends ImmutableElitism.Builder {
62  	}
63  
64  	public static Builder builder() {
65  		return new Builder();
66  	}
67  }