-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
201 lines (162 loc) · 8.21 KB
/
main.py
File metadata and controls
201 lines (162 loc) · 8.21 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import math
import copy
# written by Mohammad Yasin Karbasian 2023-01-27
class genetic_code:
# class for saving genetic code properties and other things like the cost to reach this from the initial state
def __init__(self,current_state,current_cost,heuristic_cost, steps):
self.current_cost = current_cost
self.heuristic_cost = heuristic_cost
self.total_cost = current_cost+heuristic_cost
self.steps = steps
self.current_state = current_state
def __lt__(self, obj):
return ((self.total_cost) < (obj.total_cost))
def __gt__(self, obj):
return ((self.total_cost) > (obj.total_cost))
def __le__(self, obj):
return ((self.total_cost) <= (obj.total_cost))
def __ge__(self, obj):
return ((self.total_cost) >= (obj.total_cost))
def __eq__(self, obj):
return (self.total_cost == obj.total_cost)
def __str__(self):
return str([self.current_cost, self.heuristic_cost, self.total_cost])
class genetic_path:
# class for finding the shortest path from the initial state to the wanted state of a genetic code using the A* algorithm
def __init__(self, array):
self.initial_state = list(range(1,len(array)+1))
self.wanted_state = array
self.explored = list()
self.frontier = list()
def heuristic(self,state):
length = len(state)
sum = 0
for i in range(length):
if state[i] != self.wanted_state[i]:
sum += 1
return math.ceil(sum/2)
def solve(self):
def find_code_one(state):
for i in range(len(state)):
if state[i] == 1:
return i
def create_neighbors(gen_code):
def swap(state, index1, index2):
state = copy.deepcopy(state)
temp = state[index1]
state[index1] = state[index2]
state[index2] = temp
return state
def check(state):
for i in range(len(self.frontier)):
if state == self.frontier[i].current_state:
return 0
for i in range(len(self.explored)):
if state == self.explored[i].current_state:
return 0
return 1
state = gen_code.current_state
step = gen_code.steps + 1
cost = gen_code.current_cost + 1
one_index = find_code_one(state)
if (one_index+1)%4 == 0:
if 0 <= one_index+4 <= len(state)-1:
new_state = swap(state,one_index,one_index+4)
if check(new_state):
new_code = genetic_code(new_state,cost,self.heuristic(new_state),step)
self.frontier.append(new_code)
if 0 <= one_index-4 <= len(state)-1:
new_state = swap(state,one_index,one_index-4)
if check(new_state):
new_code = genetic_code(new_state,cost,self.heuristic(new_state),step)
self.frontier.append(new_code)
if 0 <= one_index-3 <= len(state)-1:
new_state = swap(state,one_index,one_index-3)
if check(new_state):
new_code = genetic_code(new_state,cost,self.heuristic(new_state),step)
self.frontier.append(new_code)
if 0 <= one_index-1 <= len(state)-1:
new_state = swap(state,one_index,one_index-1)
if check(new_state):
new_code = genetic_code(new_state,cost,self.heuristic(new_state),step)
self.frontier.append(new_code)
elif (one_index+1)%4 == 1:
if 0 <= one_index+4 <= len(state)-1:
new_state = swap(state,one_index,one_index+4)
if check(new_state):
new_code = genetic_code(new_state,cost,self.heuristic(new_state),step)
self.frontier.append(new_code)
if 0 <= one_index-4 <= len(state)-1:
new_state = swap(state,one_index,one_index-4)
if check(new_state):
new_code = genetic_code(new_state,cost,self.heuristic(new_state),step)
self.frontier.append(new_code)
if 0 <= one_index+3 <= len(state)-1:
new_state = swap(state,one_index,one_index+3)
if check(new_state):
new_code = genetic_code(new_state,cost,self.heuristic(new_state),step)
self.frontier.append(new_code)
if 0 <= one_index+1 <= len(state)-1:
new_state = swap(state,one_index,one_index+1)
if check(new_state):
new_code = genetic_code(new_state,cost,self.heuristic(new_state),step)
self.frontier.append(new_code)
elif (one_index+1)%4 == 2:
if 0 <= one_index+4 <= len(state)-1:
new_state = swap(state,one_index,one_index+4)
if check(new_state):
new_code = genetic_code(new_state,cost,self.heuristic(new_state),step)
self.frontier.append(new_code)
if 0 <= one_index-4 <= len(state)-1:
new_state = swap(state,one_index,one_index-4)
if check(new_state):
new_code = genetic_code(new_state,cost,self.heuristic(new_state),step)
self.frontier.append(new_code)
if 0 <= one_index+1 <= len(state)-1:
new_state = swap(state,one_index,one_index+1)
if check(new_state):
new_code = genetic_code(new_state,cost,self.heuristic(new_state),step)
self.frontier.append(new_code)
if 0 <= one_index-1 <= len(state)-1:
new_state = swap(state,one_index,one_index-1)
if check(new_state):
new_code = genetic_code(new_state,cost,self.heuristic(new_state),step)
self.frontier.append(new_code)
elif (one_index+1)%4 == 3:
if 0 <= one_index+4 <= len(state)-1:
new_state = swap(state,one_index,one_index+4)
if check(new_state):
new_code = genetic_code(new_state,cost,self.heuristic(new_state),step)
self.frontier.append(new_code)
if 0 <= one_index-4 <= len(state)-1:
new_state = swap(state,one_index,one_index-4)
if check(new_state):
new_code = genetic_code(new_state,cost,self.heuristic(new_state),step)
self.frontier.append(new_code)
if 0 <= one_index+1 <= len(state)-1:
new_state = swap(state,one_index,one_index+1)
if check(new_state):
new_code = genetic_code(new_state,cost,self.heuristic(new_state),step)
self.frontier.append(new_code)
if 0 <= one_index-1 <= len(state)-1:
new_state = swap(state,one_index,one_index-1)
if check(new_state):
new_code = genetic_code(new_state,cost,self.heuristic(new_state),step)
self.frontier.append(new_code)
estimated_cost = self.heuristic(self.initial_state)
root = genetic_code(self.initial_state,0,estimated_cost,0)
self.frontier.append(root)
while 1:
if len(self.frontier) == 0:
return -1
mini = sorted(self.frontier)[0]
if mini.current_state == self.wanted_state:
return mini.steps
self.frontier.remove(mini)
self.explored.append(mini)
create_neighbors(mini)
file = open('./Samples/input_3.txt')
string = file.readlines()
wanted = list(map(int,string[1].strip().split()))
solver = genetic_path(wanted)
print(solver.solve())