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?

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Gribouillis

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?

There was a missing 'return' in your code as well as a mistyped 'g==H' instead of 'g = h'. Also there was a more subtle error: since energy() contains a statement 'g = ...', the interpreter thinks that 'g' is a local variable in energy(), and it complains because 'g' was not initialized before 'h = g + move()'. A solution is to add a global statement at the beginning of energy(). A better solution would be to pass g as a parameter of the function. Here is your corrected code

``````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
return random.uniform(-0.5,0.5) # <---- Added missing return statement

def energy():
global g # <---- Added global declaration
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 # <---- This was mistyped
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)``````

Also, your code is indented with tab characters, so please configure your editor to indent python code with 4 spaces instead of a tab character.
I added a picture of your function :)

Edited by Gribouillis: n/a

Attachments
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.