Simple but Powerful Simulated Annealing NPM Library (with Demo)

in utopian-io •  7 years ago  (edited)

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

image.png

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

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

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.

  • What are the real world uses of this algorithm?
  • What motivated you to implement this algorithm?
  • What documents did you use to come up with the algorithm?

You can contact us on Discord.
[utopian-moderator]

  ·  7 years ago (edited)

What are the real world uses of this algorithm?
if you need some quick (not necessarily the best) solutions SA fits this purpose as it is quick. Also, the real world problems may be so complex that the optimal solutions cannot always be found.

What motivated you to implement this algorithm?
I need a SA to solve one of my problems today and I don't want to use C++ today. Also, I think others may need this quick-and-handy library.

What documents did you use to come up with the algorithm?
Well, not so much documents apart from wiki, because I have known this algorithm inside-out (in my memory)!

Please Flow me, i am flowing you...

  ·  7 years ago 

Hey @justyy I am @utopian-io. I have just upvoted you!

Achievements

  • WOW WOW WOW People loved what you did here. GREAT JOB!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

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

thanks, your post! have a nice day.