Skip to main content

Simulated Annealing

Here, we introduce the simulated annealing (SA) [1] to solve the unconstraint optimization problems with JijZept. This method is based on the physical process of annealing the iron, and one can obtain a certain level of solutions.

What is the Simulated Annealing?

SA is a method for solving combinatorial optimization problems and is one of the meta-heuristics, which is a general term for heuristics. SA can be applied generally to many combinatorial optimization problems with no constraint conditions. The problems with some kind of constraint conditions can also be solved by SA using the penalty methods. The general process to solve the problems by SA is repeating the following two steps.

  • Obtain a new solution according to the previous computation steps
  • Evaluate the new solution and store the information to search for a new solution in the next computation step.

Let us consider a more concrete example. The solution to a combinatorial optimization problem is represented as a bit sequence of {0,1}\{0, 1\} such as (0,0,1,0,)(0, 0, 1, 0, \dots) or (1,1,1,0,)(1, 1, 1, 0, \dots). These bit sequences are called the states. The set of the states that are "close" in some kind of meaning to a certain bit sequence x\boldsymbol{x} is called a neighborhood N(x)N(\boldsymbol{x}) of x\boldsymbol{x}. The computation steps of SA can be represented in more detail as follows.

  • Select a state x\boldsymbol{x}' randomly from the neighborhood N(x)N(\boldsymbol{x}) of the state x\boldsymbol{x} in the previous step.
  • Update the state x\boldsymbol{x}' from x\boldsymbol{x} with probability P(E(x),E(x),T)P(E(\boldsymbol{x}), E(\boldsymbol{x}'), T).

Here, E(x)E(\boldsymbol{x}) represents the energy and corresponds to the value of the objective function in the mathematical model of the combinatorial optimization problem. TT is a non-negative parameter, which can be considered as the temperature. One must determine the value of TT for each calculation step in advance. Usually, the temperature TT is decreased with each calculation step. This is expected to cause the state x\boldsymbol{x} to change frequently when the temperature is high, but as the temperature decreases, the state x\boldsymbol{x} is expected to converge to the globally optimal solution. This is why it is called simulated "annealing. The figure below shows how the state improves as the calculation steps progress.

Now, in the above algorithm, we have to determine the way to construct the neighborhood N(x)N(\boldsymbol{x}) from x\boldsymbol{x} and the way to set the transition probability P(E(x),E(x),T)P(E(\boldsymbol{x}), E(\boldsymbol{x}'), T) for executing the SA. The simple way to set up a neighborhood is to define N(x)N(\boldsymbol{x}) as "the set of states that can be reached by a single bit flip from the state x\boldsymbol{x}". Algorithms using this neighborhood definition are generally called single-spin flips. As for the transition probability P(E(x),E(x),T)P(E(\boldsymbol{x}), E(\boldsymbol{x}'), T), a relatively good solution can be obtained by using the following setting.

P(E(x),E(x),T)={e(E(x)E(x))/T(E(x)>E(x))1(E(x)E(x))(1)P(E(\boldsymbol{x}), E(\boldsymbol{x}'), T) = \left\{ \begin{array}{ll} e^{-(E(\boldsymbol{x}') - E(\boldsymbol{x})) /T} & (E(\boldsymbol{x}') > E(\boldsymbol{x})) \\ 1 & (E(\boldsymbol{x}') \leq E(\boldsymbol{x})) \end{array} \right.\tag{1}

Using this setting of the transition probability is called the Metropolis method. The figure below plots the probability P(E(x),E(x),T)P(E(\boldsymbol{x}), E(\boldsymbol{x}'), T) set by the above equation (1).

Here, the x-axis is the energy difference between the states Δ=E(x)E(x)\Delta=E(\boldsymbol{x}') - E(\boldsymbol{x}). Note that the transition probability is always 1 in the case of Δ0\Delta \leq 0. When the temperature TT is high, a random update of the state is more likely to occur since the transition probability is large even when Δ>0\Delta>0. This prevents the state x\boldsymbol{x} from remaining in the locally optimal solution. As the calculation progresses and the temperature decreases, the transition probability becomes smaller for Δ>0\Delta>0. This results in the state being updated only when the energy is reduced. This is expected to cause the state x\boldsymbol{x} to converge to the globally optimal solution.

Characteristics of SA

The SA has the following advantages and is often used to solve combinatorial optimization problems.

  • The SA can prevent the state from being trapped in locally optimal solutions by using thermal fluctuations.
  • The algorithm does not depend on the details of objective functions, and thus it can be applied to a wide range of combinatorial optimization problems.
  • It is easy to understand and implement the algorithm of the SA.

In contrast, the SA has the following disadvantage.

  • Obtaining the exact optimal solution needs a long calculation time or may be impossible in the reasonable calculation time.
  • It is difficult to handle constraint conditions directly. They must be handled indirectly by using penalty methods or something.
  • Proper scheduling for temperature TT depends on the mathematical problems and needs some effort.

The SA is useful for obtaining solutions in practical computation time that may not be optimal, but are reasonably good.

Calculation with JijZept

In this section, we explain how to obtain solutions by the SA using JijZept. First, you must finish setup of JijZept and get API key. Then you can prepare JijSASampler in JijZept, which solve the problem by using the SA.

import jijzept as jz

# Setup SA Sampler
sampler = jz.JijSASampler(token='*** your API key ***', url='')

BINARY Variable

Let us consider the simple optimization problem with binary variables.

minx0x1,      x0,x1{0,1}.{\rm min} -x_0x_1,\;\;\;x_0,x_1\in\{0, 1\}.

One can easily understand that the exact optimal solution is (x0,x1)=(1,1)(x_0,x_1)=(1,1) with corresponding energy E=1E=-1. This problem can be solved through the sample_qubo method in JijSASampler.

response = sampler.sample_qubo(Q={(0, 1): -1})

After executing this source code, one can see the outputs {0: 1, 1: 1} and -1.0, which means that the solution is (x0,x1)=(1,1)(x_0,x_1)=(1,1) and the corresponding energy is -1. The return value of sample_qubo is provided as dimod.SampleSet. See the reference for details.

SPIN Variable

Then, we try to solve the following problem with spin variables.

mins0s1+s0+s1,      s0,s1{1,+1}.{\rm min} -s_0s_1 + s_0 + s_1,\;\;\;s_0,s_1\in\{-1, +1\}.

The exact optimal solution is (s0,s1)=(1,1)(s_0,s_1)=(-1, -1) with the energy -3. One can calculate by using sample_ising.

response = sampler.sample_ising(h={0: 1, 1: 1}, J={(0, 1): -1})

Please check that the output is {0: -1, 1: -1} and -3.0

Higher-Order Problem

The sample_hubo method can calculate higher-order problems like this.

min2s0s1s2s0s1s1s2,      s0,s1,s2{1,+1}.{\rm min} -2s_0s_1s_2 - s_0s_1 - s_1s_2,\;\;\;s_0,s_1,s_2\in\{-1, +1\}.

The exact optimal solution is (s0,s1,s2)=(1,1,1)(s_0,s_1,s_2)=(1, 1, 1) with the energy -4.

response = sampler.sample_hubo(J={(0, 1): -1, (1, 2): -1, (0, 1, 2): -2}, vartype='SPIN')

The output must be {0: 1, 1: 1, 2: 1} and -4.0. In the case of binary variables, please set vartype='BINARY'.

Solve with JijModeling

Here, we explain how to solve mathematical problems using JijModeling. Please see the documentation for how to use JijModeling. We use the following optimization problem with the constraint condition as a simple example.

mini=0N1dixisubject  to      i=0N1xi=1xi{0,1}{\rm min} -\sum^{N-1}_{i=0}d_i x_i\\ {\rm subject\;to}\;\;\;\sum^{N-1}_{i=0}x_i=1\\ x_i\in\{0,1\}

This problem can be implemented by JijModeling as follows.

import jijmodeling as jm

d = jm.Placeholder('d', dim=1) # Define cariable d
d.shape[0].set_latex('N') # Set latex expression of the length of d
x = jm.Binary('x', shape=(d.shape[0],)) # Define binary variable
i = jm.Element('i', (0, d.shape[0])) # Define dummy index in summation
problem = jm.Problem('simple problem') # Create problem instance
problem += -jm.Sum(i, d[i]*x[i]) # Add objective function
problem += jm.Constraint('one hot', jm.Sum(i, x[i]) == 1) # Add constraint condition
problem # Display the problem

Using the Jupyter Notebook environment, one can see the following mathematical expression of the problem.

Then, we consider the exact optimal solution. If di>0d_i>0 for all i=0,1,,N1i=0,1,\ldots,N-1, solving the above problem is equal to finding the maximum value of did_i. Thus, considering the instance value d=(1,2,3,2,1)d=(1,2,3,2,1), the exact optimal solution is x=(0,0,1,0,0)\boldsymbol{x}=(0,0,1,0,0) with the energy -3.0. Now we check the solution obtained by JijZept.

instance_data = {'d': [1,2,3,2,1]} # The actual value of d

# serch=Ture: Use parameter search, num_serch: The number of trials for parameter search
response = sampler.sample_model(problem, instance_data, search=True, num_search=5)
print(response.to_dense().record.solution) # Convert dense type to make it easy to see the solution
print(response.evaluation.objective) # Display the objective value

The outputs are, for example, {'x': [array([0, 0, 1, 0, 0]), array([1, 1, 1, 1, 1]), array([0, 0, 1, 0, 0]), array([0, 0, 1, 0, 0]), array([0, 0, 1, 0, 0])]} and [-3.0, -9.0, -3.0, -3.0, -3.0]. In this case, the exact optimal solution x=(0,0,1,0,0)\boldsymbol{x}=(0,0,1,0,0) was obtained four times in the calculation.

Finally, we note that the above samplers have additional parameters to obtain better solutions. If you want to tune these parameters, see the class reference.


[1] S. Kirkpatrick, C. D. Gelatt, Jr., and M. P. Vecchi, Optimization by Simulated Annealing