-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hill_Climbing_Algorithm.py
68 lines (55 loc) · 2.49 KB
/
Hill_Climbing_Algorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# This code uses Travelling Salesman Problem to solve the Hill Climbing Algorithm
import random
def random_Solution(travelling_salesman_problem):
cities = list(range(len(travelling_salesman_problem)))
solution = []
for i in range(len(travelling_salesman_problem)):
random_City = cities[random.randint(0, len(cities) - 1)]
solution.append(random_City)
cities.remove(random_City)
return solution
def route_Length(travelling_salesman_problem, solution):
route_Length = 0
for i in range(len(solution)):
route_Length += travelling_salesman_problem[solution[i - 1]][solution[i]]
return route_Length
def get_Neighbours(solution):
neighbours = []
for i in range(len(solution)):
for j in range(i + 1, len(solution)):
neighbour = solution.copy()
neighbour[i] = solution[j]
neighbour[j] = solution[i]
neighbours.append(neighbour)
return neighbours
def get_Best_Neighbour(travelling_salesman_problem, neighbours):
best_Route_Length = route_Length(travelling_salesman_problem, neighbours[0])
best_Neighbour = neighbours[0]
for neighbour in neighbours:
current_Route_Length = route_Length(travelling_salesman_problem, neighbour)
if current_Route_Length < best_Route_Length:
best_Route_Length = current_Route_Length
best_Neighbour = neighbour
return best_Neighbour, best_Route_Length
def hill_Climbing(travelling_salesman_problem):
current_Solution = random_Solution(travelling_salesman_problem)
current_Route_Length = route_Length(travelling_salesman_problem, current_Solution)
neighbours = get_Neighbours(current_Solution)
best_Neighbour, best_Neighbour_Route_Length = get_Best_Neighbour(travelling_salesman_problem, neighbours)
while best_Neighbour_Route_Length < current_Route_Length:
current_Solution = best_Neighbour
current_Route_Length = best_Neighbour_Route_Length
neighbours = get_Neighbours(current_Solution)
best_Neighbour, best_Neighbour_Route_Length = get_Best_Neighbour(travelling_salesman_problem, neighbours)
return current_Solution, current_Route_Length
def main():
travelling_salesman_problem = [
[0, 1200, 2000, 1600, 2500],
[1200, 0, 2500, 1600, 2000],
[2500, 2000, 1600, 0, 1200],
[1600, 2000, 2500, 1200, 0],
[2000, 2500, 1200, 0, 1600]
]
print(hill_Climbing(travelling_salesman_problem))
if __name__ == "__main__":
main()