1

Here is my code for 8 puzzle using dfs. Can someone tell how to remove duplicate elements from qList and visitedList? I want only one element representing a single state in queue and visitedList. The problem solves the 8 puzzle problem with the use of brute force dfs search. While executing it goes in infinite loop as it expands same node again and again.

from copy import deepcopy
initState=[0,1,2,3,4,5,6,7,8]
goalState=[1,4,2,3,5,0,6,7,8]
q=[]
qList=[]
visited=[]
visitedList=[]
class state:
    def __init__(self,state=[],prev=None,tree=None):
        self.state=state
        self.children=[]
        self.parent=prev
        self.tree=[]
        if tree==None:
            self.tree.append(state)
        if tree is not None:
            self.tree.extend(deepcopy(tree)

    def sameState(self,state):
        return bool(state==self.state)

    def retState(self):
        return list(self.state)

    def isPrevState(self):
        if self.parent in self.children:
            self.children.remove(self.parent)

    def moveUp(self):
        child=deepcopy(self.state)
        if child.index(0)-3>=0:
            i=child.index(0)
            temp=child[i]
            child[i]=child[i-3]
            child[i-3]=temp
            children=state(child,self.state,self.tree)
            if children.state not in qList and children.state not in visitedList:
                self.children.append(children)
        else:
            del child

    def moveRight(self):
        child=deepcopy(self.state)
        if child.index(0)%3<2:
            i=child.index(0)
            temp=child[i]
            child[i]=child[i+1]
            child[i+1]=temp
            children=state(child,self.state,self.tree)
            if children.state not in qList and children.state not in visitedList:
                self.children.append(children)
        else:
            del child

    def moveLeft(self):
        child=deepcopy(self.state)
        if child.index(0)%3>0:
            i=child.index(0)
            temp=child[i]
            child[i]=child[i-1]
            child[i-1]=temp
            children=state(child,self.state,self.tree)
            if children.state not in qList and children.state not in visitedList:
                self.children.append(children)
        else:
            del child

    def moveDown(self):
        child=deepcopy(self.state)
        if child.index(0)+3<=8:
            i=child.index(0)
            temp=child[i]
            child[i]=child[i+3]
            child[i+3]=temp
            children=state(child,self.state,self.tree)
            if children.state not in qList and children.state not in visitedList:
                self.children.append(children)
        else:
            del child

    def expandNode(self):
        self.moveLeft()
        self.moveUp()
        self.moveRight()
        self.moveDown()
        self.isPrevState()
el=state(initState)
q.append(el)
while len(q)>0:
    if list(q[len(q)-1].retState())==goalState:
        print("goal state found")
        break
    else:
        if q[len(q)-1] not in visited:
            q[len(q)-1].expandNode()
            visited.append(deepcopy(q[len(q)-1]))
            visitedList.append((q[len(q)-1].retState()))
            List=q[len(q)-1].children
            for i in range(len(List)):
                q.append(List[i])
                if List[i].retState not in qList and List[i] not in visitedList :
                    qList.append(List[i].retState())
            nq=list(dict.fromkeys(q))
            q.clear()
            q=nq 
            q.pop()
        else:
            del q[len(q)-1]
  • 1
    It seems you posted a lot more code than necessary for your problem. Could you boil it down to a minimal reproducable problem, with inputs, expected outputs and what you have so far? – Nathan Jan 21 '20 at 11:12
  • 1
    I think you need to pop before appending the children – SajanGohil Jan 21 '20 at 11:33
  • @Nathan first it initializes a state for 8 puzzle with initial state, then it adds it to the queue, after that it checks if the last element in the queue is the goal state or not. If it is the goal state then it prints success message otherwise it expands the last state in the queue and adds it to the queue and repeats this until the goal state is found. – rahilParmar Jan 23 '20 at 05:10
  • Thank you @SajanGohil, it indeed was the real problem – rahilParmar Jan 23 '20 at 05:11

0 Answers0