2022-06-17 23:24:41 +02:00
|
|
|
// Copyright 2022 wanderer <a_mirre at utb dot cz>
|
|
|
|
// 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-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 {
|
|
|
|
BenchName string
|
2022-06-18 00:51:51 +02:00
|
|
|
Solution []BenchRound
|
2022-06-17 23:24:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Stats struct {
|
|
|
|
Algo string
|
|
|
|
Dimens int
|
|
|
|
BenchFuncStats []FuncStats
|
|
|
|
Iterations int
|
|
|
|
Generations int
|
|
|
|
}
|
2022-06-17 23:56:36 +02:00
|
|
|
|
2022-06-18 00:51:51 +02:00
|
|
|
func GetFuncStats(funcName string, solution []BenchRound) FuncStats {
|
2022-06-17 23:56:36 +02:00
|
|
|
f := FuncStats{
|
|
|
|
BenchName: funcName,
|
2022-06-18 00:51:51 +02:00
|
|
|
Solution: solution,
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|