math-optim/algo/plot.go
surtur 8f50a1ea51
All checks were successful
continuous-integration/drone/push Build is passing
go(plot): use createPath instead of createFolder
..as it's more universal, should we start/allow passing more complex
paths than "./res-folder".. should e.g. "./res/subpath" be passed to the
former func, it would error out more easily, as the subpath would likely
be non-existent, akin to `mkdir <path>` vs `mkdir -p <path>`.
2022-06-29 17:04:45 +02:00

88 lines
2.0 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)
}
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)
}
}
}
}