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
|
||||||
@@ -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
|
||||||
@@ -114,6 +123,6 @@ def escma():
|
|||||||
|
|
||||||
|
|
||||||
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