Beispiel #1
0
class PicnicLisp(cmd.Cmd):
    def __init__(self):
        cmd.Cmd.__init__(self)
        """ Setup attributes of the command line prompt """
        self.prompt = 'PicnicLisp$ '
        self.parser = Parser()
        self.evaluator = Evaluator()

    def emptyline(self):
        """ On empty line, do nothing. Default impl repeats previous command """
        pass

    def processLine(self, line):
        """ Parses and evaluates a line. Separate function for testing
		purposes """
        try:
            tokenized = self.parser.parse(line)
            value = self.evaluator.eval(tokenized)
            return value
        except Exception as e:
            print e.message

    def default(self, line):
        """ Accepts all atoms and lists """
        interpreted = self.processLine(line)
        if interpreted != None: print interpreted
        else: print "Ok"
    def _negamax(self, depth, player, alpha, beta):
        # If game is over or depth limit reached
        if depth == 0 or self._board.is_game_over():
            return Evaluator.eval(self._board, self._mycolor) * (
                -1 if player != self._mycolor else 1)

        color = self._board._flip(player)

        # If player cannot move, opponent turn
        if not self._board.at_least_one_legal_move(player):
            return -self._negamax(depth - 1, color, -beta, -alpha)
        compute = True
        currentHash = self._boardHash()
        if (
                self._checkTable(currentHash)
        ):  # we check in table whether or not we have to compute the best move
            # if(self._getDepth(currentHash)==depth):
            compute = False
            self._tableUsage += 1

        value = -sys.maxsize - 1
        if compute:
            for m in self._board.legal_moves():
                self._board.push(m)
                value = max(value,
                            -self._negamax(depth - 1, color, -beta, -alpha))
                self._board.pop()
                alpha = max(alpha, value)
                if alpha >= beta:
                    break  # Cutoff
            self._addToTable(currentHash, depth, value)
        else:
            value = self._getHeuristic(currentHash)
        return value
    def _play(self):
        # Killer move detection
        # # Corner
        max_value = -sys.maxsize - 1
        best_move = None
        corners = [[0, 0], [0, self._board.get_board_size() - 1],
                   [self._board.get_board_size() - 1, 0],
                   [
                       self._board.get_board_size() - 1,
                       self._board.get_board_size() - 1
                   ]]
        for m in self._board.legal_moves():
            self._board.push(m)
            (_, x, y) = m
            if [x, y] in corners:
                max_value = max(max_value,
                                Evaluator.eval(self._board, self._mycolor))
                best_move = m
            self._board.pop()

        if best_move is not None:
            return best_move

        # # Blocking move
        max_value = -sys.maxsize - 1
        best_move = None
        for m in self._board.legal_moves():
            self._board.push(m)
            if not self._board.at_least_one_legal_move(self._opponent):
                max_value = max(max_value,
                                Evaluator.eval(self._board, self._mycolor))
                best_move = m
            self._board.pop()

        if best_move is not None:
            return best_move

        # Opening move
        if self._OB_active:
            move = self._opening_book.get_following_move(self._move_history)
            if move is not None:
                return move
            #  Else
            self._OB_active = False

        # minimax
        return self._start_negamax(4)
Beispiel #4
0
def main(argv):

    rd = Reader() 
    ev = Evaluator()
    pr = Printer()
    program = rd.read(argv[1])
    pr.prnt(program)
    value = ev.eval(program)
    pr.prnt(value)
Beispiel #5
0
    def run(self, train_batches, test_batches):
        # training and validation error collector
        ec = ErrorCollector()

        print("-----ModelTester-----")
        logging.info("-----ModelTester-----")

        for n_hidden in self.n_hidden:
            for n_layer in self.n_layer:
                if self.is_res_net:
                    self.models.append(
                        ResNetModel(n_in=self.n_in,
                                    n_hidden=n_hidden,
                                    n_out=self.n_out,
                                    n_layer=n_layer,
                                    activation='relu'))
                else:
                    for activation in self.activation:
                        # create models
                        self.models.append(
                            Model(n_in=self.n_in,
                                  n_hidden=n_hidden,
                                  n_out=self.n_out,
                                  n_layer=n_layer,
                                  activation=activation))

        for model in self.models:
            trainer = Trainer(model, train_batches, ec)
            for learning_rate in self.learning_rates:
                trainer.train(learning_rate, self.epochs, early_stop_lim=25)
                # print error plots
                ec.plotTrainTestError(model, train_batches.batch_size,
                                      learning_rate, self.epochs,
                                      model.activation)
                ec.plotTrainTestAcc(model, train_batches.batch_size,
                                    learning_rate, self.epochs,
                                    model.activation)
                ec.resetErrors()
                evaluator = Evaluator(model, test_batches,
                                      trainer.getSaveFilePath())
                test_loss, test_acc = evaluator.eval()
                trainer.resetBestScore()

                if self.best_test_acc == 0 or test_acc > self.best_test_acc:
                    self.best_test_acc = test_acc
                    self.best_model_param = 'Param_' + model.name + '_ep-' + str(
                        self.epochs) + '_hidu-' + str(
                            model.n_hidden) + '_hidl-' + str(
                                model.n_layer) + '_lr-' + str(learning_rate)

        print(
            "-----ModelTester finished, best test acc: [%.6f] with model: %s "
            % (self.best_test_acc, self.best_model_param))
        logging.info(
            "-----ModelTester finished, best test acc: [%.6f] with model: %s "
            % (self.best_test_acc, self.best_model_param))
Beispiel #6
0
    def _negamax(self, depth, player, alpha, beta):
        alpha_origin = alpha
        self._table_usage += 1

        # Transposition table lookup
        current_hash = self._get_board_hash()
        tt_entry = self._hash_table.get_table_entry(current_hash)
        # Check in table whether or not we have to compute the best move
        if tt_entry is not None and tt_entry.depth >= depth:
            if tt_entry.flag == self._hash_table._EXACT:
                return tt_entry.value
            elif tt_entry.flag == self._hash_table._LOWERBOUND:
                alpha = max(alpha, tt_entry.value)
            else:  # tt_entry.flag == self._hash_table._UPPERBOUND:
                beta = min(beta, tt_entry.value)

            if alpha >= beta:
                return tt_entry.value

        self._table_usage -= 1

        # If game is over or depth limit reached
        if depth == 0 or self._board.is_game_over():
            return Evaluator.eval(self._board, self._mycolor) * (
                -1 if player != self._mycolor else 1)

        color = self._board._flip(player)

        # If player cannot move, opponent turn
        if not self._board.at_least_one_legal_move(player):
            return -self._negamax(depth - 1, color, -beta, -alpha)

        value = -sys.maxsize - 1
        for m in self._board.legal_moves():
            self._board.push(m)
            value = max(value, -self._negamax(depth - 1, color, -beta, -alpha))
            self._board.pop()
            alpha = max(alpha, value)
            if alpha >= beta:
                break  # Cutoff

        # Store in transposition table
        tt_entry = TableEntry()
        tt_entry.value = value
        if value <= alpha_origin:
            tt_entry.flag = self._hash_table._UPPERBOUND
        elif value >= beta:
            tt_entry.flag = self._hash_table._LOWERBOUND
        else:
            tt_entry.flag = self._hash_table._EXACT

        tt_entry.depth = depth
        self._hash_table.store(current_hash, tt_entry)

        return value
Beispiel #7
0
    def run(self, train_batches, test_batches):
        # training and validation error collector
        ec = ErrorCollector()
        # setup logging
        log_file_name = 'logs' + os.sep + 'Model_Trainer.log'
        logging.basicConfig(filename=log_file_name, level=logging.INFO)

        print("-----ModelTester-----")
        logging.info("-----ModelTester-----")

        for n_hidden in self.n_hidden:
            for n_layer in self.n_layer:
                # create models
                self.models.append(
                    Model(n_in=self.n_in,
                          n_hidden=n_hidden,
                          n_out=self.n_out,
                          n_layer=n_layer))

        for model in self.models:
            trainer = Trainer(model, train_batches, ec)
            for learning_rate in self.learning_rates:
                trainer.train(learning_rate, self.epochs, early_stop_lim=25)
                # print error plots
                ec.plotTrainTestError(model, train_batches.batch_size,
                                      learning_rate, self.epochs)
                ec.plotTrainTestAcc(model, train_batches.batch_size,
                                    learning_rate, self.epochs)
                ec.resetErrors()
                evaluator = Evaluator(model, test_batches,
                                      trainer.getSaveFilePath())
                test_loss, test_acc = evaluator.eval()
                trainer.resetBestScore()

                if self.best_test_acc == 0 or test_acc > self.best_test_acc:
                    self.best_test_acc = test_acc
                    self.best_model_param = 'Param_ep-' + str(
                        self.epochs) + '_hidu-' + str(
                            model.n_hidden) + '_hidl-' + str(
                                model.n_layer) + '_lr-' + str(learning_rate)

        print(
            "-----ModelTester finished, best test acc: [%.6f] with model: %s "
            % (self.best_test_acc, self.best_model_param))
        logging.info(
            "-----ModelTester finished, best test acc: [%.6f] with model: %s "
            % (self.best_test_acc, self.best_model_param))
    def _negamax(self, depth, player, alpha, beta):
        # If game is over or depth limit reached
        if depth == 0 or self._board.is_game_over():
            return Evaluator.eval(self._board, self._mycolor) * (-1 if player != self._mycolor else 1)

        color = self._board._flip(player)

        # If player cannot move, opponent turn
        if not self._board.at_least_one_legal_move(player):
            return -self._negamax(depth - 1, color, -beta, -alpha)

        value = - sys.maxsize - 1
        for m in self._board.legal_moves():
            self._board.push(m)
            value = max(value, -self._negamax(depth - 1, color, -beta, -alpha))
            self._board.pop()
            alpha = max(alpha, value)
            if alpha >= beta:
                break  # Cutoff
        return value
Beispiel #9
0
  def run(self, batches):
    # training and validation error collector
    ec = ErrorCollector()

    print("-----ModelTester-----")
    logging.info("-----ModelTester-----")

    # create models
    for n_hidden in self.n_hidden:
      for n_layer in self.n_layer:
        for rnn_unit in self.rnn_unit:
            print("RNN with max seq len: ", batches.max_seq_len)
            self.models.append(RnnModel(self.n_symbols, n_hidden, self.n_out,
                                        rnn_unit, batches.max_seq_len, n_layer, self.adam_optimizer))
    
    # training and evaluation
    plot_id = 0
    for model in self.models:
      trainer = Trainer(model, batches, ec)
      for learning_rate in self.learning_rates:
        plot_id += 1
        trainer.train(learning_rate, self.epochs, adam_optimizer=self.adam_optimizer, early_stopping=True, early_stop_lim=10)
        # print error plots
        ec.plotTrainTestError(model, batches.batch_size, learning_rate, self.epochs, plot_id)
        ec.resetErrors()
        evaluator = Evaluator(model, batches, trainer.getSaveFilePath())
        test_loss = evaluator.eval()

        # get best conversion time
        if trainer.best_epoch < self.best_conv_time:
          self.best_conv_time = trainer.best_epoch
          self.best_model_param = 'Param_' + model.name + '_ep-' + str(self.epochs) + '_hidu-' + str(model.n_hidden) + '_hidl-' + str(model.n_layer) + '_lr-' + str(learning_rate) + '_id-' + str(plot_id)

        # reset scores
        trainer.resetBestScore()

    # print convergence times
    ec.convergenceTimeMeanAndStd()

    print("-----ModelTester finished, best test error: [%.6f] and conv time (epochs): [%i] with model: %s " % (self.best_test_loss, self.best_conv_time, self.best_model_param))
    logging.info("-----ModelTester finished, best test error: [%.6f] and conv time (epochs): [%i] with model: %s " % (self.best_test_loss, self.best_conv_time, self.best_model_param))
Beispiel #10
0
    epochs = 5
    #learning_rate = 0.001
    learning_rate = 0.01
    model = Model(n_in=X.shape[1], n_hidden=300, n_out=26, n_layer=1)
    batch_size = 40

    # setup logging
    log_file_name = 'logs' + os.sep + 'Log' + '_ep' + str(
        epochs) + '_hidu' + str(model.n_hidden) + '_hidl' + str(
            model.n_layer) + '_lr' + str(learning_rate) + '.log'
    logging.basicConfig(filename=log_file_name, level=logging.INFO)

    # Batch Normalize
    bn = BatchNormalizer(X, C, batch_size=batch_size, shuffle=True)
    train_batches = bn.getBatches(X, C, is_validation=False)
    test_batches = bn.getBatches(X_tst, C_tst, test=True)

    print('examples to train: ', train_batches.examples_train.shape)

    # Training
    trainer = Trainer(model, train_batches, ec)
    trainer.train(learning_rate, epochs, early_stop_lim=25)

    # print error plots
    ec.plotTrainTestError(model, batch_size, learning_rate, epochs)
    ec.plotTrainTestAcc(model, batch_size, learning_rate, epochs)

    # Testing
    evaluator = Evaluator(model, test_batches, trainer.getSaveFilePath())
    evaluator.eval()