| 21 | |
| 22 | |
| 23 | class tree_node: |
| 24 | |
| 25 | def __init__(self): |
| 26 | self.is_terminal = False |
| 27 | self.pruned = False |
| 28 | self.finished = False |
| 29 | |
| 30 | self.node_type = None |
| 31 | self.description = "" |
| 32 | self.observation = "" |
| 33 | self.observation_code = None |
| 34 | self.children = [] |
| 35 | |
| 36 | self.father = None |
| 37 | |
| 38 | |
| 39 | self.io_state = None |
| 40 | |
| 41 | |
| 42 | |
| 43 | self.expand_num = 0 # The number of visits to the node, 0 means it has not been visited |
| 44 | |
| 45 | |
| 46 | self.Elo = 1000.0 |
| 47 | |
| 48 | # openai-messages of this node |
| 49 | self.messages = [] |
| 50 | |
| 51 | def compute_weight(self): |
| 52 | ''' |
| 53 | Used in the UCT algorithm to calculate the node weight of each son during selection |
| 54 | ''' |
| 55 | return 0.0 |
| 56 | |
| 57 | def get_max_depth(self): |
| 58 | ''' |
| 59 | maximum depth of subtrees including self |
| 60 | ''' |
| 61 | max_depth = 0 |
| 62 | for child in self.children: |
| 63 | max_depth = max(max_depth,child.get_max_depth()) |
| 64 | return max_depth + 1 |
| 65 | |
| 66 | def get_depth(self): |
| 67 | if self.father == None: |
| 68 | return 0 |
| 69 | return self.father.get_depth() + 1 |
| 70 | |
| 71 | def get_size(self): |
| 72 | ''' |
| 73 | subtree, including itself |
| 74 | ''' |
| 75 | size = 1 |
| 76 | for child in self.children: |
| 77 | size += child.get_size() |
| 78 | return size |
| 79 | |
| 80 | def prune(self): |