UNPKG

tycho-solver

Version:

Evolutionary computation and optimization library

99 lines (98 loc) 4.38 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MemeticAlgorithm = void 0; const localSearch_1 = require("../../search/localSearch"); const LoopOperator_1 = require("./components/LoopOperator"); // Helper: population initialization function initializePopulation(config) { return __awaiter(this, void 0, void 0, function* () { const population = []; for (let i = 0; i < config.populationSize; i++) { const result = config.initializationOperator.initialize({ populationSize: 1, individualFactory: config.individualFactory }); const genome = Array.isArray(result) ? result[0] : result; const resolvedGenome = genome instanceof Promise ? yield genome : genome; const fitness = yield config.evaluationOperator.evaluate(resolvedGenome); population.push({ genome: resolvedGenome, fitness }); } return population; }); } // Helper: local search application function applyLocalSearch(genome, config, localSearcher) { return __awaiter(this, void 0, void 0, function* () { const { objectiveFunction, neighborhoodFunction, localSearchOptions } = config; const result = yield localSearcher.search(genome, objectiveFunction, neighborhoodFunction, localSearchOptions); return result.solution; }); } class MemeticAlgorithm { constructor(config) { this.population = []; this.bestIndividual = null; this.config = config; this.localSearcher = new localSearch_1.LocalSearch(); } initializePopulation() { return __awaiter(this, void 0, void 0, function* () { this.population = yield initializePopulation(this.config); this.updateBest(); }); } evolve() { return __awaiter(this, void 0, void 0, function* () { if (this.population.length === 0) { yield this.initializePopulation(); } // Use the new LoopOperator for orchestration this.bestIndividual = yield (0, LoopOperator_1.memeticLoop)(this.population, this.config, this.localSearcher, (pop) => { if (!pop.length) return null; return pop.reduce((best, ind) => ind.fitness > best.fitness ? ind : best); }, applyLocalSearch); return this.getBestIndividual(); }); } getBestIndividual() { if (!this.bestIndividual && this.population.length > 0) { this.updateBest(); } return this.bestIndividual; } updateBest() { if (this.population.length === 0) return; this.bestIndividual = this.population.reduce((best, ind) => ind.fitness > best.fitness ? ind : best); } getPopulation() { return this.population; } } exports.MemeticAlgorithm = MemeticAlgorithm; // Export components for advanced usage __exportStar(require("./components"), exports);