npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@coool/genetics

v1.2.0

Published

A framework to make working with genetics algorithm easy

Downloads

19

Readme

Genetic algorithm framework

A framework to make working with genetics algorithm easy

Install

npm i --save @coool/genetics

Set up

Create Candidate

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<CarMove> {
    // Return turn decision of candidate
  }
}

Create Candidate Factory

import { ICandidateFactory, ICandidate } from '@coool/genetics';
import { Car } from './car';

export class CarFactory implements ICandidateFactory {
  public createRandomCandidateAsync(): Promise<Car> {
    // Create a random candidate
  }

  public createCloneCandidateAsync(originalCandidate: Readonly<Car>): Promise<Car> {
    // Clone a candidate
  }

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

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

Create Candidate Test

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<ICandidateTestResult> {
    // Run test between candidates and return the result of the test 
  }
}

export class RaceFactory implements ICandidateTestFactory {
  public async createCandidateTestAsync(): Promise<Race> {
    // 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
import { GeneticsRunner } from '@coool/genetics';

export class CarGenetics extends GeneticsRunner {
  constructor(
    carFactory: CarFactory,
    raceFactory: RaceFactory,
  ) {
    super(
      new NaturalSelectionPopulationFactory(carFactory),
      new RoundTournamentIteration(raceFactory),
    );
  }
}
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);
    }
  }
}