@zfunction/genetics-js
Version:
Genetic and evolutionary algorithms framework for the web
77 lines • 2.73 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 binary_1 = require("../../individual/binary/");
var base_1 = require("../base/");
/**
* ## Binary reader
* This class provides an implementation
* of a reader which is able to read [[BinaryIndividual]]
* the format of this individuals is the following:
* ```
* 001001001 // OK
* tfftttfft // OK
* TFTFTTFFT // OK
* tfTTF0101 // OK (Mixed case)
* 0 0 f 1T // OK (mixed case with spaces)
* ```
*/
var BinaryReader = /** @class */ (function (_super) {
__extends(BinaryReader, _super);
function BinaryReader() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.tokenDefinition = [
{
token: /[0f]/i,
value: function () { return false; },
},
{
token: /[1t]/i,
value: function () { return true; },
},
];
return _this;
}
/**
* Reads a definition and converts
* into an individual.
* @param definition of the individual.
* @return the read individual.
* @throws Error if there is an unexpected token.
*/
BinaryReader.prototype.read = function (definition) {
var genotype = this.getGenotype(this.tokenize(definition));
return new binary_1.BinaryIndividual(genotype);
};
/**
* Tokenize a string into several
* tokens. It separates the tokens
* by whitespaces, tabulars and return
* carriage.
* @param definition of the individual.
* @return the tokenized string.
*/
BinaryReader.prototype.tokenize = function (definition) {
return definition.split(/\s*/).filter(Boolean);
};
return BinaryReader;
}(base_1.BaseIndividualReader));
exports.BinaryReader = BinaryReader;
//# sourceMappingURL=BinaryReader.js.map