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?

Recommended Answers

All 2 Replies

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.