1 | package net.bmahe.genetics4j.gpu.spec; | |
2 | ||
3 | import java.util.Arrays; | |
4 | ||
5 | import org.immutables.value.Value; | |
6 | ||
7 | import net.bmahe.genetics4j.core.spec.AbstractEAConfiguration; | |
8 | import net.bmahe.genetics4j.core.spec.chromosome.ChromosomeSpec; | |
9 | import net.bmahe.genetics4j.core.spec.mutation.MutationPolicy; | |
10 | import net.bmahe.genetics4j.gpu.spec.fitness.OpenCLFitness; | |
11 | ||
12 | /** | |
13 | * GPU-specific evolutionary algorithm configuration that extends the core EA framework with OpenCL capabilities. | |
14 | * | |
15 | * <p>GPUEAConfiguration extends {@link AbstractEAConfiguration} to include GPU-specific settings required | |
16 | * for OpenCL-based fitness evaluation. This configuration combines traditional EA parameters (selection, | |
17 | * mutation, crossover) with GPU-specific components like OpenCL programs and specialized fitness functions. | |
18 | * | |
19 | * <p>Key GPU-specific additions: | |
20 | * <ul> | |
21 | * <li><strong>OpenCL Program</strong>: Specifies kernel source code, build options, and compilation settings</li> | |
22 | * <li><strong>GPU Fitness Function</strong>: Specialized fitness implementation that leverages OpenCL execution</li> | |
23 | * <li><strong>Kernel Integration</strong>: Automatic coordination between EA framework and OpenCL kernels</li> | |
24 | * </ul> | |
25 | * | |
26 | * <p>Configuration workflow: | |
27 | * <ol> | |
28 | * <li><strong>Core EA setup</strong>: Define chromosome specs, selection policies, mutation rates</li> | |
29 | * <li><strong>OpenCL program</strong>: Specify kernel source code and compilation options</li> | |
30 | * <li><strong>GPU fitness</strong>: Implement fitness evaluation using OpenCL primitives</li> | |
31 | * <li><strong>Integration</strong>: Combine components into complete GPU-accelerated EA system</li> | |
32 | * </ol> | |
33 | * | |
34 | * <p>Example configuration: | |
35 | * <pre>{@code | |
36 | * // Define OpenCL program with kernel source | |
37 | * Program optimizationKernels = Program.ofResource("/kernels/tsp_fitness.cl") | |
38 | * .withBuildOption("-DCITY_COUNT=100") | |
39 | * .withBuildOption("-DLOCAL_SIZE=256"); | |
40 | * | |
41 | * // Create GPU-specific fitness function | |
42 | * OpenCLFitness<Double> gpuFitness = new TSPGPUFitness(); | |
43 | * | |
44 | * // Build complete GPU EA configuration | |
45 | * GPUEAConfiguration<Double> config = GPUEAConfiguration.<Double>builder() | |
46 | * // Core EA components | |
47 | * .chromosomeSpecs(intPermutationSpec) | |
48 | * .parentSelectionPolicy(Tournament.of(3)) | |
49 | * .combinationPolicy(OrderCrossover.build()) | |
50 | * .mutationPolicies(SwapMutation.of(0.1)) | |
51 | * .replacementStrategy(Elitism.builder().offspringRatio(0.8).build()) | |
52 | * | |
53 | * // GPU-specific components | |
54 | * .program(optimizationKernels) | |
55 | * .fitness(gpuFitness) | |
56 | * .build(); | |
57 | * }</pre> | |
58 | * | |
59 | * <p>OpenCL integration benefits: | |
60 | * <ul> | |
61 | * <li><strong>Performance</strong>: Massive parallelism for fitness evaluation</li> | |
62 | * <li><strong>Scalability</strong>: Handle large populations efficiently</li> | |
63 | * <li><strong>Hardware utilization</strong>: Leverage GPU compute units</li> | |
64 | * <li><strong>Memory bandwidth</strong>: High-throughput data processing</li> | |
65 | * </ul> | |
66 | * | |
67 | * <p>Configuration validation: | |
68 | * <ul> | |
69 | * <li><strong>Program completeness</strong>: Ensures all required kernels are specified</li> | |
70 | * <li><strong>Fitness compatibility</strong>: Validates fitness function matches program interface</li> | |
71 | * <li><strong>Resource requirements</strong>: Checks memory and compute requirements are feasible</li> | |
72 | * </ul> | |
73 | * | |
74 | * <p>Builder pattern usage: | |
75 | * <ul> | |
76 | * <li><strong>Type safety</strong>: Generic builder ensures fitness type consistency</li> | |
77 | * <li><strong>Validation</strong>: Build-time validation of configuration completeness</li> | |
78 | * <li><strong>Convenience methods</strong>: Simplified specification of chromosome and mutation arrays</li> | |
79 | * <li><strong>Immutability</strong>: Thread-safe configuration objects</li> | |
80 | * </ul> | |
81 | * | |
82 | * @param <T> the type of fitness values produced by the GPU fitness function | |
83 | * @see AbstractEAConfiguration | |
84 | * @see Program | |
85 | * @see OpenCLFitness | |
86 | * @see net.bmahe.genetics4j.gpu.GPUEASystemFactory | |
87 | */ | |
88 | @Value.Immutable | |
89 | public abstract class GPUEAConfiguration<T extends Comparable<T>> extends AbstractEAConfiguration<T> { | |
90 | ||
91 | /** | |
92 | * Returns the OpenCL program specification containing kernel source code and build options. | |
93 | * | |
94 | * <p>The program defines the OpenCL kernels that will be compiled and executed on GPU devices | |
95 | * for fitness evaluation. This includes source code, build flags, and kernel definitions | |
96 | * required for the evolutionary algorithm execution. | |
97 | * | |
98 | * @return the OpenCL program specification for kernel compilation | |
99 | */ | |
100 | public abstract Program program(); | |
101 | ||
102 | /** | |
103 | * Returns the GPU-specific fitness function that implements evaluation using OpenCL. | |
104 | * | |
105 | * <p>The fitness function encapsulates the logic for evaluating individual solutions | |
106 | * using GPU acceleration. It coordinates data transfer, kernel execution, and result | |
107 | * extraction for efficient parallel fitness computation. | |
108 | * | |
109 | * @return the OpenCL fitness function for GPU-accelerated evaluation | |
110 | */ | |
111 | public abstract OpenCLFitness<T> fitness(); | |
112 | ||
113 | public static class Builder<T extends Comparable<T>> extends ImmutableGPUEAConfiguration.Builder<T> { | |
114 | ||
115 | public final GPUEAConfiguration.Builder<T> chromosomeSpecs(final ChromosomeSpec... elements) { | |
116 |
4
1. chromosomeSpecs : removed call to java/util/Arrays::asList → NO_COVERAGE 2. chromosomeSpecs : removed call to net/bmahe/genetics4j/gpu/spec/GPUEAConfiguration$Builder::chromosomeSpecs → NO_COVERAGE 3. chromosomeSpecs : replaced call to net/bmahe/genetics4j/gpu/spec/GPUEAConfiguration$Builder::chromosomeSpecs with receiver → NO_COVERAGE 4. chromosomeSpecs : replaced return value with null for net/bmahe/genetics4j/gpu/spec/GPUEAConfiguration$Builder::chromosomeSpecs → NO_COVERAGE |
return this.chromosomeSpecs(Arrays.asList(elements)); |
117 | } | |
118 | ||
119 | public final GPUEAConfiguration.Builder<T> mutationPolicies(final MutationPolicy... elements) { | |
120 |
4
1. mutationPolicies : replaced call to net/bmahe/genetics4j/gpu/spec/GPUEAConfiguration$Builder::mutationPolicies with receiver → NO_COVERAGE 2. mutationPolicies : replaced return value with null for net/bmahe/genetics4j/gpu/spec/GPUEAConfiguration$Builder::mutationPolicies → NO_COVERAGE 3. mutationPolicies : removed call to net/bmahe/genetics4j/gpu/spec/GPUEAConfiguration$Builder::mutationPolicies → NO_COVERAGE 4. mutationPolicies : removed call to java/util/Arrays::asList → NO_COVERAGE |
return this.mutationPolicies(Arrays.asList(elements)); |
121 | } | |
122 | } | |
123 | ||
124 | /** | |
125 | * Creates a new builder for constructing GPU EA configurations. | |
126 | * | |
127 | * <p>The builder provides a fluent interface for specifying both core EA components | |
128 | * and GPU-specific settings. Type safety is ensured through generic parameterization. | |
129 | * | |
130 | * @param <U> the type of fitness values for the EA configuration | |
131 | * @return a new builder instance for creating GPU EA configurations | |
132 | */ | |
133 | public static <U extends Comparable<U>> Builder<U> builder() { | |
134 |
2
1. builder : removed call to net/bmahe/genetics4j/gpu/spec/GPUEAConfiguration$Builder::<init> → NO_COVERAGE 2. builder : replaced return value with null for net/bmahe/genetics4j/gpu/spec/GPUEAConfiguration::builder → NO_COVERAGE |
return new Builder<>(); |
135 | } | |
136 | } | |
Mutations | ||
116 |
1.1 2.2 3.3 4.4 |
|
120 |
1.1 2.2 3.3 4.4 |
|
134 |
1.1 2.2 |