Simple simulated annealing template in C++11 This simulated annealing program tries to look for the status that minimizes the energy value calculated by the energy function.

The status class, energy function and next function may be resource-intensive on future usage, so I would like to know if this is a suitable way to code it.

#pragma once

#include &lt;cmath&gt;
#include &lt;random&gt;
#include &lt;utility&gt;
#include &lt;algorithm&gt;

// To find a status with lower energy according to the given condition
template&lt;typename status, typename count, typename energy_function, typename temperature_function, typename next_function, typename generator&gt;
status simulated_annealing(status i_old, count c, const energy_function&amp; ef, const temperature_function&amp; tf, const next_function&amp; nf, generator&amp; g){

    auto   e_old  = ef(i_old);

    status i_best = i_old;
    auto   e_best = e_old;

    std::uniform_real_distribution&lt;decltype(e_old)&gt; rf(0, 1);

    for(; c &gt; 0; --c){
        status i_new = nf(i_old, g);
        auto   e_new = ef(i_new);

        if(e_new &lt; e_best){
            i_best =           i_new ;
            e_best =           e_new ;
        }

        if( e_new &lt; e_old || std::exp( (e_old - e_new) / tf(c) ) &gt; rf(g) ){
            i_old  = std::move(i_new);
            e_old  = std::move(e_new);
        }
    }
    return(i_best);
}


Example usage:

#include &lt;cmath&gt;
#include &lt;random&gt;

#include &lt;chrono&gt;
#include &lt;cstdlib&gt;
#include &lt;iostream&gt;

#include "simulated_annealing.hpp"

template&lt;typename Clock = std::chrono::system_clock&gt;
class timer{
private:
    typedef std::chrono::time_point&lt;Clock&gt; time_point;
    time_point _start;
    time_point _finish;
public:
    void start(){
        _start = Clock::now();
    }
    void finish(){
        _finish = Clock::now();
    }
    template&lt;typename rtype&gt;
    rtype count(){
        std::chrono::duration&lt;rtype&gt; elapsed = _finish - _start;
        return elapsed.count();
    }
};


auto ef = [](long double x){
    return std::abs( (x - 10)*(x + 20)*(x - 30) );
};

auto tf = [](long double k){
    return std::exp( -20 * k );
};

template&lt;typename generator&gt;
long double nf(long double x, generator&amp; g){
    std::normal_distribution&lt;decltype(x)&gt; d(0, 1);
    return x + d(g);
}

int main(int argc, char const *argv[])
{
    std::random_device rd;
    std::mt19937_64 g(rd());
    timer&lt;std::chrono::high_resolution_clock&gt; t;

    long double root   = std::atof(argv[1]);
    std::size_t count  = std::atoi(argv[2]);

    t.start();
    root = simulated_annealing(root, count, ef, tf, nf&lt;decltype(g)&gt;, g);
    t.finish();

    std::cout &lt;&lt; "Result: " &lt;&lt; root &lt;&lt; std::endl;
    std::cout &lt;&lt; "Time elapaed: " &lt;&lt; t.count&lt;double&gt;() &lt;&lt; '\n';

    return 0;
}
I think the algorithm is well abstracted and the code is nicely written, so there is not all too much you can do in order to make it more efficient. Still I have some minor suggestions for improvement:

First, regarding the algorithm itself:

When the check if(e_new &lt; e_best) is successful, you can directly treat the step as accepted. Although this might be negligible, you avoid one more check.

In your check whether to accept the new step, you can think about choosing a cutoff (say ~ 10.0) parameter to bypass the relatively expensive calculation of std::exp as well as the random number generation.


Summarizing 1. and 2., one gets a slightly more verbose but hopefully more efficient code:
    if(e_new &lt; e_best){
        i_best =           i_new ;
        e_best =           e_new ;
        i_old  = std::move(i_new);
        e_old  = std::move(e_new);
        continue;
    }

    auto t = tf(c);
    auto delta_e = e_new - e_old;

    if( delta_e &gt; 10.0 * t) continue;  //as std::exp(-10.0) is a very small number

    if( delta_e &lt;= 0.0 || std::exp( - delta_e / t ) &gt; rf(g) ){
        i_old  = std::move(i_new);
        e_old  = std::move(e_new);
    }

Next, regarding your choice of functions: Here I would try to avoid expensive functions as well. I know, this might often not be possible as the functions are determined by your problem at hand. However, for example, in your temperature function you can think about replacing the std::exp(-20*k) by something more efficient such as 1/(k*k).

Regarding the language:

You can get a bit more of a C++11 touch if you use universal references instead of lvalue references.
template&lt;typename status, typename count, typename energy_function
       , typename temperature_function, typename next_function, typename generator&gt;
status simulated_annealing(status&amp;&amp; i_old, count&amp;&amp; c, energy_function&amp;&amp; ef
                         , temperature_function&amp;&amp; tf, next_function&amp;&amp; nf, generator&amp;&amp; g)
{
    //...
}

This is maybe also a bit more consistent with your std::move's of your status and count-type variables (which, as a side node, will resolve to simple copies when fundamental types such as double or size_t are used).
Further, you can think  about replacing your type count by size_t, since that is what this type is for. But this just because I personally try to avoid template parameters when possible.



Regarding the design:

The nf function can be made more generic by defining a static generator inside
long double nf(long double x){
    static std::mt19937_64 g(std::random_device());
    std::normal_distribution&lt;decltype(x)&gt; d(0, 1);
    return x + d(g);
}

I wouldn't pass a generator here, as it doesn't fit well into a generic desing. (For instance, the next state could also be found deterministically).

The same holds for your main simulated_annealing function. I wouldn't bother the user to define a random number generator, but just define it inside -- moreover since it is used &quot;only&quot; to create random numbers in [0,1] (once you've followed suggestion 4.).


        template&lt;typename status, typename count, typename energy_function
               , typename temperature_function, typename next_function&gt;
        status simulated_annealing(status&amp;&amp; i_old, count&amp;&amp; c, energy_function&amp;&amp; ef
                             , temperature_function&amp;&amp; tf, next_function&amp;&amp; nf)
        {
            std::mt19937_64 g(std::random_device());            
            //...
        }

This you can simply call the routine via
    root = simulated_annealing(root, count, ef, tf, nf);

which is more concise and better reflects the main components of a simulated annealing algorithm.