1 | package net.bmahe.genetics4j.core.spec.chromosome; | |
2 | ||
3 | import org.apache.commons.lang3.Validate; | |
4 | import org.immutables.value.Value; | |
5 | ||
6 | /** | |
7 | * Specification for integer array chromosomes in evolutionary algorithms. | |
8 | * | |
9 | * <p>IntChromosomeSpec defines the blueprint for creating chromosomes that represent solutions | |
10 | * as arrays of integer values. This is one of the most commonly used chromosome types, suitable | |
11 | * for discrete optimization problems, permutation problems, and integer programming. | |
12 | * | |
13 | * <p>The specification includes: | |
14 | * <ul> | |
15 | * <li><strong>Size</strong>: The number of integer genes in the chromosome</li> | |
16 | * <li><strong>Value range</strong>: Minimum and maximum bounds for each integer value</li> | |
17 | * <li><strong>Validation</strong>: Automatic constraint checking for valid parameters</li> | |
18 | * </ul> | |
19 | * | |
20 | * <p>Common use cases for integer chromosomes: | |
21 | * <ul> | |
22 | * <li><strong>Combinatorial optimization</strong>: Knapsack, bin packing, scheduling problems</li> | |
23 | * <li><strong>Parameter optimization</strong>: Integer hyperparameters, configuration values</li> | |
24 | * <li><strong>Permutation problems</strong>: Traveling salesman, job scheduling (with appropriate operators)</li> | |
25 | * <li><strong>Graph problems</strong>: Node selection, path optimization, network design</li> | |
26 | * <li><strong>Resource allocation</strong>: Assignment problems, load balancing</li> | |
27 | * </ul> | |
28 | * | |
29 | * <p>The integer values in the chromosome are constrained to the specified range [minValue, maxValue], | |
30 | * inclusive on both ends. The genetic operators (crossover and mutation) respect these constraints | |
31 | * and ensure that offspring remain within valid bounds. | |
32 | * | |
33 | * <p>Example usage: | |
34 | * <pre>{@code | |
35 | * // Create specification for 10 integers in range [0, 100] | |
36 | * IntChromosomeSpec spec = IntChromosomeSpec.of(10, 0, 100); | |
37 | * | |
38 | * // Create specification for permutation of 20 items (values 0-19) | |
39 | * IntChromosomeSpec permutationSpec = IntChromosomeSpec.of(20, 0, 19); | |
40 | * | |
41 | * // Use in EA configuration | |
42 | * EAConfiguration<Double> config = EAConfigurationBuilder.<Double>builder() | |
43 | * .chromosomeSpecs(spec) | |
44 | * .parentSelectionPolicy(Tournament.of(3)) | |
45 | * .build(); | |
46 | * }</pre> | |
47 | * | |
48 | * @see net.bmahe.genetics4j.core.chromosomes.IntChromosome | |
49 | * @see net.bmahe.genetics4j.core.chromosomes.factory.IntChromosomeFactory | |
50 | * @see ChromosomeSpec | |
51 | */ | |
52 | @Value.Immutable | |
53 | public abstract class IntChromosomeSpec implements ChromosomeSpec { | |
54 | ||
55 | /** | |
56 | * Returns the number of integer values in chromosomes created from this specification. | |
57 | * | |
58 | * @return the chromosome size, always positive | |
59 | */ | |
60 | @Value.Parameter | |
61 | public abstract int size(); | |
62 | ||
63 | /** | |
64 | * Returns the minimum value (inclusive) for integer alleles in the chromosome. | |
65 | * | |
66 | * @return the minimum allowed integer value | |
67 | */ | |
68 | @Value.Parameter | |
69 | public abstract int minValue(); | |
70 | ||
71 | /** | |
72 | * Returns the maximum value (inclusive) for integer alleles in the chromosome. | |
73 | * | |
74 | * @return the maximum allowed integer value | |
75 | */ | |
76 | @Value.Parameter | |
77 | public abstract int maxValue(); | |
78 | ||
79 | @Value.Check | |
80 | protected void check() { | |
81 | Validate.isTrue(size() > 0); | |
82 | Validate.isTrue(minValue() <= maxValue()); | |
83 | } | |
84 | ||
85 | public static class Builder extends ImmutableIntChromosomeSpec.Builder { | |
86 | } | |
87 | ||
88 | /** | |
89 | * Creates a new integer chromosome specification with the given parameters. | |
90 | * | |
91 | * @param size the number of integer values in the chromosome, must be positive | |
92 | * @param minValue the minimum value (inclusive) for each integer in the chromosome | |
93 | * @param maxValue the maximum value (inclusive) for each integer in the chromosome | |
94 | * @return a new IntChromosomeSpec instance with the specified parameters | |
95 | * @throws IllegalArgumentException if size is not positive or if minValue > maxValue | |
96 | */ | |
97 | public static IntChromosomeSpec of(final int size, final int minValue, final int maxValue) { | |
98 |
2
1. of : replaced return value with null for net/bmahe/genetics4j/core/spec/chromosome/IntChromosomeSpec::of → KILLED 2. of : removed call to net/bmahe/genetics4j/core/spec/chromosome/ImmutableIntChromosomeSpec::of → KILLED |
return ImmutableIntChromosomeSpec.of(size, minValue, maxValue); |
99 | } | |
100 | ||
101 | } | |
Mutations | ||
98 |
1.1 2.2 |