def do_alphaBeta(board, player, ply, alpha, beta, depth):
        """
        For memoization.
        """
        nonlocal node_count
        nonlocal table_hits

        # update stats
        node_count += 1

        # Transposition:
        board_hash = board.encode()
        if USE_TRANSPOSITION and board_hash in rec_table and\
                depth <= rec_table[board_hash][1]:
            table_hits += 1
            return rec_table[board_hash][0]

        # evaluate board
        b_eval = Evaluate(board)

        if b_eval.utility(ply) != Constants.NON_TERMINAL:  # terminal node
            ret = b_eval.utility(ply)
        elif depth <= 0:
            ret = b_eval.evaluation()
        else:
            successors = board.successors(player)

            if len(successors) <= 0:
                ret = Constants.DRAW
            elif player == Constants.MAX:
                v = Constants.NEGINF
                for child in successors:
                    v = max(v, do_alphaBeta(child, Constants.MIN, ply+1, \
                                         alpha, beta, depth-1))
                    alpha = max(alpha, v)
                    if beta <= alpha:
                        break  # beta cut-off
                ret = v
            else:
                v = Constants.INF
                for child in successors:
                    v = min(v, do_alphaBeta(child, Constants.MAX, ply+1, \
                                         alpha, beta, depth-1))
                    beta = min(beta, v)
                    if beta <= alpha:
                        break  # alpha cut-off
                ret = v

        # Transposition:
        if USE_TRANSPOSITION:
            rec_table[board_hash] = (ret, depth)

        return ret
    def do_minimax(board, player, ply, depth):
        """
        For memoization.
        """
        # Stats:
        nonlocal node_count
        nonlocal table_hits
        node_count += 1

        # Transposition:
        board_hash = board.encode()
        if USE_TRANSPOSITION and board_hash in rec_table and\
                depth <= rec_table[board_hash][1]:
            table_hits += 1
            return rec_table[board_hash][0]

        # evaluate board
        b_eval = Evaluate(board)

        if b_eval.utility(ply) != Constants.NON_TERMINAL:  # End game
            ret = b_eval.utility(ply)
        elif depth <= 0:  # max search depth hit
            ret = b_eval.evaluation()
        else:  # recursive case
            successors = board.successors(player)

            # No successors is a draw
            if len(successors) <= 0:
                ret = Constants.DRAW
            elif player == Constants.MAX:
                best_value = Constants.NEGINF
                for succ in successors:
                    v = do_minimax(succ, Constants.MIN, ply + 1, depth - 1)
                    best_value = max(best_value, v)
                ret = best_value
            else:  # if player is minimizer
                best_value = Constants.INF
                for succ in successors:
                    v = do_minimax(succ, Constants.MAX, ply + 1, depth - 1)
                    best_value = min(best_value, v)
                ret = best_value

        # Transposition:
        if USE_TRANSPOSITION:
            rec_table[board_hash] = (ret, depth)
        return ret
Beispiel #3
0
Created on Tue Jun 16 17:57:09 2015

@author: Paco
"""

from utils import Utils
from evaluate import Evaluate
from metrics import Metrics

# Load data
u = Utils()
train_hard = u.load_matrix('data/data_train_difficile.mat')

#generate pairs
pairs_idx, pairs_label = u.generate_pairs(train_hard['label'], 1000, 0.1)

# Calculate distance
m = Metrics()
dist = m.braycurtis_dist(train_hard['X'], pairs_idx)

# Evaluate model
e = Evaluate()
e.evaluation(pairs_label,dist)
# display results
e.display_roc()
e.hard_score()

# Evaluate test dataset and save it
test_hard = u.load_matrix('data/data_test_difficile.mat')
dist_test = m.braycurtis_dist(test_hard['X'], test_hard['pairs'])
u.save_test(dist_test,filetxt='soumission_dur.txt')