146 lines
3.2 KiB
Go
146 lines
3.2 KiB
Go
// Copyright 2022 wanderer <a_mirre at utb dot cz>
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package stats
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"log"
|
|
|
|
"git.dotya.ml/wanderer/math-optim/util"
|
|
"gonum.org/v1/gonum/stat"
|
|
)
|
|
|
|
// BenchRound holds the iteration couter value and Results of size 'maxFES'.
|
|
type BenchRound struct {
|
|
Iteration int
|
|
Results []float64
|
|
}
|
|
|
|
type FuncStats struct {
|
|
BenchName string
|
|
BenchResults []BenchRound
|
|
// MeanVals holds the mean value for each step of the iteration.
|
|
MeanVals []float64
|
|
}
|
|
|
|
// Mean structure holds mean vals and metadata of a specified
|
|
// bench-algo-settings combination.
|
|
type Mean struct {
|
|
Bench string
|
|
Dimens int
|
|
|
|
Iterations int
|
|
Generations int
|
|
// Neighbours param is used in Hill Climbing type algos, set to -1 if not
|
|
// applicable.
|
|
Neighbours int
|
|
// MeanVals holds the mean value for each step of the iteration.
|
|
MeanVals []float64
|
|
}
|
|
|
|
// AlgoMean holds Mean structs of different benchmarks but the same algorithm.
|
|
type AlgoMean struct {
|
|
Algo string
|
|
Means []Mean
|
|
}
|
|
|
|
// MeanStats aggregates AlgoMean structs of different algos.
|
|
type MeanStats struct {
|
|
AlgoMeans []AlgoMean
|
|
}
|
|
|
|
type Stats struct {
|
|
Algo string
|
|
Dimens int
|
|
BenchFuncStats []FuncStats
|
|
Iterations int
|
|
Generations int
|
|
}
|
|
|
|
// the following three methods implement the sort.Interface.
|
|
func (a AlgoMean) Len() int {
|
|
return len(a.Means)
|
|
}
|
|
|
|
func (a AlgoMean) Less(i, j int) bool {
|
|
return a.Means[i].Bench < a.Means[j].Bench
|
|
}
|
|
|
|
func (a AlgoMean) Swap(i, j int) {
|
|
a.Means[i], a.Means[j] = a.Means[j], a.Means[i]
|
|
}
|
|
|
|
// the following three methods also implement the sort.Interface.
|
|
func (m MeanStats) Len() int {
|
|
return len(m.AlgoMeans)
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
func (m MeanStats) Swap(i, j int) {
|
|
m.AlgoMeans[i], m.AlgoMeans[j] = m.AlgoMeans[j], m.AlgoMeans[i]
|
|
}
|
|
|
|
func GetFuncStats(funcName string, benchResults []BenchRound) FuncStats {
|
|
f := FuncStats{
|
|
BenchName: funcName,
|
|
BenchResults: benchResults,
|
|
}
|
|
|
|
return f
|
|
}
|
|
|
|
func GetStats(algo string, dimens int, benchFuncStats []FuncStats, iterations, generations int) Stats {
|
|
s := Stats{
|
|
Algo: algo,
|
|
Dimens: dimens,
|
|
BenchFuncStats: benchFuncStats,
|
|
Iterations: iterations,
|
|
Generations: generations,
|
|
}
|
|
|
|
return s
|
|
}
|
|
|
|
func SaveStats(stats []Stats, fName string) {
|
|
prefix := "out/stats/"
|
|
ext := ".json"
|
|
|
|
// create required folders, bail early in case of an error.
|
|
if err := util.CreatePath(prefix); err != nil {
|
|
log.Fatalln("went to create stats dir, there was an issue: ", err)
|
|
}
|
|
|
|
if j, err := json.MarshalIndent(stats, "", " "); err != nil {
|
|
log.Fatal(err)
|
|
} else {
|
|
log.Println("saving json stats to:", prefix+fName+ext)
|
|
|
|
if err = ioutil.WriteFile(prefix+fName+ext, j, 0o600); err != nil {
|
|
log.Println("error saving stats to file:", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func GetMeanVals(values []BenchRound, fes int) []float64 {
|
|
res := make([]float64, fes)
|
|
|
|
for i := 0; i < fes; i++ {
|
|
iterVals := make([]float64, len(values))
|
|
|
|
for j, v := range values {
|
|
iterVals[j] = v.Results[i]
|
|
}
|
|
|
|
res[i] = stat.Mean(iterVals, nil)
|
|
}
|
|
|
|
return res
|
|
}
|