@zfunction/genetics-js
Version:
Genetic and evolutionary algorithms framework for the web
114 lines • 4.65 kB
JavaScript
;
/*
* @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 integer_1 = require("./../../../reader/numeric/integer");
var utils_1 = require("./utils");
/**
* ## Integer individual
* Integer individual is a numeric individual
* where all the genes in the genotype are integers.
*/
var IntegerIndividual = /** @class */ (function (_super) {
__extends(IntegerIndividual, _super);
/**
* Constructor of the class.
* Takes the representation as
* an array of numbers or a string
* and an optional range.
* @param representation of the individual.
* @param range of the individual, if not provided
* is the default range.
*/
function IntegerIndividual(representation, range) {
if (range === void 0) { range = base_1.NumericRange.DEFAULT; }
var _this = this;
if (typeof representation === 'string') {
_this = _super.call(this, []) || this;
var reader = new integer_1.IntegerReader();
_this.copy(reader.read(representation));
}
else {
_this = _super.call(this, utils_1.IntegerNormalizer.normalizeGenotype(representation), utils_1.IntegerNormalizer.normalizeRange(range)) || this;
}
return _this;
}
/**
* Copy other individual into the current.
* Creates a shallow copy, with the references
* only.
* @param other individual to copy
*/
IntegerIndividual.prototype.copy = function (other) {
this.setGenotype(other.genotype);
this.setRange(other.range);
};
/**
* Creates a deep copy of the other individual
* in the current.
* @param other individual to copy.
*/
IntegerIndividual.prototype.deepCopy = function (other) {
this.setGenotype(Array.from(other.genotype));
this.setRange(new base_1.NumericRange(other.range.lowest, other.range.highest));
};
/**
* Fill the genotype with the specified gene.
* @param gene we want to fill the genotype with.
* if it is not an integer it will be normalized.
* @param start position.
* @param end position.
* @throws RangeError if gene is not in range.
*/
IntegerIndividual.prototype.fill = function (gene, start, end) {
if (start === void 0) { start = 0; }
if (end === void 0) { end = this.length(); }
_super.prototype.fill.call(this, utils_1.IntegerNormalizer.normalize(gene), start, end);
};
/**
* Creates a new genotype with the result of
* the execution of the specified callback for each
* element.
* Result of the callback will be normalized to
* an integer if it is not.
* @param callback called for each gene.
* @throws RangeError if callback result is not in range.
*/
IntegerIndividual.prototype.map = function (callback) {
var normalizedCallback = function (gene, geneIndex, genotype) {
return utils_1.IntegerNormalizer.normalize(callback(gene, geneIndex, genotype));
};
return _super.prototype.map.call(this, normalizedCallback);
};
/**
* Sets the gene at specified index to
* the specified value.
* @param geneIndex index of the gene to be set.
* if it is not an integer it will be normalized.
* @param gene new value of the gene.
* @throws RangeError if gene is not in range.
*/
IntegerIndividual.prototype.set = function (geneIndex, gene) {
_super.prototype.set.call(this, geneIndex, utils_1.IntegerNormalizer.normalize(gene));
};
return IntegerIndividual;
}(base_1.NumericIndividual));
exports.IntegerIndividual = IntegerIndividual;
//# sourceMappingURL=IntegerIndividual.js.map