UNPKG

tycho-solver

Version:

Evolutionary computation and optimization library

23 lines (22 loc) 868 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SelectionOperatorImpl = void 0; class SelectionOperatorImpl { // Tournament selection as the default logic select(population, fitnesses, numParents, tournamentSize = 2) { const selected = []; for (let i = 0; i < numParents; i++) { // Randomly pick tournamentSize individuals const indices = Array.from({ length: tournamentSize }, () => Math.floor(Math.random() * population.length)); let bestIdx = indices[0]; for (const idx of indices) { if (fitnesses[idx] > fitnesses[bestIdx]) { bestIdx = idx; } } selected.push(population[bestIdx]); } return selected; } } exports.SelectionOperatorImpl = SelectionOperatorImpl;