math-optim/stats/table.go

105 lines
2.6 KiB
Go
Raw Normal View History

2022-06-24 00:45:43 +02:00
// Copyright 2022 wanderer <a_mirre at utb dot cz>
// SPDX-License-Identifier: GPL-3.0-or-later
package stats
import (
"fmt"
"git.dotya.ml/wanderer/math-optim/report"
2022-06-24 00:45:43 +02:00
"gonum.org/v1/gonum/floats"
"gonum.org/v1/gonum/stat"
)
// getColLayout returns a string slice of Latex table column alignment
// settings.
func getColLayout() []string {
return []string{"c", "c", "c", "c", "c"}
2022-06-24 00:45:43 +02:00
}
// getColNames returns names of table columns, i.e. statistical features we are
// interested in.
func getColNames() []string {
// the first column describes specific iteration settings and is therefore
// dynamically set, hence not present here nor mentioned in
// `getColLayout()`.
return []string{"min", "max", "mean", "median", "stddev"}
2022-06-24 00:45:43 +02:00
}
// SaveTable sifts through computed values, organises data in table-like
// structure as defined in the `report` pkg and passes it on to be fed to a
// tmpl, result of which is then saved in a `.tex` file (filename based on the
// algo name string).
func SaveTable(algo string, algoStats [][]Stats) {
table := report.NewTable()
table.Algo = algo
table.Header = getColNames()
table.ColLayout = getColLayout()
2022-06-24 00:45:43 +02:00
for _, singleFunc := range algoStats {
// append/merge(...) if necessary.
table.Rows = append(table.Rows, parseSingleBenchStats(singleFunc)...)
2022-06-24 00:45:43 +02:00
}
report.SaveTableToFile(*table)
2022-06-24 00:45:43 +02:00
}
// parseSingleBenchStats processes results of a particular bench and constructs
// statistics.
func parseSingleBenchStats(benchStats []Stats) []report.Row {
rows := make([]report.Row, 0)
for _, s := range benchStats {
2022-06-24 00:45:43 +02:00
for _, dim := range s.BenchFuncStats {
row := report.NewRow()
row.Title = "D=" + fmt.Sprint(s.Dimens) + ", f=" + dim.BenchName +
", G=" + fmt.Sprint(s.Generations) +
", I=" + fmt.Sprint(s.Iterations)
row.Title = makeRowTitle(
dim.BenchName,
2022-06-24 00:45:43 +02:00
s.Dimens,
s.Generations,
s.Iterations,
)
// collect the best.
var best []float64
for _, iter := range dim.Solution {
last := s.Generations - 1
best = append(best, iter.Results[last])
}
row.Values = statsFromBest(best)
2022-06-24 00:45:43 +02:00
rows = append(rows, *row)
2022-06-24 00:45:43 +02:00
}
}
return rows
2022-06-24 00:45:43 +02:00
}
func makeRowTitle(bench string, dimens, generations, iterations int) string {
return "D=" + fmt.Sprint(dimens) + ", f=" + bench +
", G=" + fmt.Sprint(generations) +
", I=" + fmt.Sprint(iterations)
2022-06-24 00:45:43 +02:00
}
// statsFromBest computes the actual statistics upon the slice of best results,
// returns a slice of float64s.
func statsFromBest(best []float64) []float64 {
s := make([]float64, len(getColNames()))
s[0] = floats.Min(best)
s[1] = floats.Max(best)
s[2] = stat.Mean(best, nil)
s[3] = stat.Mean(best, nil)
s[4] = stat.StdDev(best, nil)
return s
2022-06-24 00:45:43 +02:00
}