math-optim/algo/algo.go

95 lines
2.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/stats"
2022-06-14 22:34:52 +02:00
)
2022-06-15 23:29:28 +02:00
// Values type is just a fancy named []float64 that will allow us to define
// methods over it.
type Values []float64
2022-07-08 22:51:31 +02:00
var plotWg sync.WaitGroup
2022-06-14 22:34:52 +02:00
// DoRandomSearch executes a search using the 'Random search' method.
func DoRandomSearch(wg *sync.WaitGroup) {
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.
2022-07-09 16:19:56 +02:00
go RandomSearchNG(10000, 30, []int{5, 10, 20}, 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
for i := range algoStats {
2022-07-08 22:51:31 +02:00
plotWg.Add(1)
go plotAllDims(algoStats[i], "plot", ".svg", &plotWg)
2022-06-24 00:45:43 +02:00
}
2022-06-24 00:45:43 +02:00
stats.PrintStatisticTable(algoStats)
2022-07-08 22:51:31 +02:00
plotWg.Wait()
2022-06-14 22:34:52 +02:00
}
// DoStochasticHillClimbing performs a search using the 'Stochastic Hill
// Climbing' method.
func DoStochasticHillClimbing(wg *sync.WaitGroup) {
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 {
2022-07-09 16:19:56 +02:00
go HillClimb(10000, 30, []int{5, 10, 20}, 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
}
for _, algoStat := range algoStats {
2022-07-08 22:51:31 +02:00
plotWg.Add(1)
go plotAllDims(algoStat, "plot", ".svg", &plotWg)
2022-07-08 21:40:04 +02:00
}
stats.PrintStatisticTable(algoStats)
2022-07-08 22:51:31 +02:00
plotWg.Wait()
2022-06-14 22:34:52 +02:00
}
2022-06-19 19:44:18 +02:00
func newValues() *Values {
var v Values
return &v
}