View Javadoc
1   package net.bmahe.genetics4j.core.spec.selection;
2   
3   import java.util.Comparator;
4   
5   import org.immutables.value.Value;
6   
7   import net.bmahe.genetics4j.core.Individual;
8   
9   @Value.Style(overshadowImplementation = true)
10  @Value.Immutable
11  public abstract class Tournament<T extends Comparable<T>> implements SelectionPolicy {
12  
13  	@Value.Parameter
14  	public abstract int numCandidates();
15  
16  	@Value.Default
17  	public Comparator<Individual<T>> comparator() {
18  		return Comparator.comparing(Individual::fitness);
19  	}
20  
21  	/*
22  	 * TODO: Enabling this makes immutables create non-compilable code
23  	 * 
24  	 * @Value.Check
25  	 * protected void check() {
26  	 * Validate.isTrue(numCandidates() > 0);
27  	 * }
28  	 */
29  
30  	public static class Builder<T extends Comparable<T>> extends ImmutableTournament.Builder<T> {
31  	}
32  
33  	public static <U extends Comparable<U>> Tournament<U> of(final int numCandidates) {
34  		return ImmutableTournament.of(numCandidates);
35  	}
36  
37  	public static <U extends Comparable<U>> Tournament<U> of(final int numCandidates,
38  			final Comparator<Individual<U>> comparator) {
39  		return new Builder<U>().numCandidates(numCandidates).comparator(comparator).build();
40  	}
41  
42  }