Simulated Annealing is a general-purpose meta heuristic optimisation algorithm. It is similar to hill climbing but SA has the ability to jump out of local optimal with a decreasing probability.
Image Credit: wiki
We can think of SA as the following scenario: A drunk rabbit jumps randomly as she wants to reach the hill top. As she wakes up gradually little by little, she walks steady to the hill top...
I have created a easy (simple to use) and yet very powerful tiny framework to adopt the SA to general math optimisation problems.
Technology Stack
Latest Javascript (ECMAScript 2016) and wrapped in NPM Library:
Project Page
NPM: https://www.npmjs.com/package/simulated_annealling
Unit Test
Unit tests are built upon mocha and chai unit testing framework. And you can run test via npm test
Demo
The following will use the SA library to search the answer(s) for equation x*x = 16
var SimulatedAnnealing = require('simulated_annealling').SimulatedAnnealing;
var GetAnswerOfXSquareEqualsSixteen = (function() {
// parameters
let options = {
coolingFactor: 0.09,
stabilizingFactor: 1.005,
freezingTemperature: 0.001,
initialTemperature: 15,
initialStabilizer: 30
}
// final solution
let x;
// current solution
let cur;
const getCost = (v) => {
return Math.abs(v * v - 16);
}
const generateNeighbor = () => {
// neighbour is within 0.5 distance
cur = x + (Math.random() - 0.5);
return getCost(cur);
}
const generateNewSolution = () => {
cur = Math.random() * 16; // guess a number between 0 to 16
x = cur;
return getCost(cur);
}
const acceptNeighbor = () => {
x = cur;
}
// pass parameters to SA object
let SA = SimulatedAnnealing(options, generateNewSolution, generateNeighbor, acceptNeighbor);
// we need to continue simulating if temperature is still high
while (SA.Do()) {
// console.log("Temperature: " + SA.GetCurrentTemperature());
// console.log("GetCurrentEnergy: " + SA.GetCurrentEnergy());
}
// final solution
console.log("Solution is: " + x);
})()
This example is also used as a unit test case.
Reposted to my own blog: https://helloacm.com/simple-but-powerful-simulated-annealing-npm-library-with-demo/
Contributing
Github: https://github.com/DoctorLai/simulated_annealling
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request
Posted on Utopian.io - Rewarding Open Source Contributors