コード例 #1
0
def initializeVersusAIVariables():
	global playerOne
	global playerTwo
	global current
	global nextMove
	global board
	global agent
	P1 = 0
	P2 = 1
	# multiprocess computaion
	print "board", agent	
	board = mancala_board.Board()
	playerOne = Human(P1)
	# ai Player

	print agent, 'sdddddddddddddddddddddddddddd'
	if agent == "Minimax":
		playerTwo = minimax.AI(P2, 8)
	elif agent == "Genesis":
		playerTwo = genesis.genesis(P2, 3)
	elif agent == "MonteCarlo":
		playerTwo = mcts.MCTS(P2, mancala_board.Board())
	
	print "Versus AI Agent:", agent
	# starting player is random
	# current = random.randint(0,1)  # todo this line is a f*****g problem......
	current = None
	nextMove = random.randint(0,1)
	return jsonify({'initialized' : True})
コード例 #2
0
ファイル: minimax.py プロジェクト: jfarcalas/Mancala-AI-Bot
	def move_serial(self, board):
		alpha = -48
		beta = 48
		value = alpha
		i = move = 0
		# foreach move possible
		cut = False
		self.search_count = 0
		print 'AI Thinking...'
		# for each move, check if its valid, if so get the value of the next possible move
		while i < 6 and not cut:
			board_copy = mancala_board.Board(board)
			# if i is a valid move, else ignore
			if board_copy.check_move(self.player, i):
				next_player = board_copy.move(self.player, i)
				# if the next player has no move, change to other player
				if not board_copy.has_move(self.player):
					next_player = (next_player+1)%2
				# get next max move
				value = max(value, self.alphabeta(board_copy, alpha, beta, next_player, self.lookahead))
				if alpha < value:
					alpha = value
					move = i
				if alpha > beta:
					cut = True
			i+=1
		print 'Searched ', self.search_count, ' possibilities'
		return move
コード例 #3
0
ファイル: genesis.py プロジェクト: jfarcalas/Mancala-AI-Bot
 def move(self, board):
     valid = False
     move = 0
     while not valid:
         self.board = mancala_board.Board(board)
         self.generation = self.init_pop()
         self.test_pop(self.player)
         for i in range(50):
             self.gen_encode()
             self.gen_evolution()
             self.test_pop(self.player)  # to reinitialize total_fitness
             #print ("generation", i)
         x = 50
         temp_lookahead = self.lookahead
         while self.count_legal_moves(self.player) == 0:
             if self.lookahead > 1:
                 self.lookahead -= 1
             self.gen_encode()
             #print ("generation", x)
             self.gen_evolution()
             self.test_pop(self.player)  # to reinitialize total_fitness
             x += 1
         self.test_pop_and_prune(self.player)
         self.lookahead = temp_lookahead
         self.sortandreverse()
         #print ("selecting most move with fitness: ", self.generation[0][0][0], self.generation[0][0][1])
         # self.gen_prune()
         move = int(self.generation[0][0][0])
         valid = board.check_move(self.player, move)
     return move
コード例 #4
0
    def rollout(self, current, player):

        #SIMULATE
        # no action counter
        noActions = 0
        next = (player + 1) % 2

        success = 0
        loss = 0
        tie = 0

        # current should not move, so make copy of the board to simulate
        # Copy current board
        cp_board = mancala_board.Board(current.board)
        simulatedAction = Node(current.visits, current.score, cp_board)

        currPlayer = player

        while (not simulatedAction.board.game_over()):
            #if not game over then we expand
            hasAction = self.expansion(simulatedAction, currPlayer)
            if hasAction:
                # Has action so we randomly simulate a move
                chosenAction = random.choice(simulatedAction.actions)

                #Next player is based on the action taken from expansion
                next = chosenAction.next_player
                currPlayer = next

                simulatedAction = chosenAction
                noActions = 0

            else:
                if noActions > 1:
                    break
                noActions += 1
                #pass next
                currPlayer = next
                next = (next + 1) % 2

        #average the wins / total tries
    #			print( "player {} score: ".format(self.player),simulatedAction.board.get_score(self.player))
        if simulatedAction.board.get_score(
                self.player) > simulatedAction.board.get_score(
                    (self.player + 1) % 2):
            success += 1
        elif simulatedAction.board.get_score(
                self.player) < simulatedAction.board.get_score(
                    (self.player + 1) % 2):
            loss += 1
        else:
            tie += 1

    #		print("success", success)
    #		print("Loss   ", loss)
    #		print("Tied   ",tie )
        return success / (success + loss + tie)
コード例 #5
0
ファイル: minimax.py プロジェクト: jfarcalas/Mancala-AI-Bot
	def alphabeta(self, board, alpha, beta, player, depth):
		value = 0
		# cound does not update correct when threaded, only works with serial move
		self.search_count += 1
		# traverse entire game to find best move
		if board.game_over() or depth <= 0:
			value = self.eval_heuristic(board)
		elif player == self.player:
			cut = False
			value = -48
			i = 0
			while i < 6 and not cut:
				board_copy = mancala_board.Board(board)
								
				if board_copy.check_move(self.player, i):
					next_player = board_copy.move(self.player, i)
					value = max(value, self.alphabeta(board_copy, alpha, beta, next_player, depth-1))
					alpha  = max(value, alpha)
					if alpha >= beta:
						cut = True
				else: # penalize no moves
					alpha = -48
				i+=1
		else: # opponent
			cut = False
			value = 48
			i = 0
			# for each opponent move, check if its valid, if so get the value of the next possible move
			while i < 6 and not cut:
				board_copy = mancala_board.Board(board)
				# if i is a valid move
				if board_copy.check_move(self.opponent, i):
					next_player = board_copy.move(self.opponent, i)
					value = min(value, self.alphabeta(board_copy, alpha, beta, next_player, depth-1))
					beta  = min(value, beta)
					if alpha >= beta:
						cut = True
				else: # no moves
					beta = 48
				i+=1

		return value
コード例 #6
0
ファイル: minimax.py プロジェクト: jfarcalas/Mancala-AI-Bot
	def get_move_score(self, move):
		value = -50
		board_copy = mancala_board.Board(self.board)
		next_player = self.player
		# repeats are prioritized by increasing score
		while next_player == self.player and board_copy.check_move(self.player, move):
			next_player = board_copy.move(self.player, move)
			# if the next player has no move, change to other player
			if not board_copy.has_move(self.player):
				next_player = (next_player+1)%2
			value = max(value, self.alphabeta(board_copy, -48, 48, next_player, self.lookahead))

		return value
コード例 #7
0
ファイル: genesis.py プロジェクト: jfarcalas/Mancala-AI-Bot
    def get_strand_score(self, population, strand_id, player):
        # variable init
        strand = population[strand_id]
        board_copy = mancala_board.Board(self.board)
        strand_score = 0
        # for each move in strand test if move is possible option
        for i in range(0, len(strand)):
            board_copy.move(player, int(strand[i]))
            strand_score += self.eval_heuristic(board_copy)
            player = (player + 1) % 2

        # returns if move sequence is possible
        return strand_score
コード例 #8
0
    def move(self, board):

        # simulate n random games and return the best move
        #iterate = 20
        valid = False
        move = 0
        copy = mancala_board.Board(board)
        while not valid:
            self.root = Node(0, 0.0, copy)
            action = self.findMove(self.root, self.player,
                                   random.randint(2, 25))
            move = action.moveTaken
            print move
            valid = board.check_move(self.player, move)
        return move
コード例 #9
0
def initializeAIBattleVariables():
	global playerOne
	global playerTwo
	global current
	global nextMove
	global parallel
	global board
	P1 = 0
	P2 = 1
	# multiprocess computaion
	parallel = True
	lookahead = 8 # AI lookahead depth, set to negative to search entire game
	# board = Board() commented out, moved it to global scope in hopes dynamic
	playerOne = minimax.AI(P1, lookahead, relative_score= False)
	#ai_horder = AI(P2, lookahead, horde=True) # hordes pieces on its side
	playerTwo = minimax.AI(P2, lookahead, relative_score= True, horde=True, relative_horde=True) #horde relative is better then not
	nextMove = random.randint(0,1)
	starting_ai = nextMove
	current = None # ai with current turn
	board = mancala_board.Board()
	return jsonify({'initialized' : True})
コード例 #10
0
ファイル: genesis.py プロジェクト: jfarcalas/Mancala-AI-Bot
    def test_strand(self, population, strand_id, player, istuple=False):
        # variable init
        if istuple:
            strand = population[strand_id][0]
        else:
            strand = population[strand_id]
        strand_health = True
        board_copy = mancala_board.Board(self.board)

        # for each move in strand test if move is possible option
        i = 0
        while i < len(strand) and i < self.lookahead:
            # for i in range(0, len(strand)):
            if 0 == board_copy.board[player * 6 + int(strand[i])]:
                strand_health = False
            elif strand_health:
                board_copy.move(player, int(strand[i]))
                player = (player + 1) % 2
            i += 1

        # returns if move sequence is possible
        return strand_health
コード例 #11
0
    def expansion(self, current, player):
        #check if player has a move
        if current.board.has_move(player):
            #find all possible moves
            for i in range(0, 6):
                # check if move is valid
                if current.board.check_move(player, i):
                    # Copy current board
                    cp_board = mancala_board.Board(current.board)
                    # Make possible action
                    nextPlayer = cp_board.move(player, i)

                    #Store all possible moves in actions (pointers to childresn)
                    action = Node(0, 0.0, cp_board)
                    action.update_nextPlayer(nextPlayer)
                    action.update_move_taken(i)
                    current.actions.append(action)
            #Current has children
            current.hasChildren = True
            current.isLeaf = False
            return True
        else:
            return False
コード例 #12
0
def resetBoard():
	return mancala_board.Board()