jeffheaton's picture

    The source code accompanying this book provides a generic simulated annealing class. This abstract class is named SimulatedAnnealing and can be used to implement a simulated annealing solution for a variety of problems. We will use this simulated annealing class for both the neural network example and the traveling salesman problem.

    This section will describe how the generic SimulatedAnnealing class works. The application of simulated annealing to neural networks and the traveling salesman problem will be covered later in this chapter.

Inputs to the Simulated Annealing Algorithm

    There are several variables that must be set on the SimulatedAnnealing class for it to function properly. These variables are usually set by the constructor of one of the classes that subclass the SimulatedAnnealing class. Table 7.1 summarizes these inputs.

Table 7.1: Simulated Annealing Inputs

Variable Purpose
startTemperature The temperature at which to start.
stopTemperature The temperature at which to stop.
cycles The number of cycles to be used.

    The simulated annealing training algorithm works very much like every other training algorithm in this book. Once it is set up, it progresses through a series of iterations.

Processing Iterations

    The SimulatedAnnealing class contains a method named iteration that is called to process each iteration of the training process.

public void iteration() throws NeuralNetworkError {

    First, an array is created to hold the best solution.

UNIT_TYPE bestArray[];

    Next, the starting error is determined.

setError(determineError());
bestArray = this.getArrayCopy();

    The training process is then cycled through a specified number of times. For each training pass, the randomize method is called. This method is abstract and must be implemented for any problem that is to be solved by simulated annealing.

for (int i = 0; i < this.cycles; i++) {
  double curError;
  randomize();

    The error is determined after randomize has been called.

  curError = determineError();

    If this was an improvement, then the newly created array is saved.

  if (curError < getError()) {
    bestArray = this.getArrayCopy();
    setError(curError);
  }
}

    Once the cycle is complete, the best array is stored.

this.putArray(bestArray);

    A ratio is calculated that will decrease the temperature to the desired level. This is the Java implementation of Equation 7.1, which was shown earlier.

final double ratio = Math.exp(Math.log(getStopTemperature()
  / getStartTemperature())
  / (getCycles() - 1));

    The temperature is scaled by this amount.

this.temperature *= ratio;

    This simulated annealing class is, of course, abstract; thus it only implements the simulated annealing algorithm at a primitive level. The examples in this chapter that actually put the SimulatedAnnealing class to use must implement the randomize function for their unique situations.


Copyright 2005 - 2012 by Heaton Research, Inc.. Heaton Research™ and Encog™ are trademarks of Heaton Research. Click here for copyright, license and trademark information.