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