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

simulated-annealing-ts

v1.0.1

Published

Simulated Annealing in TypeScript

Downloads

10

Readme

Simulated Annealing in TypeScript

This npm module provides a TypeScript implementation of the Simulated Annealing algorithm, offering flexibility to handle various data types and supporting both maximization and minimization objectives.

Key features

  • Type Agnostic: Designed to handle and optimize any type of data, allowing seamless integration with different problem domains.
  • Maximization and Minimization: Accommodates both maximization and minimization objectives, providing a robust solution for diverse optimization scenarios.
  • Configurable Parameters: Easily customizable parameters to fine-tune the algorithm according to the specific requirements of your problem.

Installation

npm install simulated-annealing-ts

Usage

Create a main.ts file, call the run method of SimulatedAnnealing with the appropriate parameters.

Here's a configuration example where we aim to minimize the sum between two neighboring values in an array. The energy function getEnergy returns 15 for [1,9,2]. The minimal versions are: [1,2,9] and [9,2,1] with a energy of 8.

import { SimulatedAnnealing } from "simulated-annealing-ts";

const getEnergy = (etat: Array<number>): number => {
	// Example energy function: The absolute difference between neighboring elements in the array.
	return etat.reduce((previous: number, current: number, index: number): number => {
		if (index === 0) return previous;

		return previous + Math.abs(current - etat[index - 1]);
	}, 0);
};

const randomInt = function (min: number, max: number): number {
	return Math.floor(Math.random() * (max - min + 1) + min);
};

/** Swap 2 random elements in the array. */
const swap2Elements = (etat: Array<number>): Array<number> => {
	const index1 = randomInt(0, etat.length - 1);
	const index2 = randomInt(0, etat.length - 1);

	const newArray = [...etat];
	const tmp = newArray[index1];
	newArray[index1] = newArray[index2];
	newArray[index2] = tmp;

	return newArray;
};

const initialState: Array<number> = [1, 9, 2];
const result = SimulatedAnnealing.run<Array<number>>(initialState, getEnergy, swap2Elements);

console.log("Best solution found (", getEnergy(result), "):", result);

Parameters

  1. initialValue.
  2. getEnergy: Outside of simulated annealing, this method is called the objective function. Takes a state and returns its energy/cost.
  3. smallMutation: Takes a state as a parameter and returns a new state.
  4. config: Il est possible de paramétrer le nombre maximal d'itérations, la valeur extremum de l'énergie, la fonction de calcul de la température, la fonction de calcul de la probabilité d'accepter une valeur. Il est aussi possible de maximiser plutôt que minimiser l'énergie. Aucun paramètre de la configuration n'est obligatoire. Exemple:
const result = SimulatedAnnealing.run<Array<number>>(initialState, getEnergy, swap2Elements, {
	maxSteps: 100,
	getTemperature: Temperature.linear,
	getAcceptanceProbability: AcceptanceProbability.exp,
	energyDirection: EnergyDirection.minimize,
	energyLimit: 8,
});

You don't have to manipulate an array of numbers. It's possible to manipulate any data type.

Various algorithms for energy calculation and accepting a new state are implemented. You can create your own algorithm.

	getTemperature: (currentStep: number, maxSteps: number): number => currentStep / maxSteps,
	getAcceptanceProbability: (currentEnergy: number, lastAcceptedEnergy: number, temperature: number): number => temperature,