AttributeError: ‘SearchNode’ object has no attribute ‘depth’

I have this code:

tpi1.py:

from tree_search import *

class MyNode(SearchNode):

    def __init__(self,state,parent,depth=0, cost=0, heuristic=0):
        super().__init__(state,parent)
        self.depth = depth
        self.cost = cost
        self.heuristic = heuristic
        self.eval = cost + heuristic

class MyTree(SearchTree):

    def __init__(self,problem, strategy='breadth',maxsize=None):
        super().__init__(problem,strategy)   

    def search2(self):
        while self.open_nodes:
            node = self.open_nodes.pop(0)
            if self.problem.goal_test(node.state):
                self.solution = node
                self.terminals = len(self.open_nodes) + 1
                return self.get_path(node)
            self.non_terminals += 1
            lnewnodes = []
            for a in self.problem.domain.actions(node.state):
                newstate = self.problem.domain.result(node.state, a)
                if newstate not in self.get_path(node):
                    new_depth = node.depth + 1
                    new_cost = node.cost + self.problem.domain.cost(node.state, a)
                    new_heuristic = self.problem.domain.heuristic(newstate, self.problem.goal)
                    newnode = MyNode(newstate, node, new_depth, new_cost, new_heuristic)
                    lnewnodes.append(newnode)
            self.add_to_open(lnewnodes)

        return None

tree_search.py:

class SearchProblem:
    def __init__(self, domain, initial, goal):
        self.domain = domain
        self.initial = initial
        self.goal = goal
    def goal_test(self, state):
        return self.domain.satisfies(state,self.goal)


class SearchNode:
    def __init__(self,state,parent): 
        self.state = state
        self.parent = parent
    def __str__(self):
        return "no(" + str(self.state) + "," + str(self.parent) + ")"
    def __repr__(self):
        return str(self)


class SearchTree:

    
    def __init__(self,problem, strategy='breadth'): 
        self.problem = problem
        root = SearchNode(problem.initial, None)
        self.open_nodes = [root]
        self.strategy = strategy
        self.solution = None
        self.non_terminals = 0

    
    def get_path(self,node):
        if node.parent == None:
            return [node.state]
        path = self.get_path(node.parent)
        path += [node.state]
        return(path)

    
    def search(self):
        while self.open_nodes != []:
            node = self.open_nodes.pop(0)
            if self.problem.goal_test(node.state):
                self.solution = node
                self.terminals = len(self.open_nodes)+1
                return self.get_path(node)
            self.non_terminals += 1
            lnewnodes = []
            for a in self.problem.domain.actions(node.state):
                newstate = self.problem.domain.result(node.state,a)
                if newstate not in self.get_path(node):
                    newnode = SearchNode(newstate,node)
                    lnewnodes.append(newnode)
            self.add_to_open(lnewnodes)
        return None

    
    def add_to_open(self,lnewnodes):
        if self.strategy == 'breadth':
            self.open_nodes.extend(lnewnodes)
        elif self.strategy == 'depth':
            self.open_nodes[:0] = lnewnodes
        elif self.strategy == 'A*':
            self.astar_add_to_open(lnewnodes)

I need to create the function search2(self) in tpi1.py but it keeps returning

AttributeError: 'SearchNode' object has no attribute 'depth'

in

new_depth = node.depth + 1

It seems that it is trying to access the attribute ‘depth’ in ‘SearchNode’ but I need to access it in ‘MyNode’.

Is there a way to access the attribute ‘depth’ in ‘MyNode’ without having to add that same attribute to ‘SearchNode’?

Thanks!

  • It appears your nodes are actually of type SearchNode, not MyNode.

    – 

  • So how do I use nodes of type MyNode?

    – 

  • You haven’t shown us how the nodes are created, so I can’t answer that. The code just suddenly refers to self.open_nodes without ever showing how that is created.

    – 




  • Presumably the nodes are created in SearchTree.__init__, which you did not show.

    – 

Leave a Comment