@syntest/core
Version:
The common core of the SynTest Framework
125 lines • 4.69 kB
JavaScript
;
/*
* Copyright 2020-2021 Delft University of Technology and SynTest contributors
*
* This file is part of SynTest Framework - SynTest Core.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.NSGAIIFactory = exports.NSGAII = void 0;
const EvolutionaryAlgorithm_1 = require("./EvolutionaryAlgorithm");
const SimpleObjectiveManager_1 = require("../../objective/managers/SimpleObjectiveManager");
const __1 = require("../../..");
/**
* Non-dominated Sorting Genetic Algorithm (NSGA-II).
*
* Based on:
* A fast and elitist multiobjective genetic algorithm: NSGA-II
* K. Deb; A. Pratap; S. Agarwal; T. Meyarivan
*
* @author Mitchell Olsthoorn
* @author Annibale Panichella
* @author Dimitri Stallenberg
*/
class NSGAII extends EvolutionaryAlgorithm_1.EvolutionaryAlgorithm {
/**
* Constructor.
*
* @param eventManager The event manager
* @param encodingSampler The encoding sampler
* @param runner The runner
* @param crossover The crossover operator to apply
*/
constructor(eventManager, objectiveManager, encodingSampler, crossover) {
super(eventManager, objectiveManager, encodingSampler, crossover);
}
/**
* @inheritDoc
* @protected
*/
_environmentalSelection(size) {
const fronts = (0, __1.fastNonDomSorting)(this._population, this._objectiveManager.getCurrentObjectives());
const nextPopulation = [];
let remain = size;
let index = 0;
let currentFront = fronts[index];
while (remain > 0 &&
remain >= currentFront.length &&
!currentFront.length) {
// Assign crowding distance to individuals
(0, __1.crowdingDistance)(currentFront, this._objectiveManager.getCurrentObjectives());
// Add the individuals of this front
for (const individual of currentFront) {
if (nextPopulation.length < size) {
nextPopulation.push(individual);
}
}
// Decrement remain
remain = remain - currentFront.length;
// Obtain the next front
index++;
if (remain > 0) {
currentFront = fronts[index];
}
}
// Remain is less than front(index).size, insert only the best one
if (remain > 0 && currentFront.length > 0) {
// front contains individuals to insert
(0, __1.crowdingDistance)(currentFront, this._objectiveManager.getCurrentObjectives());
currentFront.sort(function (a, b) {
// sort in descending order of crowding distance
return b.getCrowdingDistance() - a.getCrowdingDistance();
});
let counter = 0;
for (const individual of currentFront) {
if (counter > remain)
break;
nextPopulation.push(individual);
counter++;
}
}
this._population = nextPopulation;
}
}
exports.NSGAII = NSGAII;
/**
* Factory plugin for NSGAII
*
* @author Dimitri Stallenberg
*/
class NSGAIIFactory {
constructor() {
this.name = "NSGAII";
}
// This function is not implemented since it is an internal plugin
// eslint-disable-next-line @typescript-eslint/no-empty-function
register() { }
createSearchAlgorithm(options) {
if (!options.eventManager) {
throw new Error("NSGAII requires eventManager option.");
}
if (!options.encodingSampler) {
throw new Error("NSGAII requires encodingSampler option.");
}
if (!options.runner) {
throw new Error("NSGAII requires runner option.");
}
if (!options.crossover) {
throw new Error("NSGAII requires crossover option.");
}
return new NSGAII(options.eventManager, new SimpleObjectiveManager_1.SimpleObjectiveManager(options.runner), options.encodingSampler, options.crossover);
}
}
exports.NSGAIIFactory = NSGAIIFactory;
//# sourceMappingURL=NSGAII.js.map