Skip to content

Commit

Permalink
New release.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Nov 4, 2024
1 parent d99a8ae commit 45feb04
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "genetic-search",
"version": "0.2.3",
"version": "0.2.4",
"description": "Multiprocessing genetic algorithm implementation library",
"license": "MIT",
"repository": {
Expand Down
8 changes: 6 additions & 2 deletions src/strategies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export abstract class BaseRunnerStrategy<
protected async execTask(inputs: TTaskConfig[]): Promise<GenerationGradeMatrix> {
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;
}
Expand All @@ -67,7 +69,9 @@ export abstract class BaseMultiprocessingRunnerStrategy<
> extends BaseRunnerStrategy<TGenome, TConfig, TTaskConfig> {
protected async execTask(inputs: TTaskConfig[]): Promise<GenerationGradeMatrix> {
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;
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type BaseMutationStrategyConfig = {

export type RunnerStrategyConfig<TTaskConfig> = {
task: GradeGenerationTask<TTaskConfig>;
onTaskResult?: (result: GenomeGradeRow) => void;
}

export type MultiprocessingRunnerStrategyConfig<TTaskConfig> = RunnerStrategyConfig<TTaskConfig> & {
Expand Down
1 change: 1 addition & 0 deletions tests/parabola/multiprocess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions tests/parabola/single.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 45feb04

Please sign in to comment.