Можно ли иметь индивидуальную собственную ценность для каждого узла? - PullRequest
0 голосов
/ 19 марта 2020

Я делаю решатель n-головоломки на Python. У меня есть этот класс

class Node():
def __init__(self, n_size, m_size, current_state, goal_state, choosen_heuristic, parent):
    self.n_size = n_size
    self.m_size = m_size
    self.dimension = self.n_size * self.m_size
    self.current_state = current_state                              # (each new node must have a different)
    self.goal_state = goal_state                                    # (each node must have the same)
    self.choosen_heuristic = choosen_heuristic                     
    self.parent = parent                                            # (each new node must have a different)                                     
    self.child = [None, None, None, None]
    self.last_operator = None                                       # up, down, left, right
    self.heuristic_cost = 0                                         
    self.depth = 0                                                 
    self.priority = self.depth + self.heuristic_cost

В начале алгоритма A * я создаю root узел с помощью

root_node = Node(3,3 [0,1,2,3,4,5,6,7,8],[1,2,5,0,3,4,6,7,8], "misplaced_tiles", None)

Я добавляю его в открытый список. Затем я пытаюсь сгенерировать его потомки вот так (пример для MOVE UP):

possible_directions = []        # here I keep all the directions I can move into (just for a control)
current_node_blank_position = self.get_blank_tile_position()             # gets index of blank tile using custom function (works well)
# while moving (up, down, left, right) I move the tile with a number on it, not the blank tile
if current_node_blank_position < (self.dimension - self.n_size):         # if I can move up
    # I create a child node for this node, which will have it's individual current_state (this is the state of it's parent - so the node that we are making child for - moved into some direction, in this case UP)
    # the parent of the node we are making child for should not be changed
    # the current state of the node we are making child for should not be changed
    # child has its parent - in this case the current state of a node we are making a child for
    # child has its individu current state - which is it's parents current state moved into some direction(up). We didn't move the tile yet, so for now I store here just the current state of it's parent.
    # Below I finally create the child node, it should have the same nsize, msize, goalstate, and heuristic as it's parent. The last argument is parent of this child, which is a current state of the node we are making this child for. We don't have the new current state for this child, so for now I use just the current state of it's parent.
    self.child[0] = Node(self.n_size, self.m_size, self.current_state, self.goal_state, self.choosen_heuristic, self.current_state)
    self.child[0].last_operator = "UP"            # I came to this child by moving UP
    self.child[0].depth = self.depth + 1            # depth of a child is it's parent depth + 1
    # already here for some reason I can see that the values are not correct - to be specific, the parent.
    print("Parent of the node I made child for ", self.parent)
    print("Current state of node I made a child for", self.current_state)
    print("This is a parent of the child = current state of the node I made a child for", self.child[0].parent)
    print("This is the new current state of the child", self.child[0].current_state)

    # Now I am doing to change the current state of the child by swapping a tile with number and the blank tile
    new_blank_position = current_node_blank_position + self.m_size       
    # the swap
    temp = self.child[0].current_state[current_node_blank_position]
    self.child[0].current_state[current_node_blank_position] = self.child[0].current_state[new_blank_position]
    self.child[0].current_state[new_blank_position] = temp
    # here I can see that all of the the values rewrote themselves, they are not correct.
    print("Parent of the node I made child for ", self.parent)
    print("Current state of node I made a child for", self.current_state)
    print("This is a parent of the child = current state of the node I made a child for", self.child[0].parent)
    print("This is the new current state of the child", self.child[0].current_state)

    if choosen_heuristic == "misplaced":
        self.child[0].misplaced_tiles_heuristic()              
    if choosen_heuristic == "manhattan":
        self.child[0].manhattan_distance()

    possible_directions.append("UP")

else:            
    # If I can't move up
    self.child[0] = None

Почему значения родительского и дочернего узла отличаются, как и должно быть? Можно ли иметь индивидуальные значения для self.attribute?

...