title: Simulated Annealing N Queens Probability Forumula
body: <p>I am having some trouble with a simulated annealing algorithm to solve the n queens problem. Basically, I have it look for a better more, which works fine, but then I run a formula to check and see if it should take a "bad" move or not. From my understanding, the formula is e^(change in board state calculation)/CurrentTemperature. This number should be compared against a random double or float, if the random number is greater than the equation, the algorithm should take the "bad" move. The problem that I am getting is that the formula is always either REALLY close to 1 or over 1. Here some of my code (let me know if more should be provided):</p>
  
  <pre><code>temperature = n*100; //initializes starting temperature 
      currentTemp = n*100;
      int cooldown = n*2; //initializes cool down temperature 
      float examine = 0; //this is the change in board calculation
  int cost = 1;
      boolean betterMove = false;
      queen = new int[n];
      int[][] board = graph; // generates a board of n size
      float ran = 0; //random float to compare to 
      double compareAgainst = 0; //formula variable 
  cost = calculate(board, n); //calculates the cost 
  while (cost &gt; 0 &amp;&amp; currentTemp &gt; 0) 
      {
  
  
          // chooses a random queen to move that has a heuristic higher than zero
          int Q = rand.nextInt(n);
          while (queen[Q] == 0)
              Q = rand.nextInt(n);
  
          betterMove = false;
  
          for (int i = 0; i &lt; n; i++) 
          {
              for (int j = 0; j &lt; n; j++) 
              {
                  if (board[i][j] == 1 &amp;&amp; j == Q) 
                  {
                      while (!betterMove)
                      {
                          int move = i;
                          while (move == i)
  
                          move = rand.nextInt(n); //pick a random move
                          tempBoard[i][j] = 0; //erase old position 
                          tempBoard[move][j] = 1; //set new position 
  
  examine =  calculate(tempBoard, n) - calculate(board, n); //calculates the difference between the change in boards
                          ran = rand.nextFloat(); //generates random number to compare against 
                          compareAgainst = Math.pow(Math.E, (-examine / currentTemp)); //formula out of the book, basically is e^(change in board state divided by currentTemp)
  
  
                          if (calculate(tempBoard, n) &lt; calculate(board, n))  //if this is a better move
                          {
                              for (int a = 0; a &lt; n; a++)
                                  for (int b = 0; b &lt; n; b++)
                                      board[a][b] = tempBoard[a][b]; //set it to the board 
  
                              cost = calculate(board, n);
                              currentTemp -= cooldown; //cool down the temperature 
  
      betterMove = true;
                          }
  
  else if(calculate(tempBoard,n) &gt;= calculate(board,n)) //if this is a worse move
                          {
  
                            if(verbose == 1) //outputs whether or not this is a bad move and outputs function value and random float for simulated annealing purposes
                              {
                                  System.out.println("This is a worse move");
                                  System.out.println("The numbers for Simulated Annealing:");
                                  System.out.println("Random number = " + ran);
                                  System.out.println("Formula = " + compareAgainst);
                                  System.out.println("Examine = " + examine);
  
                              }
  
                              if(ran &gt; compareAgainst) //if the random float is greater than compare against, take the bad move
  
                              {
  
                                  for (int a = 0; a &lt; n; a++)
                                      for (int b = 0; b &lt; n; b++)
                                          board[a][b] = tempBoard[a][b]; //take the move
  
                                  cost = calculate(board, n);
  
                                  currentTemp-= cooldown;
  
  
  
                                  betterMove = true;
                              }
  
                              else //if not, do not take the move 
                              {
  
                                  for (int a = 0; a &lt; n; a++)
                                      for (int b = 0; b &lt; n; b++)
                                          tempBoard[a][b] = board[a][b];
  
  
  
                                  }
  
                              currentTemp-= cooldown;
                              betterMove = true;
                              }
                          }
  
  
                      }
  
                      i = n;
                      j = n;
                  }
              }
          }
  
      }
  </code></pre>
  
  <p>I have tried a number of things such as making the examine variable negative or taking the absolute value of the difference between board states. Also, the calculate function that is being called basically scans the board and returns back how many queens are in conflict, which is an int. Let me know if more clarification is needed. Thanks </p>