def UpdateInvalidMove(self, board, move):
        super().UpdateInvalidMove(board, move)

        key = BoardToKey(board) + "," + str(move)
        if key in self.SimOuputCache:
            del self.SimOuputCache[key]

        key = BoardToKey(board)
        if key in self.MoveListCalCache:
            del self.MoveListCalCache[key]

        return
	def RecordMove(self, board, move):
		if not self.RecordMoves:
			return

		self.DataSetManager.MetaDataAdd("TriedMovesPlayed", 1)

		key = BoardToKey(board)
		moveID = self.DataSetManager.MoveIDLookUp.index(move)
		found, boardInfo = self.DataSetManager.GetBoardInfo(key)

		if not found:  # never played board before
			self.DataSetManager.AddNewBoard(key, board)
			found, boardInfo = self.DataSetManager.GetBoardInfo(key)
		
		with boardInfo.Lock:
			boardInfo.BeingUsed = True
			# never played this move before
			if moveID not in boardInfo.Moves:
				boardInfo.Moves[moveID] = MoveInfo.MoveInfo()

			# mark move as played if never played before
			if boardInfo.PlayedMovesLookUpArray < self.AllMovesPlayedValue:

				if not (2**moveID & boardInfo.PlayedMovesLookUpArray):
					boardInfo.PlayedMovesLookUpArray += 2**moveID

				if boardInfo.PlayedMovesLookUpArray >= self.AllMovesPlayedValue:
					self.DataSetManager.MetaDataAdd("NumberOfCompleteBoards", 1)
				

		if self.RecordMoves:
			self.TempDataSet[str(key)+str(moveID)] = {"BoardKey": key, "MoveID": moveID, "MoveNumber":self.MoveNumber}
			self.MoveNumber += 1

		return
	def MakeAgentMove(self, turn, board, agents):
		timeMark  = time.time()

		startBoardKey = BoardToKey(board)

		agent = agents[turn-1]
		valid = False
		while not valid:
			move = agent.MoveCal(board)
			
			self.OutcomePredictor.PredictOutput(board, move)

			valid, outComeBoard, turn = self.Game.MakeMove(move)

			if not valid:
				agent.UpdateInvalidMove(board, move)

				if self.OutcomePredictor != None:
					self.OutcomePredictor.UpdateInvalidMove(board, move)


		finished, fit = self.Game.CheckFinished()

		if self.OutcomePredictor != None:
			self.OutcomePredictor.UpdateMoveOutCome(startBoardKey, move, outComeBoard, finished)

		agent.UpdateMoveOutCome(startBoardKey, move, outComeBoard, finished)

		self.MetricsLogger.Log("Agent"+str(agent.AgentNumber)+"MoveTook", time.time()-timeMark)
		return outComeBoard, turn, finished, fit
    def PredictOutput(self, board, move):
        newBoard = []

        key = BoardToKey(board)
        found, boardInfo = self.DataSetManager.GetBoardInfo(key)

        if found:
            moveID = self.DataSetManager.MoveIDLookUp.index(move)
            if moveID in boardInfo.Moves:
                with boardInfo.Lock:
                    moveOutComes = boardInfo.Moves[moveID].MoveOutComes

                    highestTimes = 0
                    for outCome, times in moveOutComes.items():

                        if highestTimes < times:
                            newBoard = outCome
                            highestTimes = times

        self.Predictions[str(key) + str(move)] = {
            "BoardKey": key,
            "Move": move,
            "Prediction": newBoard
        }
        self.NumPredictions += 1
        return newBoard
    def CalBoardValue(self, board):
        key = BoardToKey(board)

        if key in self.ValueCache:
            return self.ValueCache[key]

        value = self.ValuePredictor.PredictValue(board)
        self.ValueCache[key] = value
        return value
    def CalSimOuput(self, board, move):
        key = BoardToKey(board) + "," + str(move)

        if key in self.SimOuputCache:
            return self.SimOuputCache[key]

        predictedBoard = self.BoardPredictor.PredictOutput(board, move)
        self.SimOuputCache[key] = predictedBoard
        return predictedBoard
    def GetAgentMove(self, board):
        key = BoardToKey(board)

        if key in self.MoveListCalCache:
            return self.MoveListCalCache[key]

        moveList = self.MoveAgent.MoveListCal(board)
        self.MoveListCalCache[key] = moveList
        return moveList
Exemplo n.º 8
0
    def PredictValue(self, board):
        value = 0

        key = BoardToKey(board)
        found, boardInfo = self.DataSetManager.GetBoardInfo(key)

        if found:
            value = boardInfo.TotalAvgFitness

        self.NumPredictions += 1
        return value
    def Predict(self, board):
        key = BoardToKey(board)

        if key in self.PredictionCache:
            output = self.PredictionCache[key]
        else:
            output = self.NetworkModel.predict(board)
            if len(self.PredictionCache) >= self.MaxCacheSize:
                self.PredictionCache = {}
            self.PredictionCache[key] = output

        return output
    def UpdateMoveOutCome(self, boardKey, move, outComeBoard, finished):

        if str(boardKey) + str(move) in self.Predictions:
            prediction = self.Predictions[str(boardKey) +
                                          str(move)]["Prediction"]

            outComeBoardKey = BoardToKey(outComeBoard)
            if finished:
                outComeBoardKey = "GameFinished"

            if prediction != outComeBoardKey:
                self.NumWrongPredictions += 1
        return
    def PredictionToMove(self, dataSetManager, networkOutput, board):

        if dataSetManager.MetaDataGet("NetworkUsingOneHotEncoding"):
            key = BoardToKey(board)
            found, boardInfo = dataSetManager.GetBoardInfo(key)

            output = dataSetManager.MoveIDToMove(0)
            bestValue = -sys.maxsize

            for loop in range(len(networkOutput)):
                invaild = False

                if found:
                    if 2**loop & boardInfo.PlayedMovesLookUpArray:
                        if loop not in boardInfo.Moves:
                            invaild = True

                if networkOutput[loop] >= bestValue and (not invaild):
                    bestValue = networkOutput[loop]
                    output = dataSetManager.MoveIDToMove(loop)

            for loop in range(len(networkOutput)):
                if networkOutput[loop] >= bestValue:
                    self.NumBetterValueInvailds += 1

            self.NumBetterValueInvailds -= 1

        else:
            output = []
            for loop in range(len(networkOutput)):
                temp = networkOutput[loop]
                temp = temp / dataSetManager.OutputResolution
                temp = round(temp)
                temp = temp * dataSetManager.OutputResolution

                if temp < dataSetManager.MinOutputSize:
                    temp = dataSetManager.MinOutputSize

                if temp > dataSetManager.MaxOutputSize:
                    temp = dataSetManager.MaxOutputSize

                if dataSetManager.OutputResolution == int(
                        dataSetManager.OutputResolution):
                    temp = int(temp)

                output += [temp]

        return output
Exemplo n.º 12
0
	def UpdateInvalidMove(self, board, move):
		if not self.RecordMoves:
			return

		key = BoardToKey(board)
		moveID = self.DataSetManager.MoveIDLookUp.index(move)

		found, boardInfo = self.DataSetManager.GetBoardInfo(key)
		if found:
			with boardInfo.Lock:
				if moveID in boardInfo.Moves:
					del boardInfo.Moves[moveID]

		if str(key)+str(moveID) in self.TempDataSet:
			del self.TempDataSet[str(key)+str(moveID)]

		self.MoveNumber -= 1

		self.NumInvailds += 1
		return
Exemplo n.º 13
0
	def MoveListCal(self, board):
		key = BoardToKey(board)
		found, boardInfo = self.DataSetManager.GetBoardInfo(key)

		moves = []

		if found:
			with boardInfo.Lock:
				if boardInfo.PlayedMovesLookUpArray < self.AllMovesPlayedValue:
					for moveId in range(self.DataSetManager.MaxMoveIDs):
						if not (2**moveId & boardInfo.PlayedMovesLookUpArray):
							moves += [self.DataSetManager.MoveIDLookUp[moveId]]
				else:
					for movekey, moveValue in boardInfo.Moves.items():
						moves += [self.DataSetManager.MoveIDLookUp[movekey]]


		else:#never played board before
			for moveId in range(self.DataSetManager.MaxMoveIDs):
				moves += [self.DataSetManager.MoveIDLookUp[moveId]]

		return moves
Exemplo n.º 14
0
    def MoveCal(self, board):
        key = BoardToKey(board)
        found, boardInfo = self.DataSetManager.GetBoardInfo(key)

        if found:
            with boardInfo.Lock:
                movesIds = []
                for moveId in range(self.DataSetManager.MaxMoveIDs):

                    if not (2**moveId & boardInfo.PlayedMovesLookUpArray
                            ) or moveId in boardInfo.Moves:
                        movesIds += [moveId]

            pickedIndex = random.randint(0, len(movesIds) - 1)
            moveID = movesIds[pickedIndex]

        else:
            moveID = random.randint(0, self.DataSetManager.MaxMoveIDs - 1)

        move = self.DataSetManager.MoveIDLookUp[moveID]
        self.RecordMove(board, move)
        return move
Exemplo n.º 15
0
	def UpdateMoveOutCome(self, boardKey, move, outComeBoard, gameFinished=False):
		if not self.RecordMoves:
			return
		
		self.DataSetManager.MetaDataAdd("VaildMovesPlayed", 1)
		moveID = self.DataSetManager.MoveIDLookUp.index(move)
		found, boardInfo = self.DataSetManager.GetBoardInfo(boardKey)
		
		with boardInfo.Lock:
			if found and moveID in boardInfo.Moves:
				if gameFinished:
					outComeKey = "GameFinished"
				else:
					outComeKey = BoardToKey(outComeBoard)
	
				move = boardInfo.Moves[moveID]
				boardInfo.BeingUsed = False
				if outComeKey in move.MoveOutComes:
					move.MoveOutComes[outComeKey] += 1
				else:
					move.MoveOutComes[outComeKey] = 1
		
		self.NumMoves += 1
		return
Exemplo n.º 16
0
	def MoveCal(self, board):
		key = BoardToKey(board)
		found, boardInfo = self.DataSetManager.GetBoardInfo(key)

		moveID = None

		if self.WinningModeON:
			print("winnning mode!")
			if found and len(boardInfo.Moves) > 0:
				moveID = boardInfo.MoveIDOfBestAvgFitness

			else:#never played board before
				moveID = random.randint(0,self.DataSetManager.MaxMoveIDs-1)
				print("new Board!")


		else:  # learning mode
			if found:
				with boardInfo.Lock:
					if boardInfo.PlayedMovesLookUpArray < self.AllMovesPlayedValue:
						if key in self.MovesNotPlayedCache:
							moveID = self.MovesNotPlayedCache[key][0]
							del self.MovesNotPlayedCache[key][0]
	
							if len(self.MovesNotPlayedCache[key]) == 0:
								del self.MovesNotPlayedCache[key]
	
	
						else:
							notPlayedList = []
							for moveId in range(self.DataSetManager.MaxMoveIDs):
								if not (2**moveId & boardInfo.PlayedMovesLookUpArray):
									if moveID == None:
										moveID = moveId
									else:
										notPlayedList += [moveId]

							if len(notPlayedList) > 0:
								self.MovesNotPlayedCache[key] = notPlayedList
	
					else:#played every move once already
						nonFinishedLeastPlayed = sys.maxsize
						nonFinishedMoveID = -1
	
						finishedLeastPlayed = sys.maxsize
						finishedMoveID = -1
	
						for movekey, moveValue in boardInfo.Moves.items():
							
							if moveValue.TimesPlayed < nonFinishedLeastPlayed and not(boardInfo.Finished or self.IsMoveFinished(boardInfo, movekey)):
								nonFinishedLeastPlayed = moveValue.TimesPlayed
								nonFinishedMoveID = movekey

								if nonFinishedLeastPlayed == 1:
									break
	
							elif moveValue.TimesPlayed < finishedLeastPlayed:
								finishedLeastPlayed = moveValue.TimesPlayed
								finishedMoveID = movekey
	
						if nonFinishedMoveID != -1:
							moveID = nonFinishedMoveID
						else:
							moveID = finishedMoveID

						if moveID == -1:
							raise Exception("Cannot find move on board "+ str(board))

			else:#never played board before
				moveID = 0

		move = self.DataSetManager.MoveIDLookUp[moveID]
		self.RecordMove(board, move)
		return move