Skip to content

Commit

Permalink
Refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Nov 1, 2024
1 parent f9ba008 commit 6647a4a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
37 changes: 22 additions & 15 deletions src/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ export class GeneticSearch<TGenome extends BaseGenome> implements GeneticSearchI
this._population = population;
}

public get partitions(): [number, number, number] {
const countToSurvive = Math.round(this.config.populationSize * this.config.survivalRate);
const countToDie = this.config.populationSize - countToSurvive;

const countToCross = Math.round(countToDie * this.config.crossoverRate);
const countToClone = countToDie - countToCross;

return [countToSurvive, countToCross, countToClone];
}

public async fit(config: GeneticFitConfig): Promise<void> {
for (let i=0; i<config.generationsCount; i++) {
const result = await this.step();
Expand All @@ -56,16 +66,6 @@ export class GeneticSearch<TGenome extends BaseGenome> implements GeneticSearchI
return sortedScoreColumn;
}

public getSizes(): [number, number, number] {
const countToSurvive = Math.round(this.config.populationSize * this.config.survivalRate);
const countToDie = this.config.populationSize - countToSurvive;

const countToCross = Math.round(countToDie * this.config.crossoverRate);
const countToClone = countToDie - countToCross;

return [countToSurvive, countToCross, countToClone];
}

protected sortPopulation(scores: GenerationScoreColumn): [Population<TGenome>, GenerationScoreColumn] {
const zipped = multi.zipEqual(this._population, scores);
const sorted = single.sort(zipped, (lhs, rhs) => rhs[1] - lhs[1]);
Expand Down Expand Up @@ -102,7 +102,7 @@ export class GeneticSearch<TGenome extends BaseGenome> implements GeneticSearchI
}

protected refreshPopulation(sortedPopulation: Population<TGenome>): void {
const [countToSurvive, countToCross, countToClone] = this.getSizes();
const [countToSurvive, countToCross, countToClone] = this.partitions;

const survivedPopulation = sortedPopulation.slice(0, countToSurvive);
const crossedPopulation = this.crossover(survivedPopulation, countToCross);
Expand Down Expand Up @@ -143,6 +143,17 @@ export class ComposedGeneticSearch<TGenome extends BaseGenome> implements Geneti
}
}

public get partitions(): [number, number, number] {
const result: [number, number, number] = [0, 0, 0];
for (const eliminators of this.eliminators) {
const [countToSurvive, countToCross, countToClone] = eliminators.partitions;
result[0] += countToSurvive;
result[1] += countToCross;
result[2] += countToClone;
}
return result;
}

public async fit(config: GeneticFitConfig): Promise<void> {
for (let i=0; i<config.generationsCount; i++) {
const result = await this.step();
Expand All @@ -161,10 +172,6 @@ export class ComposedGeneticSearch<TGenome extends BaseGenome> implements Geneti
return await this.final.step();
}

public getSizes(): [number, number, number] {
return this.final.getSizes();
}

protected get bestGenomes(): Population<TGenome> {
return this.eliminators.map((eliminators) => eliminators.bestGenome);
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ export interface ScoringStrategyInterface {

export interface GeneticSearchInterface<TGenome extends BaseGenome> {
readonly bestGenome: TGenome;
readonly partitions: [number, number, number];
population: Population<TGenome>
fit(config: GeneticFitConfig): Promise<void>;
step(): Promise<GenerationScoreColumn>;
getSizes(): [number, number, number];
}
6 changes: 6 additions & 0 deletions tests/search/parabola.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ describe.each([
}

const search = new GeneticSearch(config, strategies);

expect(search.partitions).toEqual([50, 25, 25]);

await search.fit({
generationsCount: 100,
afterStep: () => void 0,
Expand Down Expand Up @@ -88,6 +91,9 @@ describe.each([
}

const search = new ComposedGeneticSearch(config, strategies);

expect(search.partitions).toEqual([50, 30, 20]);

await search.fit({
generationsCount: 100,
afterStep: () => void 0,
Expand Down

0 comments on commit 6647a4a

Please sign in to comment.