Пример #1
0
    def dynamical_systems_model_nsga_2(self, train_X, train_y, test_X, test_y, columns, equations, targets, parameters, pop_size=10, max_generations=100, per_time_step=True):
        prng = random.Random()
        evaluator = Evaluator()
        model = Model()

        # Create the model.
        model.set_model(columns, equations, parameters)

        # Set the desired/known values in our evaluator.
        evaluator.set_values(model, train_X, train_y, test_X, test_y, targets)

        # Initialize the NSGA2 algorithm.
        ea = inspyred.ec.emo.NSGA2(prng)
        ea.variator = [inspyred.ec.variators.blend_crossover,
                       inspyred.ec.variators.gaussian_mutation]
        ea.terminator = inspyred.ec.terminators.generation_termination

        # Let it run.
        final_pop = ea.evolve(generator=evaluator.generator, evaluator=evaluator.evaluator_multi_objective, pop_size=pop_size, maximize=False, bounder=None,max_generations=max_generations)
        final_arc = ea.archive

        # For all solutions (they reside on the pareto front)
        return_values = []
        for f in final_arc:

            # Predict the results.
            train_fitness, y_train_pred = evaluator.predict(f.candidate, training=True, per_time_step=per_time_step)
            test_fitness, y_test_pred = evaluator.predict(f.candidate, training=False, per_time_step=per_time_step)

            # And collect the predictions and fitness values.
            row = [y_train_pred, train_fitness, y_test_pred, test_fitness]
            return_values.append(row)
        return return_values
Пример #2
0
    def dynamical_systems_model_nsga_2(train_X, train_y, test_X, test_y, columns, equations, targets, parameters,
                                       pop_size=10, max_generations=100, per_time_step=True):
        """
        Apply a known dynamical systems model for a regression problem by tuning its parameters towards the data.
        Hereto, it can use multiple objectives as it uses the nsga 2 algorithm. To be provided are: training set (
        both input and target) test set (both input and target) a list of columns the model addresses (i.e. the
        states), the string should be preceded by 'self.' in order for the approach to work. a list of equations to
        derive the specified states, again using 'self.' preceding all parameters and columns names. a list of
        targets (a subset of the columns) (again with 'self.') a list of parameters in the equations (again with
        'self.') the population size of nsga 2 the maximum number of generations for nsga 2 whether we want to
        predict per time point (i.e. we reset the state values of the previous time point to their observed values.
        It returns a series of predictions for the training and test sets that are the results of parameter setting
        that are positioned on the Pareto front.
        """

        prng = random.Random()
        evaluator = Evaluator()
        model = Model()

        # Create the model
        model.set_model(columns, equations, parameters)

        # Set the desired/known values in our evaluator
        evaluator.set_values(model, train_X, train_y, test_X, test_y, targets)

        # Initialize the NSGA2 algorithm
        ea = inspyred.ec.emo.NSGA2(prng)
        ea.variator = [inspyred.ec.variators.blend_crossover,
                       inspyred.ec.variators.gaussian_mutation]
        ea.terminator = inspyred.ec.terminators.generation_termination

        # Let it run
        ea.evolve(generator=evaluator.generator, evaluator=evaluator.evaluator_multi_objective,
                  pop_size=pop_size, maximize=False, bounder=None, max_generations=max_generations)
        final_arc = ea.archive

        # For all solutions (they reside on the pareto front)
        return_values = []
        for f in final_arc:
            # Predict the results
            train_fitness, y_train_pred = evaluator.predict(f.candidate, training=True, per_time_step=per_time_step)
            test_fitness, y_test_pred = evaluator.predict(f.candidate, training=False, per_time_step=per_time_step)

            # And collect the predictions and fitness values
            row = [y_train_pred, train_fitness, y_test_pred, test_fitness]
            return_values.append(row)
        return return_values
Пример #3
0
    def dynamical_systems_model_sa(self, train_X, train_y, test_X, test_y, columns, equations, targets, parameters, pop_size=10, max_generations=100, per_time_step=True):
        prng = random.Random()
        evaluator = Evaluator()
        model = Model()

        # Create the model.
        model.set_model(columns, equations, parameters)

        # Set the desired/known values in our evaluator.
        evaluator.set_values(model, train_X, train_y, test_X, test_y, targets)
        ea = inspyred.ec.SA(prng)
        ea.terminator = inspyred.ec.terminators.generation_termination

        # Let it run.
        final_pop = ea.evolve(generator=evaluator.generator, evaluator=evaluator.evaluator_single_objective, maximize=False, bounder=None, max_generations=max_generations)

        # Select the best one and use it to predict.
        best = min(final_pop)
        train_fitness, y_train_pred = evaluator.predict(best.candidate, training=True, per_time_step=per_time_step)
        test_fitness, y_test_pred = evaluator.predict(best.candidate, training=False, per_time_step=per_time_step)
        return y_train_pred, y_test_pred
Пример #4
0
    def dynamical_systems_model_sa(train_X, train_y, test_X, test_y, columns, equations, targets, parameters,
                                   max_generations=100, per_time_step=True):
        """
        Apply a known dynamical systems model for a regression problem by tuning its parameters towards the data
        using SA. Hereto, it can use multiple objectives but will just average the values of each one. In the end,
        one solution will be provided. To be provided are: training set (both input and target) test set (both input
        and target) a list of columns the model addresses (i.e. the states), the string should be preceded by 'self.'
        in order for the approach to work. a list of equations to derive the specified states, again using 'self.'
        preceding all parameters and columns names. a list of targets (a subset of the columns) (again with 'self.')
        a list of parameters in the equations (again with 'self.') the population size for SA the maximum number of
        generations for the SA. whether we want to predict per time point (i.e. we reset the state values of the
        previous time point to their observed values. It returns a prediction for the training and test sets that are
        the result of the best parameter setting.
        """

        prng = random.Random()
        evaluator = Evaluator()
        model = Model()

        # Create the model
        model.set_model(columns, equations, parameters)

        # Set the desired/known values in our evaluator
        evaluator.set_values(model, train_X, train_y, test_X, test_y, targets)
        ea = inspyred.ec.SA(prng)
        ea.terminator = inspyred.ec.terminators.generation_termination

        # Let it run
        final_pop = ea.evolve(generator=evaluator.generator, evaluator=evaluator.evaluator_single_objective,
                              maximize=False, bounder=None, max_generations=max_generations)

        # Select the best one and use it to predict
        best = min(final_pop)
        train_fitness, y_train_pred = evaluator.predict(best.candidate, training=True, per_time_step=per_time_step)
        test_fitness, y_test_pred = evaluator.predict(best.candidate, training=False, per_time_step=per_time_step)
        return y_train_pred, y_test_pred