[Python] I need help implementing Negascout (principal variation search) for Othello

Discussão em 'Python' iniciado por Stack, Janeiro 27, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to implement pvs into my Othello ai, as a way to improve on alpha beta proning, but when I implement it, it is actually slower by about double, my question is, how would I implement it, and can i do it by keeping min/man and not having to make it into a single function. I welcome any suggestions. I roughly understand how pvs works, but i am still confused on some things. my scoring function gives a positive number for x and a negative for y

    def max_step(board,depth,path,a,b):
    if game_end(board):
    return score_end(board)
    if depth == 0:
    return score(board)
    my_moves = {i:score((k := make_move(board,i,"x"))) for i in find_indexses(board,"x")}
    if len(my_moves) == 0:
    return min_step(board,depth-1,path,a,b)
    results = []
    my_sorted_moves = dict(sorted(my_moves.items(), key=lambda item: item[1],reverse=True))
    result = None
    for move,index in enumerate(my_sorted_moves.keys()) :
    new_board = make_move(board,index,"x")
    if index == 0:
    result = min_step(new_board,depth-1,path+[board],a,b)
    else:
    result = min_step(new_board,depth-1,path+[board],b-1,b)
    if result > a and result < b:
    result = min_step(new_board,depth-1,path+[board],a,b)
    results.append(result)
    if result >= a:
    a = result
    if b <= a:
    break
    return max(results)

    def min_step(board,depth,path,a,b):
    if game_end(board):
    return score_end(board)
    if depth == 0:
    return score(board)
    my_moves = {i:score((k := make_move(board,i,"o"))) for i in find_indexses(board,"o")}
    if len(my_moves) == 0:
    return min_step(board,depth-1,path,a,b)
    results = []
    my_sorted_moves = dict(sorted(my_moves.items(), key=lambda item: item[1]))
    result = None
    for move,index in enumerate(my_sorted_moves.keys()) :
    new_board = make_move(board,index,"o")
    if index == 0:
    result = max_step(new_board,depth-1,path+[board],a,b)
    else:
    result = max_step(new_board,depth-1,path+[board],b-1,b)
    if result > a and result < b:
    result = max_step(new_board,depth-1,path+[board],a,b)
    results.append(result)
    if result <= b:
    b = result
    if b <= a:
    break
    return min(results)

    def find_next_move(board,token,depth):
    res = {}
    for moves in find_indexses(board,token):
    if token == "x":
    board1 = board
    board1 = make_move(board1,moves,"x")
    res[moves] = min_step(board1,depth-1,[board],-99999999,9999999)
    else:
    board1 = board
    board1 = make_move(board1,moves,"o")
    res[moves] = max_step(board1,depth-1,[board],-99999999,9999999)
    print(res)
    if token == "x":
    return max(zip(res.values(), res.keys()))[1]
    return min(zip(res.values(), res.keys()))[1]


    I tried to keep my min/max functions intact and just edited them to add pvs, but it doesn't work, I also attempted to use only one function but that just did not make sense to me in the context of othello, aside from that I don't know what to do

    Continue reading...

Compartilhe esta Página