UNPKG

@zfunction/genetics-js

Version:

Genetic and evolutionary algorithms framework for the web

87 lines 3.05 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 BinaryReader_1 = require("../../reader/binary/BinaryReader"); var base_1 = require("../base/"); var reader = new BinaryReader_1.BinaryReader(); /** * ## Binary individual * A binary individual has only boolean values * in its genotype. It could be represented by * a string of `0` and `1` or `t` and `f`. * ``` * let ind = new BinaryIndividual('0010100'); * console.log(ind.genotype) // [false, false, true, false, true, false, false] * console.log(ind) // 0 0 1 0 1 0 0 * ``` */ var BinaryIndividual = /** @class */ (function (_super) { __extends(BinaryIndividual, _super); /** * Constructor of the class, expects an array * of boolean values or an string separated by * any number of spaces, with the following tokens: * * * (`0` | `f` | `F`) -> `false` * * (`1` | `t` | `T`) -> `true` * * Example: * ``` * 01 0 10 01 // OK * tf ff tf // OK * T FTFFTF // OK * 0 1Ff tT // OK * ``` * @param representation of the individual. */ function BinaryIndividual(representation) { var _this = this; if (typeof representation === 'string') { _this = _super.call(this, []) || this; _this.copy(reader.read(representation)); } else { _this = _super.call(this, representation) || this; } return _this; } /** * Creates a deep copy of the * other individual in the current. * @param other individual to copy. */ BinaryIndividual.prototype.deepCopy = function (other) { this.setGenotype(Array.from(other.genotype)); }; BinaryIndividual.prototype.flip = function (index) { this.set(index, !this.get(index)); }; /** * Converts a gene to string, useful for method * `toString`. * @param gene that we are converting. */ BinaryIndividual.prototype.geneToString = function (gene) { return gene ? '1' : '0'; }; return BinaryIndividual; }(base_1.MutableIndividual)); exports.BinaryIndividual = BinaryIndividual; //# sourceMappingURL=BinaryIndividual.js.map