@zfunction/genetics-js
Version:
Genetic and evolutionary algorithms framework for the web
92 lines • 3.72 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 utils_1 = require("../utils");
var binary_1 = require("./../../individual/binary");
/**
* ## BinaryGenerator
* Generator of binary individuals.
*/
var BinaryGenerator = /** @class */ (function (_super) {
__extends(BinaryGenerator, _super);
function BinaryGenerator() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* Throws an exception if chance is not in
* range [0.0 - 1.0].
* @param chance that is going to be tested.
* @throws RangeError if chance is not in range.
*/
BinaryGenerator.checkChance = function (chance) {
if (!utils_1.Generator.probabilityIsValid(chance)) {
throw new RangeError("BinaryGenerator: chance " + chance + " is not in range [0.0 - 1.0]");
}
};
/**
* Generates an individual with the specified
* params.
* @param length of the individual.
* @param chance of a generated gene to be `true`.
* By default is `0.5`.
* @param engine (of `random-js`) that is going to be used.
* @return The [[BinaryIndividual]] with the generated genotype.
* @throws RangeError if `length` is not greater than `0`.
* @throws RangeError if `chance` is not in range [0.0 - 1.0].
*/
BinaryGenerator.prototype.generate = function (length, chance, engine) {
if (chance === void 0) { chance = 0.5; }
if (engine === void 0) { engine = utils_1.Generator.DEFAULT_ENGINE; }
var params = { chance: chance, engine: engine, length: length };
return this.generateWith(params);
};
/**
* Generates a gene with the specified params.
* @param params of the generator.
* @return the generated gene.
*/
BinaryGenerator.prototype.generateGene = function (params) {
return utils_1.Generator.generateBoolean(params.chance, params.engine);
};
/**
* Generates an individual with the specified
* params.
* @param params of the generator.
* @return The [[BinaryIndividual]] with the generated genotype.
* @throws RangeError if `chance` is not in range [0.0 - 1.0].
* @throws RangeError if `length` is not greater than `0`.
*/
BinaryGenerator.prototype.generateWith = function (params) {
BinaryGenerator.checkChance(params.chance);
return _super.prototype.generateWith.call(this, params);
};
/**
* Construct a [[BinaryIndividual]] given
* a genotype.
* @param genotype
*/
BinaryGenerator.prototype.construct = function (genotype) {
return new binary_1.BinaryIndividual(genotype);
};
return BinaryGenerator;
}(base_1.BaseGenerator));
exports.BinaryGenerator = BinaryGenerator;
//# sourceMappingURL=BinaryGenerator.js.map