go(plot): enable concurrent plotting
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2022-07-08 22:51:31 +02:00
parent bf08d22549
commit 1c5dfa9818
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
2 changed files with 18 additions and 4 deletions

@ -16,6 +16,8 @@ type Values []float64
var picPath = "res/" var picPath = "res/"
var plotWg sync.WaitGroup
// DoRandomSearch executes a search using the 'Random search' method. // DoRandomSearch executes a search using the 'Random search' method.
func DoRandomSearch(wg *sync.WaitGroup) { func DoRandomSearch(wg *sync.WaitGroup) {
defer wg.Done() defer wg.Done()
@ -33,10 +35,14 @@ func DoRandomSearch(wg *sync.WaitGroup) {
} }
for i := range algoStats { for i := range algoStats {
plotAllDims(algoStats[i], "plot", ".svg") plotWg.Add(1)
go plotAllDims(algoStats[i], "plot", ".svg", &plotWg)
} }
stats.PrintStatisticTable(algoStats) stats.PrintStatisticTable(algoStats)
plotWg.Wait()
} }
// DoStochasticHillClimbing performs a search using the 'Stochastic Hill // DoStochasticHillClimbing performs a search using the 'Stochastic Hill
@ -56,10 +62,14 @@ func DoStochasticHillClimbing(wg *sync.WaitGroup) {
} }
for _, algoStat := range algoStats { for _, algoStat := range algoStats {
plotAllDims(algoStat, "plot", ".svg") plotWg.Add(1)
go plotAllDims(algoStat, "plot", ".svg", &plotWg)
} }
stats.PrintStatisticTable(algoStats) stats.PrintStatisticTable(algoStats)
plotWg.Wait()
} }
func newValues() *Values { func newValues() *Values {

@ -6,6 +6,7 @@ package algo
import ( import (
"fmt" "fmt"
"log" "log"
"sync"
"time" "time"
"git.dotya.ml/wanderer/math-optim/stats" "git.dotya.ml/wanderer/math-optim/stats"
@ -20,7 +21,9 @@ const preferredFontStyle = "Mono"
// violating the limit of 30, TODO(me): split this up. // violating the limit of 30, TODO(me): split this up.
// nolint: gocognit // nolint: gocognit
func plotAllDims(algoStats []stats.Stats, fPrefix, fExt string) { 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() start := time.Now()
pWidth := 13 * vg.Centimeter pWidth := 13 * vg.Centimeter
pHeight := 13 * vg.Centimeter pHeight := 13 * vg.Centimeter
@ -81,7 +84,8 @@ func plotAllDims(algoStats []stats.Stats, fPrefix, fExt string) {
lines..., lines...,
) )
if err != nil { if err != nil {
log.Fatal(err) // panic (don't panic, I know) instead of a hard exit.
log.Panic(err)
} }
if err := createPath(picPath); err != nil { if err := createPath(picPath); err != nil {