math-optim/algo/algo.go

129 lines
3.0 KiB
Go
Raw Normal View History

2022-06-14 22:34:52 +02:00
// Copyright 2022 wanderer <a_mirre at utb dot cz>
// SPDX-License-Identifier: GPL-3.0-or-later
package algo
import (
"sync"
2022-06-24 00:45:43 +02:00
"git.dotya.ml/wanderer/math-optim/bench"
"git.dotya.ml/wanderer/math-optim/report"
2022-06-24 00:45:43 +02:00
"git.dotya.ml/wanderer/math-optim/stats"
2022-06-14 22:34:52 +02:00
)
// mu protects access to meanStats.
var mu sync.Mutex
// mCoMPL protexts access to comparisonOfMeansPicList.
var mCoMPL sync.Mutex
var meanStats = &stats.MeanStats{}
var comparisonOfMeansPicList = &report.PicList{Algo: "Comparison of Means"}
2022-06-14 22:34:52 +02:00
// DoRandomSearch executes a search using the 'Random search' method.
func DoRandomSearch(wg *sync.WaitGroup, m *sync.Mutex) {
2022-06-14 22:34:52 +02:00
defer wg.Done()
printRandomSearch("starting...")
2022-06-24 00:45:43 +02:00
// 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)
2022-06-24 00:45:43 +02:00
for i := range algoStats {
// ng y'all.
go RandomSearchNG(10000, 30, bench.Dimensions, bench.FuncNames[i], ch)
}
2022-07-09 16:19:56 +02:00
// get results.
for i := range algoStats {
s := <-ch
2022-07-09 16:19:56 +02:00
algoStats[i] = s
}
2022-06-24 00:45:43 +02:00
pCh := make(chan report.PicList, funcCount*len(bench.Dimensions))
2022-07-18 22:36:47 +02:00
pMeanCh := make(chan report.PicList, funcCount*len(bench.Dimensions))
2022-06-24 00:45:43 +02:00
for i := range algoStats {
2022-07-18 22:36:47 +02:00
go plotAllDims(algoStats[i], "plot", ".svg", pCh, pMeanCh)
}
2022-07-08 22:51:31 +02:00
pLs := []report.PicList{}
2022-07-18 22:36:47 +02:00
pLsMean := []report.PicList{}
for range algoStats {
pL := <-pCh
2022-07-18 22:36:47 +02:00
pLMean := <-pMeanCh
pLs = append(pLs, pL)
2022-07-18 22:36:47 +02:00
pLsMean = append(pLsMean, pLMean)
2022-06-24 00:45:43 +02:00
}
2022-07-18 00:23:53 +02:00
algoName := "Random Search"
// protect access to shared data.
m.Lock()
2022-07-18 22:36:47 +02:00
report.SavePicsToFile(pLs, pLsMean, algoName)
2022-07-18 00:23:53 +02:00
stats.SaveTable(algoName, algoStats)
m.Unlock()
2022-06-14 22:34:52 +02:00
}
// DoStochasticHillClimbing performs a search using the 'Stochastic Hill
// Climbing' method.
func DoStochasticHillClimbing(wg *sync.WaitGroup, m *sync.Mutex) {
2022-06-14 22:34:52 +02:00
defer wg.Done()
printSHC("starting...")
2022-07-08 21:40:04 +02:00
// 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)
2022-07-08 21:40:04 +02:00
for i := range algoStats {
go HillClimb(10000, 30, bench.Dimensions, bench.FuncNames[i], ch)
}
2022-07-09 16:19:56 +02:00
// get results.
for i := range algoStats {
s := <-ch
2022-07-09 16:19:56 +02:00
algoStats[i] = s
2022-07-08 21:40:04 +02:00
}
pCh := make(chan report.PicList, funcCount*len(bench.Dimensions))
2022-07-18 22:36:47 +02:00
pMeanCh := make(chan report.PicList, funcCount*len(bench.Dimensions))
2022-07-08 21:40:04 +02:00
for _, algoStat := range algoStats {
2022-07-18 22:36:47 +02:00
go plotAllDims(algoStat, "plot", ".svg", pCh, pMeanCh)
}
2022-07-08 22:51:31 +02:00
pLs := []report.PicList{}
2022-07-18 22:36:47 +02:00
pLsMean := []report.PicList{}
for range algoStats {
pL := <-pCh
2022-07-18 22:36:47 +02:00
pLMean := <-pMeanCh
pLs = append(pLs, pL)
2022-07-18 22:36:47 +02:00
pLsMean = append(pLsMean, pLMean)
2022-07-08 21:40:04 +02:00
}
2022-07-18 00:23:53 +02:00
algoName := "Stochastic Hill Climbing"
// protect access to shared data.
m.Lock()
2022-07-18 22:36:47 +02:00
report.SavePicsToFile(pLs, pLsMean, algoName)
2022-07-18 00:23:53 +02:00
stats.SaveTable(algoName, algoStats)
m.Unlock()
2022-06-14 22:34:52 +02:00
}