Exemplo n.º 1
0
def occurs_in(s1, lst):
    index = 0
    for s2 in lst:
        if Problem.DEEP_EQUALS(s1, s2[0]):
            return index
        else:
            index = index + 1
    return -1
Exemplo n.º 2
0
def IterativeAStar(initial_state):
    global COUNT, BACKLINKS

    OPEN = [initial_state]
    CLOSED = []
    BACKLINKS[Problem.HASHCODE(initial_state)] = -1
    g = {Problem.HASHCODE(initial_state): 0}

    while OPEN != []:
        S = OPEN[0]
        del OPEN[0]
        CLOSED.append(S)

        if Problem.GOAL_TEST(S):
            print(Problem.GOAL_MESSAGE_FUNCTION(S))
            backtrace(S)
            return

        COUNT += 1
        if (COUNT % 32) == 0:
            print(".", end="")
            if (COUNT % 128) == 0:
                print("COUNT = " + str(COUNT))
                print("len(OPEN)=" + str(len(OPEN)))
                print("len(CLOSED)=" + str(len(CLOSED)))
        L = []
        for op in Problem.OPERATORS:
            #Optionally uncomment the following when debugging
            #a new problem formulation.
            #print("Trying operator: "+op.name)
            if op.precond(S):
                new_state = op.state_transf(S)
                if not occurs_in(new_state, CLOSED):
                    L.append(new_state)
                    BACKLINKS[Problem.HASHCODE(new_state)] = S
                    g[Problem.HASHCODE(new_state)] = g[Problem.HASHCODE(S)] + 1
                    #Uncomment for debugging:
                    #print(Problem.DESCRIBE_STATE(new_state))

        for s2 in L:
            for i in range(len(OPEN)):
                if Problem.DEEP_EQUALS(s2, OPEN[i]):
                    del OPEN[i]
                    break

        OPEN = L + OPEN
        OPEN = sorted(OPEN,
                      key=lambda s: g[Problem.HASHCODE(s)] + heuristics(s))
Exemplo n.º 3
0
def IterateAStar(initial_state):
    global COUNT, BACKLINKS

    OPEN = [[initial_state, heuristics(initial_state)]]
    CLOSED = []

    # print(initial_state)
    COST = {Problem.HASHCODE(initial_state): 0}

    BACKLINKS[Problem.HASHCODE(initial_state)] = -1

    while OPEN != []:
        S = OPEN[0]
        del OPEN[0]
        CLOSED.append(S)

        if Problem.GOAL_TEST(S[0]):
            print(Problem.GOAL_MESSAGE_FUNCTION(S[0]))
            backtrace(S[0])
            return

        COUNT += 1
        if (COUNT % 32)==0:
            print(".",end="")
            if (COUNT % 128)==0:
                print("COUNT = "+str(COUNT))
                print("len(OPEN)="+str(len(OPEN)))
                print("len(CLOSED)="+str(len(CLOSED)))
        L = []

        # for each possible child in S (state)
        for op in Problem.OPERATORS:
            # is a child
            if op.precond(S[0]):
                new_state = op.state_transf(S[0])

                # index of occurrence in CLOSED
                occur_closed = occurs_in(new_state, CLOSED)

                # index of occurence in OPEN
                occur_open = occurs_in(new_state, OPEN)

                # the moves made so far + 1
                new_cost = COST[Problem.HASHCODE(S[0])] + 1
                # place in neighbor if new state
                if occur_closed == -1 and occur_open == -1:
                    L.append([new_state, heuristics(new_state)])
                    BACKLINKS[Problem.HASHCODE(new_state)] = S[0]
                    COST[Problem.HASHCODE(new_state)] = new_cost

                elif occur_open > -1:
                    # check to see if this move is more efficient
                    if COST[Problem.HASHCODE(new_state)] > new_cost:
                        COST[Problem.HASHCODE(new_state)] = new_cost
                        OPEN[occur_open] = [new_state, new_cost]

        # for all neighbors found, if it equals to states in OPEN,
        # delete it, shouldn't occur but juuuuust in case...
        for s2 in L:
            for i in range(len(OPEN)):
                if Problem.DEEP_EQUALS(s2, OPEN[i][0]):
                    del OPEN[i]; break

        OPEN = L + OPEN
        OPEN.sort(key=lambda x: x[1])
Exemplo n.º 4
0
def occurs_in(s1, lst):
    for s2 in lst:
        if Problem.DEEP_EQUALS(s1, s2): return True
    return False