surtur
2bf3a4a767
All checks were successful
continuous-integration/drone/push Build is passing
* compute and save mean values of x (e.g. 30) bench runs * safely save mean vals for all bench-dimens combinations to AlgoMeans from goroutines (use mutex to protect shared access) * rework algo/plot.go to use already computed mean values. todo: plot mean vals of all algorithms (per benchmark) in a single pic.
133 lines
3.0 KiB
Go
133 lines
3.0 KiB
Go
// Copyright 2022 wanderer <a_mirre at utb dot cz>
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package algo
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"git.dotya.ml/wanderer/math-optim/bench"
|
|
"git.dotya.ml/wanderer/math-optim/report"
|
|
"git.dotya.ml/wanderer/math-optim/stats"
|
|
)
|
|
|
|
// Values type is just a fancy named []float64 that will allow us to define
|
|
// methods over it.
|
|
type Values []float64
|
|
|
|
// mu protects access to meanStats.
|
|
var mu sync.Mutex
|
|
|
|
var meanStats = &stats.MeanStats{}
|
|
|
|
// DoRandomSearch executes a search using the 'Random search' method.
|
|
func DoRandomSearch(wg *sync.WaitGroup, m *sync.Mutex) {
|
|
defer wg.Done()
|
|
|
|
printRandomSearch("starting...")
|
|
|
|
// funcCount is the number of bench functions available.
|
|
funcCount := len(bench.Functions)
|
|
// stats for the current algo (RandomSearch).
|
|
algoStats := make([][]stats.Stats, funcCount)
|
|
// ch serves as a way to get the actual computed output.
|
|
ch := make(chan []stats.Stats, funcCount)
|
|
|
|
for i := range algoStats {
|
|
// ng y'all.
|
|
go RandomSearchNG(10000, 30, bench.Dimensions, bench.FuncNames[i], ch)
|
|
}
|
|
|
|
// get results.
|
|
for i := range algoStats {
|
|
s := <-ch
|
|
|
|
algoStats[i] = s
|
|
}
|
|
|
|
pCh := make(chan report.PicList, funcCount*len(bench.Dimensions))
|
|
pMeanCh := make(chan report.PicList, funcCount*len(bench.Dimensions))
|
|
|
|
for i := range algoStats {
|
|
go plotAllDims(algoStats[i], "plot", ".svg", pCh, pMeanCh)
|
|
}
|
|
|
|
pLs := []report.PicList{}
|
|
pLsMean := []report.PicList{}
|
|
|
|
for range algoStats {
|
|
pL := <-pCh
|
|
pLMean := <-pMeanCh
|
|
|
|
pLs = append(pLs, pL)
|
|
pLsMean = append(pLsMean, pLMean)
|
|
}
|
|
|
|
algoName := "Random Search"
|
|
|
|
// protect access to shared data.
|
|
m.Lock()
|
|
report.SavePicsToFile(pLs, pLsMean, algoName)
|
|
|
|
stats.SaveTable(algoName, algoStats)
|
|
m.Unlock()
|
|
}
|
|
|
|
// DoStochasticHillClimbing performs a search using the 'Stochastic Hill
|
|
// Climbing' method.
|
|
func DoStochasticHillClimbing(wg *sync.WaitGroup, m *sync.Mutex) {
|
|
defer wg.Done()
|
|
|
|
printSHC("starting...")
|
|
|
|
// funcCount is the number of bench functions available.
|
|
funcCount := len(bench.Functions)
|
|
// stats for the current algo (StochasticHillClimber).
|
|
algoStats := make([][]stats.Stats, funcCount)
|
|
// ch serves as a way to get the actual computed output.
|
|
ch := make(chan []stats.Stats, funcCount)
|
|
|
|
for i := range algoStats {
|
|
go HillClimb(10000, 30, bench.Dimensions, bench.FuncNames[i], ch)
|
|
}
|
|
|
|
// get results.
|
|
for i := range algoStats {
|
|
s := <-ch
|
|
|
|
algoStats[i] = s
|
|
}
|
|
|
|
pCh := make(chan report.PicList, funcCount*len(bench.Dimensions))
|
|
pMeanCh := make(chan report.PicList, funcCount*len(bench.Dimensions))
|
|
|
|
for _, algoStat := range algoStats {
|
|
go plotAllDims(algoStat, "plot", ".svg", pCh, pMeanCh)
|
|
}
|
|
|
|
pLs := []report.PicList{}
|
|
pLsMean := []report.PicList{}
|
|
|
|
for range algoStats {
|
|
pL := <-pCh
|
|
pLMean := <-pMeanCh
|
|
|
|
pLs = append(pLs, pL)
|
|
pLsMean = append(pLsMean, pLMean)
|
|
}
|
|
|
|
algoName := "Stochastic Hill Climbing"
|
|
|
|
// protect access to shared data.
|
|
m.Lock()
|
|
report.SavePicsToFile(pLs, pLsMean, algoName)
|
|
|
|
stats.SaveTable(algoName, algoStats)
|
|
m.Unlock()
|
|
}
|
|
|
|
func newValues() *Values {
|
|
var v Values
|
|
return &v
|
|
}
|