An open API service indexing awesome lists of open source software.

https://github.com/hacklone/coool-genetics

Genetic algorithm for TypeScript
https://github.com/hacklone/coool-genetics

Last synced: 11 months ago
JSON representation

Genetic algorithm for TypeScript

Awesome Lists containing this project

README

          

# Genetic algorithm framework
A framework to make working with genetics algorithm easy

## Install
> npm i --save @coool/genetics

## Set up

### Create Candidate

```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
}
}
```

### Create Candidate Factory

```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): Promise {
// Clone a candidate
}

public createCrossOverCandidateAsync(candidate1: Readonly, candidate2: Readonly): Promise {
// Create a cross over from two parent candidates
}

public createMutatedCandidateAsync(originalCandidate: Readonly): Promise {
// Create a mutated version of the candidate
}
}
```

### Create Candidate Test

```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
}
}
```

## Run

### Iterations
- Round tournament
- Round tournament between population candidates
- Simple iteration
- Run an iteration between all population candidates

### Populations
- Natural selection
- Natural selection strategy to create new population

```typescript runner.ts
import { GeneticsRunnerBase } from '@coool/genetics';

export class CarGenetics extends GeneticsRunnerBase {
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);
}
}
}
```