I'm writing a code to minimize a function and I'm having problems storing the new move.
import math
import random
#minimze the function:
def f(x):
return math.pow(x,2) + math.sin(10*x)
x_list=[]
energy_list=[]
g=2 #starting position
def move(): #Generate a random move
random.uniform(-0.5,0.5)
def energy():
h=g+move() #generate random move
E = f(g)-f(h) #energy difference
if E>0:
x_list.append(h) #store position and energy
energy_list.append(f(h))
g==H #set g equal to the new move
if E<0:
R=random.uniform(0,1)
if R<math.exp(-E/1.5): #get out of the local minimum
h=R+g
x_list.append(h)
energy_list.append(f(h))
else:
x_list.pop()
energy_list.pop()
N=50
for i in range(N):
energy()
print g
print x_list
print energy_list
print min(energy_list)
I want to store the value of g with the new move. My code keeps starting from the original position (which is 2). Does anyone know how to do this?