The __str__ method fails to execute and an attribute error occurs.

Asked 2 weeks ago, Updated 2 weeks ago, 1 views

# -*- coding: utf-8 -*-
"""
Created on Tue Sep 28 20:54:55 2021

@author: Salieli
"""

class State:
    def __init__(self, board, goal, moves=0):
        self.board = board
        self.moves = moves
        self.goal = goal

        # Exchange i1 and i2 to return a new state
        def get_new_board(self, i1, i2, moves):
            new_board = self.board[:]
            new_board[i1], new_board[i2] = new_board[i2], new_board[i1]

            return State(new_board, self.goal, moves)

        # To expand and save a child node to a list and return it
        def expand(self, moves):
            result = []
            i = self.board.index(0)
            if not i in [0,1,2]:# up
                    result.append(self.get_new_board(i,i-3,moves))          
                    if not i in [0,3,6]:# LEFT
                       result.append(self.get_new_board(i,i-1,moves))
                    if not i in [2,5,8]:# RIGHT 
                       result.append(self.get_new_board(i,i+1,moves))
                    if not i in [6,7,8]:# DOWN
                       result.append(self.get_new_board(i,i+3,moves))
            return result


        def __str__(self):
            return str(self.board[:3])+"\n"+\
                str(self.board[3:6])+"\n"+\
                str(self.board[6:])+"\n"+\
                "-----------------"

        def __eq__(self,other):
               return self.board == other.board

# Initial state
puzzle = [2,8,3,
          1,0,4,
          7,6,5]

# Target Status
goal = [1,2,3,
        4,5,6,
        7,8,0]

# # open list
open_queue = [] 
open_queue.append(State(puzzle, goal)) 

# # closed list
closed_queue = []
moves = 0;

while len(open_queue) !=0:
    current = open_queue.pop(0)
    print(current)    
    if current.board == goal:
        print ("Explore Success")
        break    
    moves = current.moves+1             
    closed_queue.append(current)

    for state in current.expand(moves): 
        if (state in closed_queue) or (state in open_queue):
            continue
        else:
            open_queue.append(State)
            print ("Discovery Failed")

Code to solve 8-puzzle with DFS method and print out the process. The code itself was taken from the university lecture materials I think it was executed in the same environment as the professor who was running normally

But in print (current) Current = State (puzzle, goal), but the list is not printed through str The address of the state is just printed out. (str does not seem to run normally)

Also, the error 'State' object has no attribute 'expand' is printed, and I looked for the cause of the error It didn't seem to have much to do with the code above. You and the rest of us have to make sure that it' I checked, but why isn't it working?

python

2022-09-20 14:56

1 Answers

Python is indent grammar.

Check the indent, or indent, carefully.

Try it with the lower chord.

class State:
    def __init__(self, board, goal, moves=0):
        self.board = board
        self.moves = moves
        self.goal = goal

    # Exchange i1 and i2 to return a new state
    def get_new_board(self, i1, i2, moves):
        new_board = self.board[:]
        new_board[i1], new_board[i2] = new_board[i2], new_board[i1]

        return State(new_board, self.goal, moves)

    # To expand and save a child node to a list and return it
    def expand(self, moves):
        result = []
        i = self.board.index(0)
        if not i in [0,1,2]:# up
            result.append(self.get_new_board(i,i-3,moves))
            if not i in [0,3,6]:# LEFT
               result.append(self.get_new_board(i,i-1,moves))
            if not i in [2,5,8]:# RIGHT 
               result.append(self.get_new_board(i,i+1,moves))
            if not i in [6,7,8]:# DOWN
               result.append(self.get_new_board(i,i+3,moves))
        return result


    def __str__(self):
        return str(self.board[:3])+"\n"+\
            str(self.board[3:6])+"\n"+\
            str(self.board[6:])+"\n"+\
            "-----------------"

    def __eq__(self,other):
        return self.board == other.board


2022-09-20 14:56

If you have any answers or tips


© 2022 pinfo. All rights reserved.