UNPKG

@zfunction/genetics-js

Version:

Genetic and evolutionary algorithms framework for the web

53 lines 2.8 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 utils_1 = require("../../generator/utils"); var BaseCrossover_1 = require("./BaseCrossover"); var UniformCrossover = /** @class */ (function (_super) { __extends(UniformCrossover, _super); function UniformCrossover() { return _super !== null && _super.apply(this, arguments) || this; } UniformCrossover.prototype.cross = function (firstParent, secondParent, individualConstructor, selectionThreshold, engine) { if (selectionThreshold === void 0) { selectionThreshold = 0.5; } if (engine === void 0) { engine = utils_1.Generator.DEFAULT_ENGINE; } return this.crossWith(firstParent, secondParent, { individualConstructor: individualConstructor, selectionThreshold: selectionThreshold, engine: engine }); }; UniformCrossover.prototype.crossWith = function (firstParent, secondParent, params) { this.checkParams(params); return _super.prototype.crossWith.call(this, firstParent, secondParent, params); }; UniformCrossover.prototype.getGenotypeValues = function (firstParent, secondParent, params, index) { var value = utils_1.Generator.generateProbability(params.engine); var parentSelectionCondition = value <= params.selectionThreshold; return { first: parentSelectionCondition ? firstParent.get(index) : secondParent.get(index), second: parentSelectionCondition ? secondParent.get(index) : firstParent.get(index), }; }; UniformCrossover.prototype.checkParams = function (params) { if (!utils_1.Generator.probabilityIsValid(params.selectionThreshold)) { throw new Error("Uniform Crossover: selection threshold " + params.selectionThreshold + " should be in range [0.0, 1.0]"); } }; return UniformCrossover; }(BaseCrossover_1.BaseCrossover)); exports.UniformCrossover = UniformCrossover; //# sourceMappingURL=UniformCrossover.js.map