95 lines
2.0 KiB
Go
95 lines
2.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/stats"
|
|
)
|
|
|
|
// Values type is just a fancy named []float64 that will allow us to define
|
|
// methods over it.
|
|
type Values []float64
|
|
|
|
var plotWg sync.WaitGroup
|
|
|
|
// DoRandomSearch executes a search using the 'Random search' method.
|
|
func DoRandomSearch(wg *sync.WaitGroup) {
|
|
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, []int{5, 10, 20}, bench.FuncNames[i], ch)
|
|
}
|
|
|
|
// get results.
|
|
for i := range algoStats {
|
|
s := <-ch
|
|
|
|
algoStats[i] = s
|
|
}
|
|
|
|
for i := range algoStats {
|
|
plotWg.Add(1)
|
|
|
|
go plotAllDims(algoStats[i], "plot", ".svg", &plotWg)
|
|
}
|
|
|
|
stats.PrintStatisticTable(algoStats)
|
|
|
|
plotWg.Wait()
|
|
}
|
|
|
|
// DoStochasticHillClimbing performs a search using the 'Stochastic Hill
|
|
// Climbing' method.
|
|
func DoStochasticHillClimbing(wg *sync.WaitGroup) {
|
|
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, []int{5, 10, 20}, bench.FuncNames[i], ch)
|
|
}
|
|
|
|
// get results.
|
|
for i := range algoStats {
|
|
s := <-ch
|
|
|
|
algoStats[i] = s
|
|
}
|
|
|
|
for _, algoStat := range algoStats {
|
|
plotWg.Add(1)
|
|
|
|
go plotAllDims(algoStat, "plot", ".svg", &plotWg)
|
|
}
|
|
|
|
stats.PrintStatisticTable(algoStats)
|
|
|
|
plotWg.Wait()
|
|
}
|
|
|
|
func newValues() *Values {
|
|
var v Values
|
|
return &v
|
|
}
|