Esempio n. 1
0
    def test_exception_handling(self):
        x0 = [5]
        bounds = tuple([(0, 10) for i in x0])
        options = {'generations': 2, 'pop_size': 4, 'trm_size': 1}

        def fun_exception(x, *args):
            raise Exception("Dummy Exception")
            return self.fun(x, *args)

        res = ga.minimize(fun_exception,
                          bounds,
                          x0=x0,
                          options=options,
                          workers=1)
Esempio n. 2
0
    def test_ga(self):
        # Test norm()
        x = [0, 2.5, 7.5, 10]
        bounds = tuple([(0, 10) for i in x])
        n = ga.norm(x, bounds)
        self.assertTrue(
            (np.abs(n - np.array([0, 0.25, 0.75, 1.0])) < 1e-12).all())

        # Test x0 and elitism
        options = {'generations': 10, 'mut_rate': 0.25}
        opt1 = ga.minimize(self.fun, bounds, options=options)
        self.assertEqual(opt1.fx, self.fun(opt1.x))

        options = {'generations': 50, 'mut_rate': 0.01}
        x0 = opt1.x
        opt2 = ga.minimize(self.fun, bounds, x0=x0, options=options)
        self.assertEqual(opt2.fx, self.fun(opt2.x))
        self.assertLessEqual(opt2.fx, opt1.fx)

        # Test small population size
        options = {'generations': 2, 'pop_size': 4, 'trm_size': 1}
        opt3 = ga.minimize(self.fun, bounds, x0=x0, options=options, workers=1)

        # Test pop_size and trm_size re-adjusting
        options = {'generations': 2, 'pop_size': 8}
        opt4 = ga.minimize(self.fun, bounds, x0=x0, options=options, workers=2)

        # Test convergence
        options['generations'] = 100
        options['mut_rate'] = 0.01
        opt = ga.minimize(self.fun, bounds)
        self.assertLess(opt.fx, 0.1)

        # Test callback
        options = {'generations': 5, 'pop_size': 8, 'trm_size': 1}

        def cb(x, fx, ng):
            print("Generation #{}".format(ng))
            print("x = {}".format(x))
            print("f(x) = {}".format(fx))
            global fx_last
            global x_last
            fx_last = fx
            x_last = x

        opt = ga.minimize(self.fun,
                          bounds,
                          callback=cb,
                          options=options,
                          workers=2)
        self.assertEqual(fx_last, opt.fx)
        self.assertTrue((np.abs(x_last - opt.x) < 1e-10).all())
Esempio n. 3
0
    def test_args_passing_2workers(self):
        x0 = tuple(np.random.random(5))
        bounds = tuple([(-1, 5) for i in x0])
        args = ['arg0_ok', 'arg1_ok']
        options = {'generations': 3}

        def fun_args_wrapper(x, *args):
            arg0 = args[0]
            arg1 = args[1]
            self.assertEqual(arg0, 'arg0_ok')
            self.assertEqual(arg1, 'arg1_ok')
            return self.fun(x)

        res = ga.minimize(fun_args_wrapper,
                          bounds,
                          x0=x0,
                          args=args,
                          options=options,
                          workers=2)
Esempio n. 4
0
    def test_exotic_pickling(self):
        x0 = tuple(np.random.random(8))
        bounds = tuple([(-1, 1) for i in x0])
        options = {'generations': 3}

        class FunWrapper:
            def __init__(self, fun_to_apply):
                self.fun_to_apply = fun_to_apply

        args = [FunWrapper(np.fft.fft)]

        def fun_to_pickle(x, *args):
            """Some exotic function to be pickled."""
            fun_wrapper = args[0]
            x = fun_wrapper.fun_to_apply(x)
            return self.fun(x)

        res = ga.minimize(fun_to_pickle,
                          bounds,
                          x0=x0,
                          args=args,
                          options=options,
                          workers=2)
Esempio n. 5
0
 def test_ga_1param(self):
     x0 = [5]
     bounds = tuple([(0, 10) for i in x0])
     options = {'generations': 20, 'pop_size': 4, 'trm_size': 1}
     res = ga.minimize(self.fun, bounds, x0=x0, options=options, workers=1)