|
| 1 | +/** |
| 2 | + * Web Worker for non-blocking optimization |
| 3 | + * |
| 4 | + * This worker runs optimization algorithms in a separate thread, |
| 5 | + * keeping the main UI responsive during long-running computations. |
| 6 | + */ |
| 7 | + |
| 8 | +// Import the WASM module |
| 9 | +import init, { |
| 10 | + RealVectorOptimizer, |
| 11 | + BitStringOptimizer, |
| 12 | + UmdaOptimizer, |
| 13 | + Nsga2Optimizer, |
| 14 | + ZdtProblem, |
| 15 | + version |
| 16 | +} from '../pkg/fugue_evo_wasm.js'; |
| 17 | + |
| 18 | +let wasmReady = false; |
| 19 | + |
| 20 | +// Initialize WASM when worker starts |
| 21 | +async function initWasm() { |
| 22 | + try { |
| 23 | + await init(); |
| 24 | + wasmReady = true; |
| 25 | + self.postMessage({ type: 'ready', version: version() }); |
| 26 | + } catch (e) { |
| 27 | + self.postMessage({ type: 'error', message: `Failed to initialize WASM: ${e.message}` }); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// Handle messages from main thread |
| 32 | +self.onmessage = async function(e) { |
| 33 | + const { id, action, params } = e.data; |
| 34 | + |
| 35 | + if (!wasmReady) { |
| 36 | + self.postMessage({ id, type: 'error', message: 'WASM not ready' }); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + try { |
| 41 | + let result; |
| 42 | + const startTime = performance.now(); |
| 43 | + |
| 44 | + switch (action) { |
| 45 | + case 'optimize-real-vector': |
| 46 | + result = runRealVectorOptimization(params); |
| 47 | + break; |
| 48 | + case 'optimize-bitstring': |
| 49 | + result = runBitStringOptimization(params); |
| 50 | + break; |
| 51 | + case 'optimize-umda': |
| 52 | + result = runUmdaOptimization(params); |
| 53 | + break; |
| 54 | + case 'optimize-nsga2': |
| 55 | + result = runNsga2Optimization(params); |
| 56 | + break; |
| 57 | + case 'optimize-zdt': |
| 58 | + result = runZdtOptimization(params); |
| 59 | + break; |
| 60 | + default: |
| 61 | + throw new Error(`Unknown action: ${action}`); |
| 62 | + } |
| 63 | + |
| 64 | + const elapsed = performance.now() - startTime; |
| 65 | + self.postMessage({ |
| 66 | + id, |
| 67 | + type: 'result', |
| 68 | + result, |
| 69 | + elapsed |
| 70 | + }); |
| 71 | + } catch (e) { |
| 72 | + self.postMessage({ |
| 73 | + id, |
| 74 | + type: 'error', |
| 75 | + message: e.message || String(e) |
| 76 | + }); |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +function runRealVectorOptimization(params) { |
| 81 | + const { |
| 82 | + dimension = 10, |
| 83 | + populationSize = 100, |
| 84 | + maxGenerations = 100, |
| 85 | + fitness = 'sphere', |
| 86 | + lowerBound = -5.12, |
| 87 | + upperBound = 5.12, |
| 88 | + seed = 0 |
| 89 | + } = params; |
| 90 | + |
| 91 | + const optimizer = new RealVectorOptimizer(dimension); |
| 92 | + optimizer.setPopulationSize(populationSize); |
| 93 | + optimizer.setMaxGenerations(maxGenerations); |
| 94 | + optimizer.setBounds(lowerBound, upperBound); |
| 95 | + optimizer.setFitness(fitness); |
| 96 | + if (seed) optimizer.setSeed(seed); |
| 97 | + |
| 98 | + const result = optimizer.optimize(); |
| 99 | + const output = { |
| 100 | + bestFitness: result.bestFitness, |
| 101 | + bestGenome: Array.from(result.bestGenome), |
| 102 | + generations: result.generations, |
| 103 | + evaluations: result.evaluations |
| 104 | + }; |
| 105 | + |
| 106 | + optimizer.free(); |
| 107 | + return output; |
| 108 | +} |
| 109 | + |
| 110 | +function runBitStringOptimization(params) { |
| 111 | + const { |
| 112 | + length = 50, |
| 113 | + populationSize = 100, |
| 114 | + maxGenerations = 100, |
| 115 | + problem = 'onemax', |
| 116 | + schemaSize = 8, |
| 117 | + seed = 0 |
| 118 | + } = params; |
| 119 | + |
| 120 | + const optimizer = new BitStringOptimizer(length); |
| 121 | + optimizer.setPopulationSize(populationSize); |
| 122 | + optimizer.setMaxGenerations(maxGenerations); |
| 123 | + if (seed) optimizer.setSeed(seed); |
| 124 | + |
| 125 | + let result; |
| 126 | + switch (problem) { |
| 127 | + case 'onemax': |
| 128 | + result = optimizer.solveOneMax(); |
| 129 | + break; |
| 130 | + case 'leadingones': |
| 131 | + result = optimizer.solveLeadingOnes(); |
| 132 | + break; |
| 133 | + case 'royalroad': |
| 134 | + result = optimizer.solveRoyalRoad(schemaSize); |
| 135 | + break; |
| 136 | + default: |
| 137 | + throw new Error(`Unknown problem: ${problem}`); |
| 138 | + } |
| 139 | + |
| 140 | + const output = { |
| 141 | + bestFitness: result.bestFitness, |
| 142 | + bestGenome: result.bestGenomeString(), |
| 143 | + generations: result.generations, |
| 144 | + evaluations: result.evaluations |
| 145 | + }; |
| 146 | + |
| 147 | + optimizer.free(); |
| 148 | + return output; |
| 149 | +} |
| 150 | + |
| 151 | +function runUmdaOptimization(params) { |
| 152 | + const { |
| 153 | + dimension = 10, |
| 154 | + populationSize = 100, |
| 155 | + maxGenerations = 100, |
| 156 | + fitness = 'sphere', |
| 157 | + selectionRatio = 0.5, |
| 158 | + lowerBound = -5.12, |
| 159 | + upperBound = 5.12, |
| 160 | + seed = 0 |
| 161 | + } = params; |
| 162 | + |
| 163 | + const optimizer = new UmdaOptimizer(dimension); |
| 164 | + optimizer.setPopulationSize(populationSize); |
| 165 | + optimizer.setMaxGenerations(maxGenerations); |
| 166 | + optimizer.setSelectionRatio(selectionRatio); |
| 167 | + optimizer.setBounds(lowerBound, upperBound); |
| 168 | + if (seed) optimizer.setSeed(seed); |
| 169 | + |
| 170 | + const result = optimizer.optimize(fitness); |
| 171 | + const output = { |
| 172 | + bestFitness: result.bestFitness, |
| 173 | + bestGenome: Array.from(result.bestGenome), |
| 174 | + generations: result.generations, |
| 175 | + evaluations: result.evaluations |
| 176 | + }; |
| 177 | + |
| 178 | + optimizer.free(); |
| 179 | + return output; |
| 180 | +} |
| 181 | + |
| 182 | +function runNsga2Optimization(params) { |
| 183 | + const { |
| 184 | + dimension = 5, |
| 185 | + numObjectives = 2, |
| 186 | + populationSize = 50, |
| 187 | + maxGenerations = 100, |
| 188 | + lowerBound = -5, |
| 189 | + upperBound = 5, |
| 190 | + seed = 0 |
| 191 | + } = params; |
| 192 | + |
| 193 | + const optimizer = new Nsga2Optimizer(dimension, numObjectives); |
| 194 | + optimizer.setPopulationSize(populationSize); |
| 195 | + optimizer.setMaxGenerations(maxGenerations); |
| 196 | + optimizer.setBounds(lowerBound, upperBound); |
| 197 | + if (seed) optimizer.setSeed(seed); |
| 198 | + |
| 199 | + // Use built-in ZDT1 as default multi-objective problem |
| 200 | + const result = optimizer.optimizeZdt(ZdtProblem.Zdt1); |
| 201 | + |
| 202 | + const paretoFront = []; |
| 203 | + for (let i = 0; i < result.frontSize; i++) { |
| 204 | + const sol = result.getSolution(i); |
| 205 | + paretoFront.push({ |
| 206 | + genome: Array.from(sol.genome), |
| 207 | + objectives: Array.from(sol.objectives) |
| 208 | + }); |
| 209 | + } |
| 210 | + |
| 211 | + const output = { |
| 212 | + frontSize: result.frontSize, |
| 213 | + paretoFront, |
| 214 | + generations: result.generations, |
| 215 | + evaluations: result.evaluations |
| 216 | + }; |
| 217 | + |
| 218 | + optimizer.free(); |
| 219 | + return output; |
| 220 | +} |
| 221 | + |
| 222 | +function runZdtOptimization(params) { |
| 223 | + const { |
| 224 | + problem = 'zdt1', |
| 225 | + dimension = 10, |
| 226 | + populationSize = 50, |
| 227 | + maxGenerations = 100, |
| 228 | + seed = 0 |
| 229 | + } = params; |
| 230 | + |
| 231 | + const optimizer = new Nsga2Optimizer(dimension, 2); |
| 232 | + optimizer.setPopulationSize(populationSize); |
| 233 | + optimizer.setMaxGenerations(maxGenerations); |
| 234 | + if (seed) optimizer.setSeed(seed); |
| 235 | + |
| 236 | + let zdtProblem; |
| 237 | + switch (problem.toLowerCase()) { |
| 238 | + case 'zdt1': |
| 239 | + zdtProblem = ZdtProblem.Zdt1; |
| 240 | + break; |
| 241 | + case 'zdt2': |
| 242 | + zdtProblem = ZdtProblem.Zdt2; |
| 243 | + break; |
| 244 | + case 'zdt3': |
| 245 | + zdtProblem = ZdtProblem.Zdt3; |
| 246 | + break; |
| 247 | + default: |
| 248 | + throw new Error(`Unknown ZDT problem: ${problem}`); |
| 249 | + } |
| 250 | + |
| 251 | + const result = optimizer.optimizeZdt(zdtProblem); |
| 252 | + |
| 253 | + const paretoFront = []; |
| 254 | + for (let i = 0; i < result.frontSize; i++) { |
| 255 | + const sol = result.getSolution(i); |
| 256 | + paretoFront.push({ |
| 257 | + genome: Array.from(sol.genome), |
| 258 | + objectives: Array.from(sol.objectives) |
| 259 | + }); |
| 260 | + } |
| 261 | + |
| 262 | + const output = { |
| 263 | + problem, |
| 264 | + frontSize: result.frontSize, |
| 265 | + paretoFront, |
| 266 | + generations: result.generations, |
| 267 | + evaluations: result.evaluations |
| 268 | + }; |
| 269 | + |
| 270 | + optimizer.free(); |
| 271 | + return output; |
| 272 | +} |
| 273 | + |
| 274 | +// Start initialization |
| 275 | +initWasm(); |
0 commit comments