2023-01-12 23:35:51 +01:00
|
|
|
// Copyright 2023 wanderer <a_mirre at utb dot cz>
|
2022-06-17 23:24:41 +02:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
package stats
|
|
|
|
|
2022-06-17 23:56:36 +02:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2022-07-13 15:41:54 +02:00
|
|
|
|
|
|
|
"git.dotya.ml/wanderer/math-optim/util"
|
2022-07-18 10:08:12 +02:00
|
|
|
"gonum.org/v1/gonum/stat"
|
2022-06-17 23:56:36 +02:00
|
|
|
)
|
|
|
|
|
2022-06-19 21:56:36 +02:00
|
|
|
// BenchRound holds the iteration couter value and Results of size 'maxFES'.
|
2022-06-18 00:51:51 +02:00
|
|
|
type BenchRound struct {
|
|
|
|
Iteration int
|
|
|
|
Results []float64
|
|
|
|
}
|
|
|
|
|
2022-06-17 23:24:41 +02:00
|
|
|
type FuncStats struct {
|
2022-07-19 19:58:45 +02:00
|
|
|
BenchName string
|
|
|
|
BenchResults []BenchRound
|
2022-07-19 21:56:49 +02:00
|
|
|
// MeanVals holds the mean value for each step of the iteration.
|
|
|
|
MeanVals []float64
|
|
|
|
}
|
|
|
|
|
2022-07-25 22:24:54 +02:00
|
|
|
// BenchMean structure holds mean vals and metadata of a bench-params
|
|
|
|
// combination.
|
|
|
|
type BenchMean struct {
|
2022-07-19 21:56:49 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-07-25 22:24:54 +02:00
|
|
|
// AlgoBenchMean holds BenchMean structs of different benchmarks but the same algorithm.
|
|
|
|
type AlgoBenchMean struct {
|
|
|
|
Algo string
|
|
|
|
BenchMeans []BenchMean
|
2022-07-19 21:56:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MeanStats aggregates AlgoMean structs of different algos.
|
|
|
|
type MeanStats struct {
|
2022-07-25 22:24:54 +02:00
|
|
|
AlgoMeans []AlgoBenchMean
|
2022-06-17 23:24:41 +02:00
|
|
|
}
|
|
|
|
|
2022-08-03 21:17:19 +02:00
|
|
|
// AlgoMeanVals holds computed mean values of an Algo.
|
|
|
|
type AlgoMeanVals struct {
|
|
|
|
Title string
|
|
|
|
// MeanVals of a particular bench func to compare.
|
|
|
|
MeanVals []float64
|
|
|
|
}
|
|
|
|
|
2022-06-17 23:24:41 +02:00
|
|
|
type Stats struct {
|
|
|
|
Algo string
|
|
|
|
Dimens int
|
|
|
|
BenchFuncStats []FuncStats
|
|
|
|
Iterations int
|
2022-08-20 23:28:30 +02:00
|
|
|
// this should perhaps be named FES as that is a smarter thing to be
|
|
|
|
// comparing than algo generations, which can vary based on the type of
|
|
|
|
// algo, number of neighbours, etc..
|
|
|
|
Generations int
|
2023-01-21 02:45:56 +01:00
|
|
|
// NP is the initial population size, disable with 0. Only applicable to
|
|
|
|
// GAs (see algo/de for more details).
|
|
|
|
NP int
|
|
|
|
// F is the mutation factor, disable with 0. Only applicable to GAs.
|
|
|
|
F float64
|
|
|
|
// CR is the crossover probability, disable with 0. Only applicable to GAs.
|
|
|
|
CR float64
|
2022-06-17 23:24:41 +02:00
|
|
|
}
|
2022-06-17 23:56:36 +02:00
|
|
|
|
2022-07-25 22:24:54 +02:00
|
|
|
// Len implements the sort.Interface.
|
|
|
|
func (a AlgoBenchMean) Len() int {
|
|
|
|
return len(a.BenchMeans)
|
2022-07-20 02:36:14 +02:00
|
|
|
}
|
|
|
|
|
2022-07-25 22:24:54 +02:00
|
|
|
// Less implements the sort.Interface.
|
|
|
|
func (a AlgoBenchMean) Less(i, j int) bool {
|
|
|
|
return a.BenchMeans[i].Bench < a.BenchMeans[j].Bench
|
2022-07-20 02:36:14 +02:00
|
|
|
}
|
|
|
|
|
2022-07-25 22:24:54 +02:00
|
|
|
// Swap implements the sort.Interface.
|
|
|
|
func (a AlgoBenchMean) Swap(i, j int) {
|
|
|
|
a.BenchMeans[i], a.BenchMeans[j] = a.BenchMeans[j], a.BenchMeans[i]
|
2022-07-20 02:36:14 +02:00
|
|
|
}
|
|
|
|
|
2022-07-25 22:24:54 +02:00
|
|
|
// Len implements the sort.Interface.
|
2022-07-20 02:36:14 +02:00
|
|
|
func (m MeanStats) Len() int {
|
|
|
|
return len(m.AlgoMeans)
|
|
|
|
}
|
|
|
|
|
2022-07-25 22:24:54 +02:00
|
|
|
// Less implements the sort.Interface.
|
|
|
|
// note: this in fact sorts in reverse (fits with our use case atm).
|
2022-07-20 02:36:14 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-07-25 22:24:54 +02:00
|
|
|
// Swap implements the sort.Interface.
|
2022-07-20 02:36:14 +02:00
|
|
|
func (m MeanStats) Swap(i, j int) {
|
|
|
|
m.AlgoMeans[i], m.AlgoMeans[j] = m.AlgoMeans[j], m.AlgoMeans[i]
|
|
|
|
}
|
|
|
|
|
2022-07-19 19:58:45 +02:00
|
|
|
func GetFuncStats(funcName string, benchResults []BenchRound) FuncStats {
|
2022-06-17 23:56:36 +02:00
|
|
|
f := FuncStats{
|
2022-07-19 19:58:45 +02:00
|
|
|
BenchName: funcName,
|
|
|
|
BenchResults: benchResults,
|
2022-06-17 23:56:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-06-20 02:49:24 +02:00
|
|
|
func SaveStats(stats []Stats, fName string) {
|
2022-07-13 15:41:54 +02:00
|
|
|
prefix := "out/stats/"
|
2022-06-20 02:49:24 +02:00
|
|
|
ext := ".json"
|
|
|
|
|
2022-07-13 15:41:54 +02:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2022-06-17 23:56:36 +02:00
|
|
|
if j, err := json.MarshalIndent(stats, "", " "); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
} else {
|
2022-06-20 02:49:24 +02:00
|
|
|
log.Println("saving json stats to:", prefix+fName+ext)
|
2022-06-17 23:56:36 +02:00
|
|
|
|
2022-06-20 02:49:24 +02:00
|
|
|
if err = ioutil.WriteFile(prefix+fName+ext, j, 0o600); err != nil {
|
2022-06-17 23:56:36 +02:00
|
|
|
log.Println("error saving stats to file:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-18 10:08:12 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|