Ejemplo n.º 1
0
    def run(self):
        """ Execute the algorithm. """
        self.start_computing_time = time.time()

        population_to_evaluate = self.create_initial_solutions()
        task_pool = as_completed(self.evaluate(population_to_evaluate))

        self.init_progress()

        auxiliar_population = []
        for future in task_pool:
            # The initial population is not full
            if len(auxiliar_population) < self.population_size:
                received_solution = future.result()
                auxiliar_population.append(received_solution)

                new_task = self.client.submit(self.problem.evaluate,
                                              self.problem.create_solution())
                task_pool.add(new_task)
            # Perform an algorithm step to create a new solution to be evaluated
            else:
                offspring_population = []

                if not self.stopping_condition_is_met():
                    offspring_population.append(future.result())

                    # Replacement
                    join_population = auxiliar_population + offspring_population
                    auxiliar_population = RankingAndCrowdingDistanceSelection(
                        self.population_size).execute(join_population)

                    # Selection
                    mating_population = []

                    for _ in range(2):
                        solution = self.selection_operator.execute(
                            population_to_evaluate)
                        mating_population.append(solution)

                    # Reproduction and evaluation
                    new_task = self.client.submit(reproduction,
                                                  mating_population,
                                                  self.problem,
                                                  self.crossover_operator,
                                                  self.mutation_operator)

                    task_pool.add(new_task)
                else:
                    for future in task_pool.futures:
                        future.cancel()
                    break

                self.evaluations += 1
                self.solutions = auxiliar_population

                self.update_progress()

        self.total_computing_time = time.time() - self.start_computing_time
        self.solutions = auxiliar_population
Ejemplo n.º 2
0
    def replacement(self, population: List[S],
                    offspring_population: List[S]) -> List[List[S]]:
        """ This method joins the current and offspring populations to produce the population of the next generation
        by applying the ranking and crowding distance selection.

        :param population: Parent population.
        :param offspring_population: Offspring population.
        :return: New population after ranking and crowding distance selection is applied.
        """
        join_population = population + offspring_population

        return RankingAndCrowdingDistanceSelection(
            self.population_size,
            dominance_comparator=self.dominance_comparator).execute(
                join_population)
Ejemplo n.º 3
0
    def replacement(self, population: List[S], offspring_population: List[FloatSolution]) -> List[List[FloatSolution]]:
        tmp_list = []

        for solution1, solution2 in zip(self.solutions, offspring_population):
            result = self.dominance_comparator.compare(solution1, solution2)
            if result == -1:
                tmp_list.append(solution1)
            elif result == 1:
                tmp_list.append(solution2)
            else:
                tmp_list.append(solution1)
                tmp_list.append(solution2)

        join_population = population + offspring_population

        return RankingAndCrowdingDistanceSelection(
            self.population_size, dominance_comparator=self.dominance_comparator
        ).execute(join_population)
Ejemplo n.º 4
0
    def run(self):
        """ Execute the algorithm. """
        self.start_computing_time = time.time()

        create_solution = dask.delayed(self.problem.create_solution)
        evaluate_solution = dask.delayed(self.problem.evaluate)

        task_pool = as_completed([], with_results=True)

        for _ in range(self.number_of_cores):
            new_solution = create_solution()
            new_evaluated_solution = evaluate_solution(new_solution)
            future = self.client.compute(new_evaluated_solution)

            task_pool.add(future)

        batches = task_pool.batches()

        auxiliar_population = []
        while len(auxiliar_population) < self.population_size:
            batch = next(batches)
            for _, received_solution in batch:
                auxiliar_population.append(received_solution)

                if len(auxiliar_population) < self.population_size:
                    break

            # submit as many new tasks as we collected
            for _ in batch:
                new_solution = create_solution()
                new_evaluated_solution = evaluate_solution(new_solution)
                future = self.client.compute(new_evaluated_solution)

                task_pool.add(future)

        self.init_progress()

        # perform an algorithm step to create a new solution to be evaluated
        while not self.stopping_condition_is_met():
            batch = next(batches)

            for _, received_solution in batch:
                offspring_population = [received_solution]

                # replacement
                join_population = auxiliar_population + offspring_population
                auxiliar_population = RankingAndCrowdingDistanceSelection(
                    self.population_size).execute(join_population)

                # selection
                mating_population = []
                for _ in range(2):
                    solution = self.selection_operator.execute(
                        auxiliar_population)
                    mating_population.append(solution)

                # Reproduction and evaluation
                new_task = self.client.submit(reproduction, mating_population,
                                              self.problem,
                                              self.crossover_operator,
                                              self.mutation_operator)
                task_pool.add(new_task)

                # update progress
                self.evaluations += 1
                self.solutions = auxiliar_population

                self.update_progress()

                if self.stopping_condition_is_met():
                    break

        self.total_computing_time = time.time() - self.start_computing_time

        # at this point, computation is done
        for future, _ in task_pool:
            future.cancel()