go: change Functions var to a string-func map
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2022-06-18 20:40:27 +02:00
parent 5b7c0a16a4
commit ed816620c1
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
2 changed files with 15 additions and 20 deletions

@ -44,31 +44,20 @@ func genValsRandomSearch(dimens uint, vals []float64, uniform *distuv.Uniform) {
// singleRandomSearch performs a single iteration of the 'RandomSearch' algorithm. // singleRandomSearch performs a single iteration of the 'RandomSearch' algorithm.
// it takes a couple of arguments: // it takes a couple of arguments:
// * dimens uint: number of dimensions of the objective function // * dimens uint: number of dimensions of the objective function
// * f string: name of the bench func to optimise (see bench/functions) // * f func([]float64) float64: bench func to execute (see Functions map in
// bench/functions.go)
// * min/max float64: the upper/lower limit of the uniform distribution span, // * min/max float64: the upper/lower limit of the uniform distribution span,
// which is relevant to the objective function. // which is relevant to the objective function.
func singleRandomSearch(dimens uint, f string, min, max float64) ([]float64, float64) { func singleRandomSearch(dimens uint, f func([]float64) float64, min, max float64) ([]float64, float64) {
vals := make([]float64, dimens) vals := make([]float64, dimens)
var res float64
// create a continuous uniform distribution representation within min/max bounds // create a continuous uniform distribution representation within min/max bounds
uniformDist := distuv.Uniform{Min: min, Max: max} uniformDist := distuv.Uniform{Min: min, Max: max}
genValsRandomSearch(dimens, vals, &uniformDist) genValsRandomSearch(dimens, vals, &uniformDist)
// result of the benchmarking function computation over vals // result of the bench function.
switch f { res := f(vals)
// Schwefel
case bench.Functions[0]:
res = bench.Schwefel(vals)
// "DeJong1st"
case bench.Functions[1]:
res = bench.DeJong1st(vals)
// "DeJong2nd"
case bench.Functions[2]:
res = bench.DeJong2nd(vals)
}
return vals, res return vals, res
} }
@ -135,7 +124,7 @@ func RandomSearch(fes uint) {
// run Schwefel. // run Schwefel.
v, r := singleRandomSearch( v, r := singleRandomSearch(
dimens, dimens,
bench.Functions[0], bench.Functions["Schwefel"],
bench.SchwefelParams.Min(), bench.SchwefelParams.Min(),
bench.SchwefelParams.Max(), bench.SchwefelParams.Max(),
) )
@ -173,7 +162,7 @@ func RandomSearch(fes uint) {
// run De Jong 1st. // run De Jong 1st.
v, r := singleRandomSearch( v, r := singleRandomSearch(
dimens, dimens,
bench.Functions[1], bench.Functions["De Jong 1st"],
bench.DeJong1Params.Min(), bench.DeJong1Params.Min(),
bench.DeJong1Params.Max(), bench.DeJong1Params.Max(),
) )
@ -205,7 +194,7 @@ func RandomSearch(fes uint) {
// run De Jong 2nd. // run De Jong 2nd.
v, r := singleRandomSearch( v, r := singleRandomSearch(
dimens, dimens,
bench.Functions[2], bench.Functions["De Jong 2nd"],
bench.DeJong2Params.Min(), bench.DeJong2Params.Min(),
bench.DeJong2Params.Max(), bench.DeJong2Params.Max(),
) )

@ -5,7 +5,13 @@ package bench
import "math" import "math"
var Functions = []string{"Schwefel", "De Jong 1st", "De Jong 2nd"} // 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,
}
// Schwefel computes a value of the Schwefel function for x. // Schwefel computes a value of the Schwefel function for x.
func Schwefel(x []float64) float64 { func Schwefel(x []float64) float64 {