diff --git a/algo/algo.go b/algo/algo.go index 1afe11f..24259a1 100644 --- a/algo/algo.go +++ b/algo/algo.go @@ -16,6 +16,8 @@ type Values []float64 var picPath = "res/" +var plotWg sync.WaitGroup + // DoRandomSearch executes a search using the 'Random search' method. func DoRandomSearch(wg *sync.WaitGroup) { defer wg.Done() @@ -33,10 +35,14 @@ func DoRandomSearch(wg *sync.WaitGroup) { } for i := range algoStats { - plotAllDims(algoStats[i], "plot", ".svg") + plotWg.Add(1) + + go plotAllDims(algoStats[i], "plot", ".svg", &plotWg) } stats.PrintStatisticTable(algoStats) + + plotWg.Wait() } // DoStochasticHillClimbing performs a search using the 'Stochastic Hill @@ -56,10 +62,14 @@ func DoStochasticHillClimbing(wg *sync.WaitGroup) { } for _, algoStat := range algoStats { - plotAllDims(algoStat, "plot", ".svg") + plotWg.Add(1) + + go plotAllDims(algoStat, "plot", ".svg", &plotWg) } stats.PrintStatisticTable(algoStats) + + plotWg.Wait() } func newValues() *Values { diff --git a/algo/plot.go b/algo/plot.go index 3836f3c..5a2e87a 100644 --- a/algo/plot.go +++ b/algo/plot.go @@ -6,6 +6,7 @@ package algo import ( "fmt" "log" + "sync" "time" "git.dotya.ml/wanderer/math-optim/stats" @@ -20,7 +21,9 @@ const preferredFontStyle = "Mono" // violating the limit of 30, TODO(me): split this up. // 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() pWidth := 13 * vg.Centimeter pHeight := 13 * vg.Centimeter @@ -81,7 +84,8 @@ func plotAllDims(algoStats []stats.Stats, fPrefix, fExt string) { lines..., ) 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 {