Skip to content

Commit 70afbaf

Browse files
perf: not create instance
1 parent 4ed7382 commit 70afbaf

File tree

7 files changed

+52
-46
lines changed

7 files changed

+52
-46
lines changed

src/logic/ga/Algorithm/Ga.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import {GeneticAlgorithmBase} from '@technote-space/ga-framework';
1+
import {GeneticAlgorithmBase} from '@technote-space/ga-framework/dist/app/logic/Algorithm';
22
import {ITermination, IMigration} from '@technote-space/genetic-algorithms-js';
33
import {Genotype} from '../Genotype';
4+
import {Phenotype} from '../Phenotype';
45
import {Termination} from '../Termination';
56
import {Migration} from '../Migration';
67
import {UpdateResult, Context} from '../../types';
@@ -53,7 +54,7 @@ export abstract class Ga extends GeneticAlgorithmBase<UpdateResult> {
5354
population: this.chromosomes.map(chromosome => {
5455
const genotype = chromosome as Genotype;
5556
return {
56-
value: genotype.phenotype.value,
57+
value: Phenotype.getValue(genotype),
5758
fitness: genotype.fitness ?? 0,
5859
};
5960
}),
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
import {CrossoverBase, IChromosome} from '@technote-space/genetic-algorithms-js';
2+
import {Genotype} from '../Genotype';
23

34
export class Cultural extends CrossoverBase {
5+
private _pool: Array<IChromosome> = [];
6+
47
public constructor(probability: number) {
58
super(2, 2, probability);
9+
10+
[...Array(this.childrenNumber)].forEach(() => {
11+
this._pool.push(new Genotype());
12+
});
613
}
714

815
// eslint-disable-next-line @typescript-eslint/no-unused-vars
916
protected performCross(parents: Array<IChromosome>, _probability: number): Array<IChromosome> {
10-
return parents.flatMap(parent => parent.clone());
17+
let index = 0;
18+
parents.forEach(parent => {
19+
this._pool[index++].copyFrom(parent);
20+
});
21+
22+
return this._pool;
1123
}
1224
}

src/logic/ga/Crossovers/Mgg.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import {CrossoverBase, IChromosome} from '@technote-space/genetic-algorithms-js';
2+
import {Genotype} from '../Genotype';
23

34
export class Mgg extends CrossoverBase {
5+
private _pool: Array<IChromosome> = [];
6+
47
public constructor(probability: number, private readonly mixProbability: number, private readonly crossoverTime: number) {
58
super(2, crossoverTime * 2, probability);
69

@@ -9,15 +12,22 @@ export class Mgg extends CrossoverBase {
912
this.mixProbability = 1 - mixProbability;
1013
}
1114
this.mixProbability = Math.min(Math.max(this.mixProbability, 0), 0.5);
15+
16+
[...Array(this.childrenNumber)].forEach(() => {
17+
this._pool.push(new Genotype());
18+
});
1219
}
1320

1421
protected performCross(parents: Array<IChromosome>, probability: number): Array<IChromosome> {
1522
const parent1 = parents[0];
1623
const parent2 = parents[1];
1724

18-
return [...Array(this.crossoverTime)].flatMap(() => {
19-
const child1 = parent1.clone();
20-
const child2 = parent2.clone();
25+
let index = 0;
26+
[...Array(this.crossoverTime)].forEach(() => {
27+
const child1 = this._pool[index++];
28+
const child2 = this._pool[index++];
29+
child1.copyFrom(parent1);
30+
child2.copyFrom(parent2);
2131

2232
if (probability > 0 && Math.random() < probability) {
2333
const len = Math.min(parent1.length, parent2.length);
@@ -30,8 +40,8 @@ export class Mgg extends CrossoverBase {
3040
}
3141
}
3242
}
33-
34-
return [child1, child2];
3543
});
44+
45+
return this._pool;
3646
}
3747
}

src/logic/ga/Fitness.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {FitnessBase, IChromosome} from '@technote-space/genetic-algorithms-js';
22
import levenshtein from 'fast-levenshtein';
33
import {Genotype} from './Genotype';
4+
import {Phenotype} from './Phenotype';
45
import {TestData} from './TestData/TestData';
56

67
export class Fitness extends FitnessBase {
@@ -9,10 +10,9 @@ export class Fitness extends FitnessBase {
910
}
1011

1112
public evaluate(chromosome: IChromosome): void {
12-
const genotype = chromosome as Genotype;
13-
const phenotype = genotype.phenotype;
13+
const genotype = chromosome as Genotype;
1414

15-
const diff = levenshtein.get(this.testData.target, phenotype.value);
16-
phenotype.setFitness(1.0 / (diff + 1));
15+
const diff = levenshtein.get(this.testData.target, Phenotype.getValue(genotype));
16+
genotype.fitness = 1.0 / (diff + 1);
1717
}
1818
}

src/logic/ga/Genotype.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,20 @@
1-
import {Acid, ChromosomeBase} from '@technote-space/genetic-algorithms-js';
2-
import {Phenotype} from './Phenotype';
1+
import {Acid, ChromosomeBase, IChromosome} from '@technote-space/genetic-algorithms-js';
32

43
export class Genotype extends ChromosomeBase {
5-
private _phenotype: Phenotype | undefined = undefined;
4+
protected _fitness: number;
65

76
public constructor() {
87
super(0, true);
8+
this._fitness = -1;
99
this.generateAcids();
1010
}
1111

1212
public get fitness(): number {
13-
return this.phenotype.fitness;
13+
return this._fitness;
1414
}
1515

16-
public set fitness(_: number) {
17-
//
18-
}
19-
20-
public get phenotype(): Phenotype {
21-
if (!this._phenotype) {
22-
this._phenotype = new Phenotype(this);
23-
}
24-
25-
return this._phenotype;
16+
public set fitness(fitness: number) {
17+
this._fitness = fitness;
2618
}
2719

2820
public createNew(): ChromosomeBase {
@@ -40,4 +32,8 @@ export class Genotype extends ChromosomeBase {
4032
public getGenes(): Array<number> {
4133
return [...Array(this.length).keys()].map(index => Number(this.getAcid(index).value));
4234
}
35+
36+
protected performCopyFrom(_from: IChromosome): void {
37+
this._fitness = _from.fitness;
38+
}
4339
}

src/logic/ga/Phenotype.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,8 @@ import encoding from 'encoding-japanese';
22
import {Genotype} from './Genotype';
33

44
export class Phenotype {
5-
protected _fitness: number;
6-
7-
public constructor(protected genotype: Genotype) {
8-
this._fitness = -1;
9-
}
10-
11-
public get fitness(): number {
12-
return this._fitness;
13-
}
14-
15-
public setFitness(fitness: number): void {
16-
this._fitness = Math.max(0, fitness);
17-
}
18-
19-
public get value(): string {
20-
return encoding.convert(this.genotype.getGenes(), {
5+
public static getValue(genotype: Genotype): string {
6+
return encoding.convert(genotype.getGenes(), {
217
to: 'UNICODE',
228
type: 'string',
239
}) as string;

src/logic/ga/Reinsertion/Mgg.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ export class Mgg extends ReinsertionBase {
66
}
77

88
public select(population: Array<IChromosome>, offspring: Array<IChromosome>, parents: Array<IChromosome>): Array<IChromosome> {
9+
const sortedOffspring: Array<IChromosome> = [...offspring];
910
if (this.addParentsToOffspring) {
10-
offspring.push(...parents);
11+
sortedOffspring.push(...parents);
1112
}
12-
offspring.sort((chromosome1, chromosome2) => (chromosome2.fitness ?? -1.0) - (chromosome1.fitness ?? -1.0));
13+
sortedOffspring.sort((chromosome1, chromosome2) => (chromosome2.fitness ?? -1.0) - (chromosome1.fitness ?? -1.0));
1314

1415
const selected = [...population];
15-
selected.push(offspring.splice(0, 1)[0]); // elite
16-
selected.push(this.take(offspring));
16+
selected.push(sortedOffspring.splice(0, 1)[0]); // elite
17+
selected.push(this.take(sortedOffspring));
1718

1819
return selected;
1920
}

0 commit comments

Comments
 (0)