2023-01-12 23:35:51 +01:00
|
|
|
// Copyright 2023 wanderer <a_mirre at utb dot cz>
|
2022-06-16 17:15:39 +02:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
package bench
|
|
|
|
|
|
|
|
import "math"
|
|
|
|
|
2022-06-18 20:40:27 +02:00
|
|
|
// Functions is a string-func map of function names and specific bench funcs
|
|
|
|
// for easier iterable access.
|
|
|
|
var Functions = map[string]func([]float64) float64{
|
|
|
|
"Schwefel": Schwefel,
|
|
|
|
"De Jong 1st": DeJong1st,
|
|
|
|
"De Jong 2nd": DeJong2nd,
|
2022-12-27 00:56:54 +01:00
|
|
|
"Rastrigin": Rastrigin,
|
2022-06-18 20:40:27 +02:00
|
|
|
}
|
2022-06-17 01:54:30 +02:00
|
|
|
|
2022-12-27 01:04:55 +01:00
|
|
|
// FuncNames represents a numbered list of function name.
|
2022-06-24 00:56:10 +02:00
|
|
|
var FuncNames = map[int]string{
|
|
|
|
0: "Schwefel",
|
|
|
|
1: "De Jong 1st",
|
|
|
|
2: "De Jong 2nd",
|
2022-12-27 00:56:54 +01:00
|
|
|
3: "Rastrigin",
|
2022-06-24 00:56:10 +02:00
|
|
|
}
|
|
|
|
|
2022-06-18 23:01:57 +02:00
|
|
|
// Function params maps function names to their funcParams for easier iterable
|
|
|
|
// access.
|
|
|
|
var FunctionParams = map[string]funcParams{
|
|
|
|
"Schwefel": SchwefelParams,
|
|
|
|
"De Jong 1st": DeJong1Params,
|
|
|
|
"De Jong 2nd": DeJong2Params,
|
2022-12-27 00:56:54 +01:00
|
|
|
"Rastrigin": RastriginParams,
|
2022-06-18 23:01:57 +02:00
|
|
|
}
|
|
|
|
|
2022-06-28 22:02:13 +02:00
|
|
|
// Schwefel computes the value of the Schwefel function for x.
|
2022-06-16 17:15:39 +02:00
|
|
|
func Schwefel(x []float64) float64 {
|
2022-12-24 11:30:22 +01:00
|
|
|
// - Domain is | x_i | < 500
|
2023-02-04 19:24:50 +01:00
|
|
|
// - Global minimum at fmin = 0 at x_i = 420.9687
|
|
|
|
var sum float64
|
2022-06-16 17:15:39 +02:00
|
|
|
|
|
|
|
for _, val := range x {
|
2023-02-04 19:24:50 +01:00
|
|
|
sum += val * math.Sin(math.Sqrt(math.Abs(val)))
|
2022-06-16 17:15:39 +02:00
|
|
|
}
|
|
|
|
|
2023-02-04 19:24:50 +01:00
|
|
|
return 418.9829*float64(len(x)) - sum
|
2022-06-16 17:15:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeJong1st computes the value of the 1st De Jong function for x.
|
|
|
|
func DeJong1st(x []float64) float64 {
|
|
|
|
var res float64
|
|
|
|
|
|
|
|
for _, val := range x {
|
2022-06-17 20:41:39 +02:00
|
|
|
res += math.Pow(val, 2)
|
2022-06-16 17:15:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeJong2nd computes the value of the 2nd De Jong function for x.
|
|
|
|
func DeJong2nd(x []float64) float64 {
|
|
|
|
var res float64
|
|
|
|
|
|
|
|
for _, val := range x {
|
2022-06-17 20:41:39 +02:00
|
|
|
res += math.Floor(val)
|
2022-06-16 17:15:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
2022-12-27 00:56:54 +01:00
|
|
|
|
|
|
|
// Rastrigin computes the value of the Rastrigin function for x.
|
|
|
|
// ref: https://www.sfu.ca/~ssurjano/rastr.html
|
|
|
|
func Rastrigin(x []float64) float64 {
|
|
|
|
var sum float64
|
|
|
|
|
|
|
|
// dimension of vector x.
|
|
|
|
d := float64(len(x))
|
|
|
|
|
|
|
|
for _, xi := range x {
|
|
|
|
sum += math.Pow(xi, 2) - 10*(math.Cos(2*math.Pi*xi))
|
|
|
|
}
|
|
|
|
|
|
|
|
y := (10 * d) + sum
|
|
|
|
|
|
|
|
return y
|
|
|
|
}
|