math-optim/algo/plot.go
surtur c0d8f8e6e8
All checks were successful
continuous-integration/drone/push Build is passing
go(algo): rework the plotting func, add util.go
2022-06-20 03:32:48 +02:00

84 lines
1.9 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/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/plotutil"
"gonum.org/v1/plot/vg"
)
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.Y.Label.Text = "CF value"
p.Legend.Top = true
p.Legend.Padding = 0 * vg.Centimeter
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 j, iter := range dim.Solution {
// mark the end of the X axis with len(iter.Results).
p.X.Max = float64(len(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, "#"+fmt.Sprint(j), pts)
}
err := plotutil.AddLines(
p,
lines...,
)
if err != nil {
log.Fatal(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)
}
}
}
}