-
Notifications
You must be signed in to change notification settings - Fork 1
/
ackley.py
38 lines (29 loc) · 1.12 KB
/
ackley.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import numpy as np
from numpy import exp, sqrt, cos, pi, sin
from base_function import BaseFunction
class Ackley(BaseFunction):
#note: this function is not smooth at the origin. the gradient will never
#converge in the minimizer
target_E = 0.
xmin = np.array([-5.,-5.])
xmax = np.array([5.,5.])
def getEnergy(self, coords):
x, y = coords
E = -20. * exp(-0.2 * sqrt(x**2 + y**2)) - \
exp(0.5 * (cos(2. *pi * x) + cos(2. * pi * y))) + 20. + np.e
return E
def getEnergyGradient(self, coords):
E = self.getEnergy(coords)
x, y = coords
R = sqrt(x**2 + y**2)
term1 = -20. * exp(-0.2 * R)
term2 = -exp(0.5 * (cos(2. *pi * x) + cos(2. * pi * y)))
deriv1 = term1 * (-0.2 * 0.5 / R)
dEdx = 2.* deriv1 * x - term2 * pi * sin(2.*pi*x)
dEdy = 2.* deriv1 * y - term2 * pi * sin(2.*pi*y)
return E, np.array([dEdx, dEdy])
if __name__ == "__main__":
f = Ackley()
f.test_potential(f.get_random_configuration())
from base_function import makeplot2d
makeplot2d(f)