Пример #1
0
    def fit(self, X: np.ndarray, y: np.ndarray):
        if isinstance(X, (pd.DataFrame, pd.Series)):
            X = X.values
        if isinstance(y, (pd.DataFrame, pd.Series)):
            y = y.values.reshape(-1, 1)
        if X.shape[0] != y.shape[0]:
            msg = (f"Ensure that the number of samples in X and y are the same"
                   f"Number of samples in X = {X.shape[0]}"
                   f"Number of samples in y = {y.shape[0]}")
            raise ModelError(msg)
        self.X = X
        self.y = y
        if self.subsets is None:
            self.subsets = []

            # Create random subsets of decision variables for each subnet

            for i in range(self.num_subnets):
                n = random.randint(1, self.X.shape[1])
                self.subsets.append(random.sample(range(self.X.shape[1]), n))

            # Ensure that each decision variable is used as an input in at least one subnet
            for n in list(range(self.X.shape[1])):
                if not any(n in k for k in self.subsets):
                    self.subsets[random.randint(0, self.num_subnets -
                                                1)].append(n)

        # Create problem
        problem = surrogateProblem(
            performance_evaluator=self._model_performance)
        problem.n_of_objectives = 2
        # Create Population
        initial_pop = self._create_individuals()
        population = SurrogatePopulation(problem, self.pop_size, initial_pop,
                                         None, None, None)
        # Do evolution
        evolver = self.training_algorithm(problem,
                                          initial_population=population)
        recombinator = EvoDN2Recombination(evolver=evolver)
        evolver.population.recombination = recombinator
        figure = animate_init_(evolver.population.objectives,
                               filename="EvoDN2.html")
        while evolver.continue_evolution():
            evolver.iterate()
            figure = animate_next_(
                evolver.population.objectives,
                figure,
                filename="EvoDN2.html",
                generation=evolver._iteration_counter,
            )
        self.model_population = evolver.population
        # Selection
        self.select()
        self.model_trained = True
Пример #2
0
    def fit(self, X: np.ndarray, y: np.ndarray):
        if isinstance(X, (pd.DataFrame, pd.Series)):
            X = X.values
        if isinstance(y, (pd.DataFrame, pd.Series)):
            y = y.values.reshape(-1, 1)
        if X.shape[0] != y.shape[0]:
            msg = (f"Ensure that the number of samples in X and y are the same"
                   f"Number of samples in X = {X.shape[0]}"
                   f"Number of samples in y = {y.shape[0]}")
            raise ModelError(msg)
        self.X = X
        self.y = y

        # Create problem
        problem = surrogateProblem(
            performance_evaluator=self._model_performance)
        problem.n_of_objectives = 2
        # Create Population
        initial_pop = self._create_individuals()
        population = SurrogatePopulation(problem, self.pop_size, initial_pop,
                                         None, None, None)
        # Do evolution
        evolver = self.training_algorithm(problem,
                                          initial_population=population)
        recombinator = EvoNNRecombination(evolver=evolver,
                                          mutation_type=self.mutation_type)
        evolver.population.recombination = recombinator
        figure = animate_init_(evolver.population.objectives,
                               filename="EvoNN.html")
        while evolver.continue_evolution():
            evolver.iterate()
            figure = animate_next_(
                evolver.population.objectives,
                figure,
                filename="EvoNN.html",
                generation=evolver._iteration_counter,
            )
        self.model_population = evolver.population
        # Selection
        self.select()
        self.model_trained = True
Пример #3
0
    def fit(self, X: pd.DataFrame, y: pd.DataFrame):
        if X.shape[0] != y.shape[0]:
            msg = (f"Ensure that the number of samples in X and y are the same"
                   f"Number of samples in X = {X.shape[0]}"
                   f"Number of samples in y = {y.shape[0]}")
            raise ModelError(msg)
        self.X = X
        self.y = y
        if self.terminal_set is None:
            self.terminal_set = X.columns.tolist()

        # Create problem
        problem = surrogateProblem(
            performance_evaluator=self._model_performance)
        problem.n_of_objectives = 2
        # Create Population
        initial_pop = self._create_individuals()
        population = SurrogatePopulation(problem, self.pop_size, initial_pop,
                                         None, None, None)
        population.xover = BioGP_xover(
            probability_crossover=self.probability_crossover)
        population.mutation = BioGP_mutation(
            probability_mutation=self.probability_mutation)
        # Do single objective evolution
        tournament_evolver = TournamentEA(
            problem,
            initial_population=population,
            population_size=self.pop_size,
            n_gen_per_iter=self.single_obj_generations,
            n_iterations=1,
        )
        figure = animate_init_(tournament_evolver.population.objectives,
                               filename="BioGP.html")
        while tournament_evolver.continue_evolution():
            tournament_evolver.iterate()
            figure = animate_next_(
                tournament_evolver.population.objectives,
                figure,
                filename="BioGP.html",
                generation=tournament_evolver._iteration_counter,
            )
        population = tournament_evolver.population

        # Do bi-objective evolution
        evolver = self.training_algorithm(
            problem,
            initial_population=population,
            population_size=self.pop_size,
            n_gen_per_iter=10,
            n_iterations=10,
        )
        while evolver.continue_evolution():
            evolver.iterate()
            figure = animate_next_(
                evolver.population.objectives,
                figure,
                filename="BioGP.html",
                generation=evolver._iteration_counter +
                tournament_evolver._iteration_counter,
            )
        self.model_population = evolver.population
        # Selection
        self.select()
        self.model_trained = True
from desdeo_problem.testproblems.TestProblems import test_problem_builder
from desdeo_emo.EAs.RVEA import RVEA
from desdeo_emo.EAs.NSGAIII import NSGAIII
from desdeo_emo.othertools.plotlyanimate import animate_init_, animate_next_


dtlz3 = test_problem_builder("DTLZ3", n_of_variables=12, n_of_objectives=11)
evolver = RVEA(dtlz3, n_iterations=10)
figure = animate_init_(evolver.population.objectives, filename="dtlz3.html")
while evolver.continue_evolution():
    evolver.iterate()
    figure = animate_next_(
        evolver.population.objectives,
        figure,
        filename="dtlz3.html",
        generation=evolver._iteration_counter,
    )