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.replacement.ImmutableDeleteNLast;
7   import net.bmahe.genetics4j.core.spec.selection.SelectionPolicy;
8   
9   /**
10   * Delete N Last
11   * <p>
12   * This replacement strategy deletes the N weakest individuals and replace them
13   * with the best offsprings
14   * 
15   */
16  @Value.Immutable
17  public interface DeleteNLast extends ReplacementStrategy {
18  
19  	static final double DEFAULT_WEAK_RATIO = 0.05;
20  
21  	/**
22  	 * How many weakest individuals to consider for replacement
23  	 * 
24  	 * @return
25  	 */
26  	@Value.Default
27  	@Value.Parameter
28  	default double weakRatio() {
29  		return DEFAULT_WEAK_RATIO;
30  	}
31  
32  	/**
33  	 * Describe which offsprings to select for the next generation
34  	 * 
35  	 * @return
36  	 */
37  	@Value.Parameter
38  	public abstract SelectionPolicy offspringSelectionPolicy();
39  
40  	@Value.Check
41  	default void check() {
42  		Validate.inclusiveBetween(0.0, 1.0, weakRatio());
43  	}
44  
45  	class Builder extends ImmutableDeleteNLast.Builder {
46  	}
47  
48  	static Builder builder() {
49  		return new Builder();
50  	}
51  
52  	static DeleteNLast of(final double weakRatio, final SelectionPolicy selectionPolicy) {
53  		return builder().weakRatio(weakRatio).offspringSelectionPolicy(selectionPolicy).build();
54  	}
55  }