コード例 #1
0
def main():

    # initialize the problems
    problem_1 = Problem1('L1.txt')
    problem_2 = Problem1('L2.txt')
    problem_3 = Problem1('L3.txt')

    # create the graphs based on the text file
    problem_1.create_graph()
    problem_2.create_graph()
    problem_3.create_graph()

    # create the actual problem to search
    problem_1 = InstrumentedProblem(
        GraphProblem(problem_1.start_coordinates, problem_1.end_coordinates,
                     problem_1.graph))
    problem_2 = InstrumentedProblem(
        GraphProblem(problem_2.start_coordinates, problem_2.end_coordinates,
                     problem_2.graph))
    problem_3 = InstrumentedProblem(
        GraphProblem(problem_3.start_coordinates, problem_3.end_coordinates,
                     problem_3.graph))

    #compare the search algorithms on all problems
    compare_searchers(problems=[problem_1, problem_2, problem_3],
                      header=[
                          'Algorithm', 'L1 ((1,1), (14,19))',
                          'L2 ((1,1), (12,11))', 'L3 ((1,1), (13,4))'
                      ],
                      searchers=[
                          uniform_cost_search, greedy_best_first_graph_search,
                          astar_search
                      ])
コード例 #2
0
def test_bidirectional_breadth_first_search_vs_depth_first_graph_search():
    eight_puzzle = EightPuzzle(initial=instance_three, goal=goal)
    eight_puzzle_reversed = EightPuzzle(initial=goal, goal=instance_three)
    ins_problem = InstrumentedProblem(eight_puzzle)
    res = instrumented_bidirectional_breadth_first_search(eight_puzzle, eight_puzzle_reversed, True)
    depth_first_graph_search(ins_problem)
    assert (res[1]) <= ins_problem.explored
コード例 #3
0
def main():
    game = solitaire([['O', '_', '_', 'O', '_'], ['O', '_', 'O', '_', 'O'],
                      ['_', 'O', '_', 'O', '_'], ['O', '_', 'O', '_', '_'],
                      ['_', 'O', '_', '_', '_']])
    p = InstrumentedProblem(game)

    resultBreadthFirstSearch = breadth_first_search(p)
    print(resultBreadthFirstSearch.solution())
    print(resultBreadthFirstSearch.path()[0].state.board)
コード例 #4
0
def test_knapsack_simulated_annealing_low_dimensional_2():
    params = open_file("../dataset/low-dimensional/f2_l-d_kp_20_878")
    problem = KnapsackProblem(params[0], params[1])
    ins_problem = InstrumentedProblem(problem)
    result = simulated_annealing(ins_problem,
                                 exp_schedule(2000, 0.00005, 200000))
    optimum = int((open(
        "../dataset/low-dimensional-optimum/f2_l-d_kp_20_878").readline()))
    assert optimum == 1024
    assert result.value == optimum
コード例 #5
0
def test_knapsack_hill_climbing_large_scale_3():
    params = open_file("../dataset/large_scale/knapPI_1_500_1000_1")
    problem = KnapsackProblem(params[0], params[1])
    ins_problem = InstrumentedProblem(problem)
    result = hill_climbing(ins_problem)
    optimum = int((
        open("../dataset/large_scale-optimum/knapPI_1_500_1000_1").readline()))
    assert optimum == 28857
    assert result.value <= 10000
    assert result.value >= 9800
コード例 #6
0
def test_knapsack_simulated_annealing_plot_large_scale_6_4():
    params = open_file("../dataset/large_scale/knapPI_1_5000_1000_1")
    problem = KnapsackProblem(params[0], params[1])
    ins_problem = InstrumentedProblem(problem)
    result = simulated_annealing_plot(ins_problem, (800, 0.0005, 4000))
    optimum = int((open(
        "../dataset/large_scale-optimum/knapPI_1_5000_1000_1").readline()))
    assert optimum == 276457
    assert result.value <= 230000
    assert result.value >= 190000
コード例 #7
0
def test_knapsack_hill_climbing_rr_large_scale_5():
    params = open_file("../dataset/large_scale/knapPI_1_2000_1000_1")
    problem = KnapsackProblem(params[0], params[1])
    ins_problem = InstrumentedProblem(problem)
    result = hill_climbing_random_restart(ins_problem, 100)
    optimum = int((open(
        "../dataset/large_scale-optimum/knapPI_1_2000_1000_1").readline()))
    assert optimum == 110625
    assert result.value <= 30000
    assert result.value >= 27000
コード例 #8
0
def test_knapsack_simulated_annealing_large_scale_5():
    params = open_file("../dataset/large_scale/knapPI_1_2000_1000_1")
    problem = KnapsackProblem(params[0], params[1])
    ins_problem = InstrumentedProblem(problem)
    result = simulated_annealing(ins_problem)
    optimum = int((open(
        "../dataset/large_scale-optimum/knapPI_1_2000_1000_1").readline()))
    assert optimum == 110625
    assert result.value <= 14200
    assert result.value >= 8500
コード例 #9
0
def main():

    L1 = graphHelper()
    L2 = graphHelper()
    L3 = graphHelper()

    print("Manhattan")
    compare_searchers(problems=[
        InstrumentedProblem(ManhattanProblem(L1[0], L1[1], L1[2])),
        InstrumentedProblem(ManhattanProblem(L2[0], L2[1], L2[2])),
        InstrumentedProblem(ManhattanProblem(L3[0], L3[1], L3[2]))
    ],
                      header=['Algorithm', "L1", "L2", "L3"],
                      searchers=[
                          uniform_cost_search, greedy_best_first_graph_search,
                          astar_search
                      ])

    print("Euclidean")
    compare_searchers(problems=[
        InstrumentedProblem(GraphProblem(L1[0], L1[1], L1[2])),
        InstrumentedProblem(GraphProblem(L2[0], L2[1], L2[2])),
        InstrumentedProblem(GraphProblem(L3[0], L3[1], L3[2]))
    ],
                      header=['Algorithm', "L1", "L2", "L3"],
                      searchers=[
                          uniform_cost_search, greedy_best_first_graph_search,
                          astar_search
                      ])
コード例 #10
0
        'hill_climbing': lambda x: hill_climbing(x),
        'annealing': lambda x: simulated_annealing(x, schedule=sim)
    }

    #    print(sys.argv)
    searchers = sys.argv[1:]
    lewisProblems = map(LewisSudokuProblem, sudokus)
    naiveProblems = map(SudokuProblem, sudokus)
    #csv header
    print("problem,searcher,explored,states,tests,solution,value")
    for (i, (p1, p2)) in enumerate(zip(lewisProblems, naiveProblems)):
        #print("Initial state:")

        for s in searchers:
            if s in ['hill_climbing', 'annealing']:
                ip = InstrumentedProblem(p1)
            else:
                ip = InstrumentedProblem(p2)

            #print(ip.initial)

            result = choices[s](ip)
            print("%d,%s,%d,%d,%d,%s,%s" %
                  (i, s, ip.succs, ip.states, ip.goal_tests,
                   bool(result) and true_goal_test(result.state),
                   ip.value(result.state)) if result else "")

            # if result:
            #     print("Stats: %s"%(ip))
            #     print("Result:\n %s"%(result.state))
            #     print("Is a solution: %s"%(ip.goal_test(result.state)))
コード例 #11
0
def test_astar_3():
    problem = InstrumentedProblem(SixteenPuzzle(initial=instance_three, goal=goal))
    result = astar_search(problem, problem.gaschnig_index)
    assert result.state == goal
コード例 #12
0
def test_iterative_deepening_astar_2():
    problem = InstrumentedProblem(SixteenPuzzle(initial=instance_two, goal=goal))
    result = iterative_deepening_astar_search(problem, problem.gaschnig_index)
    assert result.state == goal