Last active
February 3, 2022 16:51
-
-
Save nschloe/9e5f3d5d0b63e960cd8e40916acaf7a7 to your computer and use it in GitHub Desktop.
Plot optimization test functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotx | |
import matplotlib.pyplot as plt | |
import numpy as np | |
class Rastrigin: | |
def __init__(self): | |
self.roots = np.array([[0, 0]]) | |
def f(self, x): | |
A = 10 | |
n = len(x) | |
return A * n + np.sum(x**2 - A * np.cos(2 * np.pi * x), axis=0) | |
class Ackley: | |
def __init__(self): | |
self.roots = np.array([[0, 0]]) | |
def f(self, x): | |
xx = np.einsum("i...,i...->...", x, x) | |
sumcos = np.sum(np.cos(2 * np.pi * x), axis=0) | |
return ( | |
-20.0 * np.exp(-0.2 * np.sqrt(0.5 * xx)) | |
- np.exp(0.5 * sumcos) | |
+ np.exp(1) | |
+ 20.0 | |
) | |
class Sphere: | |
def __init__(self): | |
self.roots = np.array([[0, 0]]) | |
def f(self, x): | |
return np.einsum("i...,i...->...", x, x) | |
class Rosenbrock: | |
def __init__(self): | |
self.roots = np.array([[1.0, 1.0]]) | |
def f(self, x): | |
a = 1.0 | |
b = 100.0 | |
return (a - x[0]) ** 2 + b * (x[1] - x[0] ** 2) ** 2 | |
class Beale: | |
def __init__(self): | |
self.roots = np.array([[3, 0.5]]) | |
def f(self, x): | |
return ( | |
(1.5 - x[0] + x[0] * x[1]) ** 2 | |
+ (2.25 - x[0] + x[0] * x[1] ** 2) ** 2 | |
+ (2.625 - x[0] + x[0] * x[1] ** 3) ** 2 | |
) | |
class GoldsteinPrice: | |
def __init__(self): | |
self.roots = np.array([[0, -1]]) | |
def f(self, x): | |
x, y = x | |
return 1 + (x + y + +1) ** 2 * ( | |
19 - 14 * x + 3 * x**2 - 14 * y + 6 * x * y + 3 * y**2 | |
) * ( | |
30 | |
+ (2 * x - 3 * y) ** 2 | |
* (18 - 32 * x + 12 * x**2 + 48 * y - 36 * x * y + 27 * y**2) | |
) | |
class Booth: | |
def __init__(self): | |
self.roots = np.array([[1, 3]]) | |
def f(self, x): | |
x, y = x | |
return (x + 2 * y - 7) ** 2 + (2 * x + y - 5) ** 2 | |
class Bukin6: | |
def __init__(self): | |
self.roots = np.array([[-10, 1]]) | |
def f(self, x): | |
x, y = x | |
return 100 * np.sqrt(np.abs(y - 0.01 * x**2)) + 0.01 * np.abs(x + 10) | |
class Matyas: | |
def __init__(self): | |
self.roots = np.array([[0, 0]]) | |
def f(self, x): | |
x, y = x | |
return 0.26 * (x**2 + y**2) - 0.48 * x * y | |
class Levi13: | |
def __init__(self): | |
self.roots = np.array([[1, 1]]) | |
def f(self, x): | |
x, y = x | |
return ( | |
np.sin(3 * np.pi * x) ** 2 | |
+ (x - 1) ** 2 * (1 + np.sin(3 * np.pi * y) ** 2) | |
+ (y - 1) ** 2 * (1 + np.sin(2 * np.pi * y) ** 2) | |
) | |
class Himmelblau: | |
def __init__(self): | |
self.roots = np.array( | |
[ | |
[3.0, 2.0], | |
[-2.805118, 3.131312], | |
[-3.779310, -3.283186], | |
[3.584428, -1.84826], | |
] | |
) | |
def f(self, x): | |
x, y = x | |
return (x**2 + y - 11) ** 2 + (x + y**2 - 7) ** 2 | |
class ThreeHumpCamel: | |
def __init__(self): | |
self.roots = np.array([[0, 0]]) | |
def f(self, x): | |
x, y = x | |
return 2 * x**2 - 1.05 * x**4 + x**6 / 6 + x * y + y**2 | |
class Easom: | |
def __init__(self): | |
self.roots = np.array([[np.pi, np.pi]]) | |
def f(self, x): | |
x, y = x | |
return -np.cos(x) * np.cos(y) * np.exp(-((x - np.pi) ** 2 + (y - np.pi) ** 2)) | |
class CrossInTray: | |
def __init__(self): | |
a = 1.34941 | |
self.roots = np.array([[a, -a], [a, a], [-a, a], [-a, -a]]) | |
def f(self, x): | |
x, y = x | |
return ( | |
-0.0001 | |
* ( | |
np.abs( | |
np.sin(x) | |
* np.sin(y) | |
* np.exp(np.abs(100 - np.sqrt(x**2 + y**2) / np.pi)) | |
) | |
+ 1 | |
) | |
** 0.1 | |
) | |
class Eggholder: | |
def __init__(self): | |
self.roots = np.array([[512, 404.2319]]) | |
def f(self, x): | |
x, y = x | |
return -(y + 47) * np.sin(np.sqrt(np.abs(x / 2 + (y + 47)))) - x * np.sin( | |
np.sqrt(np.abs(x - (y + 47))) | |
) | |
class HoelderTable: | |
def __init__(self): | |
a = 8.05502 | |
b = 9.66459 | |
self.roots = np.array([[a, b], [-a, b], [a, -b], [-a, -b]]) | |
def f(self, x): | |
x, y = x | |
return -np.abs( | |
np.sin(x) * np.cos(y) * np.exp(np.abs(1 - np.sqrt(x**2 + y**2) / np.pi)) | |
) | |
class McCormick: | |
def __init__(self): | |
self.roots = np.array([[-0.54719, -1.54719]]) | |
def f(self, x): | |
x, y = x | |
return np.sin(x + y) + (x - y) ** 2 - 1.5 * x + 2.5 * y + 1 | |
class Schaffer2: | |
def __init__(self): | |
self.roots = np.array([[0.0, 0.0]]) | |
def f(self, x): | |
x, y = x | |
return ( | |
0.5 | |
+ (np.sin(x**2 - y**2) ** 2 - 0.5) | |
/ (1 + 0.001 * (x**2 + y**2)) ** 2 | |
) | |
class Schaffer4: | |
def __init__(self): | |
a = 1.25313 | |
self.roots = np.array([[0.0, a], [0.0, -a]]) | |
def f(self, x): | |
x, y = x | |
return ( | |
0.5 | |
+ (np.cos(np.sin(np.abs(x**2 - y**2))) ** 2 - 0.5) | |
/ (1 + 0.001 * (x**2 + y**2)) ** 2 | |
) | |
class StyblinskiTang: | |
def __init__(self): | |
a = 2.903534 | |
self.roots = np.array([[-a, -a]]) | |
def f(self, x): | |
return np.sum(x**4 - 16 * x**2 + 5 * x, axis=0) / 2 | |
class Rosenbrock2: | |
def __init__(self): | |
self.roots = np.array([[1.0, 1.0]]) | |
def f(self, x): | |
a = 1.0 | |
b = 100.0 | |
out = (a - x[0]) ** 2 + b * (x[1] - x[0] ** 2) ** 2 | |
outside = x[0] ** 2 + x[1] ** 2 > 2 | |
out[outside] = np.nan | |
return out | |
class Rosenbrock3: | |
def __init__(self): | |
self.roots = np.array([[1.0, 1.0]]) | |
def f(self, x): | |
a = 1.0 | |
b = 100.0 | |
out = (a - x[0]) ** 2 + b * (x[1] - x[0] ** 2) ** 2 | |
outside = ((x[0] - 1) ** 3 - x[1] + 1 > 0) | (x[0] + x[1] - 2 > 0) | |
out[outside] = np.nan | |
return out | |
class MishraBird: | |
def __init__(self): | |
self.roots = np.array([[-3.1302468, -1.5821422]]) | |
def f(self, x): | |
x, y = x | |
out = ( | |
np.sin(y) * np.exp((1 - np.cos(x)) ** 2) | |
+ np.cos(x) * np.exp((1 - np.sin(y)) ** 2) | |
+ (x - y) ** 2 | |
) | |
outside = (x + 5) ** 2 + (y + 5) ** 2 > 25 | |
out[outside] = np.nan | |
return out | |
class Townsend: | |
def __init__(self): | |
self.roots = np.array([[2.0052938, 1.1944509]]) | |
def f(self, x): | |
x, y = x | |
out = -((np.cos((x - 0.1) * y)) ** 2) - x * np.sin(3 * x + y) | |
t = np.arctan2(x, y) | |
outside = ( | |
x**2 + y**2 | |
> ( | |
2 * np.cos(t) | |
- 0.5 * np.cos(2 * t) | |
- 0.25 * np.cos(3 * t) | |
- 0.125 * np.cos(4 * t) | |
) | |
** 2 | |
+ (2 * np.sin(t)) ** 2 | |
) | |
out[outside] = np.nan | |
return out | |
class GomezLevi: | |
def __init__(self): | |
self.roots = np.array([[0.08904201, -0.7126564]]) | |
def f(self, x): | |
x, y = x | |
out = 4 * x**2 - 2.1 * x**4 + x**6 / 3 + x * y - 4 * y**2 + 4 * y**4 | |
outside = -np.sin(4 * np.pi * x) + 2 * np.sin(2 * np.pi * y) ** 2 > 1.5 | |
out[outside] = np.nan | |
return out | |
class Simionescu: | |
def __init__(self): | |
a = 0.84852813 | |
self.roots = np.array([[-a, a], [a, -a]]) | |
def f(self, x): | |
x, y = x | |
out = 0.1 * x * y | |
rt = 1.0 | |
rs = 0.2 | |
n = 8 | |
outside = x**2 + y**2 > (rt + rs * np.cos(n * np.arctan2(x, y))) ** 2 | |
out[outside] = np.nan | |
return out | |
flist = [ | |
(StyblinskiTang(), (-5.0, 5.0, 101), (-5.0, 5.0, 101), False, "white"), | |
(Matyas(), (-10.0, 10.0, 201), (-10.0, 10.0, 201), True, "white"), | |
(CrossInTray(), (-10.0, 10.0, 201), (-10.0, 10.0, 201), False, None), | |
(Bukin6(), (-15.0, -5.0, 301), (-4.0, 6.0, 301), False, "white"), | |
(Rastrigin(), (-5.12, 5.12, 101), (-5.12, 5.12, 101), False, "white"), | |
(Ackley(), (-5.0, 5.0, 100), (-5.0, 5.0, 100), False, "white"), | |
(Sphere(), (-2.0, 2.0, 100), (-2.0, 2.0, 100), False, "white"), | |
(Rosenbrock(), (-2.0, 2.0, 201), (-1.0, 3.0, 201), True, "white"), | |
(Beale(), (-4.5, 4.5, 201), (-4.5, 4.5, 201), True, "white"), | |
(GoldsteinPrice(), (-2.0, 2.0, 201), (-3.0, 1.0, 201), True, "white"), | |
(Booth(), (-10.0, 10.0, 101), (-10.0, 10.0, 101), True, "white"), | |
(Levi13(), (-5.0, 7.0, 301), (-5.0, 7.0, 301), False, None), | |
(Himmelblau(), (-5.0, 5.0, 201), (-5.0, 5.0, 201), True, "white"), | |
(ThreeHumpCamel(), (-5.0, 5.0, 101), (-5.0, 5.0, 101), True, "white"), | |
(Easom(), (-1.0, 7.0, 101), (-1.0, 7.0, 101), False, "white"), | |
(Eggholder(), (-1000.0, 1000.0, 201), (-1000.0, 1000.0, 201), False, None), | |
(HoelderTable(), (-10.0, 10.0, 101), (-10.0, 10.0, 101), False, "white"), | |
(McCormick(), (-3.0, 4.0, 101), (-3.0, 4.0, 101), False, "white"), | |
(Schaffer2(), (-50.0, 50.0, 401), (-50.0, 50.0, 401), False, None), | |
(Schaffer4(), (-50.0, 50.0, 401), (-50.0, 50.0, 401), False, None), | |
# | |
(Rosenbrock2(), (-1.5, 1.5, 401), (-1.5, 1.5, 401), True, "white"), | |
(Rosenbrock3(), (-2.0, 1.1, 401), (-0.55, 2.55, 401), True, "white"), | |
(MishraBird(), (-10.5, 0.5, 401), (-10.5, 0.5, 401), False, "white"), | |
(Townsend(), (-2.25, 2.25, 401), (-2.625, 1.875, 401), False, "white"), | |
(GomezLevi(), (-1, 1, 401), (-1, 1, 401), False, "white"), | |
(Simionescu(), (-1.3, 1.3, 401), (-1.3, 1.3, 401), False, "white"), | |
] | |
for obj, xrange, yrange, log_scaling, outline in flist: | |
name = type(obj).__name__.lower() | |
print(name) | |
im = matplotx.contours( | |
obj.f, | |
xrange, | |
yrange, | |
outline=outline, | |
log_scaling=log_scaling, | |
) | |
if obj.roots is not None: | |
plt.plot(obj.roots.T[0], obj.roots.T[1], ".", color="C3") | |
plt.gca().set_aspect("equal") | |
plt.xlabel("x") | |
plt.ylabel("y", rotation=0) | |
plt.colorbar(im) | |
plt.savefig(f"{name}.svg", bbox_inches="tight") | |
plt.show() | |
plt.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment