UNPKG

genetic-search

Version:

Multiprocessing genetic algorithm implementation library

602 lines 26.6 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); 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 = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["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 (g && (g = 0, op[0] && (_ = 0)), _) 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 }; } }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ProportionalSelectionStrategy = exports.TournamentSelectionStrategy = exports.TruncationSelectionStrategy = exports.RandomSelectionStrategy = exports.DescendingSortingStrategy = exports.AscendingSortingStrategy = exports.ReferenceLossFitnessStrategy = exports.BasePhenomeStrategy = exports.BaseMutationStrategy = void 0; var utils_1 = require("./utils"); var itertools_1 = require("./itertools"); /** * Base class for mutation strategies. * * @template TGenome The type of genome objects in the population. * @template TConfig The type of configuration for the mutation strategy. * * @category Strategies * @category Mutation */ var BaseMutationStrategy = /** @class */ (function () { /** * Constructs a new instance of the mutation strategy. * * @param config The configuration for the mutation strategy. */ function BaseMutationStrategy(config) { this.config = config; } return BaseMutationStrategy; }()); exports.BaseMutationStrategy = BaseMutationStrategy; /** * Base class for phenome strategies. * * @template TGenome The type of genome objects in the population. * @template TConfig The type of configuration for the phenome strategy. * @template TTaskConfig The type of configuration required to execute the task of the calculating phenome. * * @category Strategies * @category Phenome */ var BasePhenomeStrategy = /** @class */ (function () { /** * Constructs a new instance of the phenome strategy. * * @param config The configuration for the phenome strategy. */ function BasePhenomeStrategy(config) { this.config = config; } /** * Collects and caches the phenome for a given population of genomes. * * @param population The population of genomes to collect phenome for. * @param cache The cache used to store and retrieve phenomes. * @returns A promise that resolves to a matrix of phenomes for the generation. */ BasePhenomeStrategy.prototype.collect = function (population, cache) { return __awaiter(this, void 0, void 0, function () { var pairs, resultsMap, genomesToRun, newResults, _a, _b, _c, genome, result, _d, _e, _f, genome, result; var e_1, _g, e_2, _h; var _this = this; return __generator(this, function (_j) { switch (_j.label) { case 0: pairs = population.map(function (genome) { return [genome.id, cache.getReady(genome.id)]; }); resultsMap = new Map(pairs); genomesToRun = population.filter(function (genome) { return resultsMap.get(genome.id) === undefined; }); return [4 /*yield*/, this.execTasks(genomesToRun.map(function (genome) { return _this.createTaskInput(genome); }))]; case 1: newResults = _j.sent(); try { for (_a = __values((0, itertools_1.zip)(genomesToRun, newResults)), _b = _a.next(); !_b.done; _b = _a.next()) { _c = __read(_b.value, 2), genome = _c[0], result = _c[1]; cache.set(genome.id, result); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_b && !_b.done && (_g = _a.return)) _g.call(_a); } finally { if (e_1) throw e_1.error; } } try { for (_d = __values((0, itertools_1.zip)(genomesToRun, newResults)), _e = _d.next(); !_e.done; _e = _d.next()) { _f = __read(_e.value, 2), genome = _f[0], result = _f[1]; resultsMap.set(genome.id, cache.get(genome.id, result)); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_e && !_e.done && (_h = _d.return)) _h.call(_d); } finally { if (e_2) throw e_2.error; } } return [2 /*return*/, population.map(function (genome) { return resultsMap.get(genome.id); })]; } }); }); }; /** * Executes the tasks to calculate the phenome of the given genomes. * * @param inputs The inputs for the tasks to execute. * @returns A promise that resolves to an array of phenome for each genome. */ BasePhenomeStrategy.prototype.execTasks = function (inputs) { return __awaiter(this, void 0, void 0, function () { var result, inputs_1, inputs_1_1, input, taskResult, e_3_1; var e_3, _a; var _b, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: result = []; _d.label = 1; case 1: _d.trys.push([1, 6, 7, 8]); inputs_1 = __values(inputs), inputs_1_1 = inputs_1.next(); _d.label = 2; case 2: if (!!inputs_1_1.done) return [3 /*break*/, 5]; input = inputs_1_1.value; return [4 /*yield*/, this.config.task(input)]; case 3: taskResult = _d.sent(); (_c = (_b = this.config).onTaskResult) === null || _c === void 0 ? void 0 : _c.call(_b, taskResult, input); result.push(taskResult); _d.label = 4; case 4: inputs_1_1 = inputs_1.next(); return [3 /*break*/, 2]; case 5: return [3 /*break*/, 8]; case 6: e_3_1 = _d.sent(); e_3 = { error: e_3_1 }; return [3 /*break*/, 8]; case 7: try { if (inputs_1_1 && !inputs_1_1.done && (_a = inputs_1.return)) _a.call(inputs_1); } finally { if (e_3) throw e_3.error; } return [7 /*endfinally*/]; case 8: return [2 /*return*/, result]; } }); }); }; return BasePhenomeStrategy; }()); exports.BasePhenomeStrategy = BasePhenomeStrategy; /** * A fitness strategy that calculates the fitness of a genome based on a reference loss. * * The fitness of a genome is calculated as the negative sum of the absolute differences between the reference loss * and the loss calculated for the genome. * * @category Strategies * @category Fitness */ var ReferenceLossFitnessStrategy = /** @class */ (function () { /** * Constructor of the reference loss fitness strategy. * * @param referenceConfig The configuration for the reference loss fitness strategy. */ function ReferenceLossFitnessStrategy(referenceConfig) { this.referenceConfig = referenceConfig; } ReferenceLossFitnessStrategy.prototype.score = function (results) { var normalizedLosses = this.formatLosses(results); return normalizedLosses.map(function (x) { return -(0, utils_1.arraySum)(x); }); }; /** * Formats the losses by normalizing the phenome matrix and applying weights to each row. * * @param results The generation phenome matrix to format. * @returns A matrix of formatted losses. */ ReferenceLossFitnessStrategy.prototype.formatLosses = function (results) { var _this = this; return (0, utils_1.normalizePhenomeMatrix)(results, this.referenceConfig.reference).map(function (result) { return _this.weighRow(result); }); }; /** * Weighs a row of phenome by multiplying each element with its corresponding weight. * * @param result The genome phenome row to weigh. * @returns A genome phenome row with applied weights. */ ReferenceLossFitnessStrategy.prototype.weighRow = function (result) { return (0, utils_1.arrayBinaryOperation)(result, this.referenceConfig.weights, function (x, y) { return x * y; }); }; return ReferenceLossFitnessStrategy; }()); exports.ReferenceLossFitnessStrategy = ReferenceLossFitnessStrategy; /** * Sorts a given iterable of genomes, fitness scores, and phenome rows in ascending order of their fitness scores. * * @param input An iterable containing tuples of genomes, their fitness scores, and their associated phenome rows. * @returns An array of sorted tuples of genomes, fitness scores, and phenome rows. * * @category Strategies * @category Sorting */ var AscendingSortingStrategy = /** @class */ (function () { function AscendingSortingStrategy() { } /** * Sorts a given iterable of genomes, fitness scores, and phenome rows in ascending order of their fitness scores. * * @param input An iterable containing tuples of genomes, their fitness scores, and their associated phenome rows. * @returns An array of sorted tuples of genomes, fitness scores, and phenome rows. */ AscendingSortingStrategy.prototype.sort = function (input) { return __spreadArray([], __read((0, itertools_1.sort)(input, function (lhs, rhs) { return lhs.fitness - rhs.fitness; })), false); }; return AscendingSortingStrategy; }()); exports.AscendingSortingStrategy = AscendingSortingStrategy; /** * Sorts a given iterable of genomes, fitness scores, and phenome rows in descending order of their fitness scores. * * @param input An iterable containing tuples of genomes, their fitness scores, and their associated phenome rows. * @returns An array of sorted tuples of genomes, fitness scores, and phenome rows. * * @category Strategies * @category Sorting */ var DescendingSortingStrategy = /** @class */ (function () { function DescendingSortingStrategy() { } /** * Sorts a given iterable of genomes, fitness scores, and phenome rows in descending order of their fitness scores. * * @param input An iterable containing tuples of genomes, their fitness scores, and their associated phenome rows. * @returns An array of sorted tuples of genomes, fitness scores, and phenome rows. */ DescendingSortingStrategy.prototype.sort = function (input) { return __spreadArray([], __read((0, itertools_1.sort)(input, function (lhs, rhs) { return rhs.fitness - lhs.fitness; })), false); }; return DescendingSortingStrategy; }()); exports.DescendingSortingStrategy = DescendingSortingStrategy; /** * A random selection strategy. * * This selection strategy randomly selects parents for mutation and crossover. * * @template TGenome The type of genome objects in the population. * * @category Strategies * @category Selection */ var RandomSelectionStrategy = /** @class */ (function () { /** * Constructor of the random selection strategy. * * @param crossoverParentsCount The number of parents to select for crossover. */ function RandomSelectionStrategy(crossoverParentsCount) { this.crossoverParentsCount = crossoverParentsCount; } /** * Selects parents for crossover. * * @param input The population extended with fitness scores and phenome to select parents from. * @param count The number of parents to select. * @returns An array of parents arrays. */ RandomSelectionStrategy.prototype.selectForCrossover = function (input, count) { var result = []; for (var i = 0; i < count; i++) { var parents = []; for (var j = 0; j < this.crossoverParentsCount; j++) { parents.push((0, utils_1.getRandomArrayItem)(input).genome); } result.push(parents); } return result; }; /** * Selects parents for mutation. * * @param input The population extended with fitness scores and phenome to select parents from. * @param count The number of parents to select. * @returns An array of parents. */ RandomSelectionStrategy.prototype.selectForMutation = function (input, count) { var result = []; for (var i = 0; i < count; i++) { result.push((0, utils_1.getRandomArrayItem)(input).genome); } return result; }; return RandomSelectionStrategy; }()); exports.RandomSelectionStrategy = RandomSelectionStrategy; /** * A truncation selection strategy. * * This selection strategy selects the top `sliceThresholdRate` fraction of the population * as parents for mutation and crossover. * * @template TGenome The type of genome objects in the population. * * @category Strategies * @category Selection */ var TruncationSelectionStrategy = /** @class */ (function (_super) { __extends(TruncationSelectionStrategy, _super); /** * Constructor of the truncation selection strategy. * * @param crossoverParentsCount The number of parents to select for crossover. * @param sliceThresholdRate The rate at which to slice the population. */ function TruncationSelectionStrategy(crossoverParentsCount, sliceThresholdRate) { var _this = _super.call(this, crossoverParentsCount) || this; _this.sliceThresholdRate = sliceThresholdRate; return _this; } /** * Selects parents for crossover. * * @param input The population extended with fitness scores and phenome to select parents from. * @param count The number of parents to select. * @returns An array of parents arrays. */ TruncationSelectionStrategy.prototype.selectForCrossover = function (input, count) { if (this.sliceThresholdRate) { input = input.slice(0, input.length * this.sliceThresholdRate); } return _super.prototype.selectForCrossover.call(this, input, count); }; /** * Selects parents for mutation. * * @param input The population extended with fitness scores and phenome to select parents from. * @param count The number of parents to select. * @returns An array of parents. */ TruncationSelectionStrategy.prototype.selectForMutation = function (input, count) { if (this.sliceThresholdRate) { input = input.slice(0, input.length * this.sliceThresholdRate); } return _super.prototype.selectForMutation.call(this, input, count); }; return TruncationSelectionStrategy; }(RandomSelectionStrategy)); exports.TruncationSelectionStrategy = TruncationSelectionStrategy; /** * A selection strategy that uses a tournament to select parents. * * This selection strategy runs a tournament between random participants from the population, * and selects the best participant as a parent. * * @template TGenome The type of genome objects in the population. * * @category Strategies * @category Selection */ var TournamentSelectionStrategy = /** @class */ (function () { /** * Constructor of the tournament selection strategy. * * @param crossoverParentsCount The number of parents to select for crossover. * @param tournamentSize The number of participants in a tournament. */ function TournamentSelectionStrategy(crossoverParentsCount, tournamentSize) { this.crossoverParentsCount = crossoverParentsCount; this.tournamentSize = tournamentSize; } /** * Selects parents for crossover. * * @param input The population extended with fitness scores and phenome to select parents from. * @param count The number of parents to select. * @returns An array of parents arrays. */ TournamentSelectionStrategy.prototype.selectForCrossover = function (input, count) { var result = []; for (var i = 0; i < count; i++) { var parents = []; for (var j = 0; j < this.crossoverParentsCount; j++) { // Select the best from the tournament parents.push(this.runTournament(input).genome); } result.push(parents); } return result; }; /** * Selects parents for mutation. * * @param input The population extended with fitness scores and phenome to select parents from. * @param count The number of parents to select. * @returns An array of parents. */ TournamentSelectionStrategy.prototype.selectForMutation = function (input, count) { var result = []; for (var i = 0; i < count; i++) { // Select the best from the tournament result.push(this.runTournament(input).genome); } return result; }; /** * Conducts a tournament and returns the best participant. * * @param input The population. * @returns The best `EvaluatedGenome` from the tournament. */ TournamentSelectionStrategy.prototype.runTournament = function (input) { // Select random participants var tournamentParticipants = []; for (var k = 0; k < this.tournamentSize; k++) { tournamentParticipants.push((0, utils_1.getRandomArrayItem)(input)); } // Sort participants and select the best tournamentParticipants.sort(function (lhs, rhs) { return rhs.fitness - lhs.fitness; }); return tournamentParticipants[0]; // Best participant }; return TournamentSelectionStrategy; }()); exports.TournamentSelectionStrategy = TournamentSelectionStrategy; /** * A proportional selection strategy. * * This selection strategy selects parents for mutation and crossover based on their fitness proportion. * * @template TGenome The type of genome objects in the population. * * @category Strategies * @category Selection */ var ProportionalSelectionStrategy = /** @class */ (function () { /** * Constructor of the proportional selection strategy. * * @param crossoverParentsCount The number of parents to select for crossover. */ function ProportionalSelectionStrategy(crossoverParentsCount) { this.crossoverParentsCount = crossoverParentsCount; } /** * Selects parents for crossover using proportional selection. * * @param input The population extended with fitness scores and phenome to select parents from. * @param count The number of parents to select. * @returns An array of parents arrays. */ ProportionalSelectionStrategy.prototype.selectForCrossover = function (input, count) { var result = []; // Calculate total fitness of the population var totalFitness = input.reduce(function (acc, evaluatedGenome) { return acc + evaluatedGenome.fitness; }, 0); for (var i = 0; i < count; i++) { var parents = []; for (var j = 0; j < this.crossoverParentsCount; j++) { // Roulette wheel selection based on fitness proportion var selectedGenome = this.selectByFitness(input, totalFitness); parents.push(selectedGenome.genome); } result.push(parents); } return result; }; /** * Selects parents for mutation using proportional selection. * * @param input The population extended with fitness scores and phenome to select parents from. * @param count The number of parents to select. * @returns An array of parents. */ ProportionalSelectionStrategy.prototype.selectForMutation = function (input, count) { var result = []; // Calculate total fitness of the population var totalFitness = input.reduce(function (acc, evaluatedGenome) { return acc + evaluatedGenome.fitness; }, 0); for (var i = 0; i < count; i++) { // Roulette wheel selection based on fitness proportion var selectedGenome = this.selectByFitness(input, totalFitness); result.push(selectedGenome.genome); } return result; }; /** * Helper method to select an individual based on fitness proportion. * * @param input The population. * @param totalFitness The sum of all fitness scores in the population. * @returns A selected `EvaluatedGenome`. */ ProportionalSelectionStrategy.prototype.selectByFitness = function (input, totalFitness) { var e_4, _a; var randomValue = Math.random() * totalFitness; var result = input[input.length - 1]; // Fallback value in case of floating-point imprecisions try { for (var input_1 = __values(input), input_1_1 = input_1.next(); !input_1_1.done; input_1_1 = input_1.next()) { var evaluatedGenome = input_1_1.value; randomValue -= evaluatedGenome.fitness; if (randomValue <= 0) { result = evaluatedGenome; break; } } } catch (e_4_1) { e_4 = { error: e_4_1 }; } finally { try { if (input_1_1 && !input_1_1.done && (_a = input_1.return)) _a.call(input_1); } finally { if (e_4) throw e_4.error; } } return result; }; return ProportionalSelectionStrategy; }()); exports.ProportionalSelectionStrategy = ProportionalSelectionStrategy; //# sourceMappingURL=strategies.js.map