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
Thank you for the contribution. It has been approved.
I would like to know and perhaps others in the open source community, so feel free to add the answers to your post.
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Please Flow me, i am flowing you...
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
@originalworks
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey @justyy I am @utopian-io. I have just upvoted you!
Achievements
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
thanks, your post! have a nice day.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit