Java Simulated Annealing from Pseudocode

The basic code should look like this:

public class YourClass {
  public static Solution doYourStuff(double startingTemperature, int numberOfIterations, double coolingRate) {
    double t = startingTemperature;
    Solution x = createRandomSolution();
    double ti = t;

    for (int i = 0; i < numberOfIterations; i ++) {
      double f = calculateFitness(x);
      Solution mutatedX = mutate(x);
      double newF = calculateFitness(mutatedX);
      if (newF < f) {
        double p = PR(); // no idea what you're talking about here
        if (p > UR(0, 1)) { // likewise
          // then do nothing
        } else {
          x = mutatedX;
        }
        ti = t * coolingRate;
      }
    }
    return x;
  }

  static class Solution {
    // no idea what's in here...
  }
}

Now as far as wanting different versions of smallChange() method - totally doable, but you have to read up on inheritance a little bit


You can compare your answer to the code provided for the textbook
Artificial Intelligence a Modern Approach.

  • SimulatedAnnealingSearch.java

Also, a Java-based approach to teaching simulated annealing (with sample code) is here:

Neller, Todd. Teaching Stochastic Local Search, in I. Russell and Z. Markov, eds. Proceedings of the 18th International FLAIRS Conference (FLAIRS-2005), Clearwater Beach, Florida, May 15-17, 2005, AAAI Press, pp. 8-13.

Related resources, references, and demos are here: http://cs.gettysburg.edu/~tneller/resources/sls/index.html