math-optim/algo/algo.go
surtur 6db4b60875
All checks were successful
continuous-integration/drone/push Build is passing
go(algo): use Dimensions slice from the bench pkg
2022-07-17 11:57:40 +02:00

116 lines
2.5 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
// 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))
for i := range algoStats {
go plotAllDims(algoStats[i], "plot", ".svg", pCh)
}
pLs := []report.PicList{}
for range algoStats {
pL := <-pCh
pLs = append(pLs, pL)
}
// protect access to shared data.
m.Lock()
report.SavePicsToFile(pLs, "Random Search")
stats.SaveTable("Random Search", 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))
for _, algoStat := range algoStats {
go plotAllDims(algoStat, "plot", ".svg", pCh)
}
pLs := []report.PicList{}
for range algoStats {
pL := <-pCh
pLs = append(pLs, pL)
}
// protect access to shared data.
m.Lock()
report.SavePicsToFile(pLs, "Stochastic Hill Climbing")
stats.SaveTable("Stochastic Hill CLimbing", algoStats)
m.Unlock()
}
func newValues() *Values {
var v Values
return &v
}