Пример #1
0
    def empty_individual(cls):
        """
        # Top-down construction of behavior tree
        root = Selector(name='High Level Ordering of Strategies')
        
        offensive_plan = Sequence(name='Offensive Strategy')
        largest_fleet_check = Check(have_largest_fleet)
        attack = Action(attack_weakest_enemy_planet)
        offensive_plan.child_nodes = [largest_fleet_check, attack]

        for node in offensive_plan.child_nodes:
            node.parent_node = offensive_plan

        spread_sequence = Sequence(name='Spread Strategy')
        neutral_planet_check = Check(if_neutral_planet_available)
        spread_action = Action(spread_to_weakest_neutral_planet)
        spread_sequence.child_nodes = [neutral_planet_check, spread_action]

        for node in spread_sequence.child_nodes:
            node.parent_node = spread_sequence

        root.child_nodes = [offensive_plan, spread_sequence, attack.copy()]

        for node in root.child_nodes:
            node.parent_node = root

        root.parent_node = None
        """

        root = Selector(name='High Level Ordering of Strategies')
        root.child_nodes = []
        #print("Starting with: " + root.tree_to_string())
        #input()
        root.parent_node = None
        return cls(root)
Пример #2
0
def parse_tree(tree_string):

    root = Selector(name="Root")

    #Current node has the current selector or sequence that we are processing
    current_node = root

    for char in tree_string:
        if char == "-":
            #Selector
            new_node = Selector(name="Selector")
            new_node.parent_node = current_node
            new_node.child_nodes = []
            current_node.child_nodes.append(new_node)

        elif char == "_":
            #Sequence
            new_node = Sequence(name="Sequence")
            new_node.parent_node = current_node
            new_node.child_nodes = []
            current_node.child_nodes.append(new_node)

        elif char == "[":
            #Start children
            current_node = current_node.child_nodes[-1]

        elif char == "]":
            #End children
            current_node = current_node.parent_node

        elif char.isupper():
            #Check
            new_node = Check(node_dict[char])
            new_node.parent_node = current_node
            current_node.child_nodes.append(new_node)

        elif char.islower():                   
            #Action
            new_node = Action(node_dict[char])
            new_node.parent_node = current_node
            current_node.child_nodes.append(new_node)  
    logging.info('\n Decoded tree as: ' + root.tree_to_string())

    return root   
Пример #3
0
def generate_tree(root):
    num_children = random.randint(2, 5)
    while True:

        is_composite = random.randint(0, 100) < 10
        if is_composite:
            new_class = ["-", Selector] if random.randint(
                0, 2) == 0 else ["_", Sequence]
        else:
            new_class = random.choice(list(node_dict.items()))

        if new_class[1] == Selector:

            new_node = Selector(name="Selector")
            new_node.child_nodes = []
            new_root = generate_tree(new_node)
            new_root.parent_node = root
            root.child_nodes.append(new_root)

        elif new_class[1] == Sequence:

            new_node = Sequence(name="Sequence")
            new_node.child_nodes = []
            new_root = generate_tree(new_node)
            new_root.parent_node = root
            root.child_nodes.append(new_root)

        elif new_class[0].isupper():
            new_node = Check(new_class[1])
            new_node.parent_node = root
            root.child_nodes.append(new_node)

        elif new_class[0].islower():
            new_node = Action(new_class[1])
            new_node.parent_node = root
            root.child_nodes.append(new_node)

        if num_children > 0:
            num_children -= 1
        if num_children <= 0:
            break
    return root
Пример #4
0
def parse_tree(tree_string):

    root = Selector(name="Root")

    #Current node has the current selector or sequence that we are processing
    current_node = root

    for char in tree_string[1,-1]:
        if char == "-":
            #Selector
            new_node = Selector(name="Selector")
            new_node.parent = current_node
            current_node.child_nodes.append(new_node)

        elif char == "_":
            #Sequence
            new_node = Selector(name="Sequence")
            new_node.parent = current_node
            current_node.child_nodes.append(new_node)

        elif char == "[":
            #Start children
            current_node = current_node.child_nodes[-1]

        elif char == "]":
            #End children
            current_node = current_node.parent_node

        elif char.isupper():
            #Check
            new_node = Check(node_dict(char))
            new_node.parent_node = current_node
            current_node.child_nodes.append(new_node)

        elif char.islower():                   
            #Action
            new_node = Action(node_dict(char))
            new_node.parent_node = current_node
            current_node.child_nodes.append(new_node)  

    return root   
Пример #5
0
    def random_individual(cls):
        root = Selector([], name='High Level Ordering of Strategies')
        root.parent_node = None

        #print("Starting with: " + root.tree_to_string())
        g = generate_tree(root)

        #print("test")

        #print("Generated random treE: " + g.tree_to_string())
        #input()
        #print("Created: " + g.tree_to_string())
        return cls(g)