From b785da87e9271bf851c52e91eb39030a441bda24 Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 6 Feb 2023 22:05:01 +0100 Subject: [PATCH] cec2020: implement Weierstrass func --- bench/cec2020/basicFunctions.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/bench/cec2020/basicFunctions.go b/bench/cec2020/basicFunctions.go index 08f1a71..5f01d3d 100644 --- a/bench/cec2020/basicFunctions.go +++ b/bench/cec2020/basicFunctions.go @@ -167,4 +167,30 @@ func Schaffer(x []float64) float64 { return 0 } // Weierstrass is the "Weierstrass Function" of CEC2020 with a=0.5, b=3 and // kmax=20. -func Weierstrass(x []float64) float64 { return 0 } +func Weierstrass(x []float64) float64 { + a := 0.5 + b := 3.0 + kmax := 20.0 + // float64 version of the length of x. + fnx := float64(len(x)) + + var sum1 float64 + + var sum2 float64 + + for i := range x { + var ksum float64 + + for k := 0.0; k < kmax; k++ { + ksum += math.Pow(a, k) * math.Cos(2*math.Pi*math.Pow(b, k)*(x[i]+0.5)) + } + + sum1 += ksum + } + + for k := 0.0; k < kmax; k++ { + sum2 += math.Pow(a, k) * math.Cos(2*math.Pi*math.Pow(b, k)*0.5) + } + + return sum1 - (fnx * sum2) +}