def append(self, item):
        if self.head is None:
            self.head = Node(item)
            return

        p = self.head
        while p.next is not None:
            p = p.next
        p.next = Node(item)
Example #2
0
    def insert_in_order(self, data):
        temp = Node(data)
        if self.start == None or data < self.start.info:
            temp.link = self.start
            self.start = temp
            return

        p = self.start
        while p.link is not None and p.link.info <= data:
            p = p.link

        temp.link = p.link
        p.link = temp
    def add(self, item):
        temp = Node(item)
        larger = False
        p = self.head
        prev = None

        while p is not None and not larger:
            if p.info > item:
                larger = True
            else:
                prev = p
                p = p.next
        if prev is None:
            self.head = temp
        else:
            temp.next = p
            prev.next = temp
Example #4
0
    def __init__(self, puzzle, max):
        currentState = State(puzzle)
        self.first_node = Node(currentState, 0)

        self.path = []
        self.path.append(self.first_node)

        self.max_tries = max
        self.d = dict()
        self.Open_dic = dict()
        self.Close_dic = dict()
        self.state_id = 1
        self.Close = []
        self.Open = []
Example #5
0
    def expand(self, node, H):
        moves = node.moves
        n = len(moves)
        depth = node.depth

        currentState = node.state

        if (n == 0):
            print("Solution not found")
            return False

        for i in range(0, n):
            #copy current state
            nextState = copy.deepcopy(currentState)

            #perform move [i]
            nextState.run_command(moves[i][-3:])
            s = nextState.get_string_board()

            # Eval nextState
            if (H == 1):
                h = self.H1(nextState)
            elif (H == 2):
                h = self.H2(nextState)
            else:
                h = self.H3(nextState)

            f = h + depth + 1
            """CASE 1 : new state is neither in OPEN nor in CLOSE"""
            if (not s in self.Open_dic) and (not s in self.Close_dic):
                # Create child node
                nextNode = Node(nextState, depth + 1)
                nextNode.F = f
                nextNode.parent = node
                nextNode.previous_move = moves[i]

                #insert child node to OPEN
                self.openPush(nextNode)
                self.Open_dic.update({s: f})

            #CASE 2: nextState in OPEN and our value is better
            elif (s in self.Open_dic):
                other_F = self.Open_dic.get(s)
                if (other_F > f):
                    # DO: Repalce previous state with this state

                    # Create child node
                    nextNode = Node(nextState, depth + 1)
                    nextNode.F = f
                    nextNode.parent = node
                    nextNode.previous_move = moves[i]

                    #Replace nodes
                    self.removeNode(self.Open, s)
                    self.openPush(nextNode)

                    #update dic
                    self.Open_dic.update({s: f})

            # CASE 3: nextState in CLOSE and our value is better
            elif (s in self.Close_dic):
                other_F = self.Close_dic.get(s)
                if (other_F > f):
                    #DO: Repalce previous state with this state
                    #Move this state to OPEN

                    # Create child node
                    nextNode = Node(nextState, depth + 1)
                    nextNode.F = f
                    nextNode.parent = node
                    nextNode.previous_move = moves[i]

                    # remove old node
                    self.removeNode(self.Close, s)

                    #put node in OPEN
                    self.openPush(nextNode)

                    # update both dictionaries
                    self.Open_dic.update({s: f})
                    self.Close_dic.pop(s)

        return True
 def add_at_head(self, item):  # Easiest place to add a  node is in the head
     temp = Node(item)  # Create a temp Node to be linked or added
     temp.next = self.head  # same as temp.self_next(self.head)
     self.head = temp
Example #7
0
 def push(self, number=None):
     prev = self.head
     newnode = Node(number)
     newnode.set_next(prev)
     self.head = newnode
Example #8
0
 def add(self, data):
     n = Node(data)
     if self.head is not None:
         n.set_next(self.head)
     self.head = n