UNPKG

genetic-search

Version:

Multiprocessing genetic algorithm implementation library

202 lines 8.88 kB
"use strict"; 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PopulationSummaryManager = exports.GenomeStatsManager = void 0; var itertools_1 = require("./itertools"); var utils_1 = require("./utils"); /** * A manager for the statistics of a population of genomes. * * This class implements the [[GenomeStatsManagerInterface]] interface. * * @category Statistics */ var GenomeStatsManager = /** @class */ (function () { function GenomeStatsManager() { } GenomeStatsManager.prototype.init = function (population, origin) { var e_1, _a; try { for (var population_1 = __values(population), population_1_1 = population_1.next(); !population_1_1.done; population_1_1 = population_1.next()) { var genome = population_1_1.value; this.initItem(genome, origin); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (population_1_1 && !population_1_1.done && (_a = population_1.return)) _a.call(population_1); } finally { if (e_1) throw e_1.error; } } }; GenomeStatsManager.prototype.update = function (population, phenomeMatrix, fitnessColumn) { var e_2, _a; try { for (var _b = __values((0, itertools_1.zip)(population, phenomeMatrix, fitnessColumn)), _c = _b.next(); !_c.done; _c = _b.next()) { var _d = __read(_c.value, 3), genome = _d[0], phenome = _d[1], fitness = _d[2]; this.updateItem(genome, phenome, fitness); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_2) throw e_2.error; } } }; GenomeStatsManager.prototype.initItem = function (genome, origin, parents) { if (parents === void 0) { parents = []; } if (genome.stats !== undefined) { return genome.stats; } genome.stats = { fitness: 0, age: 0, phenome: [], origin: origin, originCounters: { crossover: (0, utils_1.arraySum)(parents.map(function (p) { var _a, _b, _c; return (_c = (_b = (_a = p.stats) === null || _a === void 0 ? void 0 : _a.originCounters) === null || _b === void 0 ? void 0 : _b.crossover) !== null && _c !== void 0 ? _c : 0; })) + Number(origin === 'crossover'), mutation: (0, utils_1.arraySum)(parents.map(function (p) { var _a, _b, _c; return (_c = (_b = (_a = p.stats) === null || _a === void 0 ? void 0 : _a.originCounters) === null || _b === void 0 ? void 0 : _b.mutation) !== null && _c !== void 0 ? _c : 0; })) + Number(origin === 'mutation'), }, parentIds: parents.map(function (p) { return p.id; }), }; return genome.stats; }; /** * Updates the statistics of a genome. * * @param genome The genome to update. * @param phenome The phenome of the genome. * @param fitness The fitness of the genome. * * @returns The updated genome statistics. */ GenomeStatsManager.prototype.updateItem = function (genome, phenome, fitness) { var stats = this.initItem(genome, 'initial'); stats.age++; stats.fitness = fitness; stats.phenome = phenome; return stats; }; return GenomeStatsManager; }()); exports.GenomeStatsManager = GenomeStatsManager; /** * A manager for the population summary. * * This class implements the [[PopulationSummaryManagerInterface]] interface. * It is used to manage the population summary, which is a summary of the * statistics of a population of genomes. * * @category Statistics */ var PopulationSummaryManager = /** @class */ (function () { /** * Constructs a new population summary manager. */ function PopulationSummaryManager() { /** * The number of generations since the best genome has changed. */ this.stagnationCounter = 0; this.fitnessSummary = (0, utils_1.createEmptyStatSummary)(); this.groupedFitnessSummary = (0, utils_1.createEmptyGroupedStatSummary)(); this.ageSummary = (0, utils_1.createEmptyRangeStatSummary)(); } PopulationSummaryManager.prototype.get = function () { return { fitnessSummary: (0, utils_1.fullCopyObject)(this.fitnessSummary), groupedFitnessSummary: (0, utils_1.fullCopyObject)(this.groupedFitnessSummary), ageSummary: (0, utils_1.fullCopyObject)(this.ageSummary), stagnationCounter: this.stagnationCounter, }; }; PopulationSummaryManager.prototype.getRounded = function (precision) { return { fitnessSummary: (0, utils_1.roundStatSummary)(this.fitnessSummary, precision), groupedFitnessSummary: (0, utils_1.roundGroupedStatSummary)(this.groupedFitnessSummary, precision), ageSummary: (0, utils_1.roundRangeStatSummary)(this.ageSummary, precision), stagnationCounter: this.stagnationCounter, }; }; PopulationSummaryManager.prototype.update = function (sortedPopulation) { var _a; var bestGenomeId = (_a = sortedPopulation[0]) === null || _a === void 0 ? void 0 : _a.id; if (this.bestGenomeId !== bestGenomeId) { this.bestGenomeId = bestGenomeId; this.stagnationCounter = 0; } else { this.stagnationCounter++; } var statsCollection = sortedPopulation .filter(function (genome) { return genome.stats !== undefined; }) .map(function (genome) { return genome.stats; }); this.updateSummary(statsCollection); this.updateGroupedSummary(statsCollection); this.updateAgeSummary(statsCollection); }; /** * Updates the summary of the population. * * @param sortedStatsCollection The sorted collection of genome statistics. */ PopulationSummaryManager.prototype.updateSummary = function (sortedStatsCollection) { this.fitnessSummary = (0, utils_1.calcStatSummary)(sortedStatsCollection.map(function (stats) { return stats.fitness; })); }; /** * Updates the grouped summary of the population. * * @param sortedStatsCollection The sorted collection of genome statistics. */ PopulationSummaryManager.prototype.updateGroupedSummary = function (sortedStatsCollection) { var initialCollection = sortedStatsCollection.filter(function (stats) { return stats.origin === 'initial'; }); var crossoverCollection = sortedStatsCollection.filter(function (stats) { return stats.origin === 'crossover'; }); var mutationCollection = sortedStatsCollection.filter(function (stats) { return stats.origin === 'mutation'; }); this.groupedFitnessSummary = { initial: (0, utils_1.calcStatSummary)(initialCollection.map(function (stats) { return stats.fitness; })), crossover: (0, utils_1.calcStatSummary)(crossoverCollection.map(function (stats) { return stats.fitness; })), mutation: (0, utils_1.calcStatSummary)(mutationCollection.map(function (stats) { return stats.fitness; })), }; }; /** * Updates the summary of the age of the population. * * @param sortedStatsCollection The sorted collection of genome statistics. */ PopulationSummaryManager.prototype.updateAgeSummary = function (sortedStatsCollection) { var ageCollection = sortedStatsCollection.map(function (stats) { return stats.age; }); this.ageSummary = (0, utils_1.calcRangeStatSummary)(ageCollection); }; return PopulationSummaryManager; }()); exports.PopulationSummaryManager = PopulationSummaryManager; //# sourceMappingURL=stats.js.map