Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

simulated annealing Python

from numpy import asarray, exp
from numpy.random import randn, rand, seed
from matplotlib import pyplot

# Define objective function
def objective(step):
    return step[0] ** 2.0

# Define simulated annealing algorithm
def sa(objective, area, iterations, step_size, temperature):
    # create initial point
    start_point = area[:, 0] + rand( len( area ) ) * ( area[:, 1] - area[:, 0] )
    # evaluate initial point
    start_point_eval = objective(start_point)
    # Assign previous and new solution to previous and new_point_eval variable 
    mia_start_point, mia_start_eval = start_point, start_point_eval
    outputs = []
    for i in range(iterations):
        # First step by mia
        mia_step = mia_start_point + randn( len( area ) ) * step_size  
        mia_step_eval = objective(mia_step)
        if mia_step_eval < start_point_eval:
            start_point, start_point_eval = mia_step, mia_step_eval
      #Append the new values into the output list
            outputs.append(start_point_eval)
            print('Acceptance Criteria = %.5f' % mac," ",'iteration Number = ',i," ", 'best_so_far = ',start_point," " ,'new_best = %.5f' % start_point_eval)
        difference = mia_step_eval - mia_start_eval
        t = temperature / float(i + 1)
        # calculate Metropolis Acceptance Criterion / Acceptance Probability
        mac = exp(-difference / t)
        # check whether the new point is acceptable 
        if difference < 0 or rand() < mac:
            mia_start_point, mia_start_eval = mia_step, mia_step_eval
    return [start_point, start_point_eval, outputs]

seed(1)
# define the area of the search space
area = asarray([[-6.0, 6.0]])
# initial temperature
temperature = 12
# define the total no. of iterations
iterations = 1200
# define maximum step_size
step_size = 0.1
# perform the simulated annealing search
start_point, output, outputs = sa(objective, area, iterations, step_size, temperature)
#plotting the values
pyplot.plot(outputs, 'ro-')
pyplot.xlabel('Improvement Value')
pyplot.ylabel('Evaluation of Objective Function')
pyplot.show()
Comment

PREVIOUS NEXT
Code Example
Python :: python install gimp 
Python :: get index pandas condition 
Python :: fastest sort python 
Python :: how to delete a turtle in python 
Python :: how to reapete the code in python 
Python :: coco.py 
Python :: random choice without replacement python 
Python :: How can I get terminal output in python 
Python :: text size legend to bottom matplotlib 
Python :: pandas casting into integer 
Python :: print all alphabets from a to z in python 
Python :: pandas add header to existing dataframe 
Python :: get adjacent cells in grid 
Python :: django template datetime-local 
Python :: debugar python 
Python :: pil overlay images 
Python :: python remove duplicates from a list 
Python :: spacy remove stop words 
Python :: how to record pyttsx3 file using python 
Python :: encrypt and decrypt python 
Python :: how to convert a pandas series from int to float in python 
Python :: PIL Make Circle 
Python :: how to get key and value from json array object in python 
Python :: force utf-8 encoding python 
Python :: remove last element from dictionary python 
Python :: python list methods 
Python :: df drop column 
Python :: sqlalchemy lock row 
Python :: selenium scroll down python 
Python :: python filename without extension 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =