Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.25 KB | None | 0 0
  1.  
  2. # some utilities to explore game trees
  3. import minimax
  4. import copy
  5.  
  6. class GameStateNode(object):
  7.    
  8.     """Represent a GameState with possible links to successors.
  9.    """
  10.  
  11.     def __init__(self, game_state):
  12.         """Create a node to represent game_state.
  13.  
  14.           Arguments:
  15.           game_state:  A GameState instance of minimax.GameState
  16.        """
  17.  
  18.         self.value = game_state
  19.         self.children = []  # populate from game_state.next_move()
  20.  
  21.     def grow(self):
  22.         """Grow the tree of GameStates, starting from this one.
  23.  
  24.           Assumptions:
  25.           The tree of GameStates generated from this one is finite, and is
  26.           specified (recursively) by self.value.make_move()
  27.        """
  28.  
  29.         for m in self.value.next_move():
  30.             g = self.value.make_move(m)
  31.             self.children.append(GameStateNode(g))
  32.             self.children[-1].grow()  # grow latest child
  33.  
  34. def node_count(game_state_node):
  35.     """Return the number of nodes in tree rooted at game_state_node.
  36.  
  37.       Arguments:
  38.       game_state_node:  A GameStateNode instance.
  39.  
  40.       Returns:
  41.       Number of nodes in tree rooted at game_state_node.
  42.    """
  43.  
  44.     # this node, plus kids
  45.     # note the list comprehension
  46.     return 1 + sum([node_count(c) for c in game_state_node.children])
  47.  
  48. def leaf_count(game_state_node):
  49.     """Return the number of leaves in tree rooted at game_state_node.
  50.  
  51.       Arguments:
  52.       game_state_node:  A GameStateNode instance.
  53.  
  54.       Returns:
  55.       Number of childless nodes in tree rooted at game_state_node.
  56.    """
  57.    
  58.     # count the number of leaves in each child
  59.     if game_state_node.children == []:
  60.         return 1
  61.     else:
  62.         return sum([leaf_count(c) for c in game_state_node.children])
  63.  
  64.  
  65. def distinct_node_count(game_state_node):
  66.     """Return number of nodes with distinct layouts in tree rooted at
  67.       game_state_node.
  68.  
  69.       Arguments:
  70.       game_state_node:  A GameStateNode instance.
  71.  
  72.       Returns:
  73.       Number of nodes with distinct layouts in tree rooted at
  74.       game_state_node, counting nodes with the same layout only once."""
  75.  
  76.         # count the number of distinct nodes, don't bother exploring subtrees
  77.         # where you have already visited the root!
  78.        
  79.     def _mark_distinct_leaf_states(game_state_node, leaf_states, node_states):
  80.             '''Record distinct leaves and nodes in game_state_node.'''
  81.             sig = str(game_state_node.value) # immutable signature
  82.             if sig not in node_states: # never been here
  83.                 node_states[sig] = 1 # mark visited
  84.                 children = game_state_node.children
  85.                 if not children: # no children, must be a leaf
  86.                     leaf_states[sig] = 1 # never been to this leaf
  87.                 else:
  88.                     for c in children:
  89.                         _mark_distinct_leaf_states(c, leaf_states, node_states)
  90.     node_states = {}
  91.     leaf_states = {}
  92.     _mark_distinct_leaf_states(game_state_node, leaf_states, node_states)
  93.     return len(node_states.items())
  94.            
  95.  
  96.  
  97. def distinct_leaf_count(game_state_node):
  98.     """Return number of leaves with distinct layouts in tree rooted at
  99.       game_state_node.
  100.  
  101.       Arguments:
  102.       game_state_node:  A GameStateNode instance.
  103.  
  104.       Returns:
  105.       Number of leaves with distinct layouts in the tree rooted at
  106.       game_state_node, that is two leaves with the same state are counted
  107.       only once.
  108.    """
  109.  
  110.     def _mark_distinct_leaf_states(game_state_node, leaf_states, node_states):
  111.         '''Record distinct leaves and nodes in game_state_node.'''
  112.         sig = str(game_state_node.value) # immutable signature
  113.         if sig not in node_states: # never been here
  114.             node_states[sig] = 1 # mark visited
  115.             children = game_state_node.children
  116.             if not children: # no children, must be a leaf
  117.                 leaf_states[sig] = 1 # never been to this leaf
  118.             else:
  119.                 for c in children:
  120.                     _mark_distinct_leaf_states(c, leaf_states, node_states)
  121.     node_states = {}
  122.     leaf_states = {}
  123.     _mark_distinct_leaf_states(game_state_node, leaf_states, node_states)
  124.     return len(leaf_states.items())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement