A framework to make working with genetics algorithm easy
npm install @coool/genetics``typescript car.ts
import { ICandidate, CandidateId, ICandidateMove } from '@coool/genetics';
import { RoadState } from './race';
export interface CarMove extends ICandidateMove {
// Decision data of candidate
}
export class Car implements ICandidate {
constructor(
public id: CandidateId,
) {
}
public async getNextMoveAsync(roadState: RoadState): Promise
// Return turn decision of candidate
}
}
`
`typescript car.factory.ts
import { ICandidateFactory, ICandidate } from '@coool/genetics';
import { Car } from './car';
export class CarFactory implements ICandidateFactory {
public createRandomCandidateAsync(): Promise
// Create a random candidate
}
public createCloneCandidateAsync(originalCandidate: Readonly
// Clone a candidate
}
public createCrossOverCandidateAsync(candidate1: Readonly
// Create a cross over from two parent candidates
}
public createMutatedCandidateAsync(originalCandidate: Readonly
// Create a mutated version of the candidate
}
}
`
`typescript race.ts
import { ICandidateTest, ICandidateTestResult, ICandidateTestFactory, ICandidateTestHistoryEntry, CandidateId } from '@coool/genetics';
import { Car } from 'car.ts';
export interface RaceResult extends ICandidateTestResult {
history: RaceHistoryEntry[];
candidateRanks: {
candidateId: CandidateId;
score: number;
}[];
}
export interface RaceHistoryEntry extends ICandidateTestHistoryEntry {
// Data relevant to turns of the candidate test
}
export class Race implements ICandidateTest {
public async runAsync(cars: Car[]): Promise
// Run test between candidates and return the result of the test
}
}
export class RaceFactory implements ICandidateTestFactory {
public async createCandidateTestAsync(): Promise
// Create candidate test instance
}
}
`
`typescript runner.ts
import { GeneticsRunner } from '@coool/genetics';
export class CarGenetics extends GeneticsRunner {
constructor(
carFactory: CarFactory,
raceFactory: RaceFactory,
) {
super(
new NaturalSelectionPopulationFactory(carFactory),
new RoundTournamentIteration(raceFactory),
);
}
}
`
`typescript main.ts
export class Main {
constructor(
private _carGenetics: CarGenetics,
) {
}
public async runAsync() {
for (let i = 0; i < 100; i++) {
const iterationResult = this._carGenetics.runNextPopulationAsync();
console.log(iterationResult);
}
}
}
``