UNPKG

@kometbomb/genetic-algorithm

Version:

Simple genetic algorithm helper class. Supports asynchronous fitness evaluation.

246 lines (245 loc) 13.5 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; exports.__esModule = true; exports.GeneticAlgorithm = void 0; var GeneticAlgorithm = /** @class */ (function () { /** * Construct a new GeneticAlgorithm. The type parameter is the type of the genotype passed to the callback functions. * * @param config Initial configuration. Can be changed runtime with .evolve() * @param initialPopulation The initial population. If config.populationSize is larger than this value, the rest will be filled with versions of the initial population. */ function GeneticAlgorithm(config, initialPopulation) { var _this = this; this.setConfig = function (config) { if (config.populationSize < 2) { throw new Error("populationSize has to be greater than one."); } if (config.elitistRatio !== undefined && !(config.elitistRatio >= 0 && config.elitistRatio <= 1)) { throw new Error("elititstRatio has to be between 0.0 and 1.0."); } _this.config = __assign({}, config); return config; }; this.getRankedPopulation = function (recalculateFitness) { if (recalculateFitness === void 0) { recalculateFitness = false; } return __awaiter(_this, void 0, void 0, function () { var hasNoFitness, hasFitness, fitness_1, rankedPopulation; return __generator(this, function (_a) { switch (_a.label) { case 0: hasNoFitness = this.population.filter(function (ranked) { return typeof ranked.fitness !== "number" || recalculateFitness; }); hasFitness = this.population.filter(function (ranked) { return typeof ranked.fitness === "number" && !recalculateFitness; }); if (!(hasNoFitness.length > 0)) return [3 /*break*/, 2]; return [4 /*yield*/, this.config.fitnessFunction(hasNoFitness.map(function (ranked) { return ranked.genotype; }))]; case 1: fitness_1 = _a.sent(); if (fitness_1.length !== hasNoFitness.length) { throw new Error("fitnessFunction should return as many fitness values as there are input genotypes."); } rankedPopulation = hasNoFitness.map(function (ranked, index) { return ({ genotype: ranked.genotype, fitness: fitness_1[index] }); }).concat(hasFitness); this.population = rankedPopulation; return [2 /*return*/, rankedPopulation]; case 2: return [2 /*return*/, hasFitness]; } }); }); }; this.crossover = function (phenotype, mate) { return _this.config.crossoverFunction(phenotype, mate); }; this.compete = function () { return __awaiter(_this, void 0, void 0, function () { var crossoverProbability, rankedPopulation, total, accumulatedFitness, elitistRatio, nextGeneration, getRandomParent, a, b; return __generator(this, function (_a) { switch (_a.label) { case 0: crossoverProbability = this.config.crossoverProbability !== undefined ? this.config.crossoverProbability : 0.5; return [4 /*yield*/, this.getRankedPopulation(!!this.config.recalculateFitnessBeforeEachGeneration)]; case 1: rankedPopulation = (_a.sent()) .map(function (item) { return (__assign(__assign({}, item), { accumulatedFitness: 0 })); }); rankedPopulation.sort(function (a, b) { return b.fitness - a.fitness; }); total = rankedPopulation.reduce(function (prev, curr) { return prev + curr.fitness; }, 0) || 1; accumulatedFitness = 0; rankedPopulation = rankedPopulation.map(function (genotype) { accumulatedFitness += genotype.fitness / total; return __assign(__assign({}, genotype), { accumulatedFitness: accumulatedFitness }); }); elitistRatio = this.config.elitistRatio !== undefined ? this.config.elitistRatio : 0.25; nextGeneration = rankedPopulation.slice(0, this.config.populationSize * elitistRatio); getRandomParent = function () { var r = Math.random(); var genotype = rankedPopulation.find(function (genotype) { return genotype.accumulatedFitness >= r; }); if (!genotype) { return rankedPopulation[Math.floor(Math.random() * rankedPopulation.length)]; } return genotype; }; while (nextGeneration.length < this.config.populationSize) { a = getRandomParent(); if (this.config.crossoverFunction && Math.random() < crossoverProbability) { b = getRandomParent(); nextGeneration.push({ genotype: this.crossover(a.genotype, b.genotype), fitness: null }); } else { nextGeneration.push({ genotype: this.mutate(a.genotype), fitness: null }); } } // Cull back to populationSize this.population = nextGeneration.slice(0, this.config.populationSize); return [2 /*return*/]; } }); }); }; this.mutate = function (genotype) { return _this.config.mutationFunction(genotype); }; this.populate = function () { var size = _this.population.length; while (_this.population.length < _this.config.populationSize) { _this.population.push({ genotype: _this.mutate(_this.population[Math.floor(Math.random() * size)].genotype), fitness: null }); } }; /** * Run for one generation. * * @param config Optional config to replace the config given to the constructor */ this.evolve = function (config) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: if (config) { this.setConfig(config); } this.populate(); return [4 /*yield*/, this.compete()]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; /** * @returns The best ranked genotype with fitness value. */ this.bestRanked = function () { return __awaiter(_this, void 0, void 0, function () { var ranked; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getRankedPopulation()]; case 1: ranked = (_a.sent()).filter(function (ranked) { return typeof ranked.fitness === "number"; }); if (ranked.length === 0) { throw new Error("Could not find genotypes with a calculated fitness value - did you run .evolve() yet?"); } return [2 /*return*/, ranked.sort(function (a, b) { return b.fitness - a.fitness; })[0]]; } }); }); }; /** * Only valid after running evolve(). * * @returns The best ranked genotype. */ this.best = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.bestRanked()]; case 1: return [2 /*return*/, (_a.sent()).genotype]; } }); }); }; /** * Only valid after running evolve(). * * @returns The best fitness value. */ this.bestScore = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.bestRanked()]; case 1: return [2 /*return*/, (_a.sent()).fitness]; } }); }); }; /** * @returns The full genotype population. */ this.getPopulation = function () { return _this.population.map(function (ranked) { return ranked.genotype; }); }; /** * Only valid after running evolve(). * * @returns The mean fitness value. */ this.meanFitness = function () { return __awaiter(_this, void 0, void 0, function () { var withFitness; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getRankedPopulation()]; case 1: withFitness = (_a.sent()).filter(function (ranked) { return ranked.fitness !== null; }); return [2 /*return*/, withFitness.reduce(function (acc, ranked) { return acc + ranked.fitness; }, 0) / withFitness.length]; } }); }); }; if (initialPopulation.length < 1) { throw new Error("Initial population has to be given."); } this.population = initialPopulation.map(function (genotype) { return ({ genotype: genotype, fitness: null }); }); this.config = this.setConfig(config); } return GeneticAlgorithm; }()); exports.GeneticAlgorithm = GeneticAlgorithm;