@zfunction/genetics-js
Version:
Genetic and evolutionary algorithms framework for the web
46 lines • 2.33 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 BaseSelection_1 = require("./BaseSelection");
var FitnessProportionalSelection = /** @class */ (function (_super) {
__extends(FitnessProportionalSelection, _super);
function FitnessProportionalSelection() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.cumulativeProbability = [];
_this.getCumulativeProbability = function (item, index) {
return _this.cumulativeProbability[index];
};
return _this;
}
FitnessProportionalSelection.prototype.selectWith = function (population, params) {
this.checkParams(population, params);
this.cumulativeProbability = [];
for (var i = 0; i < population.getPopulationSize(); i++) {
var fitness = population.getPopulationItem(i).fitness;
var normalizedFitness = fitness / population.populationStatistics.fitnessSum;
var cumulativeProb = i === 0 ? normalizedFitness : this.cumulativeProbability[i - 1] + normalizedFitness;
this.cumulativeProbability.push(cumulativeProb);
}
return params.subSelection.select(population, { engine: params.engine, selectionCount: params.selectionCount }, this.getCumulativeProbability);
};
return FitnessProportionalSelection;
}(BaseSelection_1.BaseSelection));
exports.FitnessProportionalSelection = FitnessProportionalSelection;
//# sourceMappingURL=FitnessProportionalSelection.js.map