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