Exemplo n.º 1
0
def test_get_lower_cost_of_states_with_different_cost():
    board = [[" "," ","C"],["Z"," "," "],[" ","Z"," "]]
    a_test = A_Star(board, vision=2, carrots=2, MaxLastMovements=2)
    a_test.open = [[1,2],[2,2]]
    a_test.f = [3,2]
    best, next_state = a_test.get_lower()
    assert best == 2 and next_state == [2,2]
Exemplo n.º 2
0
def test_h_considering_carrots():
    board = [[" "," ","C"],["Z"," "," "],[" "," ","Z"]]
    a_test = A_Star(board, vision=2, carrots=2, MaxLastMovements=2)
    carrots_coordinates = a_test.carrots_in_sight(current_state=[0,2], board=board)
    a_test.open = [[0,1],[1,2]]
    h = a_test.h_with_carrots(carrots_coordinates, current=[0,2])
    assert h == [3,1]
Exemplo n.º 3
0
def test_h_not_considering_carrots():
    board = [[" "," ","C"],["Z"," "," "],[" ","Z"," "]]
    a_test = A_Star(board, vision=1, carrots=2, MaxLastMovements=2)
    a_test.open = [[0,1],[1,0],[2,1],[1,2]]
    a_test.LastMovements = [[1,0],[1,2],[0,1]]
    h = a_test.h_without_carrots()
    assert h == [10,5,1,5]
Exemplo n.º 4
0
def test_total_cost():
    board = [[" "," ","C"],["Z"," "," "],[" "," ","Z"]]
    a_test = A_Star(board, vision=2, carrots=2, MaxLastMovements=2)
    a_test.g = 1
    a_test.open = [[0,2],[1,1],[2,2]]
    a_test.LastMovements = [[0,2]]
    a_test.calculate_f_score(current_state=[1,2], board=board)
    assert a_test.f == [15,4,3]
Exemplo n.º 5
0
def test_put_state_in_last_movements():
    board = [[" "," ","C"],["Z"," "," "],[" ","Z"," "]]
    a_test = A_Star(board, vision=2, carrots=2, MaxLastMovements=2)
    a_test.put_Last_Movement([1,2])
    assert a_test.LastMovements[-1] == [1,2]
Exemplo n.º 6
0
def test_search_rabbit_on_board_at_start():
    board = [[" "," ","C"],["Z"," "," "],[" ","Z"," "]]
    a_test = A_Star(board, vision=2, carrots=2, MaxLastMovements=2)
    [row, column] = a_test.search_start(board)
    assert [row, column] == [0,2]
Exemplo n.º 7
0
def test_neighbors_of_2_2():
    board = [[" "," ","C"],["Z"," "," "],[" ","Z"," "]]
    a_test = A_Star(board, vision=2, carrots=2, MaxLastMovements=2)
    neigbors = a_test.search_neighbors(current_state=[2,2], board=board)
    assert [2,1] in neigbors and [1,2] in neigbors
Exemplo n.º 8
0
def test_4_5_is_in_a_board_of_3x3():
    board = [[" "," ","C"],["Z"," "," "],[" ","Z"," "]]
    a_test = A_Star(board, vision=2, carrots=2, MaxLastMovements=2)
    result = a_test.is_in_board(x=-2, y=-1, board=board)
    assert result == False
Exemplo n.º 9
0
def test_0_carrots_in_sight():
    board = [[" "," ","C"],["Z"," "," "],[" ","Z"," "]]
    a_test = A_Star(board, vision=1, carrots=2, MaxLastMovements=2)
    coordinates = a_test.carrots_in_sight(current_state=[0,2], board=board)
    assert coordinates == []
Exemplo n.º 10
0
# Checks algorithm selected parameters
if args.a_estrella:
    if args.vision is None or args.zanahorias is None:
        parser.error(
            "The A* algorithm needs to know the vision field range and carrot's amount.")
        exit(-1)
    elif args.vision < 1 or args.zanahorias < 1:
        parser.error(
            "The A* algorithm needs to know the vision field range and carrot's amount. (>=1)")
        exit(-1)
    if args.movimientos_pasados is not None:
        movimientos_pasados = args.movimientos_pasados
    else:
        movimientos_pasados = 5

    algorithm = A_Star(board=None, vision=args.vision,
                       carrots=args.zanahorias, MaxLastMovements=movimientos_pasados)

elif args.genetico:
    # Checks if just one direction is selected
    cont_unique_flag = 0
    direction = ""
    unique_flags = ["derecha", "izquierda", "arriba", "abajo"]
    for flag in unique_flags:
        if args.__dict__[flag]:
            cont_unique_flag += 1
            direction = flag

    if cont_unique_flag != 1:
        parser.error("The genetic algorithm need just one direction.")
        exit(-1)
    if args.individuos is None or args.generaciones is None: