go: allow modifying HillClimb's 'neighbours' param
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2022-08-05 17:11:39 +02:00
parent a9dfe4cc6b
commit c4077a449d
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
3 changed files with 27 additions and 8 deletions

View File

@ -198,7 +198,12 @@ funcCount := len(bench.Functions)
ch := make(chan []stats.Stats, funcCount) ch := make(chan []stats.Stats, funcCount)
for i := range algoStats { for i := range algoStats {
go HillClimb(10000, 30, bench.Dimensions, bench.FuncNames[i], ch) // params:
// maxFES, benchMinIters, neighbours int,
// theD []int,
// benchFunc string,
// ch chan []stats.Stats
go HillClimb(10000, 30, 10, bench.Dimensions, bench.FuncNames[i], ch)
} }
// get results. // get results.

View File

@ -298,6 +298,7 @@ func plotAllDims(algoStats []stats.Stats, fPrefix, fExt string, ch chan report.P
log.Panic(err) log.Panic(err)
} }
// TODO(me): add Neighbourhood param
filename := fmt.Sprintf("%s%s-%s-%s-%dD-%dG-%dI", filename := fmt.Sprintf("%s%s-%s-%s-%dD-%dG-%dI",
picsDir, picsDir,
fPrefix, fPrefix,

View File

@ -133,7 +133,12 @@ func genNeighbours(n, dimens int, benchName string, origin []float64, neighbVals
} }
// singleHillClimb runs a single iteration of SHC. // singleHillClimb runs a single iteration of SHC.
func singleHillClimb(dimens uint, benchName string, oldVals []float64) ([]float64, float64) { func singleHillClimb(
dimens uint,
neighbCount int,
benchName string,
oldVals []float64,
) ([]float64, float64) {
neighbours := make([][]float64, dimens) neighbours := make([][]float64, dimens)
// prealloc the slice the size of dimens (and oldVals, should be the same). // prealloc the slice the size of dimens (and oldVals, should be the same).
vals := make([]float64, len(oldVals)) vals := make([]float64, len(oldVals))
@ -142,7 +147,7 @@ func singleHillClimb(dimens uint, benchName string, oldVals []float64) ([]float6
neighbours[i] = vals neighbours[i] = vals
} }
genNeighbours(bench.Neighbourhood, int(dimens), benchName, oldVals, neighbours) genNeighbours(neighbCount, int(dimens), benchName, oldVals, neighbours)
newVals, newRes := selectBestNeighbour(neighbours, benchName) newVals, newRes := selectBestNeighbour(neighbours, benchName)
@ -152,7 +157,12 @@ func singleHillClimb(dimens uint, benchName string, oldVals []float64) ([]float6
// HillClimb performs 30 iterations of SHC (30 singleHillClimb func calls // HillClimb performs 30 iterations of SHC (30 singleHillClimb func calls
// internally) to establish a semi-relevant statistical baseline, and reports // internally) to establish a semi-relevant statistical baseline, and reports
// the results of the computation. // the results of the computation.
func HillClimb(maxFES, benchMinIters int, theD []int, benchFunc string, ch chan []stats.Stats) { func HillClimb(
maxFES, benchMinIters, neighbours int,
theD []int,
benchFunc string,
ch chan []stats.Stats,
) {
if maxFES <= 0 { if maxFES <= 0 {
} else if benchMinIters <= 0 { } else if benchMinIters <= 0 {
log.Fatalln(fmtSHCOut("benchMinIters cannot be <= 0, bailing")) log.Fatalln(fmtSHCOut("benchMinIters cannot be <= 0, bailing"))
@ -183,7 +193,7 @@ func HillClimb(maxFES, benchMinIters int, theD []int, benchFunc string, ch chan
fes = maxFES fes = maxFES
// no need to call math.Floor() apparently, as `int()` does that implicitly. // no need to call math.Floor() apparently, as `int()` does that implicitly.
fesPerIter = int(float64(fes / bench.Neighbourhood)) fesPerIter = int(float64(fes / neighbours))
localD = theD localD = theD
minIters = benchMinIters minIters = benchMinIters
@ -209,15 +219,16 @@ funcStats := &stats.FuncStats{BenchName: benchFunc}
Dimens: dimens, Dimens: dimens,
Iterations: minIters, Iterations: minIters,
Generations: fesPerIter, Generations: fesPerIter,
Neighbours: bench.Neighbourhood, Neighbours: neighbours,
} }
uniDist := distuv.Uniform{ uniDist := distuv.Uniform{
Min: benchFuncParams.Min(), Min: benchFuncParams.Min(),
Max: benchFuncParams.Max(), Max: benchFuncParams.Max(),
} }
printSHC("running bench \"" + benchFunc + "\" for " + printSHC(fmt.Sprintf("running bench \"%s\" for %dD with %d Neighbours",
fmt.Sprint(stochasticHCStatDimX.Dimens) + "D") benchFunc, stochasticHCStatDimX.Dimens, neighbours),
)
funcStats.BenchResults = make([]stats.BenchRound, minIters) funcStats.BenchResults = make([]stats.BenchRound, minIters)
@ -248,6 +259,7 @@ funcStats.BenchResults[iter].Iteration = iter
if bV, bR := singleHillClimb( if bV, bR := singleHillClimb(
uint(dimens), uint(dimens),
neighbours,
benchFunc, benchFunc,
initVals, initVals,
); bR != 0 { ); bR != 0 {
@ -260,6 +272,7 @@ funcStats.BenchResults[iter].Iteration = iter
for i := 0; i < fesPerIter; i++ { for i := 0; i < fesPerIter; i++ {
v, r := singleHillClimb( v, r := singleHillClimb(
uint(dimens), uint(dimens),
neighbours,
benchFunc, benchFunc,
bestVals, bestVals,
) )