def __init__(self, name, model): super().__init__(name, model) self.location = () self.direction = model.random.rand() * (2 * np.pi) # self.speed = 2 self.radius = 3 self.moveable = True self.shared_content = dict() self.shared_content['Sites'] = [model.target] # Defining the composite behavior movetowards = MoveTowards('MoveTowards') # Setup for the behavior movetowards.setup(0, self, 'Sites') # This behavior is just defined to check if the composite tree # can be combined with other primite behaviors is_carrying = IsCarrying('IsCarrying') is_carrying.setup(0, self, 'Food') seq = py_trees.composites.Sequence('Seq') seq.add_children([movetowards, is_carrying]) # Since its root is a sequence, we can use it directly self.behaviour_tree = py_trees.trees.BehaviourTree(seq)
def setup(self, timeout, agent, item): """Have defined the setup method. This method defines the other objects required for the behavior. Agent is the actor in the environment, item is the name of the item we are trying to find in the environment and timeout defines the execution time for the behavior. """ self.agent = agent self.item = item dropseq = Sequence('CD_Sequence') iscarrying = IsCarrying('CD_IsCarrying') iscarrying.setup(0, self.agent, self.item) drop = Drop('CD_Drop') drop.setup(0, self.agent, self.item) dropseq.add_children([iscarrying, drop]) self.behaviour_tree = BehaviourTree(dropseq)
def __init__(self, name, model, xmlstring=None): super().__init__(name, model) self.location = () self.direction = model.random.rand() * (2 * np.pi) self.speed = 2 self.radius = 3 self.moveable = True self.shared_content = dict() self.carryable = False # Define a BTContruct object self.bt = BTConstruct(None, self) class DummyIndividual: def __init__(self): self.phenotype = None dummyind = DummyIndividual() self.individual = [dummyind] self.individual[0].phenotype = xmlstring # self.bt.xmlstring = xmlstring # self.bt.construct() # Drop branch dseq = py_trees.composites.Sequence('DSequence') iscarrying = IsCarrying('IsCarrying_Food') iscarrying.setup(0, self, 'Food') neighhub = NeighbourObjects('NeighbourObjects_Hub') neighhub.setup(0, self, 'Hub') notneighhub = py_trees.meta.inverter(NeighbourObjects)( 'NeighbourObjects_Hub') notneighhub.setup(0, self, 'Hub') drop = CompositeDrop('CompositeDrop_Food') drop.setup(0, self, 'Food') dseq.add_children([neighhub, drop]) # Carry branch cseq = py_trees.composites.Sequence('CSequence') neighsite = NeighbourObjects('NeighbourObjects_Sites') neighsite.setup(0, self, 'Sites') neighfood = NeighbourObjects('NeighbourObjects_Food') neighfood.setup(0, self, 'Food') invcarrying = py_trees.meta.inverter(IsCarrying)('IsCarrying_Food') invcarrying.setup(0, self, 'Food') carry = CompositeSingleCarry('CompositeSingleCarry_Food') carry.setup(0, self, 'Food') cseq.add_children([neighsite, neighfood, invcarrying, carry]) # Locomotion branch # Move to site siteseq = py_trees.composites.Sequence('SiteSeq') sitefound = IsVisitedBefore('IsVisitedBefore_Sites') sitefound.setup(0, self, 'Sites') gotosite = MoveTowards('MoveTowards_Sites') gotosite.setup(0, self, 'Sites') siteseq.add_children([sitefound, invcarrying, gotosite]) # siteseq.add_children([invcarrying]) # Move to hub hubseq = py_trees.composites.Sequence('HubSeq') gotohub = MoveTowards('MoveTowards_Hub') gotohub.setup(0, self, 'Hub') hubseq.add_children([iscarrying, gotohub]) sitenotfound = py_trees.meta.inverter(IsVisitedBefore)( 'IsVisitedBefore_Sites') sitenotfound.setup(0, self, 'Sites') explore = Explore('Explore') explore.setup(0, self) randwalk = py_trees.composites.Sequence('Randwalk') randwalk.add_children([sitenotfound, explore]) locoselect = py_trees.composites.Selector('Move') # locoselect.add_children([siteseq, hubseq, explore]) locoselect.add_children([hubseq, randwalk]) select = py_trees.composites.Selector('Main') select.add_children([dseq, cseq, locoselect]) self.behaviour_tree = py_trees.trees.BehaviourTree(select) py_trees.display.render_dot_tree(self.behaviour_tree.root, name=model.pname + '/forgehc')
def __init__(self, name, model): super().__init__(name, model) self.location = () self.direction = model.random.rand() * (2 * np.pi) self.speed = 2 self.radius = 3 self.moveable = True self.shared_content = dict() # self.shared_content['Hub'] = model.hub # self.shared_content['Sites'] = model.site self.shared_content['Hub'] = {model.hub} # root = py_trees.composites.Sequence("Sequence") # root = py_trees.composites.Selector('Selector') mseq = py_trees.composites.Sequence('MSequence') nseq = py_trees.composites.Sequence('NSequence') select = py_trees.composites.Selector('RSelector') carryseq = py_trees.composites.Sequence('CSequence') dropseq = py_trees.composites.Sequence('DSequence') lowest1 = py_trees.meta.inverter(NeighbourObjects)('00') lowest1.setup(0, self, 'Hub') lowest11 = NeighbourObjects('0') lowest11.setup(0, self, 'Sites') lowest = NeighbourObjects('0') lowest.setup(0, self, 'Food') low = IsCarryable('1') low.setup(0, self, 'Food') medium = IsSingleCarry('2') medium.setup(0, self, 'Food') high = SingleCarry('3') high.setup(0, self, 'Food') carryseq.add_children([lowest1, lowest11, lowest, low, medium, high]) repeathub = RepeatUntilFalse("RepeatSeqHub") repeatsite = RepeatUntilFalse("RepeatSeqSite") high1 = py_trees.meta.inverter(NeighbourObjects)('4') # high1 = NeighbourObjects('4') high1.setup(0, self, 'Hub') med1 = GoTo('5') med1.setup(0, self, 'Hub') # low1 = py_trees.meta.inverter(Move)('6') low1 = Move('6') low1.setup(0, self, None) high2 = py_trees.meta.inverter(NeighbourObjects)('12') # high2 = NeighbourObjects('12') high2.setup(0, self, 'Sites') # med2 = py_trees.meta.inverter(GoTo)('13') med2 = GoTo('13') med2.setup(0, self, 'Sites') # low1 = py_trees.meta.inverter(Move)('6') low2 = Move('14') low2.setup(0, self, None) # Drop dropseq = py_trees.composites.Sequence('DSequence') c1 = IsCarrying('7') c1.setup(0, self, 'Food') d1 = IsDropable('8') d1.setup(0, self, 'Hub') d2 = Drop('9') d2.setup(0, self, 'Food') dropseq.add_children([c1, d1, d2]) repeathub.add_children([high1, med1, low1]) repeatsite.add_children([high2, med2, low2]) mseq.add_children([carryseq, repeathub]) nseq.add_children([dropseq, repeatsite]) # For randomwalk to work the agents shouldn't know the location of Site v1 = py_trees.meta.inverter(IsVisitedBefore)('15') v1.setup(0, self, 'Sites') r1 = RandomWalk('16') r1.setup(0, self, None) m1 = Move('17') m1.setup(0, self, None) randseq = py_trees.composites.Sequence('RSequence') randseq.add_children([v1, r1, m1]) select.add_children([nseq, mseq, randseq]) self.behaviour_tree = py_trees.trees.BehaviourTree(select)
def __init__(self, name, model): super().__init__(name, model) self.location = () self.direction = model.random.rand() * (2 * np.pi) self.speed = 2 self.radius = 3 self.moveable = True self.shared_content = dict() self.shared_content['Hub'] = {model.hub} # Drop branch dseq = py_trees.composites.Sequence('DSequence') iscarrying = IsCarrying('1') iscarrying.setup(0, self, 'Food') neighhub = NeighbourObjects('2') neighhub.setup(0, self, 'Hub') drop = CompositeDrop('4') drop.setup(0, self, 'Food') dseq.add_children([neighhub, drop]) # Carry branch cseq = py_trees.composites.Sequence('CSequence') neighsite = NeighbourObjects('5') neighsite.setup(0, self, 'Sites') neighfood = NeighbourObjects('50') neighfood.setup(0, self, 'Food') invcarrying = py_trees.meta.inverter(IsCarrying)('8') invcarrying.setup(0, self, 'Food') carry = CompositeSingleCarry('6') carry.setup(0, self, 'Food') cseq.add_children([neighsite, neighfood, invcarrying, carry]) # Locomotion branch # Move to site siteseq = py_trees.composites.Sequence('SiteSeq') sitefound = IsVisitedBefore('7') sitefound.setup(0, self, 'Sites') gotosite = MoveTowards('9') gotosite.setup(0, self, 'Sites') siteseq.add_children([sitefound, invcarrying, gotosite]) # Move to hub hubseq = py_trees.composites.Sequence('HubSeq') gotohub = MoveTowards('10') gotohub.setup(0, self, 'Hub') hubseq.add_children([iscarrying, gotohub]) sitenotfound = py_trees.meta.inverter(IsVisitedBefore)('11') sitenotfound.setup(0, self, 'Sites') explore = Explore('12') explore.setup(0, self) randwalk = py_trees.composites.Sequence('Randwalk') randwalk.add_children([explore]) locoselect = py_trees.composites.Selector('Move') locoselect.add_children([siteseq, hubseq, explore]) select = py_trees.composites.Selector('Main') select.add_children([dseq, cseq, locoselect]) self.behaviour_tree = py_trees.trees.BehaviourTree(select)
def construct_bt(self): """Construct BT.""" # Get the phenotype of the genome and store as xmlstring # self.bt.xmlstring = self.xmlstring # Drop branch # Drop branch dseq = py_trees.composites.Sequence('DSequence') iscarrying = IsCarrying('IsCarrying_Debris') iscarrying.setup(0, self, 'Debris') neighhub = NeighbourObjects('NeighbourObjects') neighhub.setup(0, self, 'Obstacles') drop = CompositeDrop('CompositeDrop_Debris') drop.setup(0, self, 'Debris') dseq.add_children([neighhub, drop]) # Carry branch cseq = py_trees.composites.Sequence('CSequence') neighsite = py_trees.meta.inverter(NeighbourObjects)( 'NeighbourObjects') neighsite.setup(0, self, 'Obstacles') neighfood = NeighbourObjects('NeighbourObjects_Debris') neighfood.setup(0, self, 'Debris') invcarrying = py_trees.meta.inverter(IsCarrying)('IsCarrying_Debris') invcarrying.setup(0, self, 'Debris') carry = CompositeSingleCarry('CompositeSingleCarry_Debris') carry.setup(0, self, 'Debris') cseq.add_children([neighsite, neighfood, invcarrying, carry]) # Locomotion branch # Move to site siteseq = py_trees.composites.Sequence('SiteSeq') sitefound = IsVisitedBefore('IsVisitedBefore') sitefound.setup(0, self, 'Obstacles') gotosite = MoveTowards('MoveTowards') gotosite.setup(0, self, 'Obstacles') siteseq.add_children([sitefound, iscarrying, gotosite]) # Move to hub hubseq = py_trees.composites.Sequence('HubSeq') gotohub = MoveTowards('MoveTowards_Hub') gotohub.setup(0, self, 'Hub') hubseq.add_children([iscarrying, gotohub]) sitenotfound = py_trees.meta.inverter(IsVisitedBefore)( 'IsVisitedBefore') sitenotfound.setup(0, self, 'Obstacles') explore = Explore('Explore') explore.setup(0, self) randwalk = py_trees.composites.Sequence('Randwalk') randwalk.add_children([explore]) locoselect = py_trees.composites.Selector('Move') locoselect.add_children([siteseq, explore]) select = py_trees.composites.Selector('Main') select.add_children([dseq, cseq, locoselect]) self.behaviour_tree = py_trees.trees.BehaviourTree(select)