2023-01-12 23:35:51 +01:00
|
|
|
// Copyright 2023 wanderer <a_mirre at utb dot cz>
|
2022-06-14 22:34:52 +02:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
package algo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-07-08 19:57:44 +02:00
|
|
|
"log"
|
|
|
|
"math"
|
2022-06-14 22:34:52 +02:00
|
|
|
"os"
|
2022-07-20 17:16:44 +02:00
|
|
|
"sort"
|
2022-07-08 19:57:44 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.dotya.ml/wanderer/math-optim/bench"
|
|
|
|
"git.dotya.ml/wanderer/math-optim/stats"
|
|
|
|
"golang.org/x/exp/rand"
|
|
|
|
"gonum.org/v1/gonum/stat/distuv"
|
2022-06-14 22:34:52 +02:00
|
|
|
)
|
|
|
|
|
2022-08-21 14:26:58 +02:00
|
|
|
// neighbour is an alias for a float64 slice used for more natural handling of
|
|
|
|
// the underlying data.
|
|
|
|
type neighbour []float64
|
|
|
|
|
2022-06-14 22:34:52 +02:00
|
|
|
func getSHCLogPrefix() string {
|
|
|
|
return " *** stochastic hill climbing:"
|
|
|
|
}
|
|
|
|
|
|
|
|
func fmtSHCOut(input string) string {
|
|
|
|
return getSHCLogPrefix() + " " + input
|
|
|
|
}
|
|
|
|
|
2022-06-17 19:55:23 +02:00
|
|
|
func printSHC(input string) {
|
|
|
|
if _, err := fmt.Fprintln(os.Stderr, fmtSHCOut(input)); err != nil {
|
|
|
|
fmt.Fprintf(
|
|
|
|
os.Stdout,
|
|
|
|
getSHCLogPrefix(),
|
|
|
|
"error while printing to stderr: %q\n * original message was: %q",
|
|
|
|
err, input,
|
|
|
|
)
|
|
|
|
}
|
2022-06-14 22:34:52 +02:00
|
|
|
}
|
2022-07-08 19:57:44 +02:00
|
|
|
|
|
|
|
func initValsSHC(dimens uint, vals []float64, uniform *distuv.Uniform) {
|
|
|
|
for i := uint(0); i < dimens; i++ {
|
|
|
|
vals[i] = uniform.Rand()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// selectBestNeighbour evaluates the randomly generated neighbours passed to it
|
|
|
|
// as a parameter and points to the best one (the one yielding the greatest
|
|
|
|
// progress in a favourable way).
|
2022-08-21 14:26:58 +02:00
|
|
|
func selectBestNeighbour(neighbours []neighbour, benchFuncName string) ([]float64, float64) {
|
2022-07-08 19:57:44 +02:00
|
|
|
// bestNeighbour is selected based on the value of the bench function it yields.
|
|
|
|
var bestNeighbour []float64
|
|
|
|
|
|
|
|
// bestSolution is used to store the best bench func name value.
|
|
|
|
var bestSolution float64
|
|
|
|
|
|
|
|
// f is the actual bench function, based on the name.
|
|
|
|
f := bench.Functions[benchFuncName]
|
|
|
|
|
2022-12-24 11:30:22 +01:00
|
|
|
// fmt.Println("select best\nlen(neighbours)", len(neighbours))
|
|
|
|
|
2022-07-08 19:57:44 +02:00
|
|
|
for i, v := range neighbours {
|
|
|
|
switch i {
|
|
|
|
case 0:
|
|
|
|
// the first one is automatically the best.
|
|
|
|
bestSolution = f(v)
|
|
|
|
bestNeighbour = v
|
|
|
|
default:
|
|
|
|
// calculate the value of the bench function for neighbour and
|
|
|
|
// decide if this was the best we've seen so far or not.
|
|
|
|
if solution := f(v); solution < bestSolution {
|
|
|
|
bestSolution = solution
|
|
|
|
bestNeighbour = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bestNeighbour, bestSolution
|
|
|
|
}
|
|
|
|
|
|
|
|
// getBenchSearchSpaceSize calculates and returns the absolute distance between
|
|
|
|
// min,max for a given bench func.
|
|
|
|
func getBenchSearchSpaceSize(benchName string) float64 {
|
|
|
|
params := bench.FunctionParams[benchName]
|
|
|
|
|
|
|
|
return math.Abs(params.Min() - params.Max())
|
|
|
|
}
|
|
|
|
|
|
|
|
// getAllowedTweak calculates the value of allowed movement correction based on
|
|
|
|
// the bench func's searchSpaceSize, also taking into account the globally
|
|
|
|
// applicable MaxNeighbourVariance (in %) value from the bench pkg.
|
|
|
|
// the allowedTweak value should therefore the amount to at most 10% of the
|
|
|
|
// searchSpaceSize. TODO(me): floor this down.
|
|
|
|
func getAllowedTweak(searchSpaceSize float64) float64 {
|
2022-12-24 11:30:22 +01:00
|
|
|
// TODO(me): have this passed to HillClimb and from there into this func.
|
|
|
|
// return (bench.MaxNeighbourVariancePercent * 0.5) * (searchSpaceSize * 0.01)
|
2022-07-08 19:57:44 +02:00
|
|
|
return bench.MaxNeighbourVariancePercent * (searchSpaceSize * 0.01)
|
|
|
|
}
|
|
|
|
|
2022-08-21 14:26:58 +02:00
|
|
|
func genNeighbours(n, dimens int, benchName string, origin []float64, neighbVals []neighbour) {
|
2022-07-08 19:57:44 +02:00
|
|
|
if dimens < 1 {
|
|
|
|
log.Fatalf("error: neighbour count set to: %d; want len(neighbVals) > 0, bailing\n", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a new representation of the uniform distribution with the bounds
|
|
|
|
// unset for the moment, since we need to find out what exactly those ought
|
|
|
|
// to be (and we will - a couple of lines later).
|
2022-12-24 11:30:22 +01:00
|
|
|
// uniform := distuv.Uniform{Src: time.Now().UnixMicro()}
|
2022-07-08 19:57:44 +02:00
|
|
|
uniform := distuv.Uniform{}
|
2022-12-24 11:30:22 +01:00
|
|
|
// uniform.Src.Seed(uint64(time.Now().UnixNano()))
|
2022-07-08 19:57:44 +02:00
|
|
|
params := bench.FunctionParams[benchName]
|
|
|
|
// get bench function bounds.
|
|
|
|
benchMin := params.Min()
|
|
|
|
benchMax := params.Max()
|
|
|
|
searchSpaceSize := getBenchSearchSpaceSize(benchName)
|
|
|
|
// allowedTweak tells how big of a tweak to each side is allowed.
|
|
|
|
allowedTweak := getAllowedTweak(searchSpaceSize)
|
|
|
|
|
|
|
|
// seed the src.
|
|
|
|
uniform.Src = rand.NewSource(uint64(time.Now().UnixNano()))
|
|
|
|
|
|
|
|
for _, v := range neighbVals {
|
2022-12-24 11:30:22 +01:00
|
|
|
// for _, v := range neighbVals {
|
2022-07-08 19:57:44 +02:00
|
|
|
for i := 0; i < dimens; i++ {
|
|
|
|
newMin := origin[i] - allowedTweak
|
|
|
|
|
|
|
|
// if newMin is not within the bounds, fall back to benchMin.
|
|
|
|
if newMin < benchMin {
|
|
|
|
uniform.Min = newMin
|
|
|
|
}
|
|
|
|
|
|
|
|
// if newMax is not within the bounds, fall back to benchMax.
|
|
|
|
newMax := origin[i] + allowedTweak
|
|
|
|
|
|
|
|
if newMin > benchMax {
|
|
|
|
uniform.Max = newMax
|
|
|
|
}
|
2022-12-24 11:30:22 +01:00
|
|
|
// fmt.Println("newMin:", newMin, "newMax", newMax)
|
2022-07-08 19:57:44 +02:00
|
|
|
|
2022-12-24 11:30:22 +01:00
|
|
|
// v = append(v, uniform.Rand())
|
2022-07-08 19:57:44 +02:00
|
|
|
v[i] = uniform.Rand()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// singleHillClimb runs a single iteration of SHC.
|
2022-08-05 17:11:39 +02:00
|
|
|
func singleHillClimb(
|
|
|
|
dimens uint,
|
|
|
|
neighbCount int,
|
|
|
|
benchName string,
|
|
|
|
oldVals []float64,
|
|
|
|
) ([]float64, float64) {
|
2022-08-21 14:26:58 +02:00
|
|
|
neighbours := make([]neighbour, dimens)
|
2022-07-08 19:57:44 +02:00
|
|
|
// prealloc the slice the size of dimens (and oldVals, should be the same).
|
|
|
|
vals := make([]float64, len(oldVals))
|
|
|
|
|
|
|
|
for i := range neighbours {
|
|
|
|
neighbours[i] = vals
|
|
|
|
}
|
|
|
|
|
2022-08-05 17:11:39 +02:00
|
|
|
genNeighbours(neighbCount, int(dimens), benchName, oldVals, neighbours)
|
2022-07-08 19:57:44 +02:00
|
|
|
|
|
|
|
newVals, newRes := selectBestNeighbour(neighbours, benchName)
|
|
|
|
|
|
|
|
return newVals, newRes
|
|
|
|
}
|
|
|
|
|
|
|
|
// HillClimb performs 30 iterations of SHC (30 singleHillClimb func calls
|
|
|
|
// internally) to establish a semi-relevant statistical baseline, and reports
|
|
|
|
// the results of the computation.
|
2022-08-20 23:28:30 +02:00
|
|
|
// nolint: gocognit
|
2022-08-05 17:11:39 +02:00
|
|
|
func HillClimb(
|
|
|
|
maxFES, benchMinIters, neighbours int,
|
|
|
|
theD []int,
|
|
|
|
benchFunc string,
|
|
|
|
ch chan []stats.Stats,
|
|
|
|
) {
|
2022-07-08 19:57:44 +02:00
|
|
|
if maxFES <= 0 {
|
|
|
|
} else if benchMinIters <= 0 {
|
|
|
|
log.Fatalln(fmtSHCOut("benchMinIters cannot be <= 0, bailing"))
|
|
|
|
} else if _, ok := bench.Functions[benchFunc]; !ok {
|
|
|
|
log.Fatalln(fmtSHCOut(
|
|
|
|
"unknown benchFunc used: '" + benchFunc + "', bailing",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range theD {
|
|
|
|
if theD[i] <= 0 {
|
|
|
|
log.Fatalln(fmtSHCOut(" no dimension in D can be <= 0, bailing"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
fes int
|
|
|
|
// since Stochastic Hill Climbing performs multiple cost function
|
|
|
|
// evaluations during each run (in search of a better neighbouring
|
|
|
|
// value), fesPerIter stores how many evaluations are allowed during
|
|
|
|
// each run based on the number of allowed evaluations in total (passed
|
|
|
|
// into function in maxFES).
|
|
|
|
fesPerIter int
|
|
|
|
localD []int
|
|
|
|
minIters int
|
|
|
|
stochasticHCStats []stats.Stats
|
|
|
|
)
|
|
|
|
|
|
|
|
fes = maxFES
|
|
|
|
// no need to call math.Floor() apparently, as `int()` does that implicitly.
|
2022-08-05 17:11:39 +02:00
|
|
|
fesPerIter = int(float64(fes / neighbours))
|
2022-07-08 19:57:44 +02:00
|
|
|
localD = theD
|
|
|
|
minIters = benchMinIters
|
|
|
|
|
2022-07-25 22:24:54 +02:00
|
|
|
shcMeans := &stats.AlgoBenchMean{
|
|
|
|
Algo: "Stochastic Hill Climbing",
|
|
|
|
BenchMeans: make([]stats.BenchMean, 0, len(localD)),
|
|
|
|
}
|
2022-07-19 21:56:49 +02:00
|
|
|
|
2022-07-08 19:57:44 +02:00
|
|
|
for _, dimens := range localD {
|
|
|
|
stochasticHCStatDimX := &stats.Stats{
|
|
|
|
Algo: "Stochastic Hill Climbing",
|
|
|
|
Dimens: dimens,
|
|
|
|
Iterations: minIters,
|
2022-08-20 23:28:30 +02:00
|
|
|
// this is subject to change, see note on Stats struct in pkg
|
|
|
|
// stats.
|
|
|
|
Generations: fes,
|
2022-07-08 19:57:44 +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,
|
2022-08-20 23:28:30 +02:00
|
|
|
Generations: fes,
|
2022-08-05 17:11:39 +02:00
|
|
|
Neighbours: neighbours,
|
2022-07-19 21:56:49 +02:00
|
|
|
}
|
2022-07-08 19:57:44 +02:00
|
|
|
uniDist := distuv.Uniform{
|
|
|
|
Min: benchFuncParams.Min(),
|
|
|
|
Max: benchFuncParams.Max(),
|
|
|
|
}
|
|
|
|
|
2022-08-05 17:11:39 +02:00
|
|
|
printSHC(fmt.Sprintf("running bench \"%s\" for %dD with %d Neighbours",
|
|
|
|
benchFunc, stochasticHCStatDimX.Dimens, neighbours),
|
|
|
|
)
|
2022-07-08 19:57:44 +02:00
|
|
|
|
2022-07-19 19:58:45 +02:00
|
|
|
funcStats.BenchResults = make([]stats.BenchRound, minIters)
|
2022-07-08 19:57:44 +02:00
|
|
|
|
2022-12-24 11:30:22 +01:00
|
|
|
// create and seed a source of preudo-randomness
|
2023-01-13 22:01:44 +01:00
|
|
|
// rand.Seed(uint64(time.Now().UnixNano()))
|
2022-07-08 19:57:44 +02:00
|
|
|
src := rand.NewSource(uint64(rand.Int63()))
|
|
|
|
// src := rand.NewSource(uint64(time.Now().UnixNano()))
|
|
|
|
|
|
|
|
// track execution time.
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
for iter := 0; iter < minIters; iter++ {
|
2022-07-19 19:58:45 +02:00
|
|
|
funcStats.BenchResults[iter].Iteration = iter
|
2022-07-08 19:57:44 +02:00
|
|
|
|
|
|
|
// create and stochastically populate the vals slice.
|
|
|
|
initVals := make([]float64, dimens)
|
|
|
|
|
2022-12-24 11:30:22 +01:00
|
|
|
// reseed using current time.
|
2022-07-08 19:57:44 +02:00
|
|
|
uniDist.Src = src
|
|
|
|
|
|
|
|
var bestResult float64
|
|
|
|
|
|
|
|
var bestVals []float64
|
|
|
|
|
|
|
|
initialised := false
|
|
|
|
|
|
|
|
for !initialised {
|
|
|
|
// generate initial vals, make sure they are not 0s.
|
|
|
|
initValsSHC(uint(dimens), initVals, &uniDist)
|
|
|
|
|
|
|
|
if bV, bR := singleHillClimb(
|
|
|
|
uint(dimens),
|
2022-08-05 17:11:39 +02:00
|
|
|
neighbours,
|
2022-07-08 19:57:44 +02:00
|
|
|
benchFunc,
|
|
|
|
initVals,
|
|
|
|
); bR != 0 {
|
|
|
|
bestVals = bV
|
|
|
|
bestResult = bR
|
|
|
|
initialised = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < fesPerIter; i++ {
|
|
|
|
v, r := singleHillClimb(
|
|
|
|
uint(dimens),
|
2022-08-05 17:11:39 +02:00
|
|
|
neighbours,
|
2022-07-08 19:57:44 +02:00
|
|
|
benchFunc,
|
|
|
|
bestVals,
|
|
|
|
)
|
|
|
|
|
2023-01-13 22:01:44 +01:00
|
|
|
// first or any of the subsequent iterations.
|
2022-07-08 19:57:44 +02:00
|
|
|
switch i {
|
|
|
|
case 0:
|
|
|
|
bestResult = r
|
|
|
|
bestVals = v
|
|
|
|
default:
|
|
|
|
if r < bestResult {
|
|
|
|
bestResult = r
|
|
|
|
bestVals = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 19:58:45 +02:00
|
|
|
funcStats.BenchResults[iter].Results = append(
|
|
|
|
funcStats.BenchResults[iter].Results,
|
2022-07-08 19:57:44 +02:00
|
|
|
bestResult,
|
|
|
|
)
|
2022-08-20 23:28:30 +02:00
|
|
|
|
|
|
|
// this block makes sure we properly count func evaluations for
|
|
|
|
// the purpose of correctly comparable plot comparison. i.e.
|
|
|
|
// append the winning (current best) value neighbours-1 (the
|
|
|
|
// first best is already saved at this point) times to
|
|
|
|
// represent the fact that while evaluating the neighbours to
|
|
|
|
// the current best value is taking place in the background,
|
|
|
|
// the current best value itself is kept around and
|
|
|
|
// symbolically saved as the best of the Generation.
|
|
|
|
for x := 0; x < neighbours-1; x++ {
|
|
|
|
funcStats.BenchResults[iter].Results = append(
|
|
|
|
funcStats.BenchResults[iter].Results,
|
|
|
|
bestResult,
|
|
|
|
)
|
|
|
|
}
|
2022-07-08 19:57:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
|
|
|
printSHC("computing " + fmt.Sprint(benchMinIters) + "I of bench \"" +
|
|
|
|
benchFunc + "\" for " + fmt.Sprint(dimens) + "D took " + fmt.Sprint(elapsed),
|
|
|
|
)
|
|
|
|
|
2022-07-19 21:56:49 +02:00
|
|
|
// get mean vals.
|
2022-08-20 23:28:30 +02:00
|
|
|
dimXMean.MeanVals = stats.GetMeanVals(funcStats.BenchResults, fes)
|
2022-07-19 21:56:49 +02:00
|
|
|
// save to funcStats, too.
|
|
|
|
funcStats.MeanVals = dimXMean.MeanVals
|
|
|
|
|
2022-07-08 19:57:44 +02:00
|
|
|
stochasticHCStatDimX.BenchFuncStats = append(
|
|
|
|
stochasticHCStatDimX.BenchFuncStats,
|
|
|
|
*funcStats,
|
|
|
|
)
|
|
|
|
|
|
|
|
stochasticHCStats = append(stochasticHCStats, *stochasticHCStatDimX)
|
2022-07-19 21:56:49 +02:00
|
|
|
|
|
|
|
// save to AlgoMeans.
|
2022-07-25 22:24:54 +02:00
|
|
|
shcMeans.BenchMeans = append(shcMeans.BenchMeans, *dimXMean)
|
2022-07-08 19:57:44 +02:00
|
|
|
}
|
|
|
|
|
2022-12-24 11:30:22 +01:00
|
|
|
// log.Printf("%+v\n", shcMeans)
|
2022-07-20 17:16:44 +02:00
|
|
|
sort.Sort(shcMeans)
|
|
|
|
|
2022-07-19 21:56:49 +02:00
|
|
|
// export AlgoMeans.
|
|
|
|
mu.Lock()
|
|
|
|
meanStats.AlgoMeans = append(meanStats.AlgoMeans, *shcMeans)
|
|
|
|
mu.Unlock()
|
|
|
|
|
2022-07-09 16:19:56 +02:00
|
|
|
ch <- stochasticHCStats
|
2022-07-08 19:57:44 +02:00
|
|
|
}
|