예제 #1
0
def Maya_MCTS(game: ".game.Game"):
    while True:
        copyTree = copy.deepcopy(game)
        player = copyTree.current_player
        print("--------------------simulate start!!----------------")
        #探索編
        candidates = getCandidates(copyTree)
        print("getCandidates")
        if len(candidates) == 0:
            print("len(candidates)==0")
            return ExceptionPlay.VALID
            pass
        takingAction = try_montecarlo_tree_search(copyTree,
                                                  candidates,
                                                  _trialPerTree=1000)
        print("--------------------simulate end!!------------------")
        print(takingAction)
        # iterate over our hand and play whatever is playable
        #多分executeActionで大丈夫だろ
        exc = executeAction(game, takingAction)
        postAction(player)
        if exc == ExceptionPlay.GAMEOVER:
            return ExceptionPlay.GAMEOVER
        else:
            continue
    return ExceptionPlay.VALID
    pass
예제 #2
0
 def Maya_MCTS(self,
               thisgame: ".game.Game",
               option=[],
               gameLog=[],
               debugLog=False):
     player = thisgame.current_player
     print("--------------------simulate start!!----------------")
     while True:
         #探索編
         candidates = getCandidates(thisgame, _includeTurnEnd=True)
         if len(candidates) == 0:  #ここは1になっていたが、0だと思われる。
             print("len(candidates)==0")
             return ExceptionPlay.VALID
             pass
         takingAction = try_montecarlo_tree_search(thisgame, candidates)
         print("--------------------simulate end!!------------------")
         # print(takingAction)
         # iterate over our hand and play whatever is playable
         #多分executeActionで大丈夫だろ
         if takingAction.type == ExceptionPlay.TURNEND:
             return ExceptionPlay.VALID
             pass
         exc = executeAction(thisgame, takingAction)
         postAction(player)
         if exc == ExceptionPlay.GAMEOVER:
             return ExceptionPlay.GAMEOVER
         else:
             continue
     return ExceptionPlay.VALID
     pass
예제 #3
0
def AngryCatAIold(thisGame: Game, option=[], debugLog=True):
    while True:
        myCandidates = getCandidates(thisGame)
        if len(myCandidates) == 0:
            return
        else:
            myChoice1 = myChoice2 = myChoice3 = []
            M1 = M2 = M3 = 0
            for myChoice in myCandidates:
                hisMin, myPositive, myBigPositive = getNegativity(
                    getDiffHisWorth(thisGame, myChoice))
                myMax = -hisMin
                if M1 < myMax:
                    M1 = myMax
                    myChoice1 = [myChoice]
                elif M1 > 0 and M1 == myMax:
                    myChoice1.append(myChoice)
                if M2 < myPositive:
                    M2 = myPositive
                    myChoice2 = [myChoice]
                elif M2 > 0 and M2 == myPositive:
                    myChoice2.append(myChoice)
                if M3 < myBigPositive:
                    M3 = myBigPositive
                    myChoice3 = [myChoice]
                elif M3 > 0 and M3 == myBigPositive:
                    myChoice3.append(myChoice)
            myChoices = myChoice1 + myChoice2 + myChoice3
            if len(myChoices) == 0:
                return
            else:
                myChoice = random.choice(myChoices)
                executeAction(thisGame, myChoice, debugLog=True)
                postAction(thisGame.current_player)
 def __init__(self, upgradeNum, repoCache, entrySearchCallback):
     '''Init for top bar.'''
     # Init.
     self.paddingX = 5
     self.numColor = '#00BBBB'
     self.textColor = '#1A3E88'
     self.repoCache = repoCache
     self.entrySearchCallback = entrySearchCallback
     
     self.box = gtk.HBox()
     self.boxAlign = gtk.Alignment()
     self.boxAlign.set(0.0, 0.5, 1.0, 1.0)
     self.boxAlign.set_padding(0, 0, TOPBAR_PADDING_LEFT, TOPBAR_PADDING_RIGHT)
     self.boxAlign.add(self.box)
     self.eventbox = gtk.EventBox()
     drawTopbar(self.eventbox)
     self.numLabel = gtk.Label()
     
     # Add search entry and label.
     (self.searchEntry, searchAlign, self.searchCompletion) = newSearchUI(
         "请输入你要卸载的软件名称、版本或其他信息",
         lambda text: utils.getCandidates(self.repoCache.uninstallablePkgs, text),
         self.clickCandidate,
         self.search)
     
     # Connect.
     self.updateNum(upgradeNum)
     self.numLabel.set_alignment(0.0, 0.5)
     self.box.pack_start(self.numLabel, True, True, self.paddingX)
     self.box.pack_start(searchAlign)
     self.eventbox.add(self.boxAlign)
예제 #5
0
 def expandChild(self, action):
     self.originalGame = copy.deepcopy(self.gameTree)
     executeAction(self.gameTree, action)
     postAction(self.gameTree.current_player)
     child = Node(self.gameTree, action, self, getCandidates(self.gameTree))
     self.childNodes.append(child)
     self.gameTree = self.originalGame
     return child
예제 #6
0
def AngryCatAI(thisGame: Game, option=[2, 1, 1, 1, 1, 1, 1], debugLog=True):
    while True:
        myCandidates = getCandidates(thisGame)
        if len(myCandidates) == 0:
            return
        else:
            for myChoice in myCandidates:
                myChoice.score = getDiffHisWorth(thisGame, myChoice)
            myChoice = myChoiceAngryCat(thisGame, myCandidates)
            if myChoice == None:
                return
            else:
                executeAction(thisGame, myChoice, debugLog=debugLog)
                postAction(thisGame.current_player)
예제 #7
0
 def expandChild(self, action):
     self.expandingGame = copy.deepcopy(self.gameTree)
     simcand = getCandidates(self.expandingGame, _includeTurnEnd=True)
     myPolicy = ""
     for item in simcand:
         if item == action:
             myPolicy = item
             pass
         pass
     exc = executeAction(self.expandingGame, myPolicy, debugLog=False)
     postAction(self.expandingGame.current_player)
     if exc == ExceptionPlay.GAMEOVER:
         print("the game has been ended.")
         child = Node(self.expandingGame, action, self, [])
         self.childNodes.append(child)
         return child
         pass
     elif action.type == ExceptionPlay.TURNEND:
         self.expandingGame.end_turn()
         pass
     child = Node(self.expandingGame, action, self,
                  getCandidates(self.expandingGame, _includeTurnEnd=True))
     self.childNodes.append(child)
     return child
def StandardRandom(thisgame: ".game.Game", option=[], debugLog=False):
    player = thisgame.current_player
    loopCount = 0
    while loopCount < 20:
        loopCount += 1
        myCandidate = getCandidates(thisgame)
        if len(myCandidate) > 0:
            myChoice = random.choice(myCandidate)
            exc = executeAction(thisgame, myChoice, debugLog=debugLog)
            postAction(player)
            if exc == ExceptionPlay.GAMEOVER:
                return ExceptionPlay.GAMEOVER
            else:
                continue
        return ExceptionPlay.VALID
def StandardStep1(game: ".game.Game", option=None, debugLog=True):
    debug = False
    if option == None:
        print("StandardStep1 needs an option")
        return ExceptionPlay.INVALID
    myWeight = option
    myCandidate = getCandidates(game)
    myChoices = []
    maxScore = -100000
    maxChoice = None
    if debug:
        print(">>>>>>>>>>>>>>>>>>>")
    for myChoice in myCandidate:
        tmpGame = copy.deepcopy(game)
        if executeAction(tmpGame, myChoice,
                         debugLog=False) == ExceptionPlay.GAMEOVER:
            score = 100000
        else:
            if StandardRandom(
                    tmpGame,
                    debugLog=False) == ExceptionPlay.GAMEOVER:  #ここをもっと賢くしてもよい
                score = 100000
            else:
                score = getStageScore(tmpGame, myWeight)
        if debug:
            print("%s %s %s %f" %
                  (myChoice.card, myChoice.type, myChoice.target, score))
        if score > maxScore:
            maxScore = score
            myChoices = [myChoice]
            if score == 100000:
                break
        elif score == maxScore:
            myChoices.append(myChoice)
    if debug:
        print("<<<<<<<<<<<<<<<<<<<")
    if len(myChoices) > 0:
        myChoice = random.choice(myChoices)
        ret = executeAction(game, myChoice, debugLog=debugLog)
        if ret == ExceptionPlay.GAMEOVER:
            return ExceptionPlay.GAMEOVER
        if ret == ExceptionPlay.INVALID:
            return ExceptionPlay.INVALID
        player = game.current_player
        postAction(player)
        return StandardStep1(game, option=myWeight, debugLog=debugLog)
    else:
        return ExceptionPlay.VALID
예제 #10
0
def simulate_random_turn(game: ".game.Game"):
    #申し訳ないがちょっとだけ賢い可能性がある
    player = game.current_player
    while True:
        #getCandidate使った方が早くないか?
        # iterate over our hand and play whatever is playable
        simCandidates = getCandidates(game, _includeTurnEnd=True)
        index = int(random.random() * len(simCandidates))
        if simCandidates[index].type == ExceptionPlay.TURNEND:
            game.end_turn()
            return ExceptionPlay.VALID
        exc = executeAction(game, simCandidates[index], debugLog=False)
        postAction(player)
        if exc == ExceptionPlay.GAMEOVER:
            return ExceptionPlay.GAMEOVER
        else:
            continue
            pass
예제 #11
0
def simulate_random_turn(game: ".game.Game"):
    #申し訳ないがちょっとだけ賢い可能性がある
    player = game.current_player
    while True:
        #getCandidate使った方が早くないか?
        # iterate over our hand and play whatever is playable
        simCandidates = getCandidates(game)
        index = int(random.random() * (len(simCandidates) + 1))
        if index == len(simCandidates):
            pass
        else:
            exc = executeAction(game, simCandidates[index])
            postAction(player)
            if exc == ExceptionPlay.GAMEOVER:
                return ExceptionPlay.GAMEOVER
            else:
                continue
                pass
        game.end_turn()
        return ExceptionPlay.VALID
    def __init__(self, pageId, keyword, itemNum, repoCache,
                 exitSearchPageCallback, searchCallback, clickCandidateCallback):
        '''Init for top bar.'''
        self.repoCache = repoCache
        self.paddingX = 5
        self.box = gtk.HBox()
        self.boxAlign = gtk.Alignment()
        self.boxAlign.set(0.0, 0.5, 1.0, 1.0)
        if itemNum > 20:
            paddingRight = TOPBAR_SEARCH_RIGHT
        else:
            paddingRight = TOPBAR_SEARCH_ADJUST_RIGHT
        self.boxAlign.set_padding(0, 0, TOPBAR_PADDING_LEFT, paddingRight)
        self.boxAlign.add(self.box)
        self.eventbox = gtk.EventBox()
        drawTopbar(self.eventbox)
        self.eventbox.add(self.boxAlign)
        self.keywordLabel = gtk.Label()
        self.numLabel = gtk.Label()
        self.updateTopbar(keyword, itemNum)

        # Add search entry and label.
        (self.searchEntry, searchAlign, self.searchCompletion) = newSearchUI(
            "请输入你要卸载的软件名称、版本或其他信息",
            lambda text: utils.getCandidates(self.repoCache.uninstallablePkgs, text),
            clickCandidateCallback,
            searchCallback)
        
        # Add return button.
        (returnButton, returnButtonAlign) = newActionButton(
            "search", 1.0, 0.5, "cell", False, "返回", BUTTON_FONT_SIZE_MEDIUM, "#FFFFFF",
            0, 10
            )
        returnButton.connect("button-release-event", lambda widget, event: exitSearchPageCallback(pageId))
        
        # Connect widgets.
        self.box.pack_start(self.keywordLabel, False, False, self.paddingX)
        self.box.pack_start(self.numLabel, False, False, self.paddingX)
        self.box.pack_start(searchAlign, True, True, self.paddingX)
        self.box.pack_start(returnButtonAlign, False, False)
예제 #13
0
 def StandardStep1(self, game, option=None, gameLog=[], debugLog=True):
     debugChoice = False  ###  Display parameters and scores
     if option == None:
         print("StandardStep1 needs an option")
         return ExceptionPlay.INVALID
     myWeight = option
     loopCount = 0
     while loopCount < 20:
         loopCount += 1
         if debugChoice:
             print(">>>>>>>>>>>>>>>>>>>")
         myCandidate = getCandidates(game)  #「何もしない」選択肢は入れていない
         myChoices = [
             Candidate(None, type=ExceptionPlay.TURNEND, turn=game.turn)
         ]  #何もしない選択
         maxScore = self.getStageScore(game, myWeight,
                                       debugChoice)  #何もしないときのスコア
         if debugChoice:
             print("   %s %d" % (myChoices[0], maxScore))
         maxChoice = None
         for myChoice in myCandidate:
             tmpGame = fireplace_deepcopy(game)
             #tmpGame = copy.deepcopy(game)
             log.info("Estimate the score for [%s]" % (myChoice))
             result = executeAction(tmpGame, myChoice, debugLog=False)
             postAction(tmpGame.current_player)
             if result == ExceptionPlay.INVALID:
                 stop = True
             if result == ExceptionPlay.GAMEOVER:
                 score = 100000
             else:
                 if self.__standard_agent__.StandardRandom(
                         tmpGame, debugLog=False
                 ) == ExceptionPlay.GAMEOVER:  #ここをもっと賢くしてもよい
                     score = 100000
                 else:
                     score = self.getStageScore(tmpGame, myWeight,
                                                debugChoice)
             if debugChoice:
                 print("   %s %d" % (myChoice, score))
             if score > maxScore:
                 maxScore = score
                 myChoices = [myChoice]
                 if score == 100000:
                     break
             elif score == maxScore:
                 myChoices.append(myChoice)
         if debugChoice:
             print("<<<<<<<<<<<<<<<<<<<")
         if len(myChoices) > 0:
             myChoice = random.choice(myChoices)
             if myChoice.type == ExceptionPlay.TURNEND:
                 if debugLog:
                     print(">%s -> turnend." % (self.name))
                 return ExceptionPlay.VALID
             ret = executeAction(game, myChoice, debugLog=debugLog)
             if ret == ExceptionPlay.GAMEOVER:
                 return ExceptionPlay.GAMEOVER
             if ret == ExceptionPlay.INVALID:
                 return ExceptionPlay.INVALID
             player = game.current_player
             postAction(player)
             continue
         else:
             return ExceptionPlay.VALID
예제 #14
0
def try_montecarlo_tree_search(_game,
                               _candidates=[],
                               _trialPerTree=50,
                               _numOfTree=10):
    from fireplace.deck import Deck
    copyGame = copy.deepcopy(_game)  #フルコピーをとる。
    myPlayer = copyGame.current_player  #呼び出した「自分」
    enemy = myPlayer.opponent  #呼び出した「相手」
    handNum = len(enemy.hand)  #相手のハンドの枚数
    totalScores = []
    if len(_candidates) == 0:  #事前にはじいているので、これは起こらない。
        return
        pass
    if len(_candidates) == 1:  #そもそもアクション候補が1つなら、そのアクションを行う。
        return _candidates[0]
        pass
    for i in range(_numOfTree):
        #シミュレーション下準備(この下準備は運営側で提供すべき。具体的にはdeepcopyのときに組み込むべき。)
        #random_sampling
        exclude = []  # 除外すべきカードは、読み込みを停めている。
        d = random_draft(enemy.hero.card_class,
                         exclude)  #カードクラスに従ったランダムなデッキ。第1引数はクラス名に変更。
        enemy.hand = CardList()  #敵のハンドをクリア
        enemy.deck = Deck()  #敵のデッキをクリア
        for item in d:  #敵のデッキを更新
            enemy.card(item, zone=Zone.DECK)
            pass
        enemy.draw(count=handNum)  #敵のハンドを更新
        #ゲーム木展開
        #あとでcandidatesをpopするからそのまま使うと_candidatesは空説
        cand = getCandidates(copyGame, _includeTurnEnd=True)
        root = Node(copyGame, None, None, cand)  #ファイル内クラス
        for k in range(_trialPerTree):
            current_node = root
            while len(current_node.untriedMoves) == 0 and len(
                    current_node.childNodes) != 0:
                current_node = current_node.selectChild()
            if len(current_node.untriedMoves) != 0:
                expanding_action = current_node.choose_expanding_action()
                current_node = current_node.expandChild(expanding_action)
            result = current_node.simulate()
            current_node.backPropagate(result)
        visitScores = list(
            map(lambda node: myActionValue(node.move, node.visits),
                root.childNodes))
        totalScores = addActionValues(totalScores, visitScores)
        print("totalScores")
        for item in totalScores:
            print("{action} -->{score}".format(action=item.action,
                                               score=item.score))
            pass
    maxScore = max(
        list(map(lambda actionValue: actionValue.score, totalScores)))
    retAction = 0
    for item in totalScores:
        if item.score == maxScore:
            retAction = item.action
            pass
        pass
    print(retAction)
    for item in _candidates:
        if item == retAction:
            retAction = item
            pass
        pass
    #time.sleep(5)
    return retAction
    pass