Exemple #1
0
    def __init__(self, name, model):
        """Initialize the agent."""
        super().__init__(name, model)
        self.location = ()

        self.direction = model.random.rand() * (2 * np.pi)
        self.speed = 2
        self.radius = 3
        self.carryable = False

        # Define a BTContruct object
        self.bt = BTConstruct(None, self)

        self.shared_content = dict()

        self.food_collected = 0

        self.bt.xmlstring = model.xmlstring
        self.bt.construct()

        # self.diversity_fitness = model.fitness

        # Location history
        self.location_history = set()
        self.timestamp = 0
        self.step_count = 0
Exemple #2
0
    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()
        """
Exemple #3
0
def gen_from_xmlstring():
    """Generate BT graph from xml string."""
    dname = '/home/aadeshnpn/Documents/BYU/hcmi/hri/supporting_materials/nest_maintenance'  # noqa: E501
    xmlstring = ''
    m = Model()
    a = Agent('1', m)
    bt = BTConstruct(None, a, xmlstring)
    bt.construct()
    bt.visualize(name=dname + '/' + str(90))
Exemple #4
0
    def __init__(self, name, model):
        """Initialize the agent."""
        super().__init__(name, model)
        self.location = ()

        self.direction = model.random.rand() * (2 * np.pi)
        self.speed = 2
        self.radius = 3
        self.results = "db"  # This can take 2 values. db or file

        # self.exchange_time = model.random.randint(2, 4)
        # This doesn't help. Maybe only perform genetic operations when
        # an agents meet 10% of its total population
        # """
        self.operation_threshold = 2
        self.genome_storage = []

        # Define a BTContruct object
        self.bt = BTConstruct(None, self)

        # self.blackboard = Blackboard()
        # self.blackboard.shared_content = dict()

        self.shared_content = dict()
        # self.shared_content = dict(
        self.carryable = False
        self.beta = 0.0001
        self.food_collected = 0
        # Grammatical Evolution part
        from ponyge.algorithm.parameters import Parameters
        parameter = Parameters()
        parameter_list = ['--parameters', '../..,' + model.parm]
        # Comment when different results is desired.
        # Else set this for testing purpose
        # parameter.params['RANDOM_SEED'] = name
        # # np.random.randint(1, 99999999)
        parameter.params['POPULATION_SIZE'] = self.operation_threshold // 2
        parameter.set_params(parameter_list)
        self.parameter = parameter
        individual = initialisation(self.parameter, 1)
        individual = evaluate_fitness(individual, self.parameter)

        self.individual = individual
        self.bt.xmlstring = self.individual[0].phenotype
        self.bt.construct()

        self.diversity_fitness = self.individual[0].fitness
        self.delayed_reward = 0
        # Location history
        self.location_history = set()
        self.timestamp = 0
        self.step_count = 0

        self.fitness_name = True
Exemple #5
0
def main():
    """Parse args and call bt visualize module."""
    # dname = '/home/aadeshnpn/Documents/BYU/hcmi/hri/supporting_materials/nest_maintenance'    # noqa: E501
    # jfname = dname + '/1539014820252.json'
    # dname = '/home/aadeshnpn/Documents/BYU/hcmi/hri/supporting_materials/cooperative_transport'   # noqa: E501
    # jfname = dname + '/1538447335350.json'
    dname = '/home/aadeshnpn/Documents/BYU/hcmi/hri/supporting_materials/foraging'  # noqa: E501
    jfname = dname + '/1538473090382007.json'
    jdata = JsonPhenotypeData.load_json_file(jfname)
    phenotypes = jdata['phenotypes']
    for i in range(len(phenotypes)):
        # if i >= 5:
        #    break
        m = Model()
        a = Agent('1', m)
        bt = BTConstruct(None, a, phenotypes[i])
        bt.construct()
        bt.visualize(name=dname + '/' + str(i))
Exemple #6
0
    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.exchange_time = model.random.randint(2, 4)
        # This doesn't help. Maybe only perform genetic operations when
        # an agents meet 10% of its total population
        # """
        self.operation_threshold = 2
        self.genome_storage = []

        # Define a BTContruct object
        self.bt = BTConstruct(None, self)

        self.blackboard = Blackboard()
        self.blackboard.shared_content = dict()

        self.shared_content = dict()

        # Grammatical Evolution part
        from ponyge.algorithm.parameters import Parameters
        parameter = Parameters()
        parameter_list = ['--parameters', 'swarm.txt']
        # Comment when different results is desired.
        # Else set this for testing purpose
        parameter.params['RANDOM_SEED'] = name
        # np.random.randint(1, 99999999)
        parameter.params['POPULATION_SIZE'] = self.operation_threshold // 2
        parameter.set_params(parameter_list)
        self.parameter = parameter
        individual = initialisation(self.parameter, 1)
        individual = evaluate_fitness(individual, self.parameter)

        self.individual = individual
        self.bt.xmlstring = self.individual[0].phenotype
        self.bt.construct()
Exemple #7
0
    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.operation_threshold = 2
        self.genome_storage = []

        # Define a BTContruct object
        self.bt = BTConstruct(None, self)

        self.blackboard = Blackboard()
        self.blackboard.shared_content = dict()

        # Grammatical Evolution part
        from ponyge.algorithm.parameters import Parameters

        parameter = Parameters()
        parameter_list = ['--parameters', 'swarm.txt']
        parameter.params['POPULATION_SIZE'] = self.operation_threshold // 2
        parameter.params['RANDOM_SEED'] = model.seed
        parameter.set_params(parameter_list)
        self.parameter = parameter
        individual = initialisation(self.parameter, 1)

        self.individual = individual

        self.bt.xmlstring = self.individual[0].phenotype

        self.bt.construct()

        self.output = py_trees.display.ascii_tree(self.bt.behaviour_tree.root)

        # Location history
        self.location_history = set()
        self.timestamp = 0
Exemple #8
0
    def __init__(self, name, model):
        """Initialize the agent."""
        super().__init__(name, model)
        self.location = ()
        # Agent attributes for motion
        self.direction = model.random.rand() * (2 * np.pi)
        self.speed = 2
        self.radius = 3
        self.carryable = False
        self.shared_content = dict()
        self.debris_collected = 0
        # Results
        self.results = "db"  # This can take 2 values. db or file
        # Behavior Tree Class
        self.bt = BTConstruct(None, self)
        # Location history
        self.location_history = set()
        # Step count
        self.timestamp = 0
        self.step_count = 0

        self.fitness_name = True
Exemple #9
0
    def __init__(self, name, model, xmlstring=None, prob=0.5):
        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.prob = prob

        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()

        # When the actuator is not working. We repalce the move
        # premitive behavior with dummpy move behaviors 50% of
        # the time.
        self.dummy_bt = self.replace_nodes()

        # Move node only works 50% of the time
        # py_trees.logging.level = py_trees.logging.Level.DEBUG
        # py_trees.display.print_ascii_tree(self.dummy_bt.behaviour_tree.root)
        """
Exemple #10
0
    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')
Exemple #11
0
    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()

        # self.shared_content['Hub'] = {model.hub}
        # self.shared_content['Sites'] = {model.site}

        # Drop branch
        dseq = py_trees.composites.Sequence('DSequence')

        iscarrying = IsInPartialAttached('IsInPartialAttached_Food')
        iscarrying.setup(0, self, 'Food')

        # If near hub and carrying food with other agents drop
        neighhub = NeighbourObjects('NeighbourObjects_Hub')
        neighhub.setup(0, self, 'Hub')

        drop = CompositeDropPartial('CompositeDropPartial_Food')
        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')
        neighhub = py_trees.meta.inverter(NeighbourObjects)(
            'NeighbourObjects_Hub')
        neighhub.setup(0, self, 'Hub')

        neighfood = NeighbourObjects('NeighbourObjects_Food')
        neighfood.setup(0, self, 'Food')

        invcarrying = py_trees.meta.inverter(IsInPartialAttached)(
            'IsInPartialAttached_Food')
        invcarrying.setup(0, self, 'Food')

        carry = CompositeMultipleCarry('CompositeMultipleCarry_Food')
        carry.setup(0, self, 'Food')

        # Move to hub
        hubseq = py_trees.composites.Sequence('HubSeq')
        # If carrying something to go to hub
        gotohub = MoveTowards('MoveTowards_Hub')
        gotohub.setup(0, self, 'Hub')

        hubseq.add_children([iscarrying, gotohub])

        cseq.add_children([neighhub, neighfood, carry, hubseq])

        # Locomotion branch

        # Move to site
        siteseq = py_trees.composites.Sequence('SiteSeq')
        # If site already found and not carrying anything go to site
        sitefound = IsVisitedBefore('IsVisitedBefore_Sites')
        sitefound.setup(0, self, 'Sites')

        gotosite = MoveTowards('MoveTowards_Sites')
        gotosite.setup(0, self, 'Sites')

        siteseq.add_children([sitefound, invcarrying, gotosite])

        # Do Random walk
        explore = Explore('Explore')
        explore.setup(0, self)

        randwalk = py_trees.composites.Sequence('Randwalk')
        randwalk.add_children([explore])
        # Try to got to site and hub if not explore
        locoselect = py_trees.composites.Selector('Move')
        locoselect.add_children([iscarrying, siteseq, explore])
        # First try to drop then collect or explore
        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 + '/cthc')