|
| 1 | +import numpy as np |
| 2 | +import argparse |
| 3 | + |
| 4 | +##### To read the user input ##### |
| 5 | + |
| 6 | +def read(configuration): |
| 7 | + initial_state = [] |
| 8 | + data = configuration.split(",") |
| 9 | + for element in data: |
| 10 | + initial_state.append(int(element)) |
| 11 | + return np.reshape(initial_state,(3,3)) |
| 12 | + |
| 13 | + |
| 14 | +##### Blank Tile Position ##### |
| 15 | +def zeropos(Initial_state): |
| 16 | + index = np.argwhere(Initial_state == 0) #we get the position of zero in initial state |
| 17 | + return index |
| 18 | + |
| 19 | + |
| 20 | +##### Action set : Move UP ##### |
| 21 | +def move_up(Curr_state): |
| 22 | + A = np.copy(Curr_state) |
| 23 | + loc = zeropos(A) #moving the slider up if possible |
| 24 | + i = loc[:,0] |
| 25 | + j= loc[:,1] |
| 26 | + if i-1<0: |
| 27 | + status = False |
| 28 | + return A, status |
| 29 | + else: |
| 30 | + #print("UP") |
| 31 | + A[i,j]= A[i-1, j] |
| 32 | + A[i-1, j] = 0 |
| 33 | + status = True |
| 34 | + #print(A) |
| 35 | + return A, status |
| 36 | + |
| 37 | +##### Action set : Move Down ##### |
| 38 | +def move_down(Curr_state): |
| 39 | + A = np.copy(Curr_state) |
| 40 | + loc = zeropos(A) |
| 41 | + |
| 42 | + i = loc[:,0] |
| 43 | + j= loc[:,1] #moving the slider down if possible |
| 44 | + if i+1>2: |
| 45 | + status = False |
| 46 | + return A, status |
| 47 | + else: |
| 48 | + #print("DOWN") |
| 49 | + A[i,j]= A[i+1, j] |
| 50 | + A[i+1, j] = 0 |
| 51 | + status = True |
| 52 | + #print(A) |
| 53 | + return A, status |
| 54 | + |
| 55 | +##### Action set : Move Left ##### |
| 56 | +def move_left(Curr_state): |
| 57 | + A = np.copy(Curr_state) |
| 58 | + loc = zeropos(A) |
| 59 | + |
| 60 | + i = loc[:,0] |
| 61 | + j= loc[:,1] #moving the slider left if possible |
| 62 | + if j-1<0: |
| 63 | + status = False |
| 64 | + return A, status |
| 65 | + else: |
| 66 | + #print("Left") |
| 67 | + A[i,j]= A[i, j-1] |
| 68 | + A[i, j-1] = 0 |
| 69 | + status = True |
| 70 | + #print(A) |
| 71 | + return A, status |
| 72 | + |
| 73 | +##### Action set : Move Right ##### |
| 74 | +def move_right(Curr_state): |
| 75 | + A = np.copy(Curr_state) |
| 76 | + loc = zeropos(A) |
| 77 | + |
| 78 | + i = loc[:,0] |
| 79 | + j= loc[:,1] #moving the slider right if possible |
| 80 | + if j+1>2: |
| 81 | + status = False |
| 82 | + return A, status |
| 83 | + else: |
| 84 | + #print("Right") |
| 85 | + A[i,j]= A[i, j+1] |
| 86 | + A[i, j+1] = 0 |
| 87 | + status = True |
| 88 | + #print(A) |
| 89 | + return A, status |
| 90 | + |
| 91 | +##### A logic to compare states ##### |
| 92 | +def set_conversion(A): |
| 93 | + i = j = 0 |
| 94 | + for iter1 in A: |
| 95 | + for iter2 in iter1: |
| 96 | + j+=iter2*(10**i) |
| 97 | + i+=1 |
| 98 | + return j |
| 99 | + |
| 100 | + |
| 101 | +##### To see if the current state is already existing ##### |
| 102 | +def check_if_visited(Current_state, exist_states): |
| 103 | + a = set_conversion(Current_state) |
| 104 | + return a in exist_states |
| 105 | + |
| 106 | + |
| 107 | +##### To see if the current state mathes with goal state ##### |
| 108 | +def check_goal(A, goal_state): |
| 109 | + status = np.array_equal(A,goal_state) |
| 110 | + #print(status) |
| 111 | + return status |
| 112 | + |
| 113 | + |
| 114 | +##### Initializing a default Goal state ##### |
| 115 | +goal_state = np.array([[1,2,3],[4,5,6],[7,8,0]]) |
| 116 | + |
| 117 | +##### Innitializing a state list ##### |
| 118 | +state_list =[] |
| 119 | +exist_states = set([]) |
| 120 | + |
| 121 | +##### To get user input of initial state from Terminal ##### |
| 122 | +parser = argparse.ArgumentParser() |
| 123 | +parser.add_argument('Initial_state') |
| 124 | +args = parser.parse_args() |
| 125 | +initial_state = read(args.Initial_state) |
| 126 | +state_list.append(initial_state) |
| 127 | +exist_states.add(set_conversion(initial_state)) |
| 128 | + |
| 129 | + |
| 130 | +##### Initializing container lists for bactracking and calculating cost ##### |
| 131 | +child_state_index = [] |
| 132 | +child_state_index.append(0) |
| 133 | +temp_index = [] ### A temporary list for copying new child states indices to child state index |
| 134 | +cost = 0 |
| 135 | +count = 0 |
| 136 | +child_state_number = 0 |
| 137 | + |
| 138 | +state_info = [] |
| 139 | +reached = False |
| 140 | + |
| 141 | + |
| 142 | +##### An iterative loop to perform an action set on parent states to create child states ##### |
| 143 | +##### Also checks if any state that was created is possibly a goal state ##### |
| 144 | + |
| 145 | +while len(child_state_index)>0: |
| 146 | + |
| 147 | + temp_index = [] |
| 148 | + for i in child_state_index: |
| 149 | + |
| 150 | + new_state, status = move_up(state_list[i]) |
| 151 | + if status == True and not check_if_visited(new_state, exist_states): |
| 152 | + child_state_number += 1 |
| 153 | + temp_index.append(child_state_number) |
| 154 | + state_list.append(new_state) |
| 155 | + temp_node_info = np.array([child_state_number,i,cost]) |
| 156 | + state_info.append(temp_node_info) |
| 157 | + exist_states.add(set_conversion(new_state)) |
| 158 | + |
| 159 | + if check_goal(new_state,goal_state): |
| 160 | + reached = True |
| 161 | + goal_state_index = child_state_number |
| 162 | + break |
| 163 | + |
| 164 | + new_state, status = move_down(state_list[i]) |
| 165 | + if status == True and not check_if_visited(new_state, exist_states): |
| 166 | + child_state_number += 1 |
| 167 | + temp_index.append(child_state_number) |
| 168 | + state_list.append(new_state) |
| 169 | + temp_node_info = np.array([child_state_number,i,cost]) |
| 170 | + state_info.append(temp_node_info) |
| 171 | + exist_states.add(set_conversion(new_state)) |
| 172 | + |
| 173 | + if check_goal(new_state,goal_state): |
| 174 | + reached = True |
| 175 | + goal_state_index = child_state_number |
| 176 | + break |
| 177 | + |
| 178 | + new_state, status = move_right(state_list[i]) |
| 179 | + if status == True and not check_if_visited(new_state, exist_states): |
| 180 | + child_state_number += 1 |
| 181 | + temp_index.append(child_state_number) |
| 182 | + state_list.append(new_state) |
| 183 | + temp_node_info = np.array([child_state_number,i,cost]) |
| 184 | + state_info.append(temp_node_info) |
| 185 | + exist_states.add(set_conversion(new_state)) |
| 186 | + |
| 187 | + if check_goal(new_state,goal_state): |
| 188 | + reached = True |
| 189 | + goal_state_index = child_state_number |
| 190 | + break |
| 191 | + |
| 192 | + new_state, status = move_left(state_list[i]) |
| 193 | + if status == True and not check_if_visited(new_state, exist_states): |
| 194 | + child_state_number += 1 |
| 195 | + temp_index.append(child_state_number) |
| 196 | + state_list.append(new_state) |
| 197 | + temp_node_info = np.array([child_state_number,i,cost]) |
| 198 | + state_info.append(temp_node_info) |
| 199 | + exist_states.add(set_conversion(new_state)) |
| 200 | + |
| 201 | + if check_goal(new_state,goal_state): |
| 202 | + reached = True |
| 203 | + goal_state_index = child_state_number |
| 204 | + break |
| 205 | + |
| 206 | +##### When the goal state is reached, we need to back track to find the shortest path #### |
| 207 | + if reached == True: |
| 208 | + ### Generating the path to reach the goal state ### |
| 209 | + generate_path = [] |
| 210 | + gl_temp = goal_state_index-1 |
| 211 | + generate_path.append(state_list[goal_state_index]) |
| 212 | + print(generate_path) |
| 213 | + |
| 214 | + #writing the output(generated path) to a file |
| 215 | + while gl_temp>0: |
| 216 | + x = state_info[gl_temp] |
| 217 | + gl_temp = x[1] |
| 218 | + generate_path.append(state_list[gl_temp]) |
| 219 | + print('Goal state reached =',reached) |
| 220 | + generate_path.reverse() |
| 221 | + generate_path_t = np.asarray(generate_path) |
| 222 | + |
| 223 | + |
| 224 | + with open('nodePath.txt', 'w') as node_path_file: |
| 225 | + for i in generate_path_t: |
| 226 | + t = np.empty([1,9]) |
| 227 | + count = 0 |
| 228 | + for j in i.T: |
| 229 | + for k in j: |
| 230 | + t[0,count] = k |
| 231 | + count+=1 |
| 232 | + np.savetxt(node_path_file,t,delimiter='\t') |
| 233 | + break |
| 234 | + |
| 235 | + child_state_index = temp_index |
| 236 | + cost+=10 |
| 237 | + |
| 238 | +state_info_t = np.asarray(state_info) |
| 239 | + |
| 240 | +#writing the output(Information of all states CHILD | PARENT | COST2COME) to the file |
| 241 | +with open('NodesInfo.txt', 'w') as node_info_file: |
| 242 | + for i in state_info_t: |
| 243 | + t = np.empty([1,3]) |
| 244 | + t[0,:]=i |
| 245 | + np.savetxt(node_info_file,t,delimiter='\t') |
| 246 | + |
| 247 | +state_list_t = np.asarray(state_list) |
| 248 | + |
| 249 | +#writing the output(All states possible) to the file |
| 250 | +with open('Nodes.txt', 'w') as node_list_file: |
| 251 | + for i in state_list_t: |
| 252 | + t = np.empty([1,9]) |
| 253 | + count = 0 |
| 254 | + for j in i.T: |
| 255 | + for k in j: |
| 256 | + t[0,count] = k |
| 257 | + count+=1 |
| 258 | + np.savetxt(node_list_file,t,delimiter='\t') |
| 259 | + |
| 260 | +# If the goal node is not reached |
| 261 | +if reached==False: |
| 262 | + print('Goal state cannot be achieved') |
0 commit comments