From 45feb04543f66fe4044ef21731e1c1dd1a62fc1e Mon Sep 17 00:00:00 2001 From: Smoren Date: Mon, 4 Nov 2024 17:01:35 +0300 Subject: [PATCH] New release. --- package.json | 2 +- src/strategies.ts | 8 ++++++-- src/types.ts | 1 + tests/parabola/multiprocess.test.ts | 1 + tests/parabola/single.test.ts | 1 + 5 files changed, 10 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 0927664..de251d9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "genetic-search", - "version": "0.2.3", + "version": "0.2.4", "description": "Multiprocessing genetic algorithm implementation library", "license": "MIT", "repository": { diff --git a/src/strategies.ts b/src/strategies.ts index c74e3a7..35f1155 100644 --- a/src/strategies.ts +++ b/src/strategies.ts @@ -48,7 +48,9 @@ export abstract class BaseRunnerStrategy< protected async execTask(inputs: TTaskConfig[]): Promise { const result: GenerationGradeMatrix = []; for (const input of inputs) { - result.push(await this.config.task(input)); + const taskResult = await this.config.task(input); + this.config.onTaskResult?.(taskResult); + result.push(taskResult); } return result; } @@ -67,7 +69,9 @@ export abstract class BaseMultiprocessingRunnerStrategy< > extends BaseRunnerStrategy { protected async execTask(inputs: TTaskConfig[]): Promise { const pool = new Pool(this.config.poolSize); - const result: GenerationGradeMatrix = await pool.map(inputs, this.config.task); + const result: GenerationGradeMatrix = await pool.map(inputs, this.config.task, { + onResult: (result: any) => this.config.onTaskResult?.(result as GenomeGradeRow), + }); pool.close(); return result; diff --git a/src/types.ts b/src/types.ts index a01c83b..639b29e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -35,6 +35,7 @@ export type BaseMutationStrategyConfig = { export type RunnerStrategyConfig = { task: GradeGenerationTask; + onTaskResult?: (result: GenomeGradeRow) => void; } export type MultiprocessingRunnerStrategyConfig = RunnerStrategyConfig & { diff --git a/tests/parabola/multiprocess.test.ts b/tests/parabola/multiprocess.test.ts index 8d9efcf..639f55e 100644 --- a/tests/parabola/multiprocess.test.ts +++ b/tests/parabola/multiprocess.test.ts @@ -32,6 +32,7 @@ describe('Parabola Multiprocessing', () => { runner: new ParabolaMultiprocessingRunnerStrategy({ poolSize: 4, task: (x: ParabolaTaskConfig) => Promise.resolve([-((x[1]+12)**2) - 3]), + onTaskResult: () => void 0, }), scoring: new ParabolaTransparentScoringStrategy(), mutation: new ParabolaMutationStrategy(), diff --git a/tests/parabola/single.test.ts b/tests/parabola/single.test.ts index ab17b28..4cb8c84 100644 --- a/tests/parabola/single.test.ts +++ b/tests/parabola/single.test.ts @@ -35,6 +35,7 @@ describe.each([ populate: new ParabolaPopulateStrategy(), runner: new ParabolaSingleRunnerStrategy({ task: async (data: ParabolaTaskConfig) => [-((data[1]+a)**2) + b], + onTaskResult: (result) => void 0, }), scoring: new ParabolaTransparentScoringStrategy(), mutation: new ParabolaMutationStrategy(),