【问题标题】:Decision Tree in PythonPython中的决策树
【发布时间】:2021-08-23 15:50:07
【问题描述】:

如何在python中实现DecisionTree的想法。 在井字游戏中。每个gameState字符串代表当前情况

  • 0 为空,1 为玩家 1 移动,2 为玩家 2 移动
    所以一个 ameState 的 3x3 板看起来像 '000200000'
    游戏中的 MovesTree 从头到尾看起来像
['000200000', '000210000', '002210000', '002211000', '002211020', '102211020', '102211022', '112211022', '112211222']  

问题:
如何在决策树中添加这些动作(假设来自 1000 个随机游戏),以便如果在任何阶段游戏状态存在于树中,则后续动作作为下一个分支附加到该节点。
PS。我的目标是找到 tictactoe 游戏的所有可能游戏路径并将它们保存到文件中。重新加载时,文件被加载以重新制作决策树,并且在游戏过程中,计算机可以在人类玩家采取行动后使用它来决定。

  • 列表很慢,预计有 9 个! > 362880 步,除了在所有 9 步完成之前游戏结束的地方
  • dict 将每个后续移动作为键,后续移动作为值作为 dict 键似乎最合乎逻辑,但我无法实现搜索,添加方法

【问题讨论】:

    标签: python decision-tree


    【解决方案1】:

    我能够按如下方式实现它

    def addGameMovesTreeToDataBaseTree(givenMovesTree:list):
        # Get global DataBaseTress
        global DataBaseTree
        # Get local copy of givenMovesTree
        MT = list()
        MT = givenMovesTree.copy() # Populate local MT from given givenMovesTree
        if len(MT) == 0: # Check if givenMovesTree is empty
            return
        elif len(MT) == 1: # Check if givenMovesTree has only last move
            Key = MT[0]
            if MT[0] in DataBaseTree.keys(): # Selected move exist as key in DataBaseTree
                pass # Dont do any thing
            else: # Selected move does not exisit in DataBaseTree
                Key = MT[0] # Store that move as Key
                DataBaseTree[Key] = [] # Add the key with value of empty list
        else: # Check if givenMovesTree has many moves
            if MT[0] in DataBaseTree.keys(): # Selected move exist as key in DataBaseTree
                Key = MT[0] # Store the move as Key
                if MT[1] in DataBaseTree[Key]: # Check if Next move exist in that Keys value
                    MT.pop(0) # Remove the top move
                    addGameMovesTreeToDataBaseTree(MT) # Go in deeper
                else:
                    DataBaseTree[Key].append(MT[1]) # Add next move to value of Key
                    MT.pop(0)
                    addGameMovesTreeToDataBaseTree(MT) # Go in deeper
            else: # Selected move does not exist as key in DataBaseTree
                Key = MT[0] # Store the move as Key
                DataBaseTree[Key] = [MT[1]] # Add the key with value of next move
                MT.pop(0) # Remove the top move
                addGameMovesTreeToDataBaseTree(MT) # Go in deeper
    

    现在我可以将它发送到 JSON 文件,甚至在下次程序启动时加载它

    def saveToJSON(fileName:str):
        global DataBaseTree
        with open(fileName, mode='w', encoding='utf-8') as f:
            try:
                s = json.dumps(DataBaseTree, indent=2)
                f.write(s)
            except Exception as e:
                print(e)
    
    def loadFromJSON(fileName:str):
        global DataBaseTree
        with open(fileName, mode='r', encoding='utf-8') as f:
            try:
                DataBaseTree = json.load(f)
            except Exception as e:
                print(e)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-12
      • 2021-08-19
      • 2020-10-18
      • 2018-09-06
      • 2020-02-26
      • 2015-09-21
      • 2021-07-01
      • 2019-01-02
      相关资源
      最近更新 更多