genetic-search-multiprocess
Version:
Multiprocessing genetic algorithm implementation library extension
37 lines • 1.93 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { Pool } from 'multiprocessor';
import { BasePhenomeStrategy, } from "genetic-search";
/**
* Base class for phenome strategies that uses multiprocessing to execute phenome calculation tasks.
*
* @template TGenome - The type of the genome.
* @template TConfig - The type of the configuration for the phenome strategy.
* @template TTaskConfig - The type of the configuration for each phenome calculation task.
*
* @category Strategies
*/
export class BaseMultiprocessingPhenomeStrategy extends BasePhenomeStrategy {
/**
* Execute the phenome calculation tasks.
*
* @param inputs The inputs to the phenome calculation tasks.
* @returns A matrix of phenome results, where each row corresponds to a single genome.
*/
execTasks(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const pool = new Pool(this.config.poolSize);
const result = yield pool.map(inputs, this.config.task, { onTaskSuccess: (result, input) => { var _a, _b; return (_b = (_a = this.config).onTaskResult) === null || _b === void 0 ? void 0 : _b.call(_a, result, input); } });
pool.close();
return result;
});
}
}
//# sourceMappingURL=strategies.js.map