Hill-climbing 算法python 实现

Posted MrCharles

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hill-climbing 算法python 实现相关的知识,希望对你有一定的参考价值。

__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]

以上是关于Hill-climbing 算法python 实现的主要内容,如果未能解决你的问题,请参考以下文章

Hill-climbing 算法python 实现

MIP启发式求解:局部搜索 (local search)

BFS算法模板(python实现)

python实现常见算法

十大经典算法 Python实现

深度解读 python 实现 dbscan算法