QDpy
Loading...
Searching...
No Matches
artificial_landscapes.py
Go to the documentation of this file.
1# This file is part of qdpy.
2#
3# qdpy is free software: you can redistribute it and/or modify
4# it under the terms of the GNU Lesser General Public License as
5# published by the Free Software Foundation, either version 3 of
6# the License, or (at your option) any later version.
7#
8# qdpy is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU Lesser General Public License for more details.
12#
13# You should have received a copy of the GNU Lesser General Public
14# License along with qdpy. If not, see <http://www.gnu.org/licenses/>.
15
16
18
19"""Collection of test functions and benchmarks for QD algorithms based on artificial landscapes. Cf https://arxiv.org/pdf/1308.4008v1.pdf and https://arxiv.org/pdf/1207.4318.pdf and https://en.wikipedia.org/wiki/Test_functions_for_optimization and https://www.sfu.ca/~ssurjano/optimization.html"""
20
21__all__ = [
22 "sphere", "illumination_sphere", "SphereBenchmark",
23 "weighted_sphere", "illumination_weighted_sphere", "WeightedSphereBenchmark",
24 "rotated_hyper_ellipsoid", "illumination_rotated_hyper_ellipsoid", "RotatedHyperEllipsoidBenchmark",
25 "rosenbrock", "illumination_rosenbrock", "RosenbrockBenchmark",
26 "rastrigin_normalised", "illumination_rastrigin_normalised", "NormalisedRastriginBenchmark",
27 "rastrigin", "illumination_rastrigin", "RastriginBenchmark",
28 "schwefel", "illumination_schwefel", "SchwefelBenchmark",
29 "small_schwefel", "illumination_small_schwefel", "SmallSchwefelBenchmark",
30 "griewangk", "illumination_griewangk", "GriewangkBenchmark",
31 "sum_of_powers", "illumination_sum_of_powers", "SumOfPowersBenchmark",
32 "ackley", "illumination_ackley", "AckleyBenchmark",
33 "styblinski_tang", "illumination_styblinski_tang", "StyblinskiTangBenchmark",
34 "levy", "illumination_levy", "LevyBenchmark",
35 "perm0db", "illumination_perm0db", "Perm0dbBenchmark",
36 "permdb", "illumination_permdb", "PermdbBenchmark",
37 "trid", "illumination_trid", "TridBenchmark",
38 "zakharov", "illumination_zakharov", "ZakharovBenchmark",
39 "dixon_price", "illumination_dixon_price", "DixonPriceBenchmark",
40 "powell", "illumination_powell", "PowellBenchmark",
41 "michalewicz", "illumination_michalewicz", "MichalewiczBenchmark",
42 "wavy", "illumination_wavy", "WavyBenchmark",
43 "trigonometric02", "illumination_trigonometric02", "Trigonometric02Benchmark",
44 "qing", "illumination_qing", "QingBenchmark",
45 "small_qing", "illumination_small_qing", "SmallQingBenchmark",
46 "deb01", "illumination_deb01", "Deb01Benchmark",
47 "shubert04", "illumination_shubert04", "Shubert04Benchmark",
48 ]
49
50from math import cos, pi
51from typing import Optional, Tuple, List, Iterable, Iterator, Any, TypeVar, Generic, Union, Sequence, MutableSet, MutableSequence, Type, Callable, Generator, Mapping, MutableMapping, overload
52import numpy as np
53
54from qdpy import algorithms
55from qdpy.base import *
56from .base import *
57
58
59
64def _illuminate_artificial_landscape(ind, func, nb_features = 2):
65 """Attach descriptors and named scores to a raw objective function.
66
67 An illumination evaluator returns fitness, a prefix of the genome
68 as descriptors, and a score mapping containing string coordinate
69 keys plus ``perf`` for the first objective.
70 """
71 fitness = func(ind)
72 features = list(ind[:nb_features])
73 scores = {f"{i}": v for i, v in enumerate(features)}
74 scores['perf'] = fitness[0]
75 return fitness, features, scores
76
77
78def sphere(ind):
79 r"""Sphere function defined as:
80 $$ f(x) = \sum_{i=1}^{n} x_i^{2} $$
81 with a search domain of $-5.12 < x_i < 5.12, 1 \leq i \leq n$.
82 The global minimum is at $f(x_1, ..., x_n) = f(0, ..., 0) = 0.
83 It is continuous, convex and unimodal.
84 """
85 return sum((x ** 2. for x in ind)),
86illumination_sphere = algorithms.partial(_illuminate_artificial_landscape, func=sphere) # type: ignore
88 """Minimise the sum of squared genome coordinates."""
89 def __init__(self, nb_features: int = 2):
90 self.nb_features = nb_features
91 ind_domain = (-5.12, 5.12)
92 super().__init__(fn=algorithms.partial(illumination_sphere, nb_features=nb_features),
93 ind_domain = ind_domain,
94 fitness_domain = ((0., math.inf),), features_domain = (ind_domain,) * nb_features,
95 default_task = "minimisation")
96
97def weighted_sphere(ind):
98 r"""Weighted sphere function defined as:
99 $$ f(x) = \sum_{i=1}^{n} i \times x_i^{2} $$
100 with a search domain of $-5.12 < x_i < 5.12, 1 \leq i \leq n$.
101 The global minimum is at $f(x_1, ..., x_n) = f(0, ..., 0) = 0.
102 It is continuous, convex and unimodal.
103 """
104 return sum(i * x**2.0 for i, x in enumerate(ind, start=1)),
105illumination_weighted_sphere = algorithms.partial(_illuminate_artificial_landscape, func=weighted_sphere) # type: ignore
107 """Minimise squared coordinates weighted by their one-based coordinate indexes."""
108 def __init__(self, nb_features: int = 2):
109 self.nb_features = nb_features
110 ind_domain = (-5.12, 5.12)
111 super().__init__(fn=algorithms.partial(illumination_weighted_sphere, nb_features=nb_features),
112 ind_domain = ind_domain,
113 fitness_domain = ((0., math.inf),), features_domain = (ind_domain,) * nb_features,
114 default_task = "minimisation")
115
116
117def rotated_hyper_ellipsoid(ind):
118 r"""Rotated hyper-ellipsoid function defined as:
119 $$ f(x) = \sum_{i=1}^{n} \sum_{j=1}^{i} x_j^{2} $$
120 with a search domain of $-65.536 < x_i < 65.536, 1 \leq i \leq n$.
121 The global minimum is at $f(x_1, ..., x_n) = f(0, ..., 0) = 0.
122 It is continuous, convex and unimodal.
123 """
124 return sum((len(ind) - i) * x**2.0 for i, x in enumerate(ind)),
125illumination_rotated_hyper_ellipsoid = algorithms.partial(_illuminate_artificial_landscape, func=rotated_hyper_ellipsoid) # type: ignore
127 """Minimise the sum of squared prefix norms."""
128 def __init__(self, nb_features: int = 2):
129 self.nb_features = nb_features
130 ind_domain = (-65.536, 65.536)
131 super().__init__(fn=algorithms.partial(illumination_rotated_hyper_ellipsoid, nb_features=nb_features),
132 ind_domain = ind_domain,
133 fitness_domain = ((0., math.inf),), features_domain = (ind_domain,) * nb_features,
134 default_task = "minimisation")
135
136
137def rosenbrock(ind):
138 r"""Rosenbrock function defined as:
139 $$ f(x) = \sum_{i=1}^{n-1} 100 \times (x_{i+1} - x_i^{2})^{2} + (1 - x_{i})^{2} $$
140 with a search domain of $-2.048 < x_i < 2.048, 1 \leq i \leq n$.
141 The global minimum is at $f(x_1, ..., x_n) = f(1, ..., 1) = 0.
142 """
143 return sum((100. * (ind[i+1]-ind[i]**2.)**2. + (1-ind[i])**2. for i in range(len(ind)-1))),
144illumination_rosenbrock = algorithms.partial(_illuminate_artificial_landscape, func=rosenbrock) # type: ignore
146 """Minimise Rosenbrock's function with its curved valley toward the all-ones genome."""
147 def __init__(self, nb_features: int = 2):
148 self.nb_features = nb_features
149 ind_domain = (-2.048, 2.048)
150 super().__init__(fn=algorithms.partial(illumination_rosenbrock, nb_features=nb_features),
151 ind_domain = ind_domain,
152 fitness_domain = ((0., math.inf),), features_domain = (ind_domain,) * nb_features,
153 default_task = "minimisation")
154
155
156
157def rastrigin_normalised(ind, a=10.):
158 """Rastrigin test function, with inputs and outputs scaled to be between 0. and 1."""
159 n = float(len(ind))
160 ind_n = [-5.12 + x * (5.12 * 2.) for x in ind]
161 return (a * n + sum(x * x - a * cos(2. * pi * x) for x in ind_n)) / (a * n + n * (5.12 * 5.12 + a)),
162illumination_rastrigin_normalised = algorithms.partial(_illuminate_artificial_landscape, func=rastrigin_normalised) # type: ignore
164 """Minimise Rastrigin with genome coordinates and output scaled to the unit interval."""
165 def __init__(self, nb_features: int = 2):
166 self.nb_features = nb_features
167 ind_domain = (0., 1.)
168 super().__init__(fn=algorithms.partial(illumination_rastrigin_normalised, nb_features=nb_features),
169 ind_domain = ind_domain,
170 fitness_domain = ((0., 1.),), features_domain = (ind_domain,) * nb_features,
171 default_task = "minimisation")
172
173
174def rastrigin(ind, a=10.):
175 """Rastrigin test function."""
176 n = float(len(ind))
177 return a * n + sum(x * x - a * cos(2. * pi * x) for x in ind),
178illumination_rastrigin = algorithms.partial(_illuminate_artificial_landscape, func=rastrigin) # type: ignore
180 """Minimise the multimodal Rastrigin objective on its native coordinate domain."""
181 def __init__(self, nb_features: int = 2):
182 self.nb_features = nb_features
183 ind_domain = (-5.12, 5.12)
184 super().__init__(fn=algorithms.partial(illumination_rastrigin, nb_features=nb_features),
185 ind_domain = ind_domain,
186 fitness_domain = ((0., math.inf),), features_domain = (ind_domain,) * nb_features,
187 default_task = "minimisation")
188
189
190
191def schwefel(ind):
192 r"""Schwefel function defined as:
193 $$ f(x) = 418.9829n - \sum_{i=1}^{n} x_i \sin(\sqrt(\norm(x_i))) $
194 with a search domain of $-500 < x_i < 500, 1 \leq i \leq n$.
195 The global minimum is at $f(x_1, ..., x_n) = f(420.9687, ..., 420.9687) = 0.
196 """
197 return 418.9829 * float(len(ind)) - sum((x * math.sin(math.sqrt(abs(x))) for x in ind)),
198illumination_schwefel = algorithms.partial(_illuminate_artificial_landscape, func=schwefel) # type: ignore
200 """Minimise Schwefel on the full coordinate range from -500 to 500."""
201 def __init__(self, nb_features: int = 2):
202 self.nb_features = nb_features
203 ind_domain = (-500., 500.)
204 super().__init__(fn=algorithms.partial(illumination_schwefel, nb_features=nb_features),
205 ind_domain = ind_domain,
206 fitness_domain = ((0., math.inf),), features_domain = (ind_domain,) * nb_features,
207 default_task = "minimisation")
208
209
210
211def small_schwefel(ind):
212 r"""Schwefel function defined as:
213 $$ f(x) = 418.9829n - \sum_{i=1}^{n} x_i \sin(\sqrt(\norm(x_i))) $
214 with a search domain of $-100 < x_i < 100, 1 \leq i \leq n$.
215 """
216 return 418.9829 * float(len(ind)) - sum((x * math.sin(math.sqrt(abs(x))) for x in ind)),
217illumination_small_schwefel = algorithms.partial(_illuminate_artificial_landscape, func=small_schwefel) # type: ignore
219 """Minimise Schwefel on the narrowed coordinate range from -100 to 100."""
220 def __init__(self, nb_features: int = 2):
221 self.nb_features = nb_features
222 ind_domain = (-100., 100.)
223 super().__init__(fn=algorithms.partial(illumination_small_schwefel, nb_features=nb_features),
224 ind_domain = ind_domain,
225 fitness_domain = ((0., math.inf),), features_domain = (ind_domain,) * nb_features,
226 default_task = "minimisation")
227
228
229def griewangk(ind):
230 r"""Griewangk function defined as:
231 $$ f(x) = 1 + \sum_{i=1}^{n} \frac{x_i^2}{4000} - \prod_{i=1}^n \cos \frac{x_i}{\sqrt{i}} $$
232 with a search domain of $-600 < x_i < 600, 1 \leq i \leq n$.
233 The global minimum is at $f(x_1, ..., x_n) = f(0, ..., 0) = 0.
234 """
235 return 1. + sum(( (x ** 2.) / 4000. for x in ind)) - np.prod(list((math.cos(ind[i] / math.sqrt(i+1.)) for i in range(len(ind))))),
236illumination_griewangk = algorithms.partial(_illuminate_artificial_landscape, func=griewangk) # type: ignore
238 """Minimise the Griewangk sum-and-product objective."""
239 def __init__(self, nb_features: int = 2):
240 self.nb_features = nb_features
241 ind_domain = (-600., 600.)
242 super().__init__(fn=algorithms.partial(illumination_griewangk, nb_features=nb_features),
243 ind_domain = ind_domain,
244 fitness_domain = ((0., math.inf),), features_domain = (ind_domain,) * nb_features,
245 default_task = "minimisation")
246
247
248
249def sum_of_powers(ind):
250 r"""Sum Of Powers function defined as:
251 $$ f(x) = \sum_{i=1}^{n} \norm{x_i}^{(i+1)} $$
252 with a search domain of $-1 < x_i < 1, 1 \leq i \leq n$.
253 The global minimum is at $f(x_1, ..., x_n) = f(0, ..., 0) = 0.
254 """
255 return sum(abs(float(x))**power for power, x in enumerate(ind, start=2)),
256illumination_sum_of_powers = algorithms.partial(_illuminate_artificial_landscape, func=sum_of_powers) # type: ignore
258 """Minimise absolute coordinate powers with exponents increasing from two."""
259 def __init__(self, nb_features: int = 2):
260 self.nb_features = nb_features
261 ind_domain = (-1., 1.)
262 super().__init__(fn=algorithms.partial(illumination_sum_of_powers, nb_features=nb_features),
263 ind_domain = ind_domain,
264 fitness_domain = ((0., math.inf),), features_domain = (ind_domain,) * nb_features,
265 default_task = "minimisation")
266
267
268
269def ackley(ind, a=20., b=0.2, c=2.*math.pi):
270 r"""Ackley function defined as:
271 $$ f(x) = -a \exp^{-b \sqrt{1/n \sum_{i=1}^n x_i^2} - \exp^{1/n \sum_{i=1}^n \cos{c x_i}} + a + \exp^{1} $$
272 with $a=20, b=0.2, c=2\pi$ and with a search domain of $-32.768 < x_i < 32.768, 1 \leq i \leq n$.
273 The global minimum is at $f(x_1, ..., x_n) = f(0, ..., 0) = 0.
274 """
275 return - a * math.exp(-b * math.sqrt(1./len(ind) * sum((x**2. for x in ind)) )) - math.exp(1./len(ind) * sum((math.cos(c*ind[i]) for i in range(len(ind)) )) ) + a + math.exp(1.),
276illumination_ackley = algorithms.partial(_illuminate_artificial_landscape, func=ackley) # type: ignore
278 """Minimise the Ackley objective using its default coefficients."""
279 def __init__(self, nb_features: int = 2):
280 self.nb_features = nb_features
281 ind_domain = (-32.768, 32.768)
282 super().__init__(fn=algorithms.partial(illumination_ackley, nb_features=nb_features),
283 ind_domain = ind_domain,
284 fitness_domain = ((0., math.inf),), features_domain = (ind_domain,) * nb_features,
285 default_task = "minimisation")
286
287
288
289def styblinski_tang(ind):
290 r"""Styblinski-Tang function defined as:
291 $$ f(x) = 1/2 \sum_{i=1}^{n} x_i^4 - 16 x_i^2 + 5 x_i $$
292 with a search domain of $-5 < x_i < 5, 1 \leq i \leq n$.
293 """
294 return sum(((x ** 4.) - 16. * (x ** 2.) + 5. * x for x in ind)) / 2.,
295illumination_styblinski_tang = algorithms.partial(_illuminate_artificial_landscape, func=styblinski_tang) # type: ignore
297 """Minimise the separable Styblinski–Tang polynomial."""
298 def __init__(self, nb_features: int = 2):
299 self.nb_features = nb_features
300 ind_domain = (-5, 5)
301 super().__init__(fn=algorithms.partial(illumination_styblinski_tang, nb_features=nb_features),
302 ind_domain = ind_domain,
303 fitness_domain = ((-math.inf, math.inf),), features_domain = (ind_domain,) * nb_features,
304 default_task = "minimisation")
305
306
307
308def levy(ind):
309 r"""Levy function defined as:
310 $$ f(x) = \sin(\pi w_1)^2 + \sum_{i=1}^{n-1}(w_i-1)^2 (1+10 \sin(\pi w_i+1)^2) + (w_n-1)^2 (1+\sin(2\pi w_d)^2)$$
311 where $w_i = 1+ \frac{x_i-1}{4}$
312 with a search domain of $-10 < x_i < 10, 1 \leq i \leq n$.
313 The global minimum is at $f(x_1, ..., x_n) = f(1, ..., 1) = 0.
314 """
315 w = list((1. + (x-1.)/4. for x in ind))
316 return (math.sin(math.pi * w[0]))**2. + \
317 sum((x-1.)**2. * (1.+10.*math.sin(math.pi*x+1.)**2.) for x in w[:-1]) + \
318 (w[-1]-1.)**2. * (1.+(math.sin(2.*math.pi*w[-1]))**2.),
319illumination_levy = algorithms.partial(_illuminate_artificial_landscape, func=levy) # type: ignore
321 """Minimise the Lévy objective including its distinct final-coordinate term."""
322 def __init__(self, nb_features: int = 2):
323 self.nb_features = nb_features
324 ind_domain = (-10., 10.)
325 super().__init__(fn=algorithms.partial(illumination_levy, nb_features=nb_features),
326 ind_domain = ind_domain,
327 fitness_domain = ((0., math.inf),), features_domain = (ind_domain,) * nb_features,
328 default_task = "minimisation")
329
330
331def perm0db(ind, beta=10.):
332 r"""Perm function 0, D, BETA defined as:
333 $$ f(x) = \sum_{i=1}^n (\sum_{j=1}^n (j+\beta) (x_j^i - \frac{1}{j^i}) )^2$$
334 with a search domain of $-n < x_i < n, 1 \leq i \leq n$.
335 The global minimum is at $f(1, 1/2, ..., 1/n) = 0$.
336 """
337 return sum(( \
338 ( sum(( (j+1.+beta) * ((ind[j])**float(i+1) - 1./(float(j+1)**float(i+1))) for j in range(len(ind)) )) )**2. \
339 for i in range(len(ind)) )),
340illumination_perm0db = algorithms.partial(_illuminate_artificial_landscape, func=perm0db) # type: ignore
342 """Minimise the Perm 0, d, beta objective with reciprocal-index optima."""
343 def __init__(self, nb_features: int = 2):
344 self.nb_features = nb_features
345 ind_domain = (float(-nb_features), float(nb_features))
346 super().__init__(fn=algorithms.partial(illumination_perm0db, nb_features=nb_features),
347 ind_domain = ind_domain,
348 fitness_domain = ((0., math.inf),), features_domain = (ind_domain,) * nb_features,
349 default_task = "minimisation")
350
351
352def permdb(ind, beta=0.5):
353 r"""Perm function D, BETA defined as:
354 $$ f(x) = \sum_{i=1}^d (\sum_{j=1}^n (j^i+\beta) ((\frac{x_j}{j})^i - 1) )^2$$
355 with a search domain of $-n < x_i < n, 1 \leq i \leq n$.
356 The global minimum is at $f(1, 2, ..., n) = 0$.
357 """
358 return sum(( \
359 ( sum(( ((j+1.)**(i+1.)+beta) * ((ind[j]/(j+1.))**(i+1.) - 1.) for j in range(len(ind)) )) )**2. \
360 for i in range(len(ind)) )),
361illumination_permdb = algorithms.partial(_illuminate_artificial_landscape, func=permdb) # type: ignore
363 """Minimise the Perm d, beta objective with one-based index optima."""
364 def __init__(self, nb_features: int = 2):
365 self.nb_features = nb_features
366 ind_domain = (float(-nb_features), float(nb_features))
367 super().__init__(fn=algorithms.partial(illumination_permdb, nb_features=nb_features),
368 ind_domain = ind_domain,
369 fitness_domain = ((0., math.inf),), features_domain = (ind_domain,) * nb_features,
370 default_task = "minimisation")
371
372
373
374def trid(ind):
375 r"""Trid function defined as:
376 $$ f(x) = \sum_{i=1}^n (x_i - 1)^2 - \sum_{i=2}^n x_i x_{i-1}$$
377 with a search domain of $-n^2 < x_i < n^2, 1 \leq i \leq n$.
378 The global minimum is $f(x^{*}) = -n(n+4)(n-1)/6$ at $x_i = i(n+1-i)$ for all $i=1,2,...,n$.
379 """
380 return sum(( (x-1)**2. for x in ind)) - sum(( ind[i] * ind[i-1] for i in range(1, len(ind)) )),
381illumination_trid = algorithms.partial(_illuminate_artificial_landscape, func=trid) # type: ignore
383 """Minimise the Trid objective with a fitness domain that admits negative values."""
384 def __init__(self, nb_features: int = 2):
385 self.nb_features = nb_features
386 ind_domain = (-float(nb_features)**2., float(nb_features)**2.)
387 super().__init__(fn=algorithms.partial(illumination_trid, nb_features=nb_features),
388 ind_domain = ind_domain,
389 fitness_domain = ((-math.inf, math.inf),), features_domain = (ind_domain,) * nb_features,
390 default_task = "minimisation")
391
392
393def zakharov(ind):
394 r"""Zakharov function defined as:
395 $$ f(x) = \sum_{i=1}^n x_i^2 + (\sum_{i=1}^n 0.5 i x_i)^2 + (\sum_{i=1}^n 0.5 i x_i)^4$$
396 with a search domain of $-5 < x_i < 10, 1 \leq i \leq n$.
397 The global minimum is at $f(x_1, ..., x_n) = f(0, ..., 0) = 0.
398 """
399 weighted_sum = sum(0.5 * i * x for i, x in enumerate(ind, start=1))
400 return sum(x**2.0 for x in ind) + weighted_sum**2 + weighted_sum**4,
401illumination_zakharov = algorithms.partial(_illuminate_artificial_landscape, func=zakharov) # type: ignore
403 """Minimise the Zakharov objective with one-based weights in its mixed terms."""
404 def __init__(self, nb_features: int = 2):
405 self.nb_features = nb_features
406 ind_domain = (-5., 10.)
407 super().__init__(fn=algorithms.partial(illumination_zakharov, nb_features=nb_features),
408 ind_domain = ind_domain,
409 fitness_domain = ((0., math.inf),), features_domain = (ind_domain,) * nb_features,
410 default_task = "minimisation")
411
412
413def dixon_price(ind):
414 r"""Dixon Price function defined as:
415 $$ f(x) = (x_1 - 1)^2 + \sum_{i=2}^n i (2 x_i^2 - x_{i-1})^2$$
416 with a search domain of $-10 < x_i < 10, 1 \leq i \leq n$.
417 The global minimum is $f(x^{*}) = 0$ at $x_i = 2^{-(2^i-2)/(2^i)}$ for all $i=1,2,...,n$.
418 """
419 return (ind[0] - 1.0)**2 + sum(
420 i * (2 * x**2.0 - previous)**2
421 for i, (previous, x) in enumerate(zip(ind, ind[1:]), start=2)
422 ),
423illumination_dixon_price = algorithms.partial(_illuminate_artificial_landscape, func=dixon_price) # type: ignore
425 """Minimise the Dixon–Price objective over adjacent coordinate pairs."""
426 def __init__(self, nb_features: int = 2):
427 self.nb_features = nb_features
428 ind_domain = (-10., 10.)
429 super().__init__(fn=algorithms.partial(illumination_dixon_price, nb_features=nb_features),
430 ind_domain = ind_domain,
431 fitness_domain = ((0., math.inf),), features_domain = (ind_domain,) * nb_features,
432 default_task = "minimisation")
433
434
435def powell(ind):
436 r"""Powell function defined as:
437 $$ f(x) = \sum_{i=1}^{n/4} ((x_{4i-3}+10x_{4i-2})^2 + 5(x_{4i-1}-x_{4i})^2 + (x_{4i-2}-2x_{4i-1})^4 + 10(x_{4i-3}-x_{4i})^4)$$
438 with a search domain of $-4 < x_i < 5, 1 \leq i \leq n$.
439 The dimension must be a positive multiple of four.
440 The global minimum is at $f(x_1, ..., x_n) = f(0, ..., 0) = 0.
441 """
442 if len(ind) == 0 or len(ind) % 4:
443 raise ValueError("Powell requires a positive multiple of four coordinates.")
444 return sum(
445 (a + 10.0*b)**2 + 5*(c - float(d))**2 + (b - 2.0*c)**4 + 10*(a - float(d))**4
446 for a, b, c, d in zip(ind[::4], ind[1::4], ind[2::4], ind[3::4])
447 ),
448illumination_powell = algorithms.partial(_illuminate_artificial_landscape, func=powell) # type: ignore
450 """Minimise Powell over complete groups of four genome coordinates."""
451 def __init__(self, nb_features: int = 2):
452 self.nb_features = nb_features
453 ind_domain = (-4., 5.)
454 super().__init__(fn=algorithms.partial(illumination_powell, nb_features=nb_features),
455 ind_domain = ind_domain,
456 fitness_domain = ((0., math.inf),), features_domain = (ind_domain,) * nb_features,
457 default_task = "minimisation")
458
459
460def michalewicz(ind, m=10.):
461 r"""Michalewicz function defined as:
462 $$ f(x) = - \sum_{i=1}^{n} \sin(x_i) \sin( \frac{i x_i^2}{\pi} )^(2*m)$$
463 with a search domain of $0 < x_i < \pi, 1 \leq i \leq n$.
464 """
465 return - sum(( math.sin(ind[i]) * (math.sin(((i+1.)*(ind[i] **2.))/math.pi))**(2.*m) for i in range(len(ind)) )),
466illumination_michalewicz = algorithms.partial(_illuminate_artificial_landscape, func=michalewicz) # type: ignore
468 """Minimise the Michalewicz objective with its default steepness parameter."""
469 def __init__(self, nb_features: int = 2):
470 self.nb_features = nb_features
471 ind_domain = (0., math.pi)
472 super().__init__(fn=algorithms.partial(illumination_michalewicz, nb_features=nb_features),
473 ind_domain = ind_domain,
474 fitness_domain = ((-math.inf, 0.),), features_domain = (ind_domain,) * nb_features,
475 default_task = "minimisation")
476
477
478def wavy(ind, k=10.):
479 r"""Wavy function defined as:
480 $$ f(x) = 1 - \frac{1}{n} \sum_{i=1}^{n} \cos(kx_i)e^{-\frac{x_i^2}{2}}$$
481 with a search domain of $-\pi < x_i < \pi, 1 \leq i \leq n$.
482 """
483 return 1. - sum(( math.cos(k * ind[i]) * math.exp(-(ind[i]*ind[i])/2.) for i in range(len(ind)))) / float(len(ind)),
484illumination_wavy = algorithms.partial(_illuminate_artificial_landscape, func=wavy) # type: ignore
486 """Minimise the Wavy objective, combining cosine and a Gaussian envelope."""
487 def __init__(self, nb_features: int = 2):
488 self.nb_features = nb_features
489 ind_domain = (-math.pi, math.pi)
490 super().__init__(fn=algorithms.partial(illumination_wavy, nb_features=nb_features),
491 ind_domain = ind_domain,
492 fitness_domain = ((0., math.inf),), features_domain = (ind_domain,) * nb_features,
493 default_task = "minimisation")
494
495
496
497def trigonometric02(ind, k=10.):
498 r"""Trigonometric02 function defined as:
499 $$ f(x) = 1 + \sum_{i=1}^n [8\sin^2(7t_i) + 6\sin^2(14t_i) + t_i], \quad t_i = (x_i-0.9)^2.$$
500 The search domain is $[-500, 500]^n$ and the minimum is 1 at $(0.9, ..., 0.9)$.
501 """
502 return 1. + sum((8.*math.sin(7. * (ind[i] - 0.9)**2.)**2. + 6.*math.sin(14.*(ind[i]-0.9)**2.)**2. + (ind[i] - 0.9)**2. for i in range(len(ind)))),
503illumination_trigonometric02 = algorithms.partial(_illuminate_artificial_landscape, func=trigonometric02) # type: ignore
505 """Minimise the second trigonometric test function with its optimum at 0.9 per coordinate."""
506 def __init__(self, nb_features: int = 2):
507 self.nb_features = nb_features
508 ind_domain = (-500., 500.)
509 super().__init__(fn=algorithms.partial(illumination_trigonometric02, nb_features=nb_features),
510 ind_domain = ind_domain,
511 fitness_domain = ((1., np.inf),), features_domain = (ind_domain,) * nb_features,
512 default_task = "minimisation")
513
514
515
516def qing(ind, k=10.):
517 r"""Qing function defined as:
518 $$ f(x) = \sum_{i=1}^{n} (x_i^2 - i)^2$$
519 with a search domain of $-500 < x_i < 500, 1 \leq i \leq n$.
520 """
521 return sum((x**2.0 - i)**2 for i, x in enumerate(ind, start=1)),
522illumination_qing = algorithms.partial(_illuminate_artificial_landscape, func=qing) # type: ignore
524 """Minimise Qing with coordinate magnitudes given by square roots of one-based indexes."""
525 def __init__(self, nb_features: int = 2):
526 self.nb_features = nb_features
527 ind_domain = (-500., 500.)
528 super().__init__(fn=algorithms.partial(illumination_qing, nb_features=nb_features),
529 ind_domain = ind_domain,
530 fitness_domain = ((0., np.inf),), features_domain = (ind_domain,) * nb_features,
531 default_task = "minimisation")
532
533
534def small_qing(ind, k=10.):
535 r"""Qing function defined as:
536 $$ f(x) = \sum_{i=1}^{n} (x_i^2 - i)^2$$
537 with a search domain of $-4 < x_i < 4, 1 \leq i \leq n$.
538 """
539 return qing(ind, k)
540illumination_small_qing = algorithms.partial(_illuminate_artificial_landscape, func=small_qing) # type: ignore
542 """Minimise Qing on the narrowed coordinate range from -4 to 4."""
543 def __init__(self, nb_features: int = 2):
544 self.nb_features = nb_features
545 ind_domain = (-4., 4.)
546 super().__init__(fn=algorithms.partial(illumination_small_qing, nb_features=nb_features),
547 ind_domain = ind_domain,
548 fitness_domain = ((0., np.inf),), features_domain = (ind_domain,) * nb_features,
549 default_task = "minimisation")
550
551
552
553
554def deb01(ind):
555 r"""Deb01 function defined as:
556 $$ f(x) = - \frac{1}{n} \sum_{i=1}^{n} \sin(5 \pi x_i)^6 $$
557 with a search domain of $-1 < x_i < 1, 1 \leq i \leq n$.
558 """
559 return -sum(( math.sin(5. * math.pi * ind[i])**6. for i in range(len(ind)))) / float(len(ind)),
560illumination_deb01 = algorithms.partial(_illuminate_artificial_landscape, func=deb01) # type: ignore
562 """Minimise the first Deb objective, a negative mean of powered sine terms."""
563 def __init__(self, nb_features: int = 2):
564 self.nb_features = nb_features
565 ind_domain = (-1., 1.)
566 super().__init__(fn=algorithms.partial(illumination_deb01, nb_features=nb_features),
567 ind_domain = ind_domain,
568 fitness_domain = ((-1., 1.),), features_domain = (ind_domain,) * nb_features,
569 default_task = "minimisation")
570
571
572
573def shubert04(ind):
574 r"""Shubert04 function defined as:
575 $$ f(x) = -\sum_{i=1}^n \sum_{j=1}^5 j \cos((j+1)x_i + j) $$
576 with a search domain of $-10 < x_i < 10, 1 \leq i \leq n$.
577 """
578 return sum(( sum((-j * math.cos((j+1) * ind[i] + j) for j in range(1, 6))) for i in range(len(ind)))),
579illumination_shubert04 = algorithms.partial(_illuminate_artificial_landscape, func=shubert04) # type: ignore
581 """Minimise the fourth Shubert variant with a dimension-independent admissible fitness domain."""
582 def __init__(self, nb_features: int = 2):
583 self.nb_features = nb_features
584 ind_domain = (-10., 10.)
585 super().__init__(fn=algorithms.partial(illumination_shubert04, nb_features=nb_features),
586 ind_domain = ind_domain,
587 fitness_domain = ((-math.inf, math.inf),), features_domain = (ind_domain,) * nb_features,
588 default_task = "minimisation")
589
590
591
592
593
594# MODELINE "{{{1
595# vim:expandtab:softtabstop=4:shiftwidth=4:fileencoding=utf-8
596# vim:foldmethod=marker