From 444689a50c5d6769a5d228c60c8147d90d47ddd6 Mon Sep 17 00:00:00 2001 From: MOHAMMAD ADNAN <112572555+Adnan1114@users.noreply.github.com> Date: Tue, 18 Apr 2023 15:27:49 +0530 Subject: [PATCH] Add files via upload --- NxNgrphColor.py | 181 ++++++++++++++++++++++++++++++++++++++++++++++++ astaralg.py | 120 ++++++++++++++++++++++++++++++++ bfsnpuzzle.py | 136 ++++++++++++++++++++++++++++++++++++ 3 files changed, 437 insertions(+) create mode 100644 NxNgrphColor.py create mode 100644 astaralg.py create mode 100644 bfsnpuzzle.py diff --git a/NxNgrphColor.py b/NxNgrphColor.py new file mode 100644 index 0000000..c79aa27 --- /dev/null +++ b/NxNgrphColor.py @@ -0,0 +1,181 @@ +n=int(input()) +adjDic={} +for i in range(n*n): + if (i%n==0): + adjDic[i]=list() + adjDic[i].append(i+1) + if (i+n<(n*n)): + adjDic[i].append(i+n) + if (i-n>=0): + adjDic[i].append(i-n) + if (i-(n-1)>=0): + adjDic[i].append(i-(n-1)) + if (i+(n+1)0): + adjDic[i].append(i-n) + if (i-(n+1)>0): + adjDic[i].append(i-(n+1)) + if (i+(n-1)0): + if adjDic.get(i): + adjDic[i].append(i-n) + else: + adjDic[i]=list() + adjDic[i].append(i-n) + adjDic[i].append(i+1) + adjDic[i].append(i-1) + if (i-(n-1)>0): + adjDic[i].append(i-(n-1)) + if (i+(n+1)=0): + adjDic[i].append(i-(n+1)) + if (i+(n-1)= 0 and x2 < len(self.data) and y2 >= 0 and y2 < len(self.data): + temp_puz = [] + temp_puz = self.copy(puz) + temp = temp_puz[x2][y2] + temp_puz[x2][y2] = temp_puz[x1][y1] + temp_puz[x1][y1] = temp + return temp_puz + else: + return None + + def copy(self, root): + # copy function to create a similar matrix of the given node + temp = [] + for i in root: + t = [] + for j in i: + t.append(j) + temp.append(t) + return temp + + def find(self, puz, x): + # Specifically used to find the position of the blank space + for i in range(0, len(self.data)): + for j in range(0, len(self.data)): + if puz[i][j] == x: + return i, j + + +class Puzzle: + def __init__(self, size): + # Initialize the puzzle size by the the specified size,open and closed lists to empty + self.n = size + self.open = [] + self.closed = [] + + def accept(self): + # Accepts the puzzle from the user + puz = [] + for i in range(0, self.n): + temp = input().split(" ") + puz.append(temp) + return puz + + def f(self, start, goal): + # Heuristic function to calculate Heuristic value f(x) = h(x) + g(x) + return self.h(start.data, goal) + start.level + + def h(self, start, goal): + # Calculates the difference between the given puzzles + temp = 0 + for i in range(0, self.n): + for j in range(0, self.n): + if start[i][j] != goal[i][j] and start[i][j] != '_': + temp += 1 + return temp + + def process(self): + # Accept Start and Goal Puzzle state + print("enter the start state matrix \n") + start = self.accept() + print("enter the goal state matrix \n") + goal = self.accept() + start = Node(start, 0, 0) + start.fval = self.f(start, goal) + # put the start node in the open list + self.open.append(start) + print("\n\n") + while True: + cur = self.open[0] + print("==================================================\n") + for i in cur.data: + for j in i: + print(j, end=" ") + print("") + # if the difference between current and goal node is 0 we have reached the goal node + if (self.h(cur.data, goal) == 0): + break + for i in cur.generate_child(): + i.fval = self.f(i, goal) + self.open.append(i) + self.closed.append(cur) + del self.open[0] + # sort the open list based on f value + self.open.sort(key=lambda x: x.fval, reverse=False) + + +puz = Puzzle(int(input("Enter the size:"))) +puz.process() diff --git a/bfsnpuzzle.py b/bfsnpuzzle.py new file mode 100644 index 0000000..1ff7cff --- /dev/null +++ b/bfsnpuzzle.py @@ -0,0 +1,136 @@ +import math +import collections + +class Node: + def __init__(self, puzzle, parent=None, action=None): + self.puzzle = puzzle + self.parent = parent + self.action = action + + def state(self): + #print(self.puzzle.board) + return self.puzzle.board + + def path(self): + node, p = self, [] + while node: + p.append(node.state()) + node = node.parent + #print(p) + return p + #print(reversed(p)) + + def solved(self): + return self.puzzle.solved() + + def actions(self): + return self.puzzle.actions() + +class Environment: + def __init__(self, board): + self.board = board + self.width = int(math.sqrt(len(self.board))) + + def copy(self): + board = [] + for element in self.board: + board.append(element) + return board + + def moved(self, (r,c),(i,j)): + copy = self.copy() + copy[r * self.width + c], copy[i * self.width + j] = copy[i * self.width + j], copy[r * self.width + c] + return copy + + def goal(self): + goal = [] + for i in range (1, len(self.board)): + goal.append(i) + goal.append(-1) + return str(goal) + + def solved(self): + return str(self.board) == self.goal() + + def actions(self): + block_pos = self.board.index(-1) + block_r = block_pos / self.width + block_c = block_pos % self.width + moves = [] + + directions = {"R":(block_r, block_c + 1), + "L":(block_r, block_c - 1), + "T":(block_r - 1, block_c), + "D":(block_r + 1, block_c)} + for action, (i,j) in directions.items(): + if i >= 0 and j >= 0 and i < self.width and j < self.width: + move = self.moved((block_r, block_c),(i,j)),action + moves.append(move) + return moves + + + def showboard(self): + newnode = Node(self.board) + print(newnode.state()[0]) + return self.board + +class Agent: + def __init__(self, start): + self.start = start + + def solver(self): + board = self.start.board + fringe = collections.deque([Node(self.start)]) + #print(Node(self.start).state()) + #print(fringe[0].puzzle) + visited = set() + visited.add(str(fringe[0].state())) + #print(visited) + '''k = 0''' + while fringe: + '''k = k + 1 + if k > 8: + break''' + node = fringe.pop() + if node.solved(): + return node.path() + for move, action in node.actions(): + child = Node(Environment(move), node, action) + #print("Parent", node.state()) + #print(action, child.state()) + if str(child.state()) not in visited: + #print("added to", child.state()) + fringe.appendleft(child) + visited.add(str(child.state())) + + + + +##input to be take here.... + +#board = [-1,2,3,1,4,5,7,8,6] +i = int(input()) +for k in range(0, i): + n = int(input()) + board = [] + for i in range(0, n): + temp = [] + temp = [int(x) for x in raw_input().split()] + #print(temp) + for t in temp: + board.append(t) + #print(str(board)) + p = Environment(board) + s = Agent(p) + solved_p = s.solver() + #print(solved_p) + + + + for path in reversed(solved_p): + soln = '' + for elem in path: + soln += str(elem)+' ' + print(soln) +#print(p.solved()) +#print(p.actions())