Esempio n. 1
0
    def test_SequentialDriver_with_rosenbrock(self):
        """Test :class:`gc3libs.optimizer.drivers.SequentialDriver
        """

        # Test parameters
        magic_seed = 100
        dim = 2
        pop_size = 100
        lower_bounds = -2 * np.ones(dim)
        upper_bounds = +2 * np.ones(dim)
        prob_cross = 0.8

        configure_logger(level=logging.CRITICAL)
        log = logging.getLogger("gc3.gc3libs")

        initial_pop = draw_population(lower_bds=lower_bounds,
                                      upper_bds=upper_bounds,
                                      dim=dim,
                                      size=pop_size,
                                      in_domain=self.rosenbrock_sample_filter,
                                      seed=magic_seed)

        algo = DifferentialEvolutionAlgorithm(
            initial_pop=initial_pop,
            de_step_size=0.85,  # DE-stepsize ex [0, 2]
            prob_crossover=prob_cross,
            # crossover probabililty constant ex [0, 1]
            itermax=1000,  # maximum number of iterations (generations)
            dx_conv_crit=None,  # stop when variation among x's is < this
            y_conv_crit=1e-5,  # stop when ofunc < y_conv_crit
            de_strategy='DE_local_to_best',
            logger=log,
            in_domain=self.rosenbrock_sample_filter,
            seed=magic_seed,
            after_update_opt_state=[print_stats, log_stats]
            #, plot_population(temp_stage_dir)]
        )
        assert algo.de_step_size == 0.85
        assert algo.prob_crossover == prob_cross
        assert algo.itermax == 1000
        assert algo.dx_conv_crit is None
        assert algo.y_conv_crit == 1e-5
        assert algo.de_strategy == 'DE_local_to_best'
        assert algo.logger == log

        opt = SequentialDriver(algo,
                               target_fn=self.rosenbrock_fn,
                               fmt="%12.8f")
        assert opt.target_fn == self.rosenbrock_fn

        # run the Diff.Evo. algorithm
        opt.de_opt()

        assert algo.has_converged()
        assert (algo.best_y - 0.) < algo.y_conv_crit
        assert (algo.best_x[0] - 1.) < 1e-3
        assert (algo.best_x[1] - 1.) < 1e-3
Esempio n. 2
0
    def new_tasks(self, extra):

        # General settings
        dim = 2
        lower_bounds = -2 * np.ones(dim)
        upper_bounds = +2 * np.ones(dim)

        self.optimization_dir = os.path.join(self.params.path_to_stage_dir,
                                             'optimize_rosenbrock')
        # create optimization_dir
        if os.path.isdir(self.optimization_dir):
            shutil.rmtree(self.optimization_dir)
        os.mkdir(self.optimization_dir)

        log = logging.getLogger('gc3.gc3libs.EvolutionaryAlgorithm')
        log.setLevel(logging.DEBUG)
        log.propagate = 0
        stream_handler = logging.StreamHandler()
        stream_handler.setLevel(logging.DEBUG)
        log_file_name = os.path.join(os.getcwd(), 'EvolutionaryAlgorithm.log')
        file_handler = logging.FileHandler(log_file_name, mode='w')
        file_handler.setLevel(logging.DEBUG)
        log.addHandler(stream_handler)
        log.addHandler(file_handler)

        initial_pop = draw_population(lower_bds=lower_bounds,
                                      upper_bds=upper_bounds,
                                      size=self.params.pop_size,
                                      dim=dim)

        de_solver = DifferentialEvolutionAlgorithm(
            initial_pop=initial_pop,
            de_step_size=0.85,  # DE-stepsize ex [0, 2]
            prob_crossover=1,  # crossover probabililty constant ex [0, 1]
            itermax=200,  # maximum number of iterations (generations)
            dx_conv_crit=None,  # stop when variation among x's is < this
            y_conv_crit=self.params.
            y_conv_crit,  # stop when ofunc < y_conv_crit
            de_strategy='DE_local_to_best',
            logger=log,
        )

        # create an instance globalObt

        jobname = 'rosenbrock'
        kwargs = extra.copy()
        kwargs['path_to_stage_dir'] = self.optimization_dir
        kwargs['opt_algorithm'] = de_solver
        kwargs['task_constructor'] = task_constructor_rosenbrock(
            path_to_exectuable=self.params.path_to_executable,
            path_to_base_dir=self.params.path_to_base_dir)
        kwargs['extract_value_fn'] = compute_target_rosenbrock
        kwargs['cur_pop_file'] = 'cur_pop'

        return [ParallelDriver(jobname=jobname, **kwargs)]