Skip to content

Commit

Permalink
schwefel class
Browse files Browse the repository at this point in the history
  • Loading branch information
Queimo committed Mar 27, 2024
1 parent 4c704e9 commit e5dc251
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/schwefel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import numpy as np

class SchwefelProblem:
def __init__(self, n_var=1, noise_level=0.01):
"""
y = f(x) + eps
"""
self.noise_level = noise_level
self.n_var = n_var # Number of variables/dimensions
self.bounds = np.array([[-500] * self.n_var, [500] * self.n_var])

def _schwefel_individual(self, x):
return x * np.sin(np.sqrt(np.abs(x)))

def f(self, x):
return 418.9829 * self.n_var - np.sum(self._schwefel_individual(x), axis=1)

def eps(self, x):
# Assuming the noise is independent of x for simplicity
return np.random.normal(0, self.noise_level, x.shape[0])

def y(self, x):
return self.f(x) + self.eps(x)

# Test code if this file is the main program being run
if __name__ == "__main__":
# Create a SchwefelProblem instance with 3 variables/dimensions
schwefel = SchwefelProblem(n_var=3, noise_level=1.)
x_test = np.array([[420, 420, 420], [420, 420, 420]]) # Example input vector
print("Objective function value (f):", schwefel.f(x_test))
print("Noisy objective function value (y):", schwefel.y(x_test))

0 comments on commit e5dc251

Please sign in to comment.