go(bench): add Rastrigin function defintion+test
This commit is contained in:
parent
fc53e2a66a
commit
f25011b7fc
12
algo/algo.go
12
algo/algo.go
@ -90,7 +90,8 @@ func PrepComparisonOfMeans(wg *sync.WaitGroup) (*report.PicList, int) {
|
||||
|
||||
algoCount := len(algos)
|
||||
dimLen := len(bench.Dimensions)
|
||||
benchCount := len(bench.Functions)
|
||||
// without Rastrigin in active duty for the moment.
|
||||
benchCount := len(bench.Functions) - 1
|
||||
|
||||
// note: this is a wee bit ugly.
|
||||
for d := 0; d < dimLen; d++ {
|
||||
@ -142,7 +143,8 @@ func DoRandomSearch(wg *sync.WaitGroup, m *sync.Mutex) {
|
||||
printRandomSearch("starting...")
|
||||
|
||||
// funcCount is the number of bench functions available.
|
||||
funcCount := len(bench.Functions)
|
||||
// without Rastrigin in active duty for the moment.
|
||||
funcCount := len(bench.Functions) - 1
|
||||
// stats for the current algo (RandomSearch).
|
||||
algoStats := make([][]stats.Stats, funcCount)
|
||||
// ch serves as a way to get the actual computed output.
|
||||
@ -214,7 +216,8 @@ func DoStochasticHillClimbing(wg *sync.WaitGroup, m *sync.Mutex) {
|
||||
printSHC("starting...")
|
||||
|
||||
// funcCount is the number of bench functions available.
|
||||
funcCount := len(bench.Functions)
|
||||
// without Rastrigin in active duty for the moment.
|
||||
funcCount := len(bench.Functions) - 1
|
||||
// stats for the current algo (StochasticHillClimber).
|
||||
algoStats := make([][]stats.Stats, funcCount)
|
||||
// ch serves as a way to get the actual computed output.
|
||||
@ -275,7 +278,8 @@ func DoStochasticHillClimbing100Neigh(wg *sync.WaitGroup, m *sync.Mutex) {
|
||||
printSHC("starting...")
|
||||
|
||||
// funcCount is the number of bench functions available.
|
||||
funcCount := len(bench.Functions)
|
||||
// without Rastrigin in active duty for the moment.
|
||||
funcCount := len(bench.Functions) - 1
|
||||
// stats for the current algo (StochasticHillClimber).
|
||||
algoStats := make([][]stats.Stats, funcCount)
|
||||
// ch serves as a way to get the actual computed output.
|
||||
|
@ -32,6 +32,9 @@ var (
|
||||
// DeJong2Params is a struct holding the min, max allowed value of inputs
|
||||
// passed to the De Jong 2nd function.
|
||||
DeJong2Params = funcParams{min: -5.0, max: 5.0}
|
||||
// RastriginParams is a struct holding the min, max allowed value of inputs
|
||||
// passed to the Rastrigin function.
|
||||
RastriginParams = funcParams{min: -5.12, max: 5.12}
|
||||
)
|
||||
|
||||
// Min returns the non-exported "min" field of a funcParams struct.
|
||||
|
@ -11,6 +11,7 @@ var Functions = map[string]func([]float64) float64{
|
||||
"Schwefel": Schwefel,
|
||||
"De Jong 1st": DeJong1st,
|
||||
"De Jong 2nd": DeJong2nd,
|
||||
"Rastrigin": Rastrigin,
|
||||
}
|
||||
|
||||
// FuncNames represents a numbered list of function name.
|
||||
@ -18,6 +19,7 @@ var FuncNames = map[int]string{
|
||||
0: "Schwefel",
|
||||
1: "De Jong 1st",
|
||||
2: "De Jong 2nd",
|
||||
3: "Rastrigin",
|
||||
}
|
||||
|
||||
// Function params maps function names to their funcParams for easier iterable
|
||||
@ -26,6 +28,7 @@ var FunctionParams = map[string]funcParams{
|
||||
"Schwefel": SchwefelParams,
|
||||
"De Jong 1st": DeJong1Params,
|
||||
"De Jong 2nd": DeJong2Params,
|
||||
"Rastrigin": RastriginParams,
|
||||
}
|
||||
|
||||
// Schwefel computes the value of the Schwefel function for x.
|
||||
@ -62,3 +65,20 @@ func DeJong2nd(x []float64) float64 {
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
@ -102,3 +102,33 @@ func TestDeJong2nd(t *testing.T) {
|
||||
t.Errorf("incorrect DeJong2nd output, want: %f, got: %f", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRastrigin(t *testing.T) {
|
||||
testInput := []float64{
|
||||
-2.7137778065280593,
|
||||
-1.9789783390895765,
|
||||
-4.38267902085263,
|
||||
-4.617877608383319,
|
||||
-2.1529909200665474,
|
||||
-3.801849324611571,
|
||||
2.4968697503272486,
|
||||
-0.27358100201730196,
|
||||
2.0444319451255977,
|
||||
3.8798952350630653,
|
||||
-0.6150024279011337,
|
||||
2.8643010641852413,
|
||||
-0.6692444051928748,
|
||||
4.4649633199687475,
|
||||
3.2510298731558507,
|
||||
-4.2095036422081495,
|
||||
-0.130882052243404,
|
||||
1.2001716295708604,
|
||||
-0.29427650577602193,
|
||||
}
|
||||
want := 343.97127081044846
|
||||
got := Rastrigin(testInput)
|
||||
|
||||
if want != got {
|
||||
t.Errorf("incorrect Rastrigin output, want: %f, got: %f", want, got)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user