math-optim/algo/plot.go

129 lines
3.2 KiB
Go
Raw Normal View History

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
import (
"fmt"
"log"
2022-07-08 22:51:31 +02:00
"sync"
"time"
"git.dotya.ml/wanderer/math-optim/report"
"git.dotya.ml/wanderer/math-optim/stats"
"git.dotya.ml/wanderer/math-optim/util"
"gonum.org/v1/gonum/floats"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/plotutil"
"gonum.org/v1/plot/vg"
)
const preferredFontStyle = "Mono"
// violating the limit of 30, TODO(me): split this up.
// nolint: gocognit
2022-07-08 22:51:31 +02:00
func plotAllDims(algoStats []stats.Stats, fPrefix, fExt string, wg *sync.WaitGroup) {
defer wg.Done() // does this work with panic in the mix? TODO(me): find out.
start := time.Now()
picsDir := report.GetPicsDir()
if err := util.CreatePath(picsDir); err != nil {
log.Println(err)
}
pWidth := 13 * vg.Centimeter
pHeight := 13 * vg.Centimeter
plotter.DefaultFont.Typeface = preferredFontStyle
plotter.DefaultLineStyle.Width = vg.Points(1.5)
2022-07-08 18:16:51 +02:00
for _, s := range algoStats {
p := plot.New()
p.Title.Text = s.Algo + ", D=" + fmt.Sprint(s.Dimens) +
", func=" + s.BenchFuncStats[0].BenchName +
", G=" + fmt.Sprint(s.Generations) +
", I=" + fmt.Sprint(s.Iterations)
p.X.Label.Text = "Generations"
p.X.Label.TextStyle.Font.Variant = preferredFontStyle
p.X.Label.TextStyle.Font.Weight = 1 // Medium
2022-07-08 17:55:15 +02:00
p.X.Tick.Label.Font.Variant = preferredFontStyle
p.Y.Label.Text = "CF value"
p.Y.Label.TextStyle.Font.Variant = preferredFontStyle
p.Y.Label.TextStyle.Font.Weight = 1 // Medium
2022-07-08 17:55:15 +02:00
p.Y.Tick.Label.Font.Variant = preferredFontStyle
2022-07-08 17:50:57 +02:00
p.Title.TextStyle.Font.Size = 11.5
p.Title.TextStyle.Font.Variant = "Sans"
p.Title.TextStyle.Font.Weight = 2 // SemiBold
2022-07-08 17:50:57 +02:00
p.Title.Padding = 5 * vg.Millimeter
for _, dim := range s.BenchFuncStats {
// infinite thanks to this SO comment for the interface "hack":
// https://stackoverflow.com/a/44872993
lines := make([]interface{}, 0)
for _, iter := range dim.Solution {
// mark the end of the X axis with len(iter.Results).
p.X.Max = float64(len(iter.Results))
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)
}
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
}
lines = append(lines, pts)
}
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)
}
filename := picsDir +
fPrefix + "-" +
util.SanitiseFName(s.Algo) + "-" +
util.SanitiseFName(dim.BenchName) + "-" +
fmt.Sprint(s.Dimens) + "D-" +
fmt.Sprint(s.Generations) + "G-" +
fmt.Sprint(len(dim.Solution)) + "I"
elapsed := time.Since(start)
info := "saving img to file: " + filename + fExt +
" [generated in " + fmt.Sprint(elapsed) + "]"
if s.Algo == "Random Search" {
printRandomSearch(info)
} else {
printSHC(info)
}
// Save the plot to a file using the above-constructed 'filename'.
if err := p.Save(
pWidth,
pHeight,
filename+fExt,
); err != nil {
panic(err)
}
}
}
}