Example #1
0
def compare_searchers(problems):

    searchers=[search.depth_first_tree_search,
               search.depth_limited_search,
               search.depth_first_graph_search,
               search.breadth_first_tree_search,
               search.recursive_best_first_search]


    print("----- Start comparison")
    print("a = total nr. of successful actions")
    print("ua =          of unsuccessful actions (when action() returns [])")
    print("g =           of goal tests")
    print("hmin:hmax = nr. good states vs. bad states ")
    print("s = solution")
    print("t = solving time \n\n")

    def do(searcher, problem):
        p = InstrumentedProblem(problem)
        argspec = inspect.getargspec(searcher)
        if len(argspec.args) == 1:
            searcher(p)
        else:
            searcher(p, p.h)

        p.set_end_time(time.time())
        return p

    table = [[search.name(s)] + [do(s, problems[0])] for s in searchers]
    search.print_table(table, ["Searcher", "Result"])
    print("")
    table = [[search.name(s)] + [do(s, problems[1])] for s in searchers]
    search.print_table(table, ["Searcher", "Result with reverse number selection list"])
Example #2
0
def compare_searchers(problems, header, searchers=[]):
    best = {}
    bestNode = {}
    for p in problems:
        best[p.label] = inf
        bestNode[p.label] = None

    def do(searcher, problem):
        nonlocal best, bestNode
        p = search.InstrumentedProblem(problem)
        goalNode = searcher(p)
        cost = goalNode.path_cost
        if cost < best[p.label]:
            best[p.label] = cost
            bestNode[p.label] = goalNode
        return p, cost

    table = [[search.name(s)] + [do(s, p) for p in problems]
             for s in searchers]
    print_table(table, header)
    print('----------------------------------------')
    for p in problems:
        bestPath = []
        node = bestNode[p.label]
        while node != None:
            bestPath.append(node.state)
            node = node.parent
        summary = "Best Path for " + p.label + ": "
        for state in reversed(bestPath):
            try:
                summary += "\n" + p.prettyPrint(state) + "\n---------"
            except:
                summary += " " + state
        print(summary)
        print('----------------------------------------')
Example #3
0
def compare_searchers(problems, header, searchers=[]):
    def do(searcher, problem):
        p = search.InstrumentedProblem(problem)
        goalNode = searcher(p)
        return p, goalNode.path_cost
    table = [[search.name(s)] + [do(s, p) for p in problems] for s in searchers]
    search.print_table(table, header)
Example #4
0
def compare_searchers(problems, header, searchers=[]):
    best = {}
    bestNode = {}
    gradeInfo = {}
    for p in problems:
        best[p.label] = inf
        bestNode[p.label] = None
    def do(searcher, problem):
        nonlocal best, bestNode, gradeInfo
        ip = search.InstrumentedProblem(problem)
        try:
            ipLabel = ip.label
            if not ipLabel in gradeInfo:
                gradeInfo[ipLabel] = {}
            goalNode = searcher(ip)
            gradeInfo[ipLabel][searcher] = \
                gradeProblem(searcher, ip, goalNode)
            cost = goalNode.path_cost
            if cost < best[ipLabel]:
                best[ipLabel] = cost
                bestNode[ipLabel] = goalNode
            return ip, cost
        except:
            # print('searcher(' + p.label + ') raised an exception:')
            # traceback.print_exc()
            return ip, inf
    table = [[search.name(s)] + [do(s, p) for p in problems]
             for s in searchers]
    print_table(table, header)
    print('----------------------------------------')
    for p in problems:
        bestPath = []
        node = bestNode[p.label]
        while node != None:
            bestPath.append(node.state)
            node = node.parent
        summary = "Best Path for " + p.label + ": "
        ppFun = getattr(p, "prettyPrint", None)
        if ppFun == None:
            ppFun = str
        ppSep = ' '
        pathLength = len(bestPath)
        if pathLength > 0:
            stateLength = len(ppFun(bestPath[0]))
            if pathLength * stateLength > 100:
                ppSep = "\n"
        for state in reversed(bestPath):
            summary += ppSep + ppFun(state)
        print(summary)
    print('----------------------------------------')
    return gradeInfo
Example #5
0
def compare_searchers(problems, header, searchers=[]):
    def do(searcher, problem):
        p = search.InstrumentedProblem(problem)
        goalNode = searcher(p)
        return p, goalNode.path_cost
    table = [[search.name(s)] + [do(s, p) for p in problems] for s in searchers]
    search.print_table(table, header)

    class GapSwitch(search.Problem):

        #A 4x4 grid, each square unmarked at the start.
        grid = (['U', 'U', 'U', 'U'],
                ['U', 'U', 'U', 'U'],
                ['U', 'U', 'U', 'U'],
                ['U', 'U', 'U', 'U'])

        #defines the state using row number first, then the spaces between marked squares required in that row,
        #  column number, and the spaces between marked squares required in that column
        state = ((2,2),(4,1))

        def actions(self, state):
            return ['M', 'U']

        def result(self, state, action):
            if action == 'up':
                return 'on'
            else:
                return 'off'

        def start(self, state, grid):
            if

        def goal_test(self, state):
            return state == 'on'

        def h(self, node):
            state = node.state
            if self.goal_test(state):
                return 0
            else:
                return 1

    switch_puzzle = LightSwitch('off')
    switch_puzzle.label = 'Light Switch'
Example #6
0
def compare_searchers(problems, header, searchers=[]):
    best = {}
    bestNode = {}
    for p in problems:
        best[p.label] = inf
        bestNode[p.label] = None
    def do(searcher, problem):
        nonlocal best, bestNode
        p = search.InstrumentedProblem(problem)
        try:
            goalNode = searcher(p)
            cost = goalNode.path_cost
            if cost < best[p.label]:
                best[p.label] = cost
                bestNode[p.label] = goalNode
            return p, cost
        except:
            # print('searcher(' + p.label + ') raised an exception:')
            # traceback.print_exc()
            return p, inf
    table = [[search.name(s)] + [do(s, p) for p in problems] for s in searchers]
    print_table(table, header)
    print('----------------------------------------')
    for p in problems:
        bestPath = []
        node = bestNode[p.label]
        while node != None:
            bestPath.append(node.state)
            node = node.parent
        summary = "Best Path for " + p.label + ": "
        for state in reversed(bestPath):
            try:
                summary += "\n" + p.prettyPrint(state) + "\n---------"
            except:
                summary += " " + str(state)
        print(summary)
        print('----------------------------------------')
Example #7
0
            header=hlist,
            searchers=searchList
        )
    except:
        traceback.print_exc()

allScores = newScores()
maxScores = {}
for student in gradeInfo:
    print('Scores for: %s' % student)
    maxScores[student] = {}
    for pLabel in gradeInfo[student]:
        table = []
        maxScores[student][pLabel] = newScores()
        for searcher in gradeInfo[student][pLabel]:
            sLabel = search.name(searcher)
            info = gradeInfo[student][pLabel][searcher]
            scoreSet = info['score']
            table.append(['%s, %s:' % (sLabel, pLabel),
                          scoreList(scoreSet)])
            for label in scoreSet:
                accumulator = accuMethods[label]
                maxScores[student][pLabel][label] = accumulator(
                    maxScores[student][pLabel][label], scoreSet[label])
        if len(table) > 1:
            table.append(['%s summary:' % (pLabel),
                  scoreList(maxScores[student][pLabel])])
        print_table(table)
        if len(table) > 1:
            print()
        for label in allScores: