def max_value_first(board, alpha, beta, depth):
    v = float("-inf")
    moves = mmUtil.get_moves(board)

    # Get number of extra processors after first level
    num_extra = numCPU - len(moves)
    # Ensure num_extra is positive
    if num_extra < 0:
        num_extra = 0

    pool = p.MyPool(numCPU - num_extra)
    func = partial(t.find_min, board, activePlayer, alpha, beta, depth)

    # Make array of tuples for each node of tree at the next depth
    # each tuple: (move, # of moves available)
    next_nodes = []
    for move in moves:
        temp_board = mmUtil.make_move(board, move, activePlayer)
        num_next = mmUtil.get_moves(temp_board)
        next_nodes.append((move, len(num_next)))

    # Sort by decreasing number of moves available
    sorted(next_nodes, key=itemgetter(1), reverse=True)

    # Create data array for number of processors allocated for each move
    data = []

    # Calculate the base number of extra processors that each move will use
    base = num_extra / len(moves)

    # For each tuple add the base number of extra processors to the default amount (1)
    for node in next_nodes:
        data.append((node[0], 1 + base))

    # Allocate remaining processor with moves that have the highest branching factor
    for i in range((num_extra % len(moves))):
        node = data[i]
        data[i] = (node[0], node[1] + 1)

    v_mins = pool.map_async(func, data)
    pool.close()
    pool.join()
    mov_val = max(v_mins.get(), key=itemgetter(1))  # (move, value)

    print v_mins.get()
    print mov_val

    return mov_val[::-1]  # (value, move)
def max_value_first(board, alpha, beta, depth):
    v = float('-inf')
    moves = mmUtil.get_moves(board)

    # Get number of extra processors after first level
    num_extra = numCPU - len(moves)
    # Ensure num_extra is positive
    if num_extra < 0:
        num_extra = 0

    pool = p.MyPool(numCPU - num_extra)
    func = partial(t.find_min, board, activePlayer, alpha, beta, depth)

    # Make array of tuples for each node of tree at the next depth
    # each tuple: (move, # of moves available)
    next_nodes = []
    for move in moves:
        temp_board = mmUtil.make_move(board, move, activePlayer)
        num_next = mmUtil.get_moves(temp_board)
        next_nodes.append((move, len(num_next)))

    # Sort by decreasing number of moves available
    sorted(next_nodes, key=itemgetter(1), reverse=True)

    # Create data array for number of processors allocated for each move
    data = []

    # Calculate the base number of extra processors that each move will use
    base = num_extra / len(moves)

    # For each tuple add the base number of extra processors to the default amount (1)
    for node in next_nodes:
        data.append((node[0], 1 + base))

    # Allocate remaining processor with moves that have the highest branching factor
    for i in range((num_extra % len(moves))):
        node = data[i]
        data[i] = (node[0], node[1] + 1)

    v_mins = pool.map_async(func, data)
    pool.close()
    pool.join()
    mov_val = max(v_mins.get(), key=itemgetter(1))  # (move, value)

    print v_mins.get()
    print mov_val

    return mov_val[::-1]  # (value, move)
def min_value(board, alpha, beta, depth):
    if mmUtil.terminal_test(board, activePlayer, depth, startTime, maxTime, maxDepth):
        return mmUtil.utility(board, activePlayer)
    v = float("inf")
    for a in mmUtil.get_moves(board):
        v = min(v, max_value(mmUtil.make_move(board, a, opponentPlayer), alpha, beta, depth + 1))
        if v <= alpha:
            return v
        beta = min(beta, v)
    return v
Exemple #4
0
def min_value(board, alpha, beta, depth):
    if mmUtil.terminal_test(board, activePlayer, depth, startTime, maxTime,
                            maxDepth):
        return mmUtil.utility(board, activePlayer)
    v = float('inf')
    for a in mmUtil.get_moves(board):
        v = min(
            v,
            max_value(mmUtil.make_move(board, a, opponentPlayer), alpha, beta,
                      depth + 1))
        if v <= alpha:
            return v
        beta = min(beta, v)
    return v
def max_value_first(board, alpha, beta, depth):
    v = float("-inf")
    move = 3
    for a in mmUtil.get_moves(board):
        vMin = min_value(mmUtil.make_move(board, a, activePlayer), alpha, beta, depth + 1)
        if v >= vMin:
            move = move
        else:
            v = vMin
            move = a
        if v >= beta:
            return (v, move)
        alpha = max(alpha, v)
    return (v, move)
Exemple #6
0
def max_value_first(board, alpha, beta, depth):
    v = float('-inf')
    move = 3
    for a in mmUtil.get_moves(board):
        vMin = min_value(mmUtil.make_move(board, a, activePlayer), alpha, beta,
                         depth + 1)
        if v >= vMin:
            move = move
        else:
            v = vMin
            move = a
        if v >= beta:
            return (v, move)
        alpha = max(alpha, v)
    return (v, move)
def find_min(board, activePlayer, alpha, beta, depth, mov_cpu):
	print "[" + str(depth) + "] Data for move " + str(mov_cpu[0]) + ": " + str(mov_cpu)
	a = mov_cpu[0]
	numCPU = mov_cpu[1]

	if numCPU == 1:
		return (a, ai.min_value(mmUtil.make_move(board,a,activePlayer), alpha, beta, depth+1))
	else:
		moves = mmUtil.get_moves(board)
		data = []
		for move in moves:
			data.append((move, 1))

		pool = multiprocessing.Pool(processes = numCPU)

		func = partial(find_max, board, activePlayer, alpha, beta, depth+1)
		v_maxs = pool.map_async(func, data)
		pool.close()
		pool.join()
		mov_val = min(v_maxs.get(), key=itemgetter(1)) # (move, value)
		return mov_val