Skip to content

Commit 9769b31

Browse files
authored
Merge pull request #71 from pratap360/main
Added Hill_climbing_algo.py
2 parents 5151293 + afe3dd6 commit 9769b31

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

Hill_climbing_algo.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import math
2+
3+
increment = 0.1
4+
startingPoint = [1, 1]
5+
point1 = [1,5]
6+
point2 = [6,4]
7+
point3 = [5,2]
8+
point4 = [2,1]
9+
def distance(x1, y1, x2, y2):
10+
dist = math.pow(x2-x1, 2) + math.pow(y2-y1, 2)
11+
return dist
12+
def sumOfDistances(x1, y1, px1, py1, px2, py2, px3, py3, px4, py4):
13+
d1 = distance(x1, y1, px1, py1)
14+
d2 = distance(x1, y1, px2, py2)
15+
d3 = distance(x1, y1, px3, py3)
16+
d4 = distance(x1, y1, px4, py4)
17+
return d1 + d2 + d3 + d4
18+
def newDistance(x1, y1, point1, point2, point3, point4):
19+
d1 = [x1, y1]
20+
d1temp = sumOfDistances(x1, y1, point1[0],point1[1], point2[0],point2[1],
21+
point3[0],point3[1], point4[0],point4[1] )
22+
d1.append(d1temp)
23+
return d1
24+
minDistance = sumOfDistances(startingPoint[0], startingPoint[1],point1[0],point1[1], point2[0],point2[1],point3[0],point3[1], point4[0],point4[1] )
25+
flag = True
26+
def newPoints(minimum, d1, d2, d3, d4):
27+
if d1[2] == minimum:
28+
return [d1[0], d1[1]]
29+
elif d2[2] == minimum:
30+
return [d2[0], d2[1]]
31+
elif d3[2] == minimum:
32+
return [d3[0], d3[1]]
33+
elif d4[2] == minimum:
34+
return [d4[0], d4[1]]
35+
36+
i = 1
37+
while flag:
38+
d1 = newDistance(startingPoint[0]+increment, startingPoint[1], point1, point2,point3,point4)
39+
d2 = newDistance(startingPoint[0]-increment, startingPoint[1], point1, point2,point3, point4)
40+
d3 = newDistance(startingPoint[0], startingPoint[1]+increment, point1, point2,point3, point4)
41+
d4 = newDistance(startingPoint[0], startingPoint[1]-increment, point1, point2,point3, point4)
42+
print (i,' ', round(startingPoint[0], 2), round(startingPoint[1], 2))
43+
minimum = min(d1[2], d2[2], d3[2], d4[2])
44+
if minimum < minDistance:
45+
startingPoint = newPoints(minimum, d1, d2, d3, d4)
46+
minDistance = minimum
47+
#print i,' ', round(startingPoint[0], 2), round(startingPoint[1], 2)
48+
i+=1
49+
else:
50+
flag = False
51+
52+
53+
54+
# output here
55+
# 1 1 1
56+
# 2 1.1 1
57+
# 3 1.2 1
58+
# 4 1.3 1
59+
# 5 1.4 1
60+
# 6 1.5 1
61+
# 7 1.6 1
62+
# 8 1.6 1.1
63+
# 9 1.7 1.1
64+
# 10 1.7 1.2
65+
# 11 1.7 1.3
66+
# 12 1.8 1.3
67+
# 13 1.8 1.4
68+
# 14 1.9 1.4
69+
# 15 2.0 1.4
70+
# 16 2.0 1.5
71+
# 17 2.1 1.5
72+
# 18 2.1 1.6
73+
# 19 2.2 1.6
74+
# 20 2.2 1.7
75+
# 21 2.3 1.7
76+
# 22 2.3 1.8
77+
# 23 2.3 1.9
78+
# 24 2.4 1.9
79+
# 25 2.5 1.9
80+
# 26 2.5 2.0
81+
# 27 2.6 2.0
82+
# 28 2.6 2.1
83+
# 29 2.7 2.1
84+
# 30 2.7 2.2
85+
# 31 2.8 2.2
86+
# 32 2.8 2.3
87+
# 33 2.9 2.3
88+
# 34 2.9 2.4
89+
# 35 3.0 2.4
90+
# 36 3.0 2.5
91+
# 37 3.1 2.5
92+
# 38 3.1 2.6
93+
# 39 3.2 2.6
94+
# 40 3.2 2.7
95+
# 41 3.2 2.8
96+
# 42 3.3 2.8
97+
# 43 3.4 2.8
98+
# 44 3.4 2.9
99+
# 45 3.5 2.9
100+
# 46 3.5 3.0

0 commit comments

Comments
 (0)