View Javadoc
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().offspringRatio(0.8).build())
53   * 
54   * 		// GPU-specific components
55   * 		.program(optimizationKernels)
56   * 		.fitness(gpuFitness)
57   * 		.build();
58   * }</pre>
59   * 
60   * <p>OpenCL integration benefits:
61   * <ul>
62   * <li><strong>Performance</strong>: Massive parallelism for fitness evaluation</li>
63   * <li><strong>Scalability</strong>: Handle large populations efficiently</li>
64   * <li><strong>Hardware utilization</strong>: Leverage GPU compute units</li>
65   * <li><strong>Memory bandwidth</strong>: High-throughput data processing</li>
66   * </ul>
67   * 
68   * <p>Configuration validation:
69   * <ul>
70   * <li><strong>Program completeness</strong>: Ensures all required kernels are specified</li>
71   * <li><strong>Fitness compatibility</strong>: Validates fitness function matches program interface</li>
72   * <li><strong>Resource requirements</strong>: Checks memory and compute requirements are feasible</li>
73   * </ul>
74   * 
75   * <p>Builder pattern usage:
76   * <ul>
77   * <li><strong>Type safety</strong>: Generic builder ensures fitness type consistency</li>
78   * <li><strong>Validation</strong>: Build-time validation of configuration completeness</li>
79   * <li><strong>Convenience methods</strong>: Simplified specification of chromosome and mutation arrays</li>
80   * <li><strong>Immutability</strong>: Thread-safe configuration objects</li>
81   * </ul>
82   * 
83   * @param <T> the type of fitness values produced by the GPU fitness function
84   * @see AbstractEAConfiguration
85   * @see Program
86   * @see OpenCLFitness
87   * @see net.bmahe.genetics4j.gpu.GPUEASystemFactory
88   */
89  @Value.Immutable
90  public abstract class GPUEAConfiguration<T extends Comparable<T>> extends AbstractEAConfiguration<T> {
91  
92  	/**
93  	 * Returns the OpenCL program specification containing kernel source code and build options.
94  	 * 
95  	 * <p>The program defines the OpenCL kernels that will be compiled and executed on GPU devices for fitness
96  	 * evaluation. This includes source code, build flags, and kernel definitions required for the evolutionary algorithm
97  	 * execution.
98  	 * 
99  	 * @return the OpenCL program specification for kernel compilation
100 	 */
101 	public abstract Program program();
102 
103 	/**
104 	 * Returns the GPU-specific fitness function that implements evaluation using OpenCL.
105 	 * 
106 	 * <p>The fitness function encapsulates the logic for evaluating individual solutions using GPU acceleration. It
107 	 * coordinates data transfer, kernel execution, and result 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 			return this.chromosomeSpecs(Arrays.asList(elements));
117 		}
118 
119 		public final GPUEAConfiguration.Builder<T> mutationPolicies(final MutationPolicy... elements) {
120 			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 and GPU-specific settings. Type
128 	 * 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 		return new Builder<>();
135 	}
136 }