Skip to content

Commit e0d2bcd

Browse files
author
Julian Blank
committed
Test Case
1 parent 72880e4 commit e0d2bcd

30 files changed

+2412
-138
lines changed

pymoo/algorithms/so_pattern_search.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,35 @@ def __init__(self,
5151
display=PatternSearchDisplay(),
5252
**kwargs):
5353
"""
54+
An implementation of well-known Hooke and Jeeves Pattern Search.
5455
5556
Parameters
5657
----------
57-
explr_delta
58-
explr_rho
59-
pattern_step
60-
eps
61-
display
62-
kwargs
58+
59+
x0 : numpy.array
60+
The initial value where the local search should be initiated. If not provided `n_sample_points` are created
61+
created using latin hypercube sampling and the best solution found is set to `x0`.
62+
63+
n_sample_points : int
64+
Number of sample points to be used to determine the initial search point. (Only used of `x0` is not provided)
65+
66+
explr_delta : float
67+
The `delta` values which is used for the exploration move. If lower and upper bounds are provided the
68+
value is in relation to the overall search space. For instance, a value of 0.25 means that initially the
69+
pattern is created in 25% distance of the initial search point.
70+
71+
explr_rho : float
72+
If the move was unsuccessful then the `delta` value is reduced by multiplying it with the value provided.
73+
For instance, `explr_rho` implies that with a value of `delta/2` is continued.
74+
75+
pattern_step : float
76+
After the exploration move the new center is determined by following a promising direction.
77+
This value defines how large to step on this direction will be.
78+
79+
eps : float
80+
This value is used for an additional termination criterion. When all delta values (maximum move along each variable)
81+
is less than epsilon the algorithm is terminated. Otherwise, the default termination criteria are also used.
82+
6383
"""
6484

6585
super().__init__(display=display, **kwargs)

pymoo/usage/algorithms/usage_nelder_mead.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@
1111
seed=1,
1212
verbose=False)
1313

14-
print(res.X)
15-
print(res.F)
14+
print("Best solution found: \nX = %s\nF = %s" % (res.X, res.F))

pymoo/usage/algorithms/usage_pattern_search.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
res = minimize(problem,
1010
algorithm,
1111
seed=1,
12-
verbose=True)
12+
verbose=False)
1313

14-
print(res.X)
15-
print(res.F)
16-
print(res.CV)
14+
print("Best solution found: \nX = %s\nF = %s" % (res.X, res.F))

tests/algorithms/test_algorithms.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,14 @@ def _evaluate(self, x, out, *args, **kwargs):
7070
self.assertEqual(res.G, None)
7171

7272
res = minimize(MyProblem(),
73-
NSGA2(return_least_infeasible=True),
73+
NSGA2(),
7474
("n_gen", 10),
7575
seed=1,
7676
verbose=True,
77+
return_least_infeasible=True,
7778
save_history=True)
7879

79-
self.assertEqual(res.CV, 1)
80+
self.assertAlmostEqual(res.CV[0], 1.0)
8081

8182

8283
def test_thread_pool(self):

tests/algorithms/test_pattern_search.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class PatternSearchTest(unittest.TestCase):
1616
def test_against_orginal_implementation(self):
1717

1818
for problem in [
19-
Sphere(n_var=10),
2019
Ackley(n_var=2),
2120
Rosenbrock(n_var=2),
21+
Sphere(n_var=10),
2222
]:
2323

2424
print(problem.__class__.__name__)
@@ -49,8 +49,8 @@ def test_against_orginal_implementation(self):
4949
n = min(len(X), len(_X))
5050
X, _X, F, _F = X[:n], _X[:n], F[:n], _F[:n]
5151

52-
np.testing.assert_allclose(X, _X, rtol=0, atol=1e-4)
53-
np.testing.assert_allclose(F, _F, rtol=0, atol=1e-4)
52+
np.testing.assert_allclose(X, _X, rtol=0, atol=1e-3)
53+
np.testing.assert_allclose(F, _F, rtol=0, atol=1e-3)
5454

5555

5656
def best_nearby(delta, point, prevbest, nvars, f, funevals):

tests/algorithms/test_single_objective.py

Lines changed: 44 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,89 +2,67 @@
22

33
import numpy as np
44

5-
from pymoo.algorithms.nsga2 import NSGA2
6-
from pymoo.factory import get_problem, Problem, ZDT
5+
from pymoo.algorithms.so_genetic_algorithm import GA
6+
from pymoo.algorithms.so_nelder_mead import NelderMead
7+
from pymoo.algorithms.so_pattern_search import PatternSearch
8+
from pymoo.algorithms.so_pso import PSO
9+
from pymoo.factory import Sphere, Problem
710
from pymoo.optimize import minimize
811

9-
class MyThreadedProblem(Problem):
1012

11-
def __init__(self):
12-
super().__init__(n_var=2,
13-
n_obj=1,
14-
n_constr=0,
15-
elementwise_evaluation=True,
16-
parallelization=("threads", 4),
17-
xl=np.array([0, 0]),
18-
xu=np.array([100, 100]))
13+
class SphereNoBounds(Sphere):
1914

20-
def _evaluate(self, x, out, *args, **kwargs):
21-
out["F"] = x[0] + x[1]
22-
23-
24-
class AlgorithmTest(unittest.TestCase):
15+
def __init__(self, n_var=10, **kwargs):
16+
super().__init__(n_var=n_var, **kwargs)
17+
self.xl = None
18+
self.xu = None
2519

26-
def test_same_seed_same_result(self):
27-
problem = get_problem("zdt3")
28-
algorithm = NSGA2(pop_size=100, eliminate_duplicates=True)
2920

30-
res1 = minimize(problem, algorithm, ('n_gen', 20), seed=1)
31-
np.random.seed(200)
32-
res2 = minimize(problem, algorithm, ('n_gen', 20), seed=1)
21+
class SphereWithConstraints(Problem):
3322

34-
self.assertEqual(res1.X.shape, res2.X.shape)
35-
self.assertTrue(np.all(np.allclose(res1.X, res2.X)))
23+
def __init__(self, n_var=10):
24+
super().__init__(n_var=n_var, n_obj=1, n_constr=1, xl=-0, xu=1, type_var=np.double)
3625

37-
def test_no_pareto_front_given(self):
38-
class ZDT1NoPF(ZDT):
39-
def _evaluate(self, x, out, *args, **kwargs):
40-
f1 = x[:, 0]
41-
g = 1 + 9.0 / (self.n_var - 1) * np.sum(x[:, 1:], axis=1)
42-
f2 = g * (1 - np.power((f1 / g), 0.5))
43-
out["F"] = np.column_stack([f1, f2])
44-
45-
algorithm = NSGA2(pop_size=100, eliminate_duplicates=True)
46-
minimize(ZDT1NoPF(), algorithm, ('n_gen', 20), seed=1, verbose=True)
26+
def _evaluate(self, x, out, *args, **kwargs):
27+
out["F"] = np.sum(np.square(x - 0.5), axis=1)
28+
out["G"] = 0.1 - out["F"]
4729

48-
def test_no_feasible_solution_found(self):
49-
class MyProblem(Problem):
30+
def _calc_pareto_front(self):
31+
return 0.1
5032

51-
def __init__(self):
52-
super().__init__(n_var=2,
53-
n_obj=1,
54-
n_constr=36,
55-
xl=np.array([0, 0]),
56-
xu=np.array([100, 100]))
5733

58-
def _evaluate(self, x, out, *args, **kwargs):
59-
f1 = x[:, 0] + x[:, 1]
60-
out["F"] = np.column_stack([f1])
61-
out["G"] = np.ones(len(x))
34+
def test(problem, algorithm):
35+
res = minimize(problem, algorithm, seed=1)
36+
f = res.F[0]
37+
print(res.CV)
38+
f_opt = problem.pareto_front()[0, 0]
39+
return f, f_opt
6240

63-
res = minimize(MyProblem(),
64-
NSGA2(),
65-
("n_gen", 10),
66-
seed=1)
6741

68-
self.assertEqual(res.X, None)
69-
self.assertEqual(res.F, None)
70-
self.assertEqual(res.G, None)
42+
class SingleObjectiveAlgorithmTest(unittest.TestCase):
7143

72-
res = minimize(MyProblem(),
73-
NSGA2(return_least_infeasible=True),
74-
("n_gen", 10),
75-
seed=1,
76-
verbose=True,
77-
save_history=True)
44+
def test_sphere(self):
45+
problem = Sphere()
46+
for algorithm in [NelderMead(), PatternSearch(), PSO(), GA()]:
47+
f, f_opt = test(problem, algorithm)
48+
self.assertAlmostEqual(f, f_opt, places=5)
49+
print(problem.__class__.__name__, algorithm.__class__.__name__, "Yes")
7850

79-
self.assertEqual(res.CV, 1)
51+
def test_sphere_with_constraints(self):
52+
problem = SphereWithConstraints()
53+
for algorithm in [GA(), NelderMead(), PatternSearch()]:
54+
f, f_opt = test(problem, algorithm)
55+
self.assertAlmostEqual(f, f_opt, places=3)
56+
print(problem.__class__.__name__, algorithm.__class__.__name__, "Yes")
8057

58+
def test_sphere_no_bounds(self):
59+
problem = SphereNoBounds()
60+
x0 = np.random.random(problem.n_var)
8161

82-
def test_thread_pool(self):
83-
minimize(MyThreadedProblem(),
84-
NSGA2(),
85-
("n_gen", 10),
86-
seed=1,
87-
save_history=False)
62+
for algorithm in [NelderMead(x0=x0), PatternSearch(x0=x0)]:
63+
f, f_opt = test(problem, algorithm)
64+
self.assertAlmostEqual(f, f_opt, places=5)
65+
print(problem.__class__.__name__, algorithm.__class__.__name__, "Yes")
8866

8967

9068
if __name__ == '__main__':

tests/scratch/dask-worker-space/global.lock

Whitespace-only changes.

tests/scratch/dask-worker-space/purge.lock

Whitespace-only changes.

tests/scratch/dask-worker-space/worker-14qmnfer.dirlock

Whitespace-only changes.

tests/scratch/dask-worker-space/worker-kc7yq9q_.dirlock

Whitespace-only changes.

0 commit comments

Comments
 (0)