Python Code of Simulated Annealing Optimization Algorithm

Did you know that Simulated Annealing Optimization Algorithm is one of the top three most popular stochastic optimization algorithms for solving complex large scale optimization problems in various fields? Only Genetic Algorithm and Particle Swarm Optimization are more popular than Simulated Annealing Algorithm.

In this video, I’m going to show you a general principle, a flowchart, and a Python code of Simulated Annealing Optimization Algorithm. In addition, I will test the performance of the Simulated Annealing Optimization Algorithm in solving both minimization and maximization problems with well-known benchmarks. You can download this Python code, and it is very easy to customize this Python code to solve your optimization problems in various fields.

For more videos like this, check my YouTube channel here.

import time
import random
import math
import numpy as np
import matplotlib.pyplot as plt
#------------------------------------------------------------------------------
# Customization section:
initial_temperature = 100
cooling = 0.8  # cooling coefficient
number_variables = 2
upper_bounds = [3, 3]   
lower_bounds = [-3, -3]  
computing_time = 1 # second(s)
 
def objective_function(X):
    x=X[0]
    y=X[1]
    value = 3*(1-x)**2*math.exp(-x**2 - (y+1)**2) - 10*(x/5 - x**3 - y**5)*math.exp(-x**2 - y**2) -1/3*math.exp(-(x+1)**2 - y**2)
    return value
 
#------------------------------------------------------------------------------
# Simulated Annealing Algorithm:
initial_solution=np.zeros((number_variables))
for v in range(number_variables):
    initial_solution[v] = random.uniform(lower_bounds[v],upper_bounds[v])
     
current_solution = initial_solution
best_solution = initial_solution
n = 1  # no of solutions accepted
best_fitness = objective_function(best_solution)
current_temperature = initial_temperature # current temperature
start = time.time()
no_attempts = 100 # number of attempts in each level of temperature
record_best_fitness =[]
 
for i in range(9999999):
    for j in range(no_attempts):
 
        for k in range(number_variables):
            current_solution[k] = best_solution[k] + 0.1*(random.uniform(lower_bounds[k],upper_bounds[k]))
.
.
.
Sorry! This is only a half of the code.

Notice: It’s possible to watch the video and re-type the Python code yourself – that would take you from 1 to 3 hours; or with just €1.99 (the cost of a cup of coffee), you can download/copy the whole Python code within 2 minutes. It’s your choice to make.

Original price is €4.99 but today it’s only €1.99 (save €3 today – available for a limited time only)

Download the whole Python Code here (Membership Code ID: 004)

No need to build the Python code from scratch because it’s very time-consuming. My idol, Jim Rohn, once said: “Time is more value than money. You can get more money, but you cannot get more time”. If you think this code can be used in your research/teaching work, you should download it and then customize/modify/apply it to your work, without any obligation of citing the original source if you don’t want. However, redistribution (i.e., downloading the code/script here and then making it available on another site on the Internet) is strictly prohibited.

If you have any question or problem, please contact Dr. Panda by email: learnwithpanda2018@gmail.com

Thank you very much and good luck with your research!

26 Replies to “Python Code of Simulated Annealing Optimization Algorithm”

    1. Hello teacher i need code of Annuealing algorithme for use same optimiser in classification using CNN.can you please Help me i need urgent

  1. hi
    thank you for this code.
    i am a master student in computer science in Iran and your code helped me for starting implementing SA and coding in python …
    I will follow your posts and hope to meet you and work with you
    good luck

  2. thanks for the code , I have a question , if i want to combine tabu search for selection and use in this code or use in a single variable function just x for example F(x)=x^7-5x^6+2x^4+60 , What should I do? Please reply to me if possible.

  3. The graph you are plotting… What is the x & y axis? What parameters in the code does x & y axis denotes??

  4. Could you please tell me, what is “EA” and “n” in the code and why they are used? and how “EA” is being updated ?

Leave a Reply

Your email address will not be published. Required fields are marked *