Beispiel #1
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 = 50
        self.genome_storage = []

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

        # Grammatical Evolution part
        from ponyge.algorithm.parameters import Parameters
        parameter = Parameters()
        # list_params_files = ['string_match.txt', 'regression.txt', 'classification.txt']
        # parameter_list = ['--parameters', 'string_match_dist.txt']
        parameter_list = ['--parameters', '../,test_swarm.txt']
        parameter.params['RANDOM_SEED'] = 1234  # 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.mapper.xmlstring = self.individual.phenotype
        self.individual = individual
        if self.name == 4:
            self.individual[0].fitness = 150
Beispiel #2
0
    def init_evolution_algo(self):
        """Agent's GE algorithm operation defination."""
        # Genetic algorithm parameters
        self.operation_threshold = 50
        self.genome_storage = []

        # Grammatical Evolution part
        from ponyge.algorithm.parameters import Parameters
        parameter = Parameters()
        parameter_list = ['--parameters', '../..,nm.txt']
        # Comment when different results is desired.
        # Else set this for testing purpose
        # parameter.params['RANDOM_SEED'] = name
        # # np.random.randint(1, 99999999)
        # Set GE runtime parameters
        parameter.params['POPULATION_SIZE'] = self.operation_threshold // 2
        parameter.set_params(parameter_list)
        self.parameter = parameter
        # Initialize the genome
        individual = initialisation(self.parameter, 1)
        individual = evaluate_fitness(individual, self.parameter)
        # Assign the genome to the agent
        self.individual = individual
        # Fitness
        self.beta = 0.9
        self.diversity_fitness = self.individual[0].fitness
        self.individual[0].fitness = 0
        self.generation = 0
Beispiel #3
0
    def step(self):
        """Take a step in the simulation."""
        # py_trees.logging.level = py_trees.logging.Level.DEBUG
        # output = py_trees.display.ascii_tree(self.bt.behaviour_tree.root)

        # Couting variables
        self.timestamp += 1
        self.step_count += 1

        # Increase beta
        # self.beta = self.timestamp / self.model.iter

        # Compute the behavior tree
        self.bt.behaviour_tree.tick()

        # Maintain location history
        _, gridval = self.model.grid.find_grid(self.location)
        self.location_history.add(gridval)

        # Find the no.of food collected from the BT execution
        self.food_collected = self.get_food_in_hub()  # * self.get_food_in_hub(
        #  False)

        # We need to move this from here to genetic step
        self.cf = self.carrying_fitness()
        self.ef = self.exploration_fitness()

        # Computes overall fitness using Beta function
        self.overall_fitness()

        # Current phenotype
        self.phenotypes = dict()
        self.phenotypes[self.individual[0].phenotype] = (
            self.individual[0].fitness)

        # Find the nearby agents
        cellmates = self.model.grid.get_objects_from_grid(
            type(self).__name__, self.location)

        # If neighbours found, store the genome
        if len(cellmates) > 1:
            self.store_genome(cellmates)
        # Logic for gentic operations.
        # If the genome storage has enough genomes and agents has done some
        # exploration then compute the genetic step OR
        # 600 time step has passed and the agent has not done anything useful
        # then also perform genetic step
        storage_threshold = len(
            self.genome_storage) >= (self.model.num_agents / 10)

        # New logic to invoke genetic step
        if self.individual[0].fitness <= 0 and self.timestamp > 100:
            individual = initialisation(self.parameter, 10)
            individual = evaluate_fitness(individual, self.parameter)
            self.genome_storage = self.genome_storage + individual
            self.genetic_step()
        elif ((self.individual[0].fitness >= 0 and storage_threshold)
              and self.timestamp > 200 and self.food_collected <= 0):
            self.genetic_step()
        """
Beispiel #4
0
    def step(self):
        """Agent action at a single time step."""
        # py_trees.logging.level = py_trees.logging.Level.DEBUG
        # output = py_trees.display.ascii_tree(self.bt.behaviour_tree.root)

        # Couting variables
        self.timestamp += 1
        self.step_count += 1

        # Increase beta
        self.beta = self.step_count / self.model.iter

        self.location_history.add(self.location)

        # Compute the behavior tree
        self.bt.behaviour_tree.tick()

        # Find the no.of food collected from the BT execution
        self.food_collected = len(self.get_food_in_hub())

        # Computes overall fitness using Beta function
        self.overall_fitness()

        cellmates = self.model.grid.get_objects_from_grid(
            type(self).__name__, self.location)

        # Create a results instance and save it to a file
        """
        self.results = Results(
            self.model.pname, self.model.connect, self.model.sn, self.name,
            self.step_count, self.timestamp, self.beta,
            self.individual[0].fitness,
            self.diversity_fitness, self.exploration_fitness(),
            self.food_collected, len(cellmates), self.individual[0].genome,
            self.individual[0].phenotype, self.bt
            )
        """
        # Save the results to a db
        # self.results.save_to_file()

        # Logic for gentic operations.
        # If the genome storage has enough genomes and agents has done some
        # exploration then compute the genetic step OR
        # 600 time step has passed and the agent has not done anything useful
        # then also perform genetic step
        storage_threshold = len(
            self.genome_storage) >= (self.model.num_agents / 1.4)
        if storage_threshold:
            self.genetic_step()
        elif (storage_threshold is False and self.timestamp > 50
              and self.exploration_fitness() < 10):
            individual = initialisation(self.parameter, 10)
            individual = evaluate_fitness(individual, self.parameter)
            self.genome_storage = individual
            self.genetic_step()

        # If neighbours found, store the genome
        if len(cellmates) > 1:
            self.store_genome(cellmates)
Beispiel #5
0
 def exchange_chromosome(self, cellmates):
     individuals = self.genome_storage
     parents = selection(self.parameter, individuals)
     cross_pop = crossover(self.parameter, parents)
     new_pop = mutation(self.parameter, cross_pop)
     new_pop = evaluate_fitness(new_pop, self.parameter)
     individuals = replacement(self.parameter, new_pop, individuals)
     individuals.sort(reverse=True)
     self.individual = [individuals[0]]
     self.genome_storage = []
Beispiel #6
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
Beispiel #7
0
 def exchange_chromosome(self, ):
     """Perform genetic operations."""
     # print('from exchange', self.name)
     individuals = self.genome_storage
     parents = selection(self.parameter, individuals)
     cross_pop = crossover(self.parameter, parents)
     new_pop = mutation(self.parameter, cross_pop)
     new_pop = evaluate_fitness(new_pop, self.parameter)
     individuals = replacement(self.parameter, new_pop, individuals)
     individuals.sort(reverse=False)
     self.individual = [individuals[0]]
     self.individual[0].fitness = 0
     self.genome_storage = []
Beispiel #8
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()
Beispiel #9
0
    def step(self):
        """Take a step in the simulation."""
        # py_trees.logging.level = py_trees.logging.Level.DEBUG
        # output = py_trees.display.ascii_tree(self.bt.behaviour_tree.root)

        # Couting variables
        self.timestamp += 1
        self.step_count += 1

        # Increase beta
        # self.beta = self.timestamp / self.model.iter

        # Maintain location history
        self.location_history.add(self.location)

        # Compute the behavior tree
        self.bt.behaviour_tree.tick()

        # Find the no.of food collected from the BT execution
        self.food_collected = self.get_food_in_hub()  # * self.get_food_in_hub(
        #  False)

        # Computes overall fitness using Beta function
        self.overall_fitness()

        # Hash the phenotype with its fitness
        # We need to move this from here to genetic step
        cf = self.carrying_fitness()
        ef = self.exploration_fitness()
        if self.individual[0].phenotype in self.phenotypes.keys():
            e, c, f = self.phenotypes[self.individual[0].phenotype]
            if f < self.food_collected:
                f = self.food_collected
            else:
                if c < cf:
                    c = cf
                else:
                    if e < ef:
                        e = ef

            self.phenotypes[self.individual[0].phenotype] = (e, c, f)
        else:
            if int(cf) == 0 and int(ef) == 0 and int(self.food_collected) == 0:
                pass
            else:
                self.phenotypes[self.individual[0].phenotype] = (
                    self.exploration_fitness(), self.carrying_fitness(),
                    self.food_collected)

        # Find the nearby agents
        cellmates = self.model.grid.get_objects_from_grid(
            type(self).__name__, self.location)
        # Logic for gentic operations.
        # If the genome storage has enough genomes and agents has done some
        # exploration then compute the genetic step OR
        # 600 time step has passed and the agent has not done anything useful
        # then also perform genetic step
        storage_threshold = len(
            self.genome_storage) >= (self.model.num_agents / 10)
        if storage_threshold:
            self.genetic_step()
        elif ((storage_threshold is False and self.timestamp > 50)
              and (self.exploration_fitness() < 10)):
            individual = initialisation(self.parameter, 10)
            individual = evaluate_fitness(individual, self.parameter)
            self.genome_storage = individual
            self.genetic_step()

        # If neighbours found, store the genome
        if len(cellmates) > 1:
            self.store_genome(cellmates)