2

I have written a Chess Engine Program in python in which one of my python file is responsible for the finding the Computer Moves . This uses MinMax algorithm with alpha-beta pruning and some other features. The move finding python file looks something like this :

def findBestMove():
    minMax()
    return BestMove

This file also contain some other functions like ScoreBoard and Openings. I have a separate file where I have implemented my move making. So I call this function in that file.

Now this MinMax function takes up a lot of time so I decided to use multiprocessing library in python . All the examples that I saw had the code structure like this :

def f(args):
    #Do Something
if __name__ == '__main__':
    p = Process(target = f , args = ())
    p.start()
    p.join()

In my program I want to apply multiprocessing to my MinMax Function but am not able to do it .

I tried something like this :

def findBestMove(gs, validMoves):
    global nextMove
    nextMove = None
    p = Process(target = findMoveNegaMaxAlphaBeta, args=(gs, validMoves, DEPTH, -CHECKMATE, CHECKMATE, 1 if gs.whiteToMove else -1))
    p.start()
    p.join()
    return nextMove 

def findMoveNegaMaxAlphaBeta(gs, validMoves, depth, alpha, beta, turnMultiplier):
    global nextMove
    if depth == 0 :
        return turnMultiplier * scoreBoard(gs)    

    maxScore = -CHECKMATE
    for move in validMoves :
        gs.makeMove(move)
        nextMoves = gs.getValidMoves()
        score = -findMoveNegaMaxAlphaBeta(gs, nextMoves, depth - 1 , -beta, -alpha, -turnMultiplier)
        if score > maxScore:
            maxScore = score
            if depth == DEPTH :
                nextMove = move
                print("Lol")
        gs.undoMove() 
        if maxScore > alpha:   # This is were pruning happens
            alpha = maxScore
        if alpha >= beta :
            break    

    return maxScore

When I run the code this "Lol" statement gets printed but the overall findBestMove function returns None which means that the program calls my findMoveNegaMaxAlphaBeta function and also reaches till the point when a value is assigned to the NextMove but that value does not change in the findBestMove function even when Next Move is a global variable. How do I get to return the BestMove which my minmax function finds but is not returned in the findBestMove function.

Falcon
  • 73
  • 6
  • I think you could do it like this (untested): ```def findBestMove(): p = Process(target = minMax , args = ()) p.start() p.join()```, `if __name__ == '__main__'` is not used within functions but just unnested in the file ([see](https://stackoverflow.com/questions/419163/what-does-if-name-main-do)). – cbolwerk Apr 03 '21 at 11:52
  • Thnx for the file. I understood the meaning of if __name__ == '__main__' but the code still does not work when I did : def findBestMove(): p = Process(target = minMax , args = ()) p.start() p.join() . My program just restarted everytime bestMove function was called and the function also returned nothing. – Falcon Apr 03 '21 at 12:23
  • 1
    I'm not very familiar with multiprocessing, but you could try using a queue to update the `nextMove` value instead of using a global variable (`queue = multiprocessing.Queue()`, [see](https://stackoverflow.com/questions/11515944/how-to-use-multiprocessing-queue-in-python)). Plus I think `p.join()` ends the process, so you might need to wait a bit using `time.sleep(10)` for example – cbolwerk Apr 03 '21 at 13:57
  • Thnx !! The queue idea worked . – Falcon Apr 03 '21 at 16:34

0 Answers0