Ejemplo n.º 1
0
          + ", #expansions: " + str(expansion_count)

    # A* consistent
    graph = _get_test_graph_consistent()
    (expansion_count, cost,
     path) = _graph_search(graph, 1, 5, mp.add_to_queue_ASTAR,
                           mp.is_queue_empty_ASTAR, mp.pop_front_ASTAR,
                           _heuristic_function)
    print "A* (consistent) path: " + str(path) + ", cost: " + str(cost)  \
          + ", #expansions: " + str(expansion_count)

    print
    print "The n-queens problem"

    n = 7
    # Get a basic state
    state = mp.get_random_state(n)
    print "A random atate: " + str(state) + ", conflicting pairs: " + str(
        mp.compute_attacking_pairs(state))

    # Call hill-climbing once
    new_state = mp.hill_desending_n_queens(state, mp.compute_attacking_pairs)
    print "Final state after hill-climbing: " + str(new_state) + ", conflicting pairs: " \
          + str(mp.compute_attacking_pairs(new_state))

    # Get a fully solved state for a given n
    print "A valid solution: " + str(
        mp.hill_desending_n_queens_random_restart(n, mp.get_random_state,
                                                  mp.compute_attacking_pairs,
                                                  mp.hill_desending_n_queens))
Ejemplo n.º 2
0
        mp.is_queue_empty_ASTAR, mp.pop_front_ASTAR, _heuristic_function)
    print "A* (admissible) path: " + str(path) + ", cost: " + str(cost) \
          + ", #expansions: " + str(expansion_count)

    # A* consistent 
    graph = _get_test_graph_consistent()
    (expansion_count, cost, path) = _graph_search(graph, 1, 5, mp.add_to_queue_ASTAR,
        mp.is_queue_empty_ASTAR, mp.pop_front_ASTAR, _heuristic_function)
    print "A* (consistent) path: " + str(path) + ", cost: " + str(cost)  \
          + ", #expansions: " + str(expansion_count)

    print
    print "The n-queens problem" 

    n = 20
    # Get a basic state
    state = mp.get_random_state(n)
    print "A random atate: " + str(state) + ", conflicting pairs: " + str(mp.compute_attacking_pairs(state))

    # Call hill-climbing once 
    new_state = mp.hill_desending_n_queens(state, mp.compute_attacking_pairs)
    print "Final state after hill-climbing: " + str(new_state) + ", conflicting pairs: " \
          + str(mp.compute_attacking_pairs(new_state))

    # Get a fully solved state for a given n    
    print "A valid solution: " + str(mp.n_queens(n,
        mp.get_random_state, mp.compute_attacking_pairs,mp.hill_desending_n_queens))