math-optim/algo/plot.go
surtur 1380cf3cdc
All checks were successful
continuous-integration/drone/push Build is passing
go(plot): remove plot legend for n-iter comparison
2022-07-08 17:17:25 +02:00

100 lines
2.4 KiB
Go

// Copyright 2022 wanderer <a_mirre at utb dot cz>
// SPDX-License-Identifier: GPL-3.0-or-later
package algo
import (
"fmt"
"log"
"git.dotya.ml/wanderer/math-optim/stats"
"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"
func plotAllDims(allStats []stats.Stats, fPrefix, fExt string) {
pWidth := 13 * vg.Centimeter
pHeight := 13 * vg.Centimeter
for _, s := range allStats {
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
p.Y.Label.Text = "CF value"
p.Y.Label.TextStyle.Font.Variant = preferredFontStyle
p.Y.Label.TextStyle.Font.Weight = 1 // Medium
p.Title.TextStyle.Font.Size = 12.5
p.Title.TextStyle.Font.Weight = 2 // SemiBold
p.Title.Padding = 10 * vg.Millimeter
p.Add(plotter.NewGrid())
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))
p.Y.Min = floats.Min(iter.Results)
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 {
log.Fatal(err)
}
if err := createPath(picPath); err != nil {
log.Println(err)
}
filename := picPath +
fPrefix + "-" +
sanitiseFName(s.Algo) + "-" +
sanitiseFName(dim.BenchName) + "-" +
fmt.Sprint(s.Dimens) + "D-" +
fmt.Sprint(s.Generations) + "G-" +
fmt.Sprint(len(dim.Solution)) + "I" +
fExt
printRandomSearch("saving img to file: " + filename)
// Save the plot to a file using the above-constructed 'filename'.
if err := p.Save(
pWidth,
pHeight,
filename,
); err != nil {
panic(err)
}
}
}
}