UNPKG

@zfunction/genetics-js

Version:

Genetic and evolutionary algorithms framework for the web

165 lines 6.14 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. */ 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 (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var base_1 = require("./../../base/"); var NumericRange_1 = require("./NumericRange"); /** * ## Numeric individual * Numeric individual represents an individual * with a numeric genotype, situated in a range. */ var NumericIndividual = /** @class */ (function (_super) { __extends(NumericIndividual, _super); /** * Constructor of the class. * Creates an individual given a numeric * genotype. * @param genotype of the individual. * @param range of the individual, if not provided * or undefined it is set to default range. */ function NumericIndividual(genotype, range) { if (range === void 0) { range = NumericRange_1.NumericRange.DEFAULT; } var _this = _super.call(this, genotype) || this; /** * range of the individual. */ _this._range = NumericRange_1.NumericRange.DEFAULT; _this.setRange(range); return _this; } Object.defineProperty(NumericIndividual.prototype, "range", { /** * Setter of the range. * @return the range of the individual. */ get: function () { return this._range; }, enumerable: true, configurable: true }); /** * Copy other individual into the current. * Creates a shallow copy, with the references * only. * @param other individual to copy */ NumericIndividual.prototype.copy = function (other) { _super.prototype.copy.call(this, other); this.setRange(other.range); }; /** * Creates a deep copy of the other individual * in the current. * @param other individual to copy. */ NumericIndividual.prototype.deepCopy = function (other) { this.setGenotype(Array.from(other.genotype)); this.setRange(new NumericRange_1.NumericRange(other.range.lowest, other.range.highest)); }; /** * Sets the gene at specified index to * the specified value. * @param geneIndex index of the gene to be set. * @param gene new value of the gene. * @throws RangeError if gene is not in range. */ NumericIndividual.prototype.set = function (geneIndex, gene) { this.checkGeneRange(gene); _super.prototype.set.call(this, geneIndex, gene); }; /** * Fill the genotype with the specified gene. * @param gene we want to fill the genotype with. * @param start position. By default is `0`. * @param end position. By default is the length of * the individual. * @throws RangeError if gene is not in range. */ NumericIndividual.prototype.fill = function (gene, start, end) { if (start === void 0) { start = 0; } if (end === void 0) { end = this.length(); } this.checkGeneRange(gene); _super.prototype.fill.call(this, gene, start, end); }; /** * Creates a new genotype with the result of * the execution of the specified callback for each * element. * @param callback called for each gene. * @throws RangeError if callback result is not in range. */ NumericIndividual.prototype.map = function (callback) { var _this = this; var inRangeCallback = function (gene, geneIndex, genotype) { var result = callback(gene, geneIndex, genotype); _this.checkGeneRange(result); return result; }; _super.prototype.map.call(this, inRangeCallback); }; /** * Sets the individual range. * * @param range that we want to set, * if it is `undefined` it is set with * the default range. * @throws Error if range is invalid. * @throws RangeError if any gene of the genotype * is not in range. */ NumericIndividual.prototype.setRange = function (range) { this._range = range; this.checkGenotype(this.genotype); }; /** * Checks if gene is in range. * @param gene that we want to check. * @throws RangeError if gene is not in range. */ NumericIndividual.prototype.checkGeneRange = function (gene) { if (!NumericRange_1.NumericRange.isValueInRange(gene, this.range)) { throw new RangeError("Range error: gene value " + gene + " is not in range"); } }; /** * Checks if all genes in genotype * are in range. If it is not in * range it throws a RangeError. * @param genotype to be checked. */ NumericIndividual.prototype.checkGenotype = function (genotype) { if (!NumericRange_1.NumericRange.isArrayInRange(genotype, this.range)) { // throw new RangeError(`RangeError: genotype is not in range.`); } }; /** * Converts a gene to string, useful for method * `toString`. * @param gene that we are converting. */ NumericIndividual.prototype.geneToString = function (gene) { return gene.toString(); }; return NumericIndividual; }(base_1.MutableIndividual)); exports.NumericIndividual = NumericIndividual; //# sourceMappingURL=NumericIndividual.js.map