Simulated Annealing in TypeScript
bash
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.
`typescript
import { SimulatedAnnealing } from "simulated-annealing-ts";
const getEnergy = (etat: Array): 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): Array => {
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 = [1, 9, 2];
const result = SimulatedAnnealing.run>(initialState, getEnergy, swap2Elements);
console.log("Best solution found (", getEnergy(result), "):", result);
`
$3
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:
`typescript
const result = SimulatedAnnealing.run>(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.
`typescript
getTemperature: (currentStep: number, maxSteps: number): number => currentStep / maxSteps,
getAcceptanceProbability: (currentEnergy: number, lastAcceptedEnergy: number, temperature: number): number => temperature,
``