Esempio n. 1
0
 def nelder_mead(problem, x):
     solver = NelderMead(X=x)
     solver.initialize(problem)
     solver._initialize()
     solver.n_gen = 1
     solver.next()
     return solver
Esempio n. 2
0
 def test_sphere_with_constraints(self):
     problem = SphereWithConstraints()
     for algorithm in [GA(), NelderMead(), PatternSearch(), CuckooSearch()]:
         f, f_opt = test(problem, algorithm)
         self.assertAlmostEqual(f, f_opt, places=3)
         print(problem.__class__.__name__, algorithm.__class__.__name__,
               "Yes")
Esempio n. 3
0
    def test_against_scipy(self):
        problem = get_problem("rosenbrock")
        problem.xl = None
        problem.xu = None
        x0 = np.array([0.5, 0.5])

        hist_scipy = []

        def fun(x):
            hist_scipy.append(x)
            return problem.evaluate(x)

        scipy_minimize(fun, x0, method='Nelder-Mead')
        hist_scipy = np.array(hist_scipy)

        hist = []

        def callback(x):
            if x.shape == 2:
                hist.extend(x)
            else:
                hist.append(x)

        problem.callback = callback
        minimize(
            problem,
            NelderMead(X=x0,
                       max_restarts=0,
                       termination=get_termination("n_eval", len(hist_scipy))))
        hist = np.row_stack(hist)[:len(hist_scipy)]

        self.assertTrue(np.all(hist - hist_scipy < 1e-7))
Esempio n. 4
0
 def test_sphere(self):
     problem = Sphere()
     for algorithm in [NelderMead(), PatternSearch(), PSO(), GA()]:
         f, f_opt = test(problem, algorithm)
         self.assertAlmostEqual(f, f_opt, places=5)
         print(problem.__class__.__name__, algorithm.__class__.__name__,
               "Yes")
Esempio n. 5
0
    def test_sphere_no_bounds(self):
        problem = SphereNoBounds()
        x0 = np.random.random(problem.n_var)

        for algorithm in [NelderMead(x0=x0), PatternSearch(x0=x0)]:
            f, f_opt = test(problem, algorithm)
            self.assertAlmostEqual(f, f_opt, places=5)
            print(problem.__class__.__name__, algorithm.__class__.__name__,
                  "Yes")
Esempio n. 6
0
from pymoo.algorithms.so_nelder_mead import NelderMead
from pymoo.factory import get_problem
from pymoo.optimize import minimize

problem = get_problem("sphere")

algorithm = NelderMead(n_max_restarts=10)

res = minimize(problem, algorithm, seed=1, verbose=False)

print(res.X)
print(res.F)
from pymoo.optimize import minimize
from pymoo.vendor.vendor_scipy import CG, BFGS, Powell


class MySphere(Problem):
    def __init__(self, n_var=3):
        super().__init__(n_var=n_var,
                         n_obj=1,
                         n_constr=0,
                         xl=None,
                         xu=None,
                         type_var=anp.double)

    def _evaluate(self, x, out, *args, **kwargs):
        out["F"] = anp.sum(anp.square(x + 10), axis=1)


problem = MySphere()

x0 = np.random.random(problem.n_var)

algorithms = [NelderMead(x0=x0), CG(x0=x0), BFGS(x0=x0), Powell(x0=x0)]

for algorithm in algorithms:

    res = minimize(problem, algorithm, seed=1, verbose=False)

    print(
        f"{algorithm.__class__.__name__}: Best solution found: X = {res.X} | F = {res.F} | CV = {res.F}"
    )
Esempio n. 8
0
 def test_with_bounds_with_restart(self):
     problem = get_problem("rastrigin")
     method = NelderMead(X=np.array([1, 1]), max_restarts=2)
     minimize(problem, method, verbose=False)
Esempio n. 9
0
 def test_with_bounds_no_initial_point(self):
     problem = get_problem("rastrigin")
     method = NelderMead(max_restarts=0)
     minimize(problem, method, verbose=False)
Esempio n. 10
0
 def test_no_bounds(self):
     problem = get_problem("rastrigin")
     problem.xl = None
     problem.xu = None
     method = NelderMead(X=np.array([1, 1]), max_restarts=0)
     minimize(problem, method, verbose=False)
Esempio n. 11
0
from pymoo.algorithms.so_nelder_mead import NelderMead
from pymoo.factory import get_problem
from pymoo.optimize import minimize

problem = get_problem("sphere")

algorithm = NelderMead()

res = minimize(problem, algorithm, seed=1, verbose=False)

print("Best solution found: \nX = %s\nF = %s" % (res.X, res.F))
Esempio n. 12
0
    prefix = "runs"

    # name of the experiment
    name = "nelder-mead-0.3.2"

    # number of runs to execute
    n_runs = 10

    # path were the files for this experiment are saved
    path = os.path.join(prefix, name)

    for _problem in setup:

        problem = get_problem(_problem)

        method = NelderMead(n_max_local_restarts=2)

        for run in range(1, n_runs + 1):
            fname = "%s_%s.run" % (_problem, run)
            _in = os.path.join(path, fname)
            _out = "results/%s/%s/%s_%s.out" % (name, _problem.replace("_", "/"), _problem, run)

            data = {
                'args': [problem, method],
                'kwargs': {
                    'seed': run,
                },
                'in': _in,
                'out': _out,
            }