From ea3d57acd893ca021b50646da9f560a1cb690d9f Mon Sep 17 00:00:00 2001 From: surtur Date: Mon, 25 Jul 2022 22:24:54 +0200 Subject: [PATCH] go: rename AlgoMean,Mean et al. also add comments to methods explaining what they're supposed to do. --- algo/randomSearch.go | 9 +++++--- algo/stochasticHillClimbing.go | 9 +++++--- stats/stats.go | 38 +++++++++++++++++++--------------- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/algo/randomSearch.go b/algo/randomSearch.go index 734d429..d112e62 100644 --- a/algo/randomSearch.go +++ b/algo/randomSearch.go @@ -100,7 +100,10 @@ func RandomSearchNG(maxFES, benchMinIters int, theD []int, benchFunc string, ch )), } - rsMeans := &stats.AlgoMean{Algo: "Random Search"} + rsMeans := &stats.AlgoBenchMean{ + Algo: "Random Search", + BenchMeans: make([]stats.BenchMean, 0, len(localD)), + } // iterate over whatever was passed to us with theD - dimens slice. for _, dimens := range localD { @@ -112,7 +115,7 @@ func RandomSearchNG(maxFES, benchMinIters int, theD []int, benchFunc string, ch } funcStats := &stats.FuncStats{BenchName: benchFunc} benchFuncParams := bench.FunctionParams[benchFunc] - dimXMean := &stats.Mean{ + dimXMean := &stats.BenchMean{ Bench: benchFunc, Dimens: dimens, Iterations: minIters, @@ -182,7 +185,7 @@ func RandomSearchNG(maxFES, benchMinIters int, theD []int, benchFunc string, ch randomSearchStats = append(randomSearchStats, *randomSearchStatDimX) // save to AlgoMeans - rsMeans.Means = append(rsMeans.Means, *dimXMean) + rsMeans.BenchMeans = append(rsMeans.BenchMeans, *dimXMean) } sort.Sort(rsMeans) diff --git a/algo/stochasticHillClimbing.go b/algo/stochasticHillClimbing.go index 2b23e80..4706759 100644 --- a/algo/stochasticHillClimbing.go +++ b/algo/stochasticHillClimbing.go @@ -187,7 +187,10 @@ func HillClimb(maxFES, benchMinIters int, theD []int, benchFunc string, ch chan localD = theD minIters = benchMinIters - shcMeans := &stats.AlgoMean{Algo: "Stochastic Hill Climbing"} + shcMeans := &stats.AlgoBenchMean{ + Algo: "Stochastic Hill Climbing", + BenchMeans: make([]stats.BenchMean, 0, len(localD)), + } for _, dimens := range localD { stochasticHCStatDimX := &stats.Stats{ @@ -201,7 +204,7 @@ func HillClimb(maxFES, benchMinIters int, theD []int, benchFunc string, ch chan } funcStats := &stats.FuncStats{BenchName: benchFunc} benchFuncParams := bench.FunctionParams[benchFunc] - dimXMean := &stats.Mean{ + dimXMean := &stats.BenchMean{ Bench: benchFunc, Dimens: dimens, Iterations: minIters, @@ -299,7 +302,7 @@ func HillClimb(maxFES, benchMinIters int, theD []int, benchFunc string, ch chan stochasticHCStats = append(stochasticHCStats, *stochasticHCStatDimX) // save to AlgoMeans. - shcMeans.Means = append(shcMeans.Means, *dimXMean) + shcMeans.BenchMeans = append(shcMeans.BenchMeans, *dimXMean) } sort.Sort(shcMeans) diff --git a/stats/stats.go b/stats/stats.go index bb72593..5adde67 100644 --- a/stats/stats.go +++ b/stats/stats.go @@ -25,9 +25,9 @@ type FuncStats struct { MeanVals []float64 } -// Mean structure holds mean vals and metadata of a specified -// bench-algo-settings combination. -type Mean struct { +// BenchMean structure holds mean vals and metadata of a bench-params +// combination. +type BenchMean struct { Bench string Dimens int @@ -40,15 +40,15 @@ type Mean struct { MeanVals []float64 } -// AlgoMean holds Mean structs of different benchmarks but the same algorithm. -type AlgoMean struct { - Algo string - Means []Mean +// AlgoBenchMean holds BenchMean structs of different benchmarks but the same algorithm. +type AlgoBenchMean struct { + Algo string + BenchMeans []BenchMean } // MeanStats aggregates AlgoMean structs of different algos. type MeanStats struct { - AlgoMeans []AlgoMean + AlgoMeans []AlgoBenchMean } type Stats struct { @@ -59,30 +59,34 @@ type Stats struct { Generations int } -// the following three methods implement the sort.Interface. -func (a AlgoMean) Len() int { - return len(a.Means) +// Len implements the sort.Interface. +func (a AlgoBenchMean) Len() int { + return len(a.BenchMeans) } -func (a AlgoMean) Less(i, j int) bool { - return a.Means[i].Bench < a.Means[j].Bench +// Less implements the sort.Interface. +func (a AlgoBenchMean) Less(i, j int) bool { + return a.BenchMeans[i].Bench < a.BenchMeans[j].Bench } -func (a AlgoMean) Swap(i, j int) { - a.Means[i], a.Means[j] = a.Means[j], a.Means[i] +// Swap implements the sort.Interface. +func (a AlgoBenchMean) Swap(i, j int) { + a.BenchMeans[i], a.BenchMeans[j] = a.BenchMeans[j], a.BenchMeans[i] } -// the following three methods also implement the sort.Interface. +// Len implements the sort.Interface. func (m MeanStats) Len() int { return len(m.AlgoMeans) } -// this in fact sorts in reverse (fits with our use case atm). +// Less implements the sort.Interface. +// note: this in fact sorts in reverse (fits with our use case atm). func (m MeanStats) Less(i, j int) bool { // return m.AlgoMeans[i].Algo < m.AlgoMeans[j].Algo return m.AlgoMeans[i].Algo > m.AlgoMeans[j].Algo } +// Swap implements the sort.Interface. func (m MeanStats) Swap(i, j int) { m.AlgoMeans[i], m.AlgoMeans[j] = m.AlgoMeans[j], m.AlgoMeans[i] }