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 (
|
|
|
|
"fmt"
|
2022-06-17 01:54:30 +02:00
|
|
|
"log"
|
2022-06-14 22:34:52 +02:00
|
|
|
"os"
|
2022-07-20 17:16:44 +02:00
|
|
|
"sort"
|
2022-07-05 21:46:09 +02:00
|
|
|
"time"
|
2022-06-17 01:54:30 +02:00
|
|
|
|
|
|
|
"git.dotya.ml/wanderer/math-optim/bench"
|
2022-06-18 05:27:10 +02:00
|
|
|
"git.dotya.ml/wanderer/math-optim/stats"
|
2022-07-05 21:46:09 +02:00
|
|
|
"golang.org/x/exp/rand"
|
2022-06-17 01:54:30 +02:00
|
|
|
"gonum.org/v1/gonum/stat/distuv"
|
2022-06-14 22:34:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func getRandomSearchLogPrefix() string {
|
|
|
|
return " *** random search:"
|
|
|
|
}
|
|
|
|
|
|
|
|
func fmtRandomSearchOut(input string) string {
|
|
|
|
return getRandomSearchLogPrefix() + " " + input
|
|
|
|
}
|
|
|
|
|
2022-06-17 19:55:23 +02:00
|
|
|
func printRandomSearch(input string) {
|
|
|
|
if _, err := fmt.Fprintln(os.Stderr, fmtRandomSearchOut(input)); err != nil {
|
|
|
|
fmt.Fprintf(
|
|
|
|
os.Stdout,
|
|
|
|
getRandomSearchLogPrefix(),
|
|
|
|
"error while printing to stderr: %q\n * original message was: %q",
|
|
|
|
err, input,
|
|
|
|
)
|
|
|
|
}
|
2022-06-14 22:34:52 +02:00
|
|
|
}
|
2022-06-17 01:54:30 +02:00
|
|
|
|
|
|
|
func genValsRandomSearch(dimens uint, vals []float64, uniform *distuv.Uniform) {
|
|
|
|
for i := uint(0); i < dimens; i++ {
|
|
|
|
// using Uniform.Rand from gonum's stat/distuv package.
|
|
|
|
// https://pkg.go.dev/gonum.org/v1/gonum/stat/distuv#Uniform.Rand
|
|
|
|
// boundaries are already set at this point.
|
|
|
|
vals[i] = uniform.Rand()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// singleRandomSearch performs a single iteration of the 'RandomSearch' algorithm.
|
|
|
|
// it takes a couple of arguments:
|
|
|
|
// * dimens uint: number of dimensions of the objective function
|
2022-06-18 20:40:27 +02:00
|
|
|
// * f func([]float64) float64: bench func to execute (see Functions map in
|
|
|
|
// bench/functions.go)
|
2022-07-07 17:55:47 +02:00
|
|
|
// * uniformDist distuv.Uniform: uniform distribution representation with
|
|
|
|
// the min/max bounds already set to function-specific limits.
|
|
|
|
func singleRandomSearch(dimens uint, f func([]float64) float64, uniformDist distuv.Uniform) ([]float64, float64) {
|
2022-06-17 22:14:56 +02:00
|
|
|
vals := make([]float64, dimens)
|
2022-06-17 01:54:30 +02:00
|
|
|
|
|
|
|
genValsRandomSearch(dimens, vals, &uniformDist)
|
|
|
|
|
2022-06-18 20:40:27 +02:00
|
|
|
// result of the bench function.
|
|
|
|
res := f(vals)
|
2022-06-17 01:54:30 +02:00
|
|
|
|
|
|
|
return vals, res
|
|
|
|
}
|
|
|
|
|
2022-07-09 16:19:56 +02:00
|
|
|
func RandomSearchNG(maxFES, benchMinIters int, theD []int, benchFunc string, ch chan []stats.Stats) {
|
2022-06-20 03:04:59 +02:00
|
|
|
// perform basic sanity checks.
|
|
|
|
if maxFES <= 0 {
|
|
|
|
log.Fatalln(fmtRandomSearchOut("maxFES cannot be <= 0, bailing"))
|
|
|
|
} else if benchMinIters <= 0 {
|
|
|
|
log.Fatalln(fmtRandomSearchOut("benchMinIters cannot be <= 0, bailing"))
|
|
|
|
} else if _, ok := bench.Functions[benchFunc]; !ok {
|
|
|
|
log.Fatalln(fmtRandomSearchOut(
|
|
|
|
"unknown benchFunc used: '" + benchFunc + "', bailing",
|
|
|
|
))
|
2022-06-17 01:54:30 +02:00
|
|
|
}
|
|
|
|
|
2022-06-20 03:04:59 +02:00
|
|
|
for i := range theD {
|
|
|
|
if theD[i] <= 0 {
|
|
|
|
log.Fatalln(fmtRandomSearchOut(" no dimension in D can be <= 0, bailing"))
|
|
|
|
}
|
|
|
|
}
|
2022-06-17 01:54:30 +02:00
|
|
|
|
2022-06-20 03:04:59 +02:00
|
|
|
// use func-local vars.
|
|
|
|
var (
|
|
|
|
fes int
|
|
|
|
localD []int
|
|
|
|
minIters int
|
|
|
|
randomSearchStats []stats.Stats
|
|
|
|
)
|
|
|
|
|
|
|
|
fes = maxFES
|
|
|
|
localD = theD
|
|
|
|
minIters = benchMinIters
|
|
|
|
|
2022-07-07 17:55:47 +02:00
|
|
|
// create a continuous uniform distribution representation.
|
|
|
|
uniformDist := &distuv.Uniform{
|
|
|
|
Src: rand.NewSource(uint64(
|
|
|
|
time.Now().
|
|
|
|
UnixNano(),
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
|
2022-07-25 22:24:54 +02:00
|
|
|
rsMeans := &stats.AlgoBenchMean{
|
|
|
|
Algo: "Random Search",
|
|
|
|
BenchMeans: make([]stats.BenchMean, 0, len(localD)),
|
|
|
|
}
|
2022-07-19 21:56:49 +02:00
|
|
|
|
2022-06-20 03:04:59 +02:00
|
|
|
// iterate over whatever was passed to us with theD - dimens slice.
|
|
|
|
for _, dimens := range localD {
|
|
|
|
randomSearchStatDimX := &stats.Stats{
|
2022-06-18 05:27:10 +02:00
|
|
|
Algo: "Random Search",
|
2022-06-20 03:04:59 +02:00
|
|
|
Dimens: dimens,
|
2022-06-18 05:27:10 +02:00
|
|
|
Iterations: minIters,
|
2022-06-20 03:04:59 +02:00
|
|
|
Generations: fes,
|
2022-06-18 05:27:10 +02:00
|
|
|
}
|
2022-06-20 03:04:59 +02:00
|
|
|
funcStats := &stats.FuncStats{BenchName: benchFunc}
|
|
|
|
benchFuncParams := bench.FunctionParams[benchFunc]
|
2022-07-25 22:24:54 +02:00
|
|
|
dimXMean := &stats.BenchMean{
|
2022-07-19 21:56:49 +02:00
|
|
|
Bench: benchFunc,
|
|
|
|
Dimens: dimens,
|
|
|
|
Iterations: minIters,
|
|
|
|
Generations: fes,
|
|
|
|
// not applicable to Random Search...
|
|
|
|
Neighbours: -1,
|
|
|
|
}
|
2022-06-18 05:27:10 +02:00
|
|
|
|
2022-07-07 17:55:47 +02:00
|
|
|
// set min/max bounds.
|
|
|
|
uniformDist.Min = benchFuncParams.Min()
|
|
|
|
uniformDist.Max = benchFuncParams.Max()
|
|
|
|
|
2022-06-25 21:55:36 +02:00
|
|
|
printRandomSearch("running bench \"" + benchFunc + "\" for " +
|
|
|
|
fmt.Sprint(randomSearchStatDimX.Dimens) + "D")
|
|
|
|
|
2022-07-19 19:58:45 +02:00
|
|
|
funcStats.BenchResults = make([]stats.BenchRound, minIters)
|
2022-06-18 05:27:10 +02:00
|
|
|
|
2022-06-20 03:04:59 +02:00
|
|
|
// perform the while dance 'minIters' times for "statistical relevance"
|
2022-06-18 00:08:10 +02:00
|
|
|
for iter := 0; iter < minIters; iter++ {
|
2022-06-20 03:04:59 +02:00
|
|
|
// have a fresh bestResult for each of the 'minIters' runs.
|
2022-06-17 22:33:37 +02:00
|
|
|
var bestResult float64
|
|
|
|
|
2022-06-20 03:04:59 +02:00
|
|
|
// set current iteration in funcStats.
|
2022-07-19 19:58:45 +02:00
|
|
|
funcStats.BenchResults[iter].Iteration = iter
|
2022-06-17 22:33:37 +02:00
|
|
|
|
2022-06-20 03:04:59 +02:00
|
|
|
// run the benchmarking function 'fes' times.
|
|
|
|
for i := 0; i < fes; i++ {
|
|
|
|
_, r := singleRandomSearch(
|
|
|
|
uint(dimens),
|
|
|
|
bench.Functions[benchFunc],
|
2022-07-07 17:55:47 +02:00
|
|
|
*uniformDist,
|
2022-06-18 05:27:10 +02:00
|
|
|
)
|
|
|
|
|
2022-06-20 03:04:59 +02:00
|
|
|
// is a switch efficient, or should an if statement be used..?
|
|
|
|
// TODO(me): perhaps benchmark this...
|
|
|
|
switch i {
|
2022-06-17 22:33:37 +02:00
|
|
|
case 0:
|
2022-06-20 03:04:59 +02:00
|
|
|
// if it's our first, bestResult is the result we just got.
|
2022-06-17 22:33:37 +02:00
|
|
|
bestResult = r
|
|
|
|
default:
|
2022-06-20 03:04:59 +02:00
|
|
|
// since we're minimising, the lower 'r' is the better.
|
2022-06-17 22:33:37 +02:00
|
|
|
if r < bestResult {
|
|
|
|
bestResult = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 03:04:59 +02:00
|
|
|
// save the 'best' result, since we only care about those.
|
2022-07-19 19:58:45 +02:00
|
|
|
funcStats.BenchResults[iter].Results = append(
|
|
|
|
funcStats.BenchResults[iter].Results,
|
2022-06-20 03:04:59 +02:00
|
|
|
bestResult,
|
2022-06-18 05:27:10 +02:00
|
|
|
)
|
2022-06-17 22:33:37 +02:00
|
|
|
}
|
2022-06-20 03:04:59 +02:00
|
|
|
}
|
2022-06-17 22:33:37 +02:00
|
|
|
|
2022-07-19 21:56:49 +02:00
|
|
|
// get mean vals.
|
|
|
|
dimXMean.MeanVals = stats.GetMeanVals(funcStats.BenchResults, fes)
|
|
|
|
// save to funcStats, too.
|
|
|
|
funcStats.MeanVals = dimXMean.MeanVals
|
|
|
|
|
2022-06-20 03:04:59 +02:00
|
|
|
// save cumulative results of 'minIters' runs.
|
|
|
|
randomSearchStatDimX.BenchFuncStats = append(
|
|
|
|
randomSearchStatDimX.BenchFuncStats,
|
|
|
|
*funcStats,
|
|
|
|
)
|
2022-06-18 05:27:10 +02:00
|
|
|
|
2022-06-20 03:04:59 +02:00
|
|
|
// save stats for each dimension to a stats slice.
|
|
|
|
randomSearchStats = append(randomSearchStats, *randomSearchStatDimX)
|
2022-07-19 21:56:49 +02:00
|
|
|
|
|
|
|
// save to AlgoMeans
|
2022-07-25 22:24:54 +02:00
|
|
|
rsMeans.BenchMeans = append(rsMeans.BenchMeans, *dimXMean)
|
2022-06-17 01:54:30 +02:00
|
|
|
}
|
2022-06-20 03:04:59 +02:00
|
|
|
|
2022-07-20 17:16:44 +02:00
|
|
|
sort.Sort(rsMeans)
|
|
|
|
|
2022-07-19 21:56:49 +02:00
|
|
|
// export AlgoMeans.
|
|
|
|
mu.Lock()
|
|
|
|
meanStats.AlgoMeans = append(meanStats.AlgoMeans, *rsMeans)
|
|
|
|
mu.Unlock()
|
|
|
|
|
2022-07-09 16:19:56 +02:00
|
|
|
ch <- randomSearchStats
|
2022-06-17 01:54:30 +02:00
|
|
|
}
|