Hill-climbing 算法python 实现
Posted MrCharles
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hill-climbing 算法python 实现相关的知识,希望对你有一定的参考价值。
Hill-climbing 其实也不是很复杂,在这个博文里面,我假定一个512维度的空间中存在一个点point1 ,我的目标是随机初始化一个点,通过Hill-climbing找到这个目标点point1。
站在当前点,通过探索所有可能的走法去判断下一步哪一个走法是能够朝着目标靠近的。过程其实也不复杂,在512里面的某一个维度,走起来只有两种选择,加一个步长或者减去一个步长。如果下一步找不到比较好的选择,都不能靠近目标点,那么算法结束。
具体代码可以看下面:
__author__ = 'xingbo, it is based on inital version of sidharthgoyal'
import math
from random import *
import random
import numpy
import copy
increment = 0.05
startingPoint = numpy.random.random(512)
point1 = numpy.random.random(512)
print('target:',point1)
# point2 = [6,4]
# point3 = [5,2]
# point4 = [2,1]
def distance(coords1, coords2):
""" Calculates the euclidean distance between 2 lists of coordinates. """
# print('coords1',coords1)
# print('coords2',coords2)
return numpy.sqrt(numpy.sum((coords1 - coords2)**2))
def sumOfDistances(x, px):
d1 = distance(x, px)
return d1
def newDistance(d1, point1):
d1temp = sumOfDistances(d1, point1 )
d1 = numpy.append(d1,d1temp)
return d1
minDistance = sumOfDistances(startingPoint, point1 )
flag = True
threshold = 0.4
i = 1
lastFitness = 99
while lastFitness > threshold:
d = []
old_point = startingPoint
for index in range(512):
increment_arr = numpy.zeros(512)
increment_arr[index] = increment
newpoint = startingPoint + increment_arr
d1 = newDistance(newpoint, point1)
newpoint = startingPoint - increment_arr
d2 = newDistance(newpoint, point1)
d.append(d1)
d.append(d2)
# print(i,' ', startingPoint[:4])
d = numpy.array(d)
minimum = min(d[:,512])
if minimum < minDistance:
minindex = numpy.argmin(d[:,512])
startingPoint = d[minindex,:512]
minDistance = minimum
print('found ',i,' ', startingPoint[:4],'score',d[minindex,512])
lastFitness = d[minindex,512]
else:
flag = False
# print('new start poiny ',i)
startingPoint = startingPoint + numpy.random.random(512) * 0.1
i+=1
print('target:',point1[:10])
print('result:',startingPoint[:10])
运行结果:
found 3319 [0.54880071 0.14943408 0.16443942 0.67552999] score 0.40665809714898343
found 3320 [0.54880071 0.14943408 0.16443942 0.67552999] score 0.4050587747707967
found 3321 [0.54880071 0.14943408 0.16443942 0.67552999] score 0.4034618667141106
found 3322 [0.54880071 0.14943408 0.16443942 0.67552999] score 0.4018617552269553
found 3323 [0.54880071 0.14943408 0.16443942 0.67552999] score 0.4002772234555598
found 3324 [0.54880071 0.14943408 0.16443942 0.67552999] score 0.3986881730615146
target: [0.54391242 0.16028716 0.15798658 0.6986903 0.74380195 0.25841304
0.40675962 0.2248158 0.76813796 0.7937874 ]
result: [0.54880071 0.14943408 0.16443942 0.67552999 0.73628524 0.2546786
0.37020921 0.2397613 0.75941863 0.75618421]
import numpy as np
from scipy.optimize import minimize
xt = np.random.rand(512) * 2
print('x terget:',xt[:10])
def rosen(x):
"""The Rosenbrock function"""
return np.sqrt(np.sum((x - xt)**2))
# 初始迭代点
x0 = np.random.rand(512) * 2
res = minimize(rosen, x0, method='BFGS', jac=[],
options='disp': True)
print('res',res.x[:10])
以上是关于Hill-climbing 算法python 实现的主要内容,如果未能解决你的问题,请参考以下文章