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 🙏

© 2025 – Pkg Stats / Ryan Hefner

gntc

v3.1.0

Published

A Genetic Algorithm

Downloads

32

Readme

gntc

Using

npm install gntc
import { createGntc } from "../index";

const utilities = {
  fitness: (choice, baseSolution) => {
    // Implementation of your fitness function
    return Math.random(); // Placeholder
  },
  crossover: (solution1, solution2) => {
    // Implementation of your crossover function
    return solution1; // Placeholder
  },
  mutate: (solution) => {
    // Implementation of your mutation function
    return solution; // Placeholder
  },
  generateChoice: (select, options) => {
    // Generate a choice based on selection criteria and options
    return options[Math.floor(Math.random() * options.length)]; // Placeholder
  },
  restrictions: [
    (choice) => {
      // Example restriction
      return choice % 2 === 0; // Placeholder
    }
  ]
};

const config = {
  options: [1, 2, 3, 4, 5],
  populationSize: 10,
  iterations: 100,
  utilities: utilities,
  select: 2,
  loader: (iteration) => console.log(`Iteration ${iteration}`),
  startingSolution: null // Starting solution if any
};

const gntcGenerator = createGntc(config);
let finalResult;

// Start the generator
const generatorInstance = gntcGenerator();

// Use a loop to process each iteration and catch the last returned value
let state = generatorInstance.next();
while (!state.done) {
  //console.log(`Progress: ${(state.value.progress * 100).toFixed(2)}%`);
  state = generatorInstance.next();
}

// The last value from the generator when 'done' is true is the best result
finalResult = state.value;
console.log('Best result:', finalResult);

Config

const {
	?utilities: {
		fitness,
		crossover,
		mutate,
		generateChoice,
		restrictions,
	},
  candidates,
	select,
	config: {
		populationSize,
		iterations,
	},
	?loader,
	?seed,
} = props;

candidates

[Optional]

An array of items from which to create a choice.

utilities

[Optional]

Functions that define how the algorithm operates.

fitness

The fitness function scores solutions.

The default fitness function expects an array of numbers, and will score them by their numerical value.

select

How many candidates to pick in each choice.

config

Genetic Algorithm config.

populationSize

How big each population should be. A larger population usually means a better end result, but will take longer.

iterations

How many generations will be created. A larger population means a better end result, but will take longer.

About genetic algorithms

A Genetic Algorithm (GA) is an search heuristic that simulates the naturally occurring processes by which biological evolution occurs (Goldberg & Holland 1988).

They can be used to efficiently find strong solutions to search problems and optimisation problems.

It works by taking an initial pool of (usually random) solutions to that problem, evaluating the performance of those solutions, and combining the best of them to create new solutions. Small mutations are also added, giving GAs the ability to find globally optimal solutions beyond any locally optimal solutions they might find (local minima).

Genetic algorithm elements

Initial population

Two main approaches:

  • randomly generating a pool of solutions
  • seeding with an existing solution you are looking to improve upon.

We provide an inbuilt utility function for randomising an initial population.

Fitness function

This is the a key step. Having generated a new population, they must be ranked. This fitness function provides the ranking, and is what the GA is ultimately optimising for.

We provide a generic utility function which takes objects and the property to optimise against as parameters.

Selection

The idea of selection phase is to select the fittest individuals and let them pass their genes to the next generation.

Two pairs of individuals (parents) are selected based on their fitness scores. Individuals with high fitness have more chance to be selected for reproduction.

These parents then have the crossover function applied to them.

Crossover

  • Crossover point method
  • Full random method

Mutation

Finally, we add a chance of mutating any children.