View Javadoc
1   package net.bmahe.genetics4j.core;
2   
3   import java.util.Arrays;
4   import java.util.Collection;
5   import java.util.Objects;
6   
7   import org.apache.commons.lang3.Validate;
8   
9   import net.bmahe.genetics4j.core.chromosomes.Chromosome;
10  
11  /**
12   * Represents a genotype in an evolutionary algorithm, which is a collection of chromosomes.
13   * 
14   * <p>A genotype encapsulates the complete genetic representation of an individual solution, consisting of one or more
15   * chromosomes that together define the individual's characteristics. Each chromosome may represent different aspects or
16   * components of the solution space.
17   * 
18   * <p>Genotypes are immutable once created and provide type-safe access to their constituent chromosomes.
19   * 
20   * @see Chromosome
21   * @see Individual
22   */
23  public class Genotype {
24  
25  	private final Chromosome[] chromosomes;
26  
27  	/**
28  	 * Creates a new genotype with the specified chromosomes.
29  	 * 
30  	 * @param _chromosomes one or more chromosomes to include in this genotype
31  	 * @throws IllegalArgumentException if chromosomes array is null or empty
32  	 */
33  	public Genotype(final Chromosome... _chromosomes) {
34  		Objects.requireNonNull(_chromosomes);
35  		Validate.isTrue(_chromosomes.length > 0);
36  
37  		this.chromosomes = _chromosomes;
38  	}
39  
40  	/**
41  	 * Creates a new genotype with chromosomes from the specified collection.
42  	 * 
43  	 * @param _chromosomes a collection of chromosomes to include in this genotype
44  	 * @throws IllegalArgumentException if chromosomes collection is null or empty
45  	 */
46  	public Genotype(final Collection<Chromosome> _chromosomes) {
47  		Objects.requireNonNull(_chromosomes);
48  		Validate.isTrue(_chromosomes.isEmpty() == false);
49  
50  		final Chromosome[] chromosomesArray = _chromosomes.toArray(new Chromosome[_chromosomes.size()]);
51  
52  		this.chromosomes = chromosomesArray;
53  	}
54  
55  	/**
56  	 * Returns the number of chromosomes in this genotype.
57  	 * 
58  	 * @return the count of chromosomes
59  	 */
60  	public int getSize() {
61  		return chromosomes.length;
62  	}
63  
64  	/**
65  	 * Returns all chromosomes in this genotype.
66  	 * 
67  	 * @return an array containing all chromosomes
68  	 */
69  	public Chromosome[] getChromosomes() {
70  		return chromosomes;
71  	}
72  
73  	/**
74  	 * Returns the chromosome at the specified index.
75  	 * 
76  	 * @param index the index of the chromosome to retrieve (0-based)
77  	 * @return the chromosome at the specified index
78  	 * @throws IllegalArgumentException if index is negative or greater than or equal to the number of chromosomes
79  	 */
80  	public Chromosome getChromosome(final int index) {
81  		Validate.isTrue(index >= 0);
82  		Validate.isTrue(index < chromosomes.length);
83  
84  		return chromosomes[index];
85  	}
86  
87  	/**
88  	 * Returns the chromosome at the specified index, cast to the specified type.
89  	 * 
90  	 * @param <T>   the expected chromosome type
91  	 * @param index the index of the chromosome to retrieve (0-based)
92  	 * @param clazz the class to cast the chromosome to
93  	 * @return the chromosome at the specified index, cast to the specified type
94  	 * @throws IllegalArgumentException if index is invalid or clazz is null
95  	 * @throws ClassCastException       if the chromosome cannot be cast to the specified type
96  	 */
97  	public <T extends Chromosome> T getChromosome(final int index, Class<T> clazz) {
98  		Validate.isTrue(index >= 0);
99  		Validate.isTrue(
100 				index < chromosomes.length,
101 				"Index (%d) larger than the number of chromosomes (%d)",
102 				index,
103 				chromosomes.length);
104 		Objects.requireNonNull(clazz);
105 
106 		return clazz.cast(chromosomes[index]);
107 	}
108 
109 	@Override
110 	public int hashCode() {
111 		final int prime = 31;
112 		int result = 1;
113 		return prime * result + Arrays.hashCode(chromosomes);
114 	}
115 
116 	@Override
117 	public boolean equals(Object obj) {
118 		if (this == obj) {
119 			return true;
120 		}
121 		if (obj == null) {
122 			return false;
123 		}
124 		if (getClass() != obj.getClass()) {
125 			return false;
126 		}
127 		Genotype other = (Genotype) obj;
128 		return Arrays.equals(chromosomes, other.chromosomes);
129 	}
130 
131 	@Override
132 	public String toString() {
133 		return "Genotype [chromosomes=" + Arrays.toString(chromosomes) + "]";
134 	}
135 }