forked from RodrigoAGM/SI404-artificial-intelligence
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhill_climbing.py
More file actions
108 lines (80 loc) · 2.67 KB
/
hill_climbing.py
File metadata and controls
108 lines (80 loc) · 2.67 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
###################################################################
#Hill climbing algorithm applied to solve a simple 3x3 sort puzzle#
###################################################################
import random
solved = "1238*4765"
def getHeuristic(first:str, second:str):
counter = 0
index = 0
for c in first:
if c != second[index] and c != "*":
counter = counter+1
index = index+1
return counter
def getNMovements(pos:int):
if pos == 4:
return 4
elif pos % 2 == 0:
return 2
else:
return 3
def moveCharacter(x:str, pos:int, movement:str):
lst = list(x)
if movement == "U":
lst[pos], lst[pos-3] = lst[pos-3], lst[pos]
elif movement == "D":
lst[pos], lst[pos+3] = lst[pos+3], lst[pos]
elif movement == "R":
lst[pos], lst[pos+1] = lst[pos+1], lst[pos]
else:
lst[pos], lst[pos-1] = lst[pos-1], lst[pos]
return "".join(lst)
def getPossibleMovements(pos:int):
movements = []
if pos > 2:
movements.append("U")
if pos < 6:
movements.append("D")
if pos % 3 != 0:
movements.append("L")
if (pos+1) % 3 != 0:
movements.append("R")
return movements
def hillClimbing():
print("Ingrese el estado inicial: ")
current = str(input())
flag = False
while True:
if current == solved:
print("SOLVED")
print(current)
break
print(current)
bestSet = current
bestHeuristic = getHeuristic(current, solved)
pos = current.index("*")
movements = getPossibleMovements(pos)
print(movements)
for mov in movements:
result = moveCharacter(current, pos, mov)
resultHe = getHeuristic(result, solved)
if resultHe <= bestHeuristic:
if bestHeuristic == resultHe:
#Probability for movements with the same heuristic
if random.randint(1,50) < 25:
bestSet = result
bestHeuristic = resultHe
#We found at least 1 better set
flag = True
else:
bestSet = result
bestHeuristic = resultHe
#We found at least 1 better set
flag = True
if flag:
flag = False
current = bestSet
else:
print("No solution found with this initial set")
break
hillClimbing()