UNPKG

@zfunction/genetics-js

Version:

Genetic and evolutionary algorithms framework for the web

51 lines 2.41 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 MutationBase_1 = require("./MutationBase"); var UniformMutation = /** @class */ (function (_super) { __extends(UniformMutation, _super); function UniformMutation() { return _super !== null && _super.apply(this, arguments) || this; } UniformMutation.checkMutationRate = function (mutationRate) { if (!utils_1.Generator.probabilityIsValid(mutationRate)) { throw new Error("Error: Mutation rate " + mutationRate + " is not in range [0.0 - 1.0]"); } }; UniformMutation.prototype.mutate = function (individual, mutationRate, engine) { if (mutationRate === void 0) { mutationRate = 0.5; } if (engine === void 0) { engine = utils_1.Generator.DEFAULT_ENGINE; } this.mutateWith(individual, { mutationRate: mutationRate, engine: engine }); }; UniformMutation.prototype.mutateWith = function (individual, params) { UniformMutation.checkMutationRate(params.mutationRate); _super.prototype.mutateWith.call(this, individual, params); }; UniformMutation.prototype.mutateGene = function (individual, index, params) { var threshold = utils_1.Generator.generateProbability(params.engine); if (threshold <= params.mutationRate) { this.mutateGeneUniformly(individual, index, params); } }; return UniformMutation; }(MutationBase_1.MutationBase)); exports.UniformMutation = UniformMutation; //# sourceMappingURL=UniformMutation.js.map