weighted_random_example.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 from pi_trees_lib.pi_trees_lib import *
00004 import time
00005 
00006 class WeightedRandomExample():
00007     def __init__(self):
00008         # The root node
00009         BEHAVE = Sequence("behave")
00010         
00011         # Create a ParallelOne composite task (returns SUCCESS as soon as any subtask returns SUCCESS)
00012         WEIGHTED_RANDOM_TASKS = WeightedRandomSelector("Weighted Random Selector", weights=[3, 2, 1], reset_after=True)
00013         
00014         # Create three counting tasks
00015         TASK1 = Message("High_Probability", "Highest Probability Task")
00016         TASK2 = Message("Medium_Probability", "Medium Probability Task")
00017         TASK3 = Message("Low_Probability", "Lowest Probability Task")
00018 
00019         # Add the tasks to the parallel composite task
00020         WEIGHTED_RANDOM_TASKS.add_child(TASK1)
00021         WEIGHTED_RANDOM_TASKS.add_child(TASK2)
00022         WEIGHTED_RANDOM_TASKS.add_child(TASK3)
00023         
00024         LOOP = Loop("Loop_Forever", iterations=-1)
00025         
00026         LOOP.add_child(WEIGHTED_RANDOM_TASKS)
00027         
00028         # Add the composite task to the root task
00029         BEHAVE.add_child(LOOP)
00030         
00031         # Print a simple representation of the tree
00032         print "Behavior Tree Structure"
00033         print_tree(BEHAVE)
00034             
00035         # Run the tree
00036         while True:
00037             status = BEHAVE.run()
00038             if status == TaskStatus.SUCCESS:
00039                 print "Finished running tree."
00040                 break
00041 
00042 # A task to print a message
00043 class Message(Task):
00044     def __init__(self, name, message, *args, **kwargs):
00045         super(Message, self).__init__(name, *args, **kwargs)
00046         
00047         self.name = name
00048         self.message = message
00049         print "Creating message task: ", self.message
00050  
00051     def run(self):
00052         print self.message
00053         time.sleep(1)
00054 
00055         return TaskStatus.SUCCESS
00056 
00057 
00058 if __name__ == '__main__':
00059     tree = WeightedRandomExample()
00060 


pi_trees_lib
Author(s): Patrick Goebel
autogenerated on Thu Jun 6 2019 17:33:29