def init_population(self, size):
     """Basic initialisation function for a random population of agents."""
     population = list()
     if self.heuristic_init:
         heuristic_tour = self.get_heuristic_tour()
         for _ in range(0, size):
             population.append(FoodSource(heuristic_tour))
     else:
         for _ in range(0, size):
             population.append(FoodSource())
     self.population = population
Ejemplo n.º 2
0
    def update(self, duration):
        if self.age >= Settings.flower_adult_age:
            return

        self.age += duration

        # Just became an adult, so create a new food source
        if self.age >= Settings.flower_adult_age:
            location = self.location + Settings.flower_food_source_offset
            radius = Settings.flower_food_source_radius
            weight = Settings.flower_food_source_weight
            self.game.food_sources.append(
                FoodSource(location=location,
                           radius=radius,
                           weight=weight,
                           level=Settings.flower_food_source_starting_level,
                           canvas=self.canvas))

            # TODO: Tidy up?
            for level in self.game.levels:
                level.flock.obstacles.append(Obstacle(location, radius, 1))
        def generate_candidate(current):
            self.run_stats["calls_generate_candidate"] += 1

            while True:
                other = rng.choice(self.population)
                if other != current:
                    break

            new_tour = []

            rnd_RC = rng.random()
            rnd_PC = rng.random()

            # open_tour, closed_tour = choose_subtour(current.tour, other.tour)
            closed_tour, open_tour = choose_random_subtour(current.tour)

            if rnd_RC <= self.p_reconnection:
                new_tour = reconnect(open_tour, closed_tour)
            elif rnd_PC <= self.p_correction_perturbation:
                new_tour = perturb(open_tour, closed_tour)
            else:
                new_tour = correct(open_tour, closed_tour)

            return FoodSource(new_tour)
Ejemplo n.º 4
0
    def create_foodsource(self):
        solution = self.generate_solution(1)
        fitness = self.fitness(solution)

        return FoodSource(solution, fitness)
Ejemplo n.º 5
0
    def create_foodsource(self):
        solution = self.candidate_solution(self.fn_lb, self.fn_ub)
        fitness = self.fitness(solution)

        return FoodSource(solution, fitness)