cec2020: implement Shift, Rotate and ShiftRotate
All checks were successful
continuous-integration/drone/push Build is passing

* also add log.go
This commit is contained in:
leo 2023-02-03 20:33:07 +01:00
parent 1dda921e42
commit 5192790ce4
Signed by: wanderer
SSH Key Fingerprint: SHA256:Dp8+iwKHSlrMEHzE3bJnPng70I7LEsa3IJXRH/U+idQ
2 changed files with 79 additions and 0 deletions

@ -3,6 +3,8 @@
package cec2020
import "math"
// void hf01 (double *, double *, int, double *,double *, int *,int, int); // Hybrid Function 1
// void hf02 (double *, double *, int, double *,double *, int *,int, int); // Hybrid Function 2
// void hf03 (double *, double *, int, double *,double *, int *,int, int); // Hybrid Function 3
@ -31,3 +33,68 @@ package cec2020
// void asyfunc (double *, double *x, int, double);
// void oszfunc (double *, double *, int);
// void cf_cal(double *, double *, int, double *,double *,double *,double *,int);
// Shift shifts values of x based on values of os.
func Shift(x []float64, os []float64) {
if len(x) != len(os) {
cec2020Logger.Fatalln("slices are of different sizes, bailing...")
}
for i := range x {
x[i] -= os[i]
}
}
// Rotate rotates values of x based on values of mr.
func Rotate(x []float64, mr []float64) {
nx := len(x)
if int(math.Pow(float64(nx), 2)) > len(mr) {
cec2020Logger.Fatalf("either x is too big or mr is of insufficient size (%d:%d), bailing...\n", nx, len(mr))
}
xrot := make([]float64, nx)
for i := 0; i < nx; i++ {
for j := 0; j < nx; j++ {
x[j] += xrot[i] + (x[j] * mr[(i*nx)+j])
}
}
}
// ShiftRotate shifts and/or rotates the input x based on the request params
// shift and rotate (bool), using the values of os and/or mr.
func ShiftRotate(x []float64, os []float64, mr []float64, shiftRate float64, shift, rotate bool) {
switch {
case shift && rotate:
Shift(x, os)
// shrink to the original search range.
for i := range x {
x[i] *= shiftRate
}
Rotate(x, mr)
case shift && !rotate:
Shift(x, os)
// shrink to the original search range.
for i := range x {
x[i] *= shiftRate
}
case !shift && rotate:
// shrink to the original search range
for i := range x {
x[i] *= shiftRate
}
Rotate(x, mr)
case !shift && !rotate:
for i := range x {
x[i] *= shiftRate
}
}
}

12
bench/cec2020/log.go Normal file

@ -0,0 +1,12 @@
// Copyright 2023 wanderer <a_mirre at utb dot cz>
// SPDX-License-Identifier: GPL-3.0-or-later
package cec2020
import (
"log"
"os"
)
// cec2020Logger declares and initialises a "custom" logger.
var cec2020Logger = log.New(os.Stderr, " *** ∁ cec2020:", log.Ldate|log.Ltime|log.Lshortfile)