From e34922d6e556499ec7d9757e27f894a9e8f92a5e Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 8 Feb 2023 01:11:20 +0100 Subject: [PATCH] cec2020: generalise Hybrid1 func's internals --- bench/cec2020/benchFunctions.go | 36 ++++++++++++-------------------- bench/cec2020/helperFunctions.go | 27 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/bench/cec2020/benchFunctions.go b/bench/cec2020/benchFunctions.go index 01b951e..678eabc 100644 --- a/bench/cec2020/benchFunctions.go +++ b/bench/cec2020/benchFunctions.go @@ -107,35 +107,25 @@ func Hybrid1(x []float64) float64 { nx := len(x) fnx := float64(nx) - f1 := SchwefelModified - f2 := Rastrigin - f3 := HighConditionedElliptic - - // percentages. - p := []float64{0.3, 0.3, 0.4} - gnx := make([]float64, len(p)) - gnxsum := 0.0 - - for i := 0; i < len(p)-1; i++ { - gnx[i] = math.Ceil(p[i] * fnx) - gnxsum += gnx[i] + funcs := []func([]float64) float64{ + SchwefelModified, + Rastrigin, + HighConditionedElliptic, } - - gnx[len(p)] = fnx - gnxsum + // percentages used to control the amount of contribution of each func. + p := []float64{0.3, 0.3, 0.4} + gnx := getGnx(p, fnx) z := newOpt(nx) - mz := make([]float64, nx) + mz := getMz(x, z) - for i := range x { - mz = append(mz, x[i]*z[i]) + var sum float64 + + for i := range funcs { + sum += funcs[i](mz) * gnx[i] } - // results. - r1 := f1(mz) - r2 := f2(mz) - r3 := f3(mz) - - return gnx[0]*r1 + gnx[1]*r2 + gnx[2]*r3 + return sum } // Hybrid2 is the "Hybrid Function 2" of CEC2020. diff --git a/bench/cec2020/helperFunctions.go b/bench/cec2020/helperFunctions.go index 883ec8c..d8df5a0 100644 --- a/bench/cec2020/helperFunctions.go +++ b/bench/cec2020/helperFunctions.go @@ -164,3 +164,30 @@ func newOpt(n int) []float64 { return xopt } + +// getGnx calculates G_nx. +func getGnx(p []float64, fnx float64) []float64 { + gnx := make([]float64, len(p)) + gnxsum := 0.0 + + for i := 0; i < len(p)-1; i++ { + gnx[i] = math.Ceil(p[i] * fnx) + gnxsum += gnx[i] + } + + gnx[len(p)] = fnx - gnxsum + + return gnx +} + +// getMz calculates Mz. +func getMz(x []float64, z []float64) []float64 { + nx := len(x) + mz := make([]float64, nx) + + for i := range x { + mz = append(mz, x[i]*z[i]) + } + + return mz +}