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