math-optim/stats/stats.go
surtur 16cbf39a79
All checks were successful
continuous-integration/drone/push Build is passing
go(stats,algo): get,plot mean vals
2022-07-18 10:08:12 +02:00

90 lines
1.8 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
Solution []BenchRound
}
type Stats struct {
Algo string
Dimens int
BenchFuncStats []FuncStats
Iterations int
Generations int
}
func GetFuncStats(funcName string, solution []BenchRound) FuncStats {
f := FuncStats{
BenchName: funcName,
Solution: solution,
}
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
}