UNPKG

@zfunction/genetics-js

Version:

Genetic and evolutionary algorithms framework for the web

221 lines 7.79 kB
"use strict"; /* * @license * Copyright (c) 2019 Cristian Abrante. All rights reserved. * Licensed under the MIT License. See LICENSE in the project root for license information. */ Object.defineProperty(exports, "__esModule", { value: true }); /** * ## BaseIndividual * Base class for creating individuals which * are one of the fundamental blocks of evolutionary * algorithms. * * The fundamental part of the individual is the * genotype, which is the array that represents the * data of the individual. This genotype is composed * of genes. * * This abstract class provides some 'array-like' * methods for iterating over the genes and modify * or access its values. * * @typeparam T is the type of the individual. */ var BaseIndividual = /** @class */ (function () { /** * Constructor of the class expects an * array for initializing the genotype. * @param genotype genotype array. */ function BaseIndividual(genotype) { /** * Genotype array. */ this.genotypeArray = []; this.setGenotype(genotype); } Object.defineProperty(BaseIndividual.prototype, "genotype", { /** * getter for the genotype. * @return genotype array. */ get: function () { return this.genotypeArray; }, enumerable: true, configurable: true }); /** * Individuals are iterable, so this method * returns an iterator. * @return iterator over the genotype array. */ BaseIndividual.prototype[Symbol.iterator] = function () { return this.genotype[Symbol.iterator](); }; /** * Returns the gene at specified index. * @param geneIndex index of the gene to be accessed. * @return gene at specified index. * @throws RangeError if index is not in range [0-length) */ BaseIndividual.prototype.get = function (geneIndex) { this.checkIndexRange(geneIndex); return this.genotype[geneIndex]; }; /** * Returns the length of the genotype. * @return length of the genotype. */ BaseIndividual.prototype.length = function () { return this.genotype.length; }; /** * Returns an object iterable iterator whose * contains the pairs `[key, value]` for each * element. * @return iterable iterator object. */ BaseIndividual.prototype.entries = function () { return this.genotype.entries(); }; /** * Returns a boolean, which is true if all genes * in the genotype accept the condition specified * in the callback function. * @param callback which specifies a condition for all genes. * @return `true` if al genes accept condition and `false` otherwise. */ BaseIndividual.prototype.every = function (callback) { return this.genotype.every(callback); }; /** * Returns the first element which accepts the condition * in the callback. * @param callback which specifies the condition. * @return first gene which accept the callback condition * or `undefined` if none accepts it. */ BaseIndividual.prototype.find = function (callback) { return this.genotype.find(callback); }; /** * Returns the index of the first element which accepts * the condition in the callback. * @param callback which specifies the condition. * @return index of the first gene which accept the * callback condition or `undefined` if none accepts it. */ BaseIndividual.prototype.findIndex = function (callback) { return this.genotype.findIndex(callback); }; /** * Executes the callback for each gene * of the genotype. * @param callback to be executed. * @returns `undefined`. */ BaseIndividual.prototype.forEach = function (callback) { this.genotype.forEach(callback); return undefined; }; /** * Tests if genotype contains specified gene. * @param gene to be searched. * @param startIndex index to start the search, by default is `0`. * @return `true` if gene in genotype or `false` otherwise. */ BaseIndividual.prototype.includes = function (gene, startIndex) { if (startIndex === void 0) { startIndex = 0; } return this.genotype.includes(gene, startIndex); }; /** * Returns the index of the first specified gene. * @param gene to be searched. * @param fromIndex index to start the search, by default is `0`. * @return index of the first specified gene or `-1` if not found. */ BaseIndividual.prototype.indexOf = function (gene, fromIndex) { if (fromIndex === void 0) { fromIndex = 0; } return this.genotype.indexOf(gene, fromIndex); }; /** * Returns an iterator which contains the indexes * of the genes. * @return iterable iterator of numbers. */ BaseIndividual.prototype.keys = function () { return this.genotype.keys(); }; /** * Last index of the specified gene in the genotype. * The genotype is visited in reverse order. * @param gene that we are searching. * @param fromIndex from which index to start, * by default is the last index. */ BaseIndividual.prototype.lastIndexOf = function (gene, fromIndex) { if (fromIndex === void 0) { fromIndex = this.length() - 1; } return this.genotype.lastIndexOf(gene, fromIndex); }; /** * Test if some gene validates the condition * specified by the callback. * @param callback that specifies condition. * @return `true` if some gene validates condition or * `false` otherwise. */ BaseIndividual.prototype.some = function (callback) { return this.genotype.some(callback); }; /** * Converts the individual to string. * @return string that represents individual. */ BaseIndividual.prototype.toString = function () { var _this = this; var representation = ''; this.forEach(function (gene, index) { representation += _this.geneToString(gene) + (index !== _this.length() - 1 ? ' ' : ''); }); return representation; }; /** * Returns an iterator that contains * each gene in the genotype. * @return iterable iterator that contains each gene. */ BaseIndividual.prototype.values = function () { return this.genotype.values(); }; /** * Sets the genotype to the specified genotype. * @param genotype new genotype. */ BaseIndividual.prototype.setGenotype = function (genotype) { this.genotypeArray = genotype; }; /** * Check if index is in range, if not * throws an exception. * @param index to be checked. * @throws RangeError if index is not in range `[0-length]`. */ BaseIndividual.prototype.checkIndexRange = function (index) { if (!this.isInRange(index)) { throw new RangeError("Range Error: Index " + index + " is not in range [0 - " + this.length() + ")"); } }; /** * Check if index is in range `[0-length]`. * @param index to be checked. * @return `true` if index is in range and `false` otherwise. */ BaseIndividual.prototype.isInRange = function (index) { return index >= 0 && index < this.length(); }; return BaseIndividual; }()); exports.BaseIndividual = BaseIndividual; //# sourceMappingURL=BaseIndividual.js.map