@zfunction/genetics-js
Version:
Genetic and evolutionary algorithms framework for the web
66 lines • 3.54 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 utils_1 = require("../../../generator/utils");
var base_1 = require("../../../individual/numeric/base");
var floating_1 = require("../../../individual/numeric/floating");
var base_2 = require("../../base");
var BaseFloatingCrossover = /** @class */ (function (_super) {
__extends(BaseFloatingCrossover, _super);
function BaseFloatingCrossover() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.recombinationPoint = 0;
return _this;
}
BaseFloatingCrossover.prototype.cross = function (firstParent, secondParent, alpha, engine) {
if (alpha === void 0) { alpha = 0.5; }
if (engine === void 0) { engine = utils_1.Generator.DEFAULT_ENGINE; }
return this.crossWith(firstParent, secondParent, { engine: engine, alpha: alpha, individualConstructor: floating_1.FloatingIndividual });
};
BaseFloatingCrossover.prototype.crossWith = function (firstParent, secondParent, params) {
this.generateRecombinationPoint(firstParent.length(), params);
this.checkParams(params);
return _super.prototype.crossWith.call(this, firstParent, secondParent, params);
};
BaseFloatingCrossover.prototype.getGenotypeValues = function (firstParent, secondParent, params, index) {
var recombinationCondition = this.getRecombinationCondition(index);
var firstValue = firstParent.get(index);
var secondValue = secondParent.get(index);
return {
first: recombinationCondition ? this.getRecombinationValue(firstValue, secondValue, params) : firstValue,
second: recombinationCondition ? this.getRecombinationValue(secondValue, firstValue, params) : secondValue,
};
};
BaseFloatingCrossover.prototype.getRecombinationValue = function (firstParentValue, secondParentValue, params) {
var alpha = params.alpha;
return alpha * firstParentValue + (1.0 - alpha) * secondParentValue;
};
BaseFloatingCrossover.prototype.checkParams = function (params) {
if (!utils_1.Generator.probabilityIsValid(params.alpha)) {
throw new Error("BaseFloatingCrossover: alpha of " + params.alpha + " is not in range [0.0, 1.0]");
}
};
BaseFloatingCrossover.prototype.generateRecombinationPoint = function (parentsLength, params) {
this.recombinationPoint = utils_1.Generator.generateInteger(new base_1.NumericRange(0, parentsLength - 1), params.engine);
};
return BaseFloatingCrossover;
}(base_2.BaseCrossover));
exports.BaseFloatingCrossover = BaseFloatingCrossover;
//# sourceMappingURL=BaseFloatingCrossover.js.map