@zfunction/genetics-js
Version:
Genetic and evolutionary algorithms framework for the web
95 lines • 3.71 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 index_1 = require("./index");
/**
* ## Mutable Individual
* Provides a basic implementation of the mutable interface.
* Also extends BaseIndividual class.
*/
var MutableIndividual = /** @class */ (function (_super) {
__extends(MutableIndividual, _super);
function MutableIndividual() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* Copy other individual into the current.
* Creates a shallow copy, with the references
* only.
* @param other individual to copy
*/
MutableIndividual.prototype.copy = function (other) {
this.setGenotype(other.genotype);
};
/**
* Transfers a copy of a section of the genotype
* between different parts of it.
* @param target position where to start the copy.
* @param start position of the desired section. By default is `0`.
* @param end position of the desired section,
* by default is the length of the individual.
*/
MutableIndividual.prototype.copyWithin = function (target, start, end) {
if (start === void 0) { start = 0; }
if (end === void 0) { end = this.length(); }
this.setGenotype(this.genotype.copyWithin(target, start, end));
};
/**
* Fill the genotype with the specified gene.
* @param gene we want to fill the genotype with.
* @param start position of the fill, by default is `0`.
* @param end position of the fill, by default is
* the length of the genotype.
*/
MutableIndividual.prototype.fill = function (gene, start, end) {
if (start === void 0) { start = 0; }
if (end === void 0) { end = this.length(); }
this.setGenotype(this.genotype.fill(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.
*/
MutableIndividual.prototype.map = function (callback) {
this.setGenotype(this.genotype.map(callback));
};
/**
* Reverses the genotype.
*/
MutableIndividual.prototype.reverse = function () {
this.setGenotype(this.genotype.reverse());
};
/**
* 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 index is not in range [0-length)
*/
MutableIndividual.prototype.set = function (geneIndex, gene) {
this.checkIndexRange(geneIndex);
this.genotype[geneIndex] = gene;
};
return MutableIndividual;
}(index_1.BaseIndividual));
exports.MutableIndividual = MutableIndividual;
//# sourceMappingURL=MutableIndividual.js.map