Parametrisiert
This commit is contained in:
@@ -9,15 +9,24 @@ def felli(x):
|
||||
return float(np.sum((1e6 ** exponents) * (x ** 2)))
|
||||
|
||||
|
||||
def escma():
|
||||
#Initialization
|
||||
def escma(func, *, N=10, xmean=None, sigma=0.5, stopfitness=1e-10, stopeval=None,
|
||||
func_args=(), func_kwargs=None, seed=None):
|
||||
|
||||
# User defined input parameters
|
||||
N = 10
|
||||
xmean = np.random.rand(N)
|
||||
sigma = 0.5
|
||||
stopfitness = 1e-10
|
||||
stopeval = int(1e3 * N**2)
|
||||
if func_kwargs is None:
|
||||
func_kwargs = {}
|
||||
|
||||
if seed is not None:
|
||||
np.random.seed(seed)
|
||||
|
||||
# Initialization (aus Parametern statt hart verdrahtet)
|
||||
if xmean is None:
|
||||
xmean = np.random.rand(N)
|
||||
else:
|
||||
xmean = np.asarray(xmean, dtype=float)
|
||||
N = xmean.shape[0]
|
||||
|
||||
if stopeval is None:
|
||||
stopeval = int(1e3 * N**2)
|
||||
|
||||
# Strategy parameter setting: Selection
|
||||
lambda_ = 4 + int(np.floor(3 * np.log(N)))
|
||||
@@ -46,7 +55,7 @@ def escma():
|
||||
eigeneval = 0
|
||||
chiN = np.sqrt(N) * (1 - 1/(4*N) + 1/(21 * N**2))
|
||||
|
||||
#Generation Loop
|
||||
# Generation Loop
|
||||
counteval = 0
|
||||
arx = np.zeros((N, lambda_))
|
||||
arz = np.zeros((N, lambda_))
|
||||
@@ -58,7 +67,7 @@ def escma():
|
||||
for k in range(lambda_):
|
||||
arz[:, k] = np.random.randn(N)
|
||||
arx[:, k] = xmean + sigma * (B @ D @ arz[:, k])
|
||||
arfitness[k] = felli(arx[:, k])
|
||||
arfitness[k] = float(func(arx[:, k], *func_args, **func_kwargs)) # <-- allgemein
|
||||
counteval += 1
|
||||
|
||||
# Sort by fitness and compute weighted mean into xmean
|
||||
@@ -107,13 +116,13 @@ def escma():
|
||||
|
||||
print(f"{counteval}: {arfitness[0]}")
|
||||
|
||||
#Final Message
|
||||
# Final Message
|
||||
print(f"{counteval}: {arfitness[0]}")
|
||||
xmin = arx[:, arindex[0]]
|
||||
return xmin
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
xmin = escma()
|
||||
xmin = escma(felli, N=10) # <-- Zielfunktion wird übergeben
|
||||
print("Bestes gefundenes x:", xmin)
|
||||
print("f(xmin) =", felli(xmin))
|
||||
|
||||
Reference in New Issue
Block a user