Spaces:
Sleeping
Sleeping
File size: 1,517 Bytes
bc75bfa |
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# Ackley N 2 function Benchmark
import numpy as np
from scipy.optimize import minimize
from .Base import BaseBenchmark
class AckleyN2(BaseBenchmark):
"""Ackley N 2 function benchmark."""
def __init__(self):
super().__init__()
self.name = "Ackley N 2"
self.dimensions = 10
self.global_minimum = [0] * self.dimensions
self.global_minimum_value = 0.0
@staticmethod
def evaluate(x):
"""Evaluate the Ackley N 2 function."""
a = 20
b = 0.2
c = 2 * np.pi
n = len(x)
sum1 = sum(xi**2 for xi in x)
sum2 = sum(np.cos(c * xi) for xi in x)
term1 = -a * np.exp(-b * np.sqrt(sum1 / n))
term2 = -np.exp(sum2 / n)
return term1 + term2 + a + np.exp(1)
def ackley_n2(x):
"""Ackley N 2 function."""
a = 20
b = 0.2
c = 2 * np.pi
n = len(x)
sum1 = sum(xi**2 for xi in x)
sum2 = sum(np.cos(c * xi) for xi in x)
term1 = -a * np.exp(-b * np.sqrt(sum1 / n))
term2 = -np.exp(sum2 / n)
return term1 + term2 + a + np.exp(1)
def benchmark_ackley_n2():
"""Benchmark the Ackley N 2 function."""
x0 = np.random.uniform(-5, 5, size=10)
result = minimize(ackley_n2, x0, method='BFGS')
print(f"Optimized parameters: {result.x}")
print(f"Function value at optimum: {result.fun}")
print("Optimization successful:", result.success)
if __name__ == "__main__":
benchmark_ackley_n2() |