| 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 for | |
| 16 | * OpenCL-based fitness evaluation. This configuration combines traditional EA parameters (selection, mutation, | |
| 17 | * 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 | * | |
| 36 | * <pre>{@code | |
| 37 | * // Define OpenCL program with kernel source | |
| 38 | * Program optimizationKernels = Program.ofResource("/kernels/tsp_fitness.cl") | |
| 39 | * .withBuildOption("-DCITY_COUNT=100") | |
| 40 | * .withBuildOption("-DLOCAL_SIZE=256"); | |
| 41 | * | |
| 42 | * // Create GPU-specific fitness function | |
| 43 | * OpenCLFitness<Double> gpuFitness = new TSPGPUFitness(); | |
| 44 | * | |
| 45 | * // Build complete GPU EA configuration | |
| 46 | * GPUEAConfiguration<Double> config = GPUEAConfiguration.<Double>builder() | |
| 47 | * // Core EA components | |
| 48 | * .chromosomeSpecs(intPermutationSpec) | |
| 49 | * .parentSelectionPolicy(Tournament.of(3)) | |
| 50 | * .combinationPolicy(OrderCrossover.build()) | |
| 51 | * .mutationPolicies(SwapMutation.of(0.1)) | |
| 52 | * .replacementStrategy(Elitism.builder() | |
| 53 | * .offspringRatio(0.8) | |
| 54 | * .build()) | |
| 55 | * | |
| 56 | * // GPU-specific components | |
| 57 | * .program(optimizationKernels) | |
| 58 | * .fitness(gpuFitness) | |
| 59 | * .build(); | |
| 60 | * }</pre> | |
| 61 | * | |
| 62 | * <p>OpenCL integration benefits: | |
| 63 | * <ul> | |
| 64 | * <li><strong>Performance</strong>: Massive parallelism for fitness evaluation</li> | |
| 65 | * <li><strong>Scalability</strong>: Handle large populations efficiently</li> | |
| 66 | * <li><strong>Hardware utilization</strong>: Leverage GPU compute units</li> | |
| 67 | * <li><strong>Memory bandwidth</strong>: High-throughput data processing</li> | |
| 68 | * </ul> | |
| 69 | * | |
| 70 | * <p>Configuration validation: | |
| 71 | * <ul> | |
| 72 | * <li><strong>Program completeness</strong>: Ensures all required kernels are specified</li> | |
| 73 | * <li><strong>Fitness compatibility</strong>: Validates fitness function matches program interface</li> | |
| 74 | * <li><strong>Resource requirements</strong>: Checks memory and compute requirements are feasible</li> | |
| 75 | * </ul> | |
| 76 | * | |
| 77 | * <p>Builder pattern usage: | |
| 78 | * <ul> | |
| 79 | * <li><strong>Type safety</strong>: Generic builder ensures fitness type consistency</li> | |
| 80 | * <li><strong>Validation</strong>: Build-time validation of configuration completeness</li> | |
| 81 | * <li><strong>Convenience methods</strong>: Simplified specification of chromosome and mutation arrays</li> | |
| 82 | * <li><strong>Immutability</strong>: Thread-safe configuration objects</li> | |
| 83 | * </ul> | |
| 84 | * | |
| 85 | * @param <T> the type of fitness values produced by the GPU fitness function | |
| 86 | * @see AbstractEAConfiguration | |
| 87 | * @see Program | |
| 88 | * @see OpenCLFitness | |
| 89 | * @see net.bmahe.genetics4j.gpu.GPUEASystemFactory | |
| 90 | */ | |
| 91 | @Value.Immutable | |
| 92 | public abstract class GPUEAConfiguration<T extends Comparable<T>> extends AbstractEAConfiguration<T> { | |
| 93 | ||
| 94 | /** | |
| 95 | * Returns the OpenCL program specification containing kernel source code and build options. | |
| 96 | * | |
| 97 | * <p>The program defines the OpenCL kernels that will be compiled and executed on GPU devices for fitness | |
| 98 | * evaluation. This includes source code, build flags, and kernel definitions required for the evolutionary algorithm | |
| 99 | * execution. | |
| 100 | * | |
| 101 | * @return the OpenCL program specification for kernel compilation | |
| 102 | */ | |
| 103 | public abstract Program program(); | |
| 104 | ||
| 105 | /** | |
| 106 | * Returns the GPU-specific fitness function that implements evaluation using OpenCL. | |
| 107 | * | |
| 108 | * <p>The fitness function encapsulates the logic for evaluating individual solutions using GPU acceleration. It | |
| 109 | * coordinates data transfer, kernel execution, and result extraction for efficient parallel fitness computation. | |
| 110 | * | |
| 111 | * @return the OpenCL fitness function for GPU-accelerated evaluation | |
| 112 | */ | |
| 113 | public abstract OpenCLFitness<T> fitness(); | |
| 114 | ||
| 115 | public static class Builder<T extends Comparable<T>> extends ImmutableGPUEAConfiguration.Builder<T> { | |
| 116 | ||
| 117 | public final GPUEAConfiguration.Builder<T> chromosomeSpecs(final ChromosomeSpec... elements) { | |
| 118 |
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)); |
| 119 | } | |
| 120 | ||
| 121 | public final GPUEAConfiguration.Builder<T> mutationPolicies(final MutationPolicy... elements) { | |
| 122 |
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)); |
| 123 | } | |
| 124 | } | |
| 125 | ||
| 126 | /** | |
| 127 | * Creates a new builder for constructing GPU EA configurations. | |
| 128 | * | |
| 129 | * <p>The builder provides a fluent interface for specifying both core EA components and GPU-specific settings. Type | |
| 130 | * safety is ensured through generic parameterization. | |
| 131 | * | |
| 132 | * @param <U> the type of fitness values for the EA configuration | |
| 133 | * @return a new builder instance for creating GPU EA configurations | |
| 134 | */ | |
| 135 | public static <U extends Comparable<U>> Builder<U> builder() { | |
| 136 |
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<>(); |
| 137 | } | |
| 138 | } | |
Mutations | ||
| 118 |
1.1 2.2 3.3 4.4 |
|
| 122 |
1.1 2.2 3.3 4.4 |
|
| 136 |
1.1 2.2 |