2022-06-18 05:27:10 +02:00
|
|
|
// Copyright 2022 wanderer <a_mirre at utb dot cz>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
package algo
|
2022-06-20 03:32:48 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2022-07-19 19:23:39 +02:00
|
|
|
"strings"
|
2022-08-03 21:17:19 +02:00
|
|
|
"sync"
|
2022-07-08 19:22:09 +02:00
|
|
|
"time"
|
2022-06-20 03:32:48 +02:00
|
|
|
|
2022-07-12 16:56:49 +02:00
|
|
|
"git.dotya.ml/wanderer/math-optim/report"
|
2022-06-20 03:32:48 +02:00
|
|
|
"git.dotya.ml/wanderer/math-optim/stats"
|
2022-07-10 20:54:48 +02:00
|
|
|
"git.dotya.ml/wanderer/math-optim/util"
|
2022-07-08 03:32:07 +02:00
|
|
|
"gonum.org/v1/gonum/floats"
|
2022-06-20 03:32:48 +02:00
|
|
|
"gonum.org/v1/plot"
|
|
|
|
"gonum.org/v1/plot/plotter"
|
|
|
|
"gonum.org/v1/plot/plotutil"
|
|
|
|
"gonum.org/v1/plot/vg"
|
|
|
|
)
|
|
|
|
|
2022-07-29 21:04:15 +02:00
|
|
|
const (
|
|
|
|
preferredFont = "Mono"
|
|
|
|
titlePreferredFont = "Sans"
|
|
|
|
yAxisLabel = "CF value"
|
|
|
|
xAxisLabel = "Generations"
|
|
|
|
)
|
2022-07-05 22:56:01 +02:00
|
|
|
|
2022-08-03 21:17:19 +02:00
|
|
|
// PlotMeanValsMulti creates plots for every member of 'stats.AlgoMeanVals' it
|
|
|
|
// is handed and saves them as 'result.Pic's results into a package-global
|
|
|
|
// slice.
|
|
|
|
func PlotMeanValsMulti(
|
|
|
|
w *sync.WaitGroup,
|
|
|
|
dimens, iterations int,
|
|
|
|
bench, fPrefix, fExt string,
|
|
|
|
algoMeanVals ...stats.AlgoMeanVals,
|
|
|
|
) {
|
|
|
|
defer w.Done()
|
|
|
|
|
|
|
|
// track time.
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
pWidth := 13 * vg.Centimeter
|
|
|
|
pHeight := 13 * vg.Centimeter
|
|
|
|
|
|
|
|
plotter.DefaultFont.Typeface = preferredFont
|
|
|
|
plotter.DefaultLineStyle.Width = vg.Points(2.0)
|
|
|
|
|
|
|
|
p := plot.New()
|
|
|
|
pic := report.NewPic()
|
|
|
|
|
|
|
|
p.Title.Text = fmt.Sprintf(
|
|
|
|
"Comparison of Means (%dI) -\n%s (%dD)",
|
|
|
|
iterations, bench, dimens,
|
|
|
|
)
|
|
|
|
|
2022-08-20 23:28:30 +02:00
|
|
|
p.X.Label.Text = "Objective func. evaluations"
|
2022-08-03 21:17:19 +02:00
|
|
|
p.X.Label.TextStyle.Font.Variant = preferredFont
|
|
|
|
p.X.Label.TextStyle.Font.Weight = 1 // Medium
|
|
|
|
p.X.Tick.Label.Font.Variant = preferredFont
|
|
|
|
p.Y.Label.Text = yAxisLabel
|
|
|
|
p.Y.Label.TextStyle.Font.Variant = preferredFont
|
|
|
|
p.Y.Label.TextStyle.Font.Weight = 1 // Medium
|
|
|
|
p.Y.Tick.Label.Font.Variant = preferredFont
|
|
|
|
|
|
|
|
p.Title.TextStyle.Font.Size = 14.5
|
|
|
|
p.Title.TextStyle.Font.Variant = titlePreferredFont
|
|
|
|
p.Title.TextStyle.Font.Weight = 2 // SemiBold
|
|
|
|
p.Title.Padding = 3 * vg.Millimeter
|
|
|
|
|
|
|
|
p.Legend.TextStyle.Font.Variant = preferredFont
|
|
|
|
p.Legend.TextStyle.Font.Size = 10
|
|
|
|
p.Legend.Top = true
|
|
|
|
p.Legend.Padding = 2 * vg.Millimeter
|
|
|
|
|
|
|
|
lines := make([]interface{}, 0)
|
|
|
|
|
|
|
|
for _, v := range algoMeanVals {
|
|
|
|
// mark the end of the X axis with the greatest of len(v)
|
|
|
|
if greatest := float64(len(v.MeanVals)); greatest > p.X.Max {
|
|
|
|
p.X.Max = greatest
|
|
|
|
}
|
|
|
|
|
|
|
|
if min := floats.Min(v.MeanVals); min < p.Y.Min {
|
|
|
|
p.Y.Min = min
|
|
|
|
}
|
|
|
|
|
|
|
|
if max := floats.Max(v.MeanVals); max > p.Y.Max {
|
|
|
|
p.Y.Max = max
|
|
|
|
}
|
|
|
|
|
|
|
|
pts := make(plotter.XYs, len(v.MeanVals))
|
|
|
|
|
|
|
|
// fill the plotter with datapoints.
|
|
|
|
for k, res := range v.MeanVals {
|
|
|
|
pts[k].X = float64(k)
|
|
|
|
pts[k].Y = res
|
|
|
|
}
|
|
|
|
|
|
|
|
lines = append(lines, v.Title, pts)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := plotutil.AddLines(
|
|
|
|
p,
|
|
|
|
lines...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := util.SanitiseFName(
|
|
|
|
fmt.Sprintf("%s%scomparison-of-means-%dI-%s-%02dD",
|
|
|
|
report.GetPicsDir(),
|
|
|
|
fPrefix,
|
|
|
|
iterations,
|
|
|
|
bench,
|
|
|
|
dimens,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
// set pic file path and caption.
|
|
|
|
pic.FilePath = filename
|
|
|
|
pic.Caption = strings.ReplaceAll(
|
|
|
|
fmt.Sprintf("Comparison of Means (%dI) - %s (%dD)",
|
|
|
|
iterations, bench, dimens,
|
|
|
|
),
|
|
|
|
" ", "~")
|
|
|
|
pic.Bench = bench
|
|
|
|
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
info := fmt.Sprintf("saving img to file: %s%s [generated in %s]",
|
|
|
|
filename, fExt, elapsed,
|
|
|
|
)
|
|
|
|
log.Println(info)
|
|
|
|
|
|
|
|
// Save the plot to a file using the above-constructed 'filename'.
|
|
|
|
if err := p.Save(
|
|
|
|
pWidth,
|
|
|
|
pHeight,
|
|
|
|
filename+fExt,
|
|
|
|
); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mCoMPL.Lock()
|
|
|
|
comparisonOfMeansPicList.Pics = append(comparisonOfMeansPicList.Pics, *pic)
|
|
|
|
mCoMPL.Unlock()
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:56:49 +02:00
|
|
|
func plotMeanVals(meanVals []float64, title string, fes int) *plot.Plot {
|
2022-07-29 21:04:15 +02:00
|
|
|
plotter.DefaultFont.Typeface = preferredFont
|
2022-07-18 22:36:47 +02:00
|
|
|
plotter.DefaultLineStyle.Width = vg.Points(2.0)
|
2022-07-18 10:08:12 +02:00
|
|
|
|
2022-08-03 17:20:49 +02:00
|
|
|
if fes != len(meanVals) {
|
|
|
|
log.Fatalf("meanVals - FES mismatch: %d vs %d , bailing\n", fes, len(meanVals))
|
|
|
|
}
|
|
|
|
|
2022-07-18 10:08:12 +02:00
|
|
|
p := plot.New()
|
|
|
|
|
2022-07-18 22:36:47 +02:00
|
|
|
p.Title.Text = "Mean - " + title
|
2022-07-18 10:08:12 +02:00
|
|
|
|
2022-07-29 21:04:15 +02:00
|
|
|
p.X.Label.Text = xAxisLabel
|
|
|
|
p.X.Label.TextStyle.Font.Variant = preferredFont
|
2022-07-18 10:08:12 +02:00
|
|
|
p.X.Label.TextStyle.Font.Weight = 1 // Medium
|
2022-07-29 21:04:15 +02:00
|
|
|
p.X.Tick.Label.Font.Variant = preferredFont
|
|
|
|
p.Y.Label.Text = yAxisLabel
|
|
|
|
p.Y.Label.TextStyle.Font.Variant = preferredFont
|
2022-07-18 10:08:12 +02:00
|
|
|
p.Y.Label.TextStyle.Font.Weight = 1 // Medium
|
2022-07-29 21:04:15 +02:00
|
|
|
p.Y.Tick.Label.Font.Variant = preferredFont
|
2022-07-18 10:08:12 +02:00
|
|
|
|
2022-07-18 22:36:47 +02:00
|
|
|
p.Title.TextStyle.Font.Size = 14.5
|
2022-07-29 21:04:15 +02:00
|
|
|
p.Title.TextStyle.Font.Variant = titlePreferredFont
|
2022-07-18 10:08:12 +02:00
|
|
|
p.Title.TextStyle.Font.Weight = 2 // SemiBold
|
2022-07-18 22:36:47 +02:00
|
|
|
p.Title.Padding = 3 * vg.Millimeter
|
2022-07-18 10:08:12 +02:00
|
|
|
|
|
|
|
// mark the end of the X axis with len(meanVals).
|
|
|
|
p.X.Max = float64(len(meanVals))
|
|
|
|
p.Y.Min = floats.Min(meanVals)
|
|
|
|
p.Y.Max = floats.Max(meanVals)
|
|
|
|
|
|
|
|
pts := make(plotter.XYs, len(meanVals))
|
|
|
|
|
|
|
|
// fill the plotter with datapoints.
|
|
|
|
for k, res := range meanVals {
|
|
|
|
pts[k].X = float64(k)
|
|
|
|
pts[k].Y = res
|
|
|
|
}
|
|
|
|
|
|
|
|
lines := make([]interface{}, 0)
|
|
|
|
lines = append(lines, pts)
|
|
|
|
|
|
|
|
err := plotutil.AddLines(
|
|
|
|
p,
|
|
|
|
lines...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// violating gocognit 30, TODO(me): still split this up.
|
|
|
|
// nolint: gocognit
|
2022-07-18 22:36:47 +02:00
|
|
|
func plotAllDims(algoStats []stats.Stats, fPrefix, fExt string, ch chan report.PicList, chMean chan report.PicList) {
|
2022-07-08 19:22:09 +02:00
|
|
|
start := time.Now()
|
2022-07-12 16:56:49 +02:00
|
|
|
|
|
|
|
picsDir := report.GetPicsDir()
|
|
|
|
|
2022-07-13 23:47:37 +02:00
|
|
|
// create picsDir (if not exists), fail on error, no point in going on
|
|
|
|
// parsing algoStats and computing images if we cannot save them.
|
2022-07-12 16:56:49 +02:00
|
|
|
if err := util.CreatePath(picsDir); err != nil {
|
2022-07-13 23:47:37 +02:00
|
|
|
log.Fatalln("went to create picsDir, there was an issue: ", err)
|
2022-07-12 16:56:49 +02:00
|
|
|
}
|
|
|
|
|
2022-07-13 23:47:37 +02:00
|
|
|
pL := report.NewPicList()
|
|
|
|
pics := make([]report.Pic, 0)
|
2022-07-18 22:36:47 +02:00
|
|
|
pLMean := report.NewPicList()
|
|
|
|
picsMean := make([]report.Pic, 0)
|
2022-07-13 23:47:37 +02:00
|
|
|
|
|
|
|
// since the algoStats only contains results of a single algo, it's safe to
|
|
|
|
// set the value like this.
|
|
|
|
pL.Algo = algoStats[0].Algo
|
2022-07-18 22:36:47 +02:00
|
|
|
pLMean.Algo = algoStats[0].Algo
|
2022-07-13 23:47:37 +02:00
|
|
|
|
2022-06-20 03:32:48 +02:00
|
|
|
pWidth := 13 * vg.Centimeter
|
|
|
|
pHeight := 13 * vg.Centimeter
|
|
|
|
|
2022-07-29 21:04:15 +02:00
|
|
|
plotter.DefaultFont.Typeface = preferredFont
|
2022-07-08 19:01:21 +02:00
|
|
|
plotter.DefaultLineStyle.Width = vg.Points(1.5)
|
2022-07-08 19:00:11 +02:00
|
|
|
|
2022-07-08 18:16:51 +02:00
|
|
|
for _, s := range algoStats {
|
2022-06-20 03:32:48 +02:00
|
|
|
p := plot.New()
|
2022-07-13 23:47:37 +02:00
|
|
|
pic := report.NewPic()
|
|
|
|
|
2022-07-20 16:24:04 +02:00
|
|
|
p.Title.Text = fmt.Sprintf("D: %d, G: %d, I: %d",
|
|
|
|
s.Dimens,
|
|
|
|
s.Generations,
|
|
|
|
s.Iterations,
|
|
|
|
)
|
2022-07-19 21:25:09 +02:00
|
|
|
// this is latex-rendered.
|
|
|
|
pic.Caption = strings.ReplaceAll(p.Title.Text, " ", "~")
|
2022-07-13 23:47:37 +02:00
|
|
|
|
|
|
|
// since a single stat slice of algoStats only contains results of a
|
|
|
|
// single bench func, it's safe to set the value like this.
|
|
|
|
pL.Bench = s.BenchFuncStats[0].BenchName
|
2022-07-18 22:36:47 +02:00
|
|
|
pLMean.Bench = s.BenchFuncStats[0].BenchName
|
2022-07-13 23:47:37 +02:00
|
|
|
|
2022-07-29 21:04:15 +02:00
|
|
|
p.X.Label.Text = xAxisLabel
|
|
|
|
p.X.Label.TextStyle.Font.Variant = preferredFont
|
2022-07-05 22:56:01 +02:00
|
|
|
p.X.Label.TextStyle.Font.Weight = 1 // Medium
|
2022-07-29 21:04:15 +02:00
|
|
|
p.X.Tick.Label.Font.Variant = preferredFont
|
|
|
|
p.Y.Label.Text = yAxisLabel
|
|
|
|
p.Y.Label.TextStyle.Font.Variant = preferredFont
|
2022-07-05 22:56:01 +02:00
|
|
|
p.Y.Label.TextStyle.Font.Weight = 1 // Medium
|
2022-07-29 21:04:15 +02:00
|
|
|
p.Y.Tick.Label.Font.Variant = preferredFont
|
2022-07-05 22:56:01 +02:00
|
|
|
|
2022-07-18 22:36:47 +02:00
|
|
|
p.Title.TextStyle.Font.Size = 14.5
|
2022-07-29 21:04:15 +02:00
|
|
|
p.Title.TextStyle.Font.Variant = titlePreferredFont
|
2022-07-05 22:56:01 +02:00
|
|
|
p.Title.TextStyle.Font.Weight = 2 // SemiBold
|
2022-07-18 22:36:47 +02:00
|
|
|
p.Title.Padding = 3 * vg.Millimeter
|
2022-06-20 03:32:48 +02:00
|
|
|
|
|
|
|
for _, dim := range s.BenchFuncStats {
|
|
|
|
// infinite thanks to this SO comment for the interface "hack":
|
|
|
|
// https://stackoverflow.com/a/44872993
|
|
|
|
lines := make([]interface{}, 0)
|
|
|
|
|
2022-07-19 19:58:45 +02:00
|
|
|
for _, iter := range dim.BenchResults {
|
2022-06-20 03:32:48 +02:00
|
|
|
// mark the end of the X axis with len(iter.Results).
|
|
|
|
p.X.Max = float64(len(iter.Results))
|
|
|
|
|
2022-07-08 19:04:05 +02:00
|
|
|
if floats.Min(iter.Results) < p.Y.Min {
|
|
|
|
p.Y.Min = floats.Min(iter.Results)
|
|
|
|
}
|
|
|
|
|
|
|
|
if floats.Max(iter.Results) > p.Y.Max {
|
|
|
|
p.Y.Max = floats.Max(iter.Results)
|
|
|
|
}
|
2022-07-08 03:32:07 +02:00
|
|
|
|
2022-06-20 03:32:48 +02:00
|
|
|
pts := make(plotter.XYs, len(iter.Results))
|
|
|
|
|
|
|
|
// fill the plotter with datapoints.
|
|
|
|
for k, res := range iter.Results {
|
|
|
|
pts[k].X = float64(k)
|
|
|
|
pts[k].Y = res
|
|
|
|
}
|
|
|
|
|
2022-07-08 17:17:25 +02:00
|
|
|
lines = append(lines, pts)
|
2022-06-20 03:32:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err := plotutil.AddLines(
|
|
|
|
p,
|
|
|
|
lines...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2022-07-08 22:51:31 +02:00
|
|
|
// panic (don't panic, I know) instead of a hard exit.
|
|
|
|
log.Panic(err)
|
2022-06-20 03:32:48 +02:00
|
|
|
}
|
|
|
|
|
2022-08-05 17:11:39 +02:00
|
|
|
// TODO(me): add Neighbourhood param
|
2022-07-20 16:24:04 +02:00
|
|
|
filename := fmt.Sprintf("%s%s-%s-%s-%dD-%dG-%dI",
|
|
|
|
picsDir,
|
|
|
|
fPrefix,
|
|
|
|
util.SanitiseFName(s.Algo),
|
|
|
|
util.SanitiseFName(dim.BenchName),
|
|
|
|
s.Dimens,
|
|
|
|
s.Generations,
|
|
|
|
len(dim.BenchResults),
|
|
|
|
)
|
2022-07-18 10:08:12 +02:00
|
|
|
filenameMean := filename + util.SanitiseFName(" Mean")
|
|
|
|
|
|
|
|
// NEVER EVER ATTEMPT TO INITIALISE THIS WITH `pic`!
|
|
|
|
picMean := report.NewPic()
|
2022-07-20 16:24:04 +02:00
|
|
|
meanTitle := fmt.Sprintf("D: %d, G: %d, I: %d",
|
|
|
|
s.Dimens,
|
|
|
|
s.Generations,
|
|
|
|
s.Iterations,
|
|
|
|
)
|
2022-07-19 19:23:39 +02:00
|
|
|
|
|
|
|
// this is latex-rendered.
|
|
|
|
picMean.Caption = "Mean - " + strings.ReplaceAll(meanTitle, " ", "~")
|
2022-06-20 03:32:48 +02:00
|
|
|
|
2022-07-13 23:47:37 +02:00
|
|
|
// set pic file path (later used in tmpl generation)
|
|
|
|
pic.FilePath = filename
|
2022-07-18 10:08:12 +02:00
|
|
|
picMean.FilePath = filenameMean
|
|
|
|
|
2022-07-18 22:36:47 +02:00
|
|
|
// get the *mean* plot.
|
2022-07-19 21:56:49 +02:00
|
|
|
pMean := plotMeanVals(dim.MeanVals, meanTitle, s.Generations)
|
2022-07-13 23:47:37 +02:00
|
|
|
|
2022-07-08 19:22:09 +02:00
|
|
|
elapsed := time.Since(start)
|
2022-07-20 16:24:04 +02:00
|
|
|
info := fmt.Sprintf("saving img to file: %s(-Mean)%s [generated in %s]",
|
|
|
|
filename, fExt, elapsed,
|
|
|
|
)
|
2022-07-08 19:20:21 +02:00
|
|
|
|
|
|
|
if s.Algo == "Random Search" {
|
|
|
|
printRandomSearch(info)
|
|
|
|
} else {
|
|
|
|
printSHC(info)
|
|
|
|
}
|
2022-06-20 03:32:48 +02:00
|
|
|
|
|
|
|
// Save the plot to a file using the above-constructed 'filename'.
|
|
|
|
if err := p.Save(
|
|
|
|
pWidth,
|
|
|
|
pHeight,
|
2022-07-11 21:41:05 +02:00
|
|
|
filename+fExt,
|
2022-06-20 03:32:48 +02:00
|
|
|
); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-07-13 23:47:37 +02:00
|
|
|
|
|
|
|
// save pic.
|
|
|
|
pics = append(pics, *pic)
|
2022-07-18 10:08:12 +02:00
|
|
|
|
|
|
|
// save plot of pMean.
|
|
|
|
if err := pMean.Save(
|
|
|
|
pWidth,
|
|
|
|
pHeight,
|
|
|
|
filenameMean+fExt,
|
|
|
|
); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-07-18 22:36:47 +02:00
|
|
|
|
|
|
|
// save mean pic.
|
|
|
|
picsMean = append(picsMean, *picMean)
|
2022-06-20 03:32:48 +02:00
|
|
|
}
|
|
|
|
}
|
2022-07-13 23:47:37 +02:00
|
|
|
|
|
|
|
pL.Pics = pics
|
2022-07-18 22:36:47 +02:00
|
|
|
pLMean.Pics = picsMean
|
2022-07-13 23:47:37 +02:00
|
|
|
|
|
|
|
ch <- *pL
|
2022-07-18 22:36:47 +02:00
|
|
|
chMean <- *pLMean
|
2022-06-20 03:32:48 +02:00
|
|
|
}
|