Skip to content

Commit

Permalink
New release.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Nov 1, 2024
1 parent d6018f0 commit 5fcd309
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 25 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Let's get a max value of the parabola: `y = -(x-12)^2 - 3`.
```typescript
import {
GeneticSearchConfig,
StrategyConfig,
GeneticSearchStrategyConfig,
GeneticSearch,
} from "genetic-search";

Expand All @@ -32,7 +32,7 @@ const config: GeneticSearchConfig = {
crossoverRate: 0.5,
};

const strategies: StrategyConfig<ParabolaArgumentGenome> = {
const strategies: GeneticSearchStrategyConfig<ParabolaArgumentGenome> = {
populate: new ParabolaPopulateStrategy(),
runner: new ParabolaCachedMultiprocessingRunnerStrategy({
poolSize: 4,
Expand Down
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.1.3",
"version": "0.2.0",
"description": "Genetic algorithm implementation library",
"license": "MIT",
"repository": {
Expand Down
14 changes: 7 additions & 7 deletions src/classes.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { multi, single } from "itertools-ts";
import {
GeneticSearchConfig,
StrategyConfig,
GeneticSearchStrategyConfig,
GeneticSearchInterface,
GenerationScoreColumn,
Population,
BaseGenome,
NextIdGetter,
GeneticFitConfig,
GeneticSearchFitConfig,
ComposedGeneticSearchConfig,
} from "./types";
import { createNextIdGetter, getRandomArrayItem } from "./utils";

export class GeneticSearch<TGenome extends BaseGenome> implements GeneticSearchInterface<TGenome> {
protected readonly config: GeneticSearchConfig;
protected readonly strategy: StrategyConfig<TGenome>;
protected readonly strategy: GeneticSearchStrategyConfig<TGenome>;
protected readonly nextId: NextIdGetter;
protected _population: Population<TGenome>;

constructor(config: GeneticSearchConfig, strategy: StrategyConfig<TGenome>, nextIdGetter?: NextIdGetter) {
constructor(config: GeneticSearchConfig, strategy: GeneticSearchStrategyConfig<TGenome>, nextIdGetter?: NextIdGetter) {
this.config = config;
this.strategy = strategy;
this.nextId = nextIdGetter ?? createNextIdGetter();
Expand Down Expand Up @@ -47,7 +47,7 @@ export class GeneticSearch<TGenome extends BaseGenome> implements GeneticSearchI
return [countToSurvive, countToCross, countToClone];
}

public async fit(config: GeneticFitConfig): Promise<void> {
public async fit(config: GeneticSearchFitConfig): Promise<void> {
for (let i=0; i<config.generationsCount; i++) {
const result = await this.step();
if (config.afterStep) {
Expand Down Expand Up @@ -116,7 +116,7 @@ export class ComposedGeneticSearch<TGenome extends BaseGenome> implements Geneti
private readonly eliminators: GeneticSearchInterface<TGenome>[];
private readonly final: GeneticSearchInterface<TGenome>;

constructor(config: ComposedGeneticSearchConfig, strategy: StrategyConfig<TGenome>) {
constructor(config: ComposedGeneticSearchConfig, strategy: GeneticSearchStrategyConfig<TGenome>) {
this.eliminators = [...single.repeat(
() => new GeneticSearch(config.eliminators, strategy),
config.final.populationSize,
Expand Down Expand Up @@ -154,7 +154,7 @@ export class ComposedGeneticSearch<TGenome extends BaseGenome> implements Geneti
return result;
}

public async fit(config: GeneticFitConfig): Promise<void> {
public async fit(config: GeneticSearchFitConfig): Promise<void> {
for (let i=0; i<config.generationsCount; i++) {
const result = await this.step();
if (config.afterStep) {
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import type {
BaseMutationStrategyConfig,
RunnerStrategyConfig,
MultiprocessingRunnerStrategyConfig,
StrategyConfig,
GeneticFitConfig,
GeneticSearchStrategyConfig,
GeneticSearchFitConfig,
GeneticSearchReferenceConfig,
PopulateStrategyInterface,
MutationStrategyInterface,
Expand Down Expand Up @@ -54,8 +54,8 @@ export type {
BaseMutationStrategyConfig,
RunnerStrategyConfig,
MultiprocessingRunnerStrategyConfig,
StrategyConfig,
GeneticFitConfig,
GeneticSearchStrategyConfig,
GeneticSearchFitConfig,
GeneticSearchReferenceConfig,
PopulateStrategyInterface,
MutationStrategyInterface,
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type GeneticSearchConfig = {
crossoverRate: number;
};

export type GeneticFitConfig = {
export type GeneticSearchFitConfig = {
generationsCount: number;
afterStep?: GenerationCallback;
}
Expand All @@ -41,7 +41,7 @@ export type MultiprocessingRunnerStrategyConfig<TTaskConfig> = RunnerStrategyCon
poolSize: number;
}

export type StrategyConfig<TGenome extends BaseGenome> = {
export type GeneticSearchStrategyConfig<TGenome extends BaseGenome> = {
populate: PopulateStrategyInterface<TGenome>;
runner: RunnerStrategyInterface<TGenome>;
scoring: ScoringStrategyInterface;
Expand Down Expand Up @@ -78,6 +78,6 @@ export interface GeneticSearchInterface<TGenome extends BaseGenome> {
readonly bestGenome: TGenome;
readonly partitions: [number, number, number];
population: Population<TGenome>
fit(config: GeneticFitConfig): Promise<void>;
fit(config: GeneticSearchFitConfig): Promise<void>;
step(): Promise<GenerationScoreColumn>;
}
8 changes: 4 additions & 4 deletions tests/parabola/multiprocess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ComposedGeneticSearchConfig,
GeneticSearch,
GeneticSearchConfig,
StrategyConfig,
GeneticSearchStrategyConfig,
} from "../../src";
import {
ParabolaArgumentGenome, ParabolaCachedMultiprocessingRunnerStrategy,
Expand All @@ -27,7 +27,7 @@ describe('Parabola Multiprocessing', () => {
crossoverRate: 0.5,
};

const strategies: StrategyConfig<ParabolaArgumentGenome> = {
const strategies: GeneticSearchStrategyConfig<ParabolaArgumentGenome> = {
populate: new ParabolaPopulateStrategy(),
runner: new ParabolaMultiprocessingRunnerStrategy({
poolSize: 4,
Expand Down Expand Up @@ -67,7 +67,7 @@ describe('Parabola Multiprocessing', () => {
crossoverRate: 0.5,
};

const strategies: StrategyConfig<ParabolaArgumentGenome> = {
const strategies: GeneticSearchStrategyConfig<ParabolaArgumentGenome> = {
populate: new ParabolaPopulateStrategy(),
runner: new ParabolaCachedMultiprocessingRunnerStrategy({
poolSize: 4,
Expand Down Expand Up @@ -114,7 +114,7 @@ describe('Parabola Multiprocessing', () => {
}
};

const strategies: StrategyConfig<ParabolaArgumentGenome> = {
const strategies: GeneticSearchStrategyConfig<ParabolaArgumentGenome> = {
populate: new ParabolaPopulateStrategy(),
runner: new ParabolaMultiprocessingRunnerStrategy({
poolSize: 4,
Expand Down
8 changes: 4 additions & 4 deletions tests/parabola/single.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ComposedGeneticSearchConfig,
GeneticSearch,
GeneticSearchConfig,
StrategyConfig,
GeneticSearchStrategyConfig,
} from "../../src";
import {
ParabolaArgumentGenome,
Expand All @@ -30,7 +30,7 @@ describe.each([
crossoverRate: 0.5,
};

const strategies: StrategyConfig<ParabolaArgumentGenome> = {
const strategies: GeneticSearchStrategyConfig<ParabolaArgumentGenome> = {
populate: new ParabolaPopulateStrategy(),
runner: new ParabolaSingleRunnerStrategy({
task: async (data: ParabolaTaskConfig) => [-((data[1]+a)**2) + b],
Expand Down Expand Up @@ -75,7 +75,7 @@ describe.each([
crossoverRate: 0.5,
};

const strategies: StrategyConfig<ParabolaArgumentGenome> = {
const strategies: GeneticSearchStrategyConfig<ParabolaArgumentGenome> = {
populate: new ParabolaPopulateStrategy(),
runner: new ParabolaSingleRunnerStrategy({
task: async (data: ParabolaTaskConfig) => [-((data[1]+a)**2) + b],
Expand Down Expand Up @@ -127,7 +127,7 @@ describe.each([
}
};

const strategies: StrategyConfig<ParabolaArgumentGenome> = {
const strategies: GeneticSearchStrategyConfig<ParabolaArgumentGenome> = {
populate: new ParabolaPopulateStrategy(),
runner: new ParabolaSingleRunnerStrategy({
task: async (data: ParabolaTaskConfig) => [-((data[1]+a)**2) + b],
Expand Down

0 comments on commit 5fcd309

Please sign in to comment.