Carnez
Carnez

Reputation: 11

Simulated Annealing Implementation in C++

I am interested in implementing simulated annealing, and my current gives the wrong output. I believe my understanding of it is flawed. Could someone help and explain how this implementation is wrong? dist is an array of my conditional probabilities.

int idx=0;
double value = dist[0];
for (int ix = 1; ix < ITERS ; ixx ){
    double temp = (1/500)*((1/ix)- (1/ITERS));
    for(int m = 0; m < input.rows()-1; m++){
      double p = transitionProbability(dist[m], dist[m+1],temp);
        if (p > unifRand() ) {
            if (dist[m+1] < value)
                value = dist[m+1];
                idx = m+1;      
            }
        else {  
      idx = m;
      }
   }
}



double inline transitionProbability(double d0, double d1,double T) {

if (d0 > d1)
    { return 1; }

else 
    { return (exp(d0 - d1)/T);  }
}

Upvotes: 0

Views: 1556

Answers (1)

borisd
borisd

Reputation: 11

It would me more easy to answer your question if you can also describe what optimization problem you are trying to solve, and what are your data structures.

For example, what does "input.rows" refer to in your implementation?

The basic idea of SA is, at each iteration:

  • make a random modification of the current solution
  • compute the cost of the new solution
  • set it as current solution (for next iteration) if it is accepted (using the transition probability function, which looks to be correct)

Upvotes: 1

Related Questions