@zfunction/genetics-js
Version:
Genetic and evolutionary algorithms framework for the web
34 lines • 1.56 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var BaseCrossover = /** @class */ (function () {
function BaseCrossover() {
}
BaseCrossover.prototype.crossWith = function (firstParent, secondParent, params) {
this.checkParents(firstParent, secondParent);
var parentsLength = firstParent.length();
var genotypes = [[], []];
for (var i = 0; i < parentsLength; i++) {
var result = this.getGenotypeValues(firstParent, secondParent, params, i);
genotypes[0].push(result.first);
genotypes[1].push(result.second);
}
// @ts-ignore
return [new params.individualConstructor(genotypes[0], firstParent.range), new params.individualConstructor(genotypes[1], secondParent.range)];
};
BaseCrossover.prototype.parentsAreValid = function (firstParent, secondParent) {
return firstParent.length() === secondParent.length() && firstParent.length() > 1;
};
BaseCrossover.prototype.checkParents = function (firstParent, secondParent) {
if (!this.parentsAreValid(firstParent, secondParent)) {
throw new Error('NPointsCrossover: both parents must have the same length.');
}
};
return BaseCrossover;
}());
exports.BaseCrossover = BaseCrossover;
//# sourceMappingURL=BaseCrossover.js.map