コード例 #1
0
ファイル: monte.py プロジェクト: brigan/rego
def MonteCarlo(log, board, ourColor, Bplayer, Wplayer):

    numRuns = 5
    movesPerRun = 10

    lettList = ["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T"]

    BListLegal = Bplayer.getListLegal()
    WListLegal = Wplayer.getListLegal()

    if ourColor == "b":
        ListLegal = BListLegal
    else:
        ListLegal = WListLegal

    # renormalize the board and get the chosen cluster. Then we will choose within the cluster using MonteCarlo
    renBoard = board.renormalize()
    cluster = mR.MonteCarloRen(renBoard, ourColor, 2)

    movesToCheck = [p for p in cluster if p in ListLegal]

    winProbList = []
    for p in movesToCheck:

        tryWinList = []
        for do in range(numRuns):
            """Initial values  """
            virBoard = board.copy()

            ourPlayer = Player(ourColor, virBoard, ListLegal)
            if ourColor == "b":
                oppPlayer = Player("w", virBoard, WListLegal)
            else:
                oppPlayer = Player("b", virBoard, BListLegal)

            # our player play first
            move = ourPlayer.numToMove([ourColor, p])
            moveObject = M.Move(posLetter=lettList[move[2]], posNumber=19 - move[1], color=move[0])
            virBoard.update(moveObject)
            ourPlayer.updateListLegal(move, virBoard.getKilled())  ### insult
            oppPlayer.updateListLegal(move, virBoard.getKilled())  ### insult
            ourListLegal = ourPlayer.getListLegal()
            oppListLegal = oppPlayer.getListLegal()

            # Playing randomly with two slave players on a pseudo board
            moveNum = 0
            while (ourListLegal != [] and oppListLegal != []) and moveNum < movesPerRun:

                # gen move for opponent
                move = oppPlayer.genMove()
                if move != None:
                    virBoard.update(move)
                    ourPlayer.updateListLegal(move.getListFormat(), virBoard.getKilled())
                    oppPlayer.updateListLegal(move.getListFormat(), virBoard.getKilled())
                    ourListLegal = ourPlayer.getListLegal()
                    oppListLegal = oppPlayer.getListLegal()

                # gen move for us
                move = ourPlayer.genMove()
                if move != None:
                    virBoard.update(move)
                    ourPlayer.updateListLegal(move.getListFormat(), virBoard.getKilled())
                    oppPlayer.updateListLegal(move.getListFormat(), virBoard.getKilled())
                    ourListLegal = ourPlayer.getListLegal()
                    oppListLegal = oppPlayer.getListLegal()

                moveNum += 1

            # end of the while

            # gen move for opponent
            move = oppPlayer.genMove()
            if move != None:
                virBoard.update(move)
                ourPlayer.updateListLegal(move.getListFormat(), virBoard.getKilled())
                oppPlayer.updateListLegal(move.getListFormat(), virBoard.getKilled())
                ourListLegal = ourPlayer.getListLegal()
                oppListLegal = oppPlayer.getListLegal()

            BListLegal = Bplayer.getListLegal()
            WListLegal = Wplayer.getListLegal()

            tryWinList.append(virBoard.evaluate())
            # print 'completed run ',do,'/',numRuns-1

        # end of runs

        numB = tryWinList.count("Black")
        numW = tryWinList.count("White")

        if ourColor == "b":
            prob = float(numB) / (numB + numW)
        else:
            prob = float(numW) / (numB + numW)

        winProbList.append(prob)
        # print 'completed evaluation of position ',ListLegal.index(p),'/',len(ListLegal)-1

    # end of evaluation of move p
    # print winProbList

    maxValue = max(winProbList)
    winProbList = np.array(winProbList)
    maxIdxs = np.where(winProbList == maxValue)[0]
    maxIndex = iran.choice(maxIdxs)

    # print maxIndex

    # max_index=winProbList.index(max(winProbList))   # improve this if more than one max
    move = ourPlayer.numToMove([ourColor, movesToCheck[maxIndex]])

    # print 'winProbList: ',winProbList
    # print 'best move: ',move
    return M.Move(posLetter=lettList[move[2]], posNumber=19 - move[1], color=move[0])
コード例 #2
0
ファイル: rego.py プロジェクト: tizyweb/rego
class ReGo:
    
    def __init__(self):
        """
        Constructor of the class ReGo
        """
    
        # known commands
        self.knownCmds = [ 'protocol_version' , 'name' , 'version' , 'known_command' , 'list_commands' ,'quit' , 'boardsize' , 'clear_board' , 'komi' , 'play' , 'genmove' ]
        
        # open log
        self.log = Log()
        self.log.open()
        
        # board
        self.board = Board(19,self.log)
        
        # score
        self.score = 0.0
        
        # players
        self.bPlayer = Player('b',self.board,self.log)
        self.wPlayer = Player('w',self.board,self.log)    
        
    
    
    def __decodeLine__(self,line):
        """
        Decodes an input line read from stdin
        """
        vals = line.split(' ')
        return (vals[0],vals[1:])
    
    def __decodeColor__(self,colorStr):
        """
        Decodes a color string
        """
        if colorStr.upper() in ('B','BLACK'):
            return 'b'
            
        elif colorStr.upper() in ('W','WHITE'):
            return 'w'
        else:
            raise SyntaxError('illegal color')
    
    def __decodePosition__(self,posStr):
        """
        Decodes a position string
        """
        try:
            posLetter = posStr[0].upper()
            posNumber = int(posStr[1:])
        except Exception:
            raise SyntaxError('invalid position')
        else:
            return posLetter,posNumber
    
    def __handleError__(self):
        """
        Checks for the exception raised and returns the error string. It logs also the exception stack-trace in case of an unexpected error.
        """
        e_type, e, tb = sys.exc_info()
        
        # syntax error
        if e_type == SyntaxError:
            output_line = '? syntax error: '+e.message+'\n\n'
        
        # consistency error
        elif e_type == ConsistencyError:
            output_line = '? consistency error: '+e.message+'\n\n'

        # unexpected error
        else:
            output_line = '? unexpected error\n\n'
            self.log.logException(e_type, e, tb )
            
        return output_line
        
    def protocol_version(self,args):
        """
        Prints GTP protocol version
        """
        return '2'
    
    def name(self,args):
        """
        Prints the name of this GO engine
        """
        return 'Rego'
    
    def version(self,args):
        """
        Prints the version of this GO engine
        """
        return '0.1'
    
    def know_command(self,args):
        """
        Returns true if the command specified is known, false otherwise
        """
        if not args[0]:
            raise SyntaxError('invalid command name')
        
        if args[0] in self.knownCmds:
            return 'true'
        else:
            return 'false'    
           
    def list_commands(self,args):
        """
        Lists known commands
        """
        return ' '.join(self.knownCmds)
    
    def boardsize(self,args):
        """
        Set the new board size to the number specified by the argument
        """
        try:
            size = int(args[0])
        except Exception:
            raise SyntaxError('board size is not an integer')
        else:
            self.board = Board(int(args[0]),self.log)
            return ''
    
    def clear_board(self,args):
        """
        Clears the board
        """
        self.board.clear()
        self.bPlayer = Player('b',self.board,self.log)
        self.wPlayer = Player('w',self.board,self.log) 
        return ''
    
    def komi(self,args):
        """
        Sets the new komi to the float value specified as argument
        """
        try:
            komi = float(args[0])
        except Exception:
            raise SyntaxError('komi is not a float')
        else:
            self.score = float(args[0])
            return ''
    
    def play(self,args):
        """
        Updates the board according to the move specified as argument
        """
        color = self.__decodeColor__(args[0])
        posLetter,posNumber = self.__decodePosition__(args[1])
    
        m = Move(color=color,posLetter=posLetter,posNumber=posNumber)
        
        self.board.update(m)

        self.bPlayer.updateListLegal(m.getListFormat(),self.board.getKilled())
        self.wPlayer.updateListLegal(m.getListFormat(),self.board.getKilled())
        
        self.log.logDebug(self.board.showBoard())
                
        return ''
    
    def genmove(self,args):
        """
        Generate a new move for the player specified as argument
        """
        
        
        move = MonteCarlo(self.log,self.board, self.__decodeColor__(args[0]), self.bPlayer, self.wPlayer)
        
        self.board.update(move)     
        
        self.bPlayer.updateListLegal(move.getListFormat(),self.board.getKilled())
        self.wPlayer.updateListLegal(move.getListFormat(),self.board.getKilled())
        
        self.log.logDebug(self.board.showBoard())

        return move.encode()
    
    
    def execute(self):
        """
        Main method: reads a command from stdin and writes the output to stdout
        
        """
        
        # read first input
        line = raw_input()
        self.log.logInput(line)
        cmd, args = self.__decodeLine__(line)

        # main loop
        while not cmd == 'quit':
    
            if cmd in self.knownCmds:
                method = getattr(self,cmd)
            
                try:    
                    output = method(args)
                except Exception:
                    output_line = self.__handleError__()
                    
                else:
                    output_line = '= '+output+'\n\n'
        
            else:
                output_line  = '? unknown command\n\n'
            

            # write back the output line to stdout
            sys.stdout.write(output_line)
            sys.stdout.flush()
                        
            # log the output            
            self.log.logOutput(output_line)
            
            # read the next input
            line = raw_input()
            self.log.logInput(line)
            cmd, args = self.__decodeLine__(line)
        
        self.log.close()