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