1 package net.bmahe.genetics4j.core.chromosomes; 2 3 import java.util.Arrays; 4 5 import org.apache.commons.lang3.Validate; 6 7 /** 8 * A chromosome implementation that represents genetic information as an array of integer values. 9 * 10 * <p>IntChromosome is widely used for discrete optimization problems where solutions can be 11 * encoded as sequences of integer values within specified ranges. Each position in the array 12 * represents a gene, and the integer value at that position represents the allele. 13 * 14 * <p>This chromosome type is particularly suitable for: 15 * <ul> 16 * <li><strong>Combinatorial optimization</strong>: Problems with discrete decision variables</li> 17 * <li><strong>Parameter optimization</strong>: Integer hyperparameters, configuration settings</li> 18 * <li><strong>Permutation encoding</strong>: When combined with appropriate operators</li> 19 * <li><strong>Resource allocation</strong>: Assignment and scheduling problems</li> 20 * <li><strong>Graph problems</strong>: Node labeling, path encoding</li> 21 * </ul> 22 * 23 * <p>Key features: 24 * <ul> 25 * <li><strong>Bounded values</strong>: All integers are constrained to [minValue, maxValue]</li> 26 * <li><strong>Fixed length</strong>: Chromosome size is determined at creation time</li> 27 * <li><strong>Immutable</strong>: Values cannot be changed after construction</li> 28 * <li><strong>Type-safe</strong>: Compile-time guarantees for integer operations</li> 29 * </ul> 30 * 31 * <p>The chromosome maintains bounds information which is used by genetic operators 32 * to ensure that crossover and mutation operations produce valid offspring within 33 * the specified constraints. 34 * 35 * @see Chromosome 36 * @see net.bmahe.genetics4j.core.spec.chromosome.IntChromosomeSpec 37 * @see net.bmahe.genetics4j.core.chromosomes.factory.IntChromosomeFactory 38 */ 39 public class IntChromosome implements Chromosome { 40 41 private final int size; 42 private final int minValue; 43 private final int maxValue; 44 private final int[] values; 45 46 /** 47 * Creates a new integer chromosome with the specified parameters and values. 48 * 49 * @param _size the number of integer values in this chromosome 50 * @param _minValue the minimum allowed value for any integer in this chromosome 51 * @param _maxValue the maximum allowed value for any integer in this chromosome 52 * @param _values the array of integer values for this chromosome 53 * @throws IllegalArgumentException if size is not positive, if minValue > maxValue, 54 * if values array is null, or if the array length 55 * doesn't match the specified size 56 */ 57 public IntChromosome(final int _size, final int _minValue, final int _maxValue, final int[] _values) { 58 Validate.isTrue(_size > 0); 59 Validate.isTrue(_minValue <= _maxValue); 60 Validate.notNull(_values); 61 Validate.isTrue(_size == _values.length, "Provided size does not match the size of the content"); 62 63 this.size = _size; 64 this.minValue = _minValue; 65 this.maxValue = _maxValue; 66 this.values = Arrays.copyOf(_values, _size); 67 } 68 69 @Override 70 public int getNumAlleles() { 71 return size; 72 } 73 74 /** 75 * Returns the integer value at the specified index. 76 * 77 * @param index the index of the allele to retrieve (0-based) 78 * @return the integer value at the specified position 79 * @throws IllegalArgumentException if index is negative or greater than or equal to the chromosome size 80 */ 81 public int getAllele(final int index) { 82 Validate.inclusiveBetween(0, size - 1, index); 83 84 return values[index]; 85 } 86 87 /** 88 * Returns the number of integer values in this chromosome. 89 * 90 * @return the chromosome size 91 */ 92 public int getSize() { 93 return size; 94 } 95 96 /** 97 * Returns the minimum allowed value for integers in this chromosome. 98 * 99 * @return the minimum value constraint 100 */ 101 public int getMinValue() { 102 return minValue; 103 } 104 105 /** 106 * Returns the maximum allowed value for integers in this chromosome. 107 * 108 * @return the maximum value constraint 109 */ 110 public int getMaxValue() { 111 return maxValue; 112 } 113 114 /** 115 * Returns a copy of the integer values in this chromosome. 116 * 117 * <p>The returned array is a defensive copy; modifications to it will not 118 * affect this chromosome. 119 * 120 * @return a copy of the integer values array 121 */ 122 public int[] getValues() { 123 return values; 124 } 125 126 @Override 127 public int hashCode() { 128 final int prime = 31; 129 int result = 1; 130 result = prime * result + maxValue; 131 result = prime * result + minValue; 132 result = prime * result + size; 133 result = prime * result + Arrays.hashCode(values); 134 return result; 135 } 136 137 @Override 138 public boolean equals(Object obj) { 139 if (this == obj) 140 return true; 141 if (obj == null) 142 return false; 143 if (getClass() != obj.getClass()) 144 return false; 145 IntChromosome other = (IntChromosome) obj; 146 if (maxValue != other.maxValue) 147 return false; 148 if (minValue != other.minValue) 149 return false; 150 if (size != other.size) 151 return false; 152 if (!Arrays.equals(values, other.values)) 153 return false; 154 return true; 155 } 156 157 @Override 158 public String toString() { 159 return "IntChromosome [size=" + size + ", minValue=" + minValue + ", maxValue=" + maxValue + ", values=" 160 + Arrays.toString(values) + "]"; 161 } 162 }