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 (
|
2022-08-03 21:28:43 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2022-08-03 21:25:37 +02:00
|
|
|
"sort"
|
2022-06-14 22:34:52 +02:00
|
|
|
"sync"
|
2022-06-24 00:45:43 +02:00
|
|
|
|
2023-01-19 20:11:22 +01:00
|
|
|
"git.dotya.ml/wanderer/math-optim/algo/de"
|
2023-02-23 17:56:33 +01:00
|
|
|
"git.dotya.ml/wanderer/math-optim/algo/ga"
|
2022-06-24 00:45:43 +02:00
|
|
|
"git.dotya.ml/wanderer/math-optim/bench"
|
2023-02-23 17:56:33 +01:00
|
|
|
"git.dotya.ml/wanderer/math-optim/bench/cec2020"
|
2022-07-13 23:47:37 +02:00
|
|
|
"git.dotya.ml/wanderer/math-optim/report"
|
2022-06-24 00:45:43 +02:00
|
|
|
"git.dotya.ml/wanderer/math-optim/stats"
|
2022-06-14 22:34:52 +02:00
|
|
|
)
|
|
|
|
|
2022-07-19 21:56:49 +02:00
|
|
|
// mu protects access to meanStats.
|
|
|
|
var mu sync.Mutex
|
|
|
|
|
2023-02-23 17:50:13 +01:00
|
|
|
// mCoMPL protects access to comparisonOfMeansPicList.
|
2022-08-03 21:17:19 +02:00
|
|
|
var mCoMPL sync.Mutex
|
|
|
|
|
2022-07-19 21:56:49 +02:00
|
|
|
var meanStats = &stats.MeanStats{}
|
|
|
|
|
2022-08-03 21:17:19 +02:00
|
|
|
var comparisonOfMeansPicList = &report.PicList{Algo: "Comparison of Means"}
|
|
|
|
|
2022-08-03 21:28:43 +02:00
|
|
|
// getComparisonOfMeansPics returns a sorted slice of pics field from the
|
|
|
|
// package global 'algoMeanPics'.
|
|
|
|
func getComparisonOfMeansPics() []report.Pic {
|
|
|
|
// note: sorting by filename (dimens value being 0-padded at generation
|
|
|
|
// time), relying on this as a hack so that we didn't have to implement our
|
|
|
|
// own natural-order sorter.
|
|
|
|
sort.Sort(comparisonOfMeansPicList.Pics)
|
|
|
|
|
|
|
|
return comparisonOfMeansPicList.Pics
|
|
|
|
}
|
|
|
|
|
2023-01-21 02:35:29 +01:00
|
|
|
// saveAlgoMeans saves algo bench means safely.
|
|
|
|
func saveAlgoMeans(sabm stats.AlgoBenchMean) {
|
|
|
|
mu.Lock()
|
|
|
|
meanStats.AlgoMeans = append(meanStats.AlgoMeans, sabm)
|
|
|
|
mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2022-08-03 21:25:37 +02:00
|
|
|
// GetMeanStats returns a pointer of type stats.MeanStats to a sorted package
|
|
|
|
// global 'meanStats'.
|
|
|
|
func GetMeanStats() *stats.MeanStats {
|
|
|
|
sort.Sort(meanStats)
|
|
|
|
|
|
|
|
return meanStats
|
|
|
|
}
|
|
|
|
|
2022-12-27 01:04:55 +01:00
|
|
|
// PrepComparisonOfMeans returns a pointer to a slice of pics (of type
|
|
|
|
// report.PicList) and an integer - the count of unique benchmarking functions
|
|
|
|
// used.
|
2022-08-03 21:28:43 +02:00
|
|
|
func PrepComparisonOfMeans(wg *sync.WaitGroup) (*report.PicList, int) {
|
|
|
|
pL := report.NewPicList()
|
|
|
|
meanStats := GetMeanStats()
|
|
|
|
algos := make([]string, 0)
|
|
|
|
|
|
|
|
// learn how many algos were processed based on the data.
|
|
|
|
for _, v := range meanStats.AlgoMeans {
|
|
|
|
// if algos is empty just add the value directly, else determine if
|
|
|
|
// it's already been added or not.
|
|
|
|
if len(algos) > 0 {
|
|
|
|
alreadyadded := false
|
|
|
|
|
|
|
|
for _, algoName := range algos {
|
|
|
|
if algoName == v.Algo {
|
|
|
|
// early bail if already added.
|
|
|
|
alreadyadded = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !alreadyadded {
|
|
|
|
algos = append(algos, v.Algo)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
algos = append(algos, v.Algo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// construct title consisting of names of all involved algorithms.
|
|
|
|
for _, v := range algos {
|
|
|
|
switch pL.Algo {
|
|
|
|
case "":
|
|
|
|
pL.Algo = v
|
|
|
|
default:
|
|
|
|
pL.Algo += " vs " + v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println(`generating "Comparison of Means" plots`)
|
|
|
|
|
|
|
|
algoCount := len(algos)
|
|
|
|
dimLen := len(bench.Dimensions)
|
2022-12-27 00:56:54 +01:00
|
|
|
// without Rastrigin in active duty for the moment.
|
|
|
|
benchCount := len(bench.Functions) - 1
|
2022-08-03 21:28:43 +02:00
|
|
|
|
|
|
|
// note: this is a wee bit ugly.
|
|
|
|
for d := 0; d < dimLen; d++ {
|
|
|
|
for i := 0; i < benchCount; i++ {
|
|
|
|
dimXAlgoMeanVals := make([]stats.AlgoMeanVals, 0, algoCount)
|
|
|
|
|
|
|
|
for j := 0; j < algoCount*benchCount; j += benchCount {
|
|
|
|
neighbInfo := ""
|
|
|
|
|
|
|
|
// only add info about neighbours if it was changed (from
|
|
|
|
// the default of -1).
|
|
|
|
if n := meanStats.AlgoMeans[d+j].BenchMeans[i].Neighbours; n != -1 {
|
|
|
|
neighbInfo = fmt.Sprintf(" (N: %d)", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
ms := &stats.AlgoMeanVals{
|
|
|
|
Title: meanStats.AlgoMeans[d+j].Algo + neighbInfo,
|
|
|
|
MeanVals: meanStats.AlgoMeans[d+j].BenchMeans[i].MeanVals,
|
|
|
|
}
|
|
|
|
dimXAlgoMeanVals = append(dimXAlgoMeanVals, *ms)
|
|
|
|
}
|
|
|
|
|
|
|
|
dimens := meanStats.AlgoMeans[d].BenchMeans[i].Dimens
|
|
|
|
iterations := meanStats.AlgoMeans[d].BenchMeans[i].Iterations
|
|
|
|
bench := meanStats.AlgoMeans[d].BenchMeans[i].Bench
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
// construct plots concurrently.
|
|
|
|
go PlotMeanValsMulti(
|
2022-08-20 13:42:41 +02:00
|
|
|
wg, dimens, iterations, bench, "plot-", ".pdf",
|
2022-08-03 21:28:43 +02:00
|
|
|
dimXAlgoMeanVals...,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for all the plotting to conclude.
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
pL.Pics = getComparisonOfMeansPics()
|
|
|
|
|
|
|
|
return pL, benchCount
|
|
|
|
}
|
|
|
|
|
2023-02-23 17:59:26 +01:00
|
|
|
// PrepCEC2020ComparisonOfMeans prepares for comparison means of CEC2020 algos.
|
|
|
|
func PrepCEC2020ComparisonOfMeans(wg *sync.WaitGroup) (*report.PicList, int) {
|
|
|
|
pL := report.NewPicList()
|
|
|
|
meanStats := GetMeanStats()
|
|
|
|
algos := getAlgosFromAlgoMeans(meanStats.AlgoMeans)
|
|
|
|
|
|
|
|
// construct title consisting of names of all involved algorithms.
|
|
|
|
for _, v := range algos {
|
|
|
|
switch pL.Algo {
|
|
|
|
case "":
|
|
|
|
pL.Algo = v
|
|
|
|
default:
|
|
|
|
pL.Algo += " vs " + v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println(`generating "Comparison of Means" plots`)
|
|
|
|
|
|
|
|
algoCount := len(algos)
|
|
|
|
dimLen := len(cec2020.Dimensions)
|
|
|
|
benchCount := len(cec2020.Functions)
|
|
|
|
|
|
|
|
for d := 0; d < dimLen; d++ {
|
|
|
|
// construct comparison for all benchmarking functions.
|
|
|
|
for i := 0; i < benchCount; i++ {
|
|
|
|
dimXAlgoMeanVals := make([]stats.AlgoMeanVals, 0, algoCount)
|
|
|
|
|
|
|
|
for j := 0; j < algoCount; j++ {
|
|
|
|
ms := &stats.AlgoMeanVals{
|
|
|
|
Title: meanStats.AlgoMeans[i+(j*benchCount)].Algo,
|
|
|
|
MeanVals: meanStats.AlgoMeans[i+(j*benchCount)].BenchMeans[d].MeanVals,
|
|
|
|
}
|
|
|
|
|
|
|
|
dimXAlgoMeanVals = append(dimXAlgoMeanVals, *ms)
|
|
|
|
}
|
|
|
|
|
|
|
|
dimens := meanStats.AlgoMeans[i].BenchMeans[d].Dimens
|
|
|
|
iterations := meanStats.AlgoMeans[i].BenchMeans[d].Iterations
|
|
|
|
bench := meanStats.AlgoMeans[i].BenchMeans[d].Bench
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
// construct plots concurrently.
|
|
|
|
go PlotMeanValsMulti(
|
|
|
|
wg, dimens, iterations, bench, "plot-", ".pdf",
|
|
|
|
dimXAlgoMeanVals...,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for all plotting goroutines.
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
pL.Pics = getComparisonOfMeansPics()
|
|
|
|
|
|
|
|
return pL, dimLen
|
|
|
|
}
|
|
|
|
|
|
|
|
// getAlgosFromAlgoMeans extracts algorithms used from the means list and
|
|
|
|
// returns it as a []string.
|
|
|
|
func getAlgosFromAlgoMeans(s []stats.AlgoBenchMean) []string {
|
|
|
|
algos := make([]string, 0)
|
|
|
|
|
|
|
|
// learn how many algos were processed based on the data.
|
|
|
|
for _, v := range s {
|
|
|
|
// if algos is empty just add the value directly, else determine if
|
|
|
|
// it's already been added or not.
|
|
|
|
if len(algos) > 0 {
|
|
|
|
alreadyadded := false
|
|
|
|
|
|
|
|
for _, algoName := range algos {
|
|
|
|
if algoName == v.Algo {
|
|
|
|
// early bail if already added.
|
|
|
|
alreadyadded = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !alreadyadded {
|
|
|
|
algos = append(algos, v.Algo)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
algos = append(algos, v.Algo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return algos
|
|
|
|
}
|
|
|
|
|
2022-06-14 22:34:52 +02:00
|
|
|
// DoRandomSearch executes a search using the 'Random search' method.
|
2022-07-16 22:05:15 +02:00
|
|
|
func DoRandomSearch(wg *sync.WaitGroup, m *sync.Mutex) {
|
2022-06-14 22:34:52 +02:00
|
|
|
defer wg.Done()
|
|
|
|
|
2022-06-17 19:55:23 +02:00
|
|
|
printRandomSearch("starting...")
|
2022-06-20 03:32:48 +02:00
|
|
|
|
2022-06-24 00:45:43 +02:00
|
|
|
// funcCount is the number of bench functions available.
|
2022-12-27 00:56:54 +01:00
|
|
|
// without Rastrigin in active duty for the moment.
|
|
|
|
funcCount := len(bench.Functions) - 1
|
2022-06-24 00:45:43 +02:00
|
|
|
// stats for the current algo (RandomSearch).
|
|
|
|
algoStats := make([][]stats.Stats, funcCount)
|
2022-07-11 20:59:18 +02:00
|
|
|
// 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
|
|
|
|
2022-08-05 17:23:10 +02:00
|
|
|
defer close(ch)
|
|
|
|
|
2022-06-24 00:56:10 +02:00
|
|
|
for i := range algoStats {
|
|
|
|
// ng y'all.
|
2022-07-17 11:57:40 +02:00
|
|
|
go RandomSearchNG(10000, 30, bench.Dimensions, bench.FuncNames[i], ch)
|
2022-07-11 20:59:18 +02:00
|
|
|
}
|
2022-07-09 16:19:56 +02:00
|
|
|
|
2022-07-11 20:59:18 +02:00
|
|
|
// get results.
|
|
|
|
for i := range algoStats {
|
|
|
|
s := <-ch
|
2022-07-09 16:19:56 +02:00
|
|
|
|
2022-07-11 20:59:18 +02:00
|
|
|
algoStats[i] = s
|
2022-06-24 00:56:10 +02:00
|
|
|
}
|
2022-06-24 00:45:43 +02:00
|
|
|
|
2022-12-24 11:30:22 +01:00
|
|
|
// save stats to json.
|
|
|
|
// stats.SaveStats(schw, "schwefel")
|
|
|
|
// stats.SaveStats(djg1, "djg1")
|
|
|
|
// stats.SaveStats(djg2, "djg2")
|
|
|
|
|
2022-07-13 23:47:37 +02:00
|
|
|
pCh := make(chan report.PicList, funcCount*len(bench.Dimensions))
|
2022-07-18 22:36:47 +02:00
|
|
|
pMeanCh := make(chan report.PicList, funcCount*len(bench.Dimensions))
|
2022-07-13 23:47:37 +02:00
|
|
|
|
2022-08-05 17:23:10 +02:00
|
|
|
defer close(pCh)
|
|
|
|
defer close(pMeanCh)
|
|
|
|
|
2022-06-24 00:45:43 +02:00
|
|
|
for i := range algoStats {
|
2022-08-20 13:42:41 +02:00
|
|
|
go plotAllDims(algoStats[i], "plot", ".pdf", pCh, pMeanCh)
|
2022-07-13 23:47:37 +02:00
|
|
|
}
|
2022-07-08 22:51:31 +02:00
|
|
|
|
2022-07-16 21:40:53 +02:00
|
|
|
pLs := []report.PicList{}
|
2022-07-18 22:36:47 +02:00
|
|
|
pLsMean := []report.PicList{}
|
2022-07-16 21:40:53 +02:00
|
|
|
|
2022-07-13 23:47:37 +02:00
|
|
|
for range algoStats {
|
|
|
|
pL := <-pCh
|
2022-07-18 22:36:47 +02:00
|
|
|
pLMean := <-pMeanCh
|
2022-07-13 23:47:37 +02:00
|
|
|
|
2022-07-16 21:40:53 +02:00
|
|
|
pLs = append(pLs, pL)
|
2022-07-18 22:36:47 +02:00
|
|
|
pLsMean = append(pLsMean, pLMean)
|
2022-06-24 00:45:43 +02:00
|
|
|
}
|
2022-06-20 03:32:48 +02:00
|
|
|
|
2022-07-18 00:23:53 +02:00
|
|
|
algoName := "Random Search"
|
|
|
|
|
2022-07-16 22:05:15 +02:00
|
|
|
// protect access to shared data.
|
|
|
|
m.Lock()
|
2022-07-18 22:36:47 +02:00
|
|
|
report.SavePicsToFile(pLs, pLsMean, algoName)
|
2022-07-16 21:40:53 +02:00
|
|
|
|
2022-07-18 00:23:53 +02:00
|
|
|
stats.SaveTable(algoName, algoStats)
|
2022-07-16 23:30:56 +02:00
|
|
|
m.Unlock()
|
2022-06-14 22:34:52 +02:00
|
|
|
}
|
|
|
|
|
2022-12-24 11:30:22 +01:00
|
|
|
// TODO(me): split this package to multiple - package per algo, common code here.
|
|
|
|
// TODO(me): implement Simulated Annaeling.
|
|
|
|
// TODO(me): implement a variant of Stochastic Hill Climber that tweaks its
|
|
|
|
// Neighbourhood size or MaxNeighbourVariancePercent based on the
|
|
|
|
// latest 5 values, if they don't change, params get tweaked to
|
|
|
|
// broaden the search space to make sure it's not stuck in a local
|
|
|
|
// extreme.
|
|
|
|
|
2022-06-14 22:34:52 +02:00
|
|
|
// DoStochasticHillClimbing performs a search using the 'Stochastic Hill
|
|
|
|
// Climbing' method.
|
2022-07-16 22:05:15 +02:00
|
|
|
func DoStochasticHillClimbing(wg *sync.WaitGroup, m *sync.Mutex) {
|
2022-06-14 22:34:52 +02:00
|
|
|
defer wg.Done()
|
|
|
|
|
2022-06-17 19:55:23 +02:00
|
|
|
printSHC("starting...")
|
2022-07-08 21:40:04 +02:00
|
|
|
|
|
|
|
// funcCount is the number of bench functions available.
|
2022-12-27 00:56:54 +01:00
|
|
|
// without Rastrigin in active duty for the moment.
|
|
|
|
funcCount := len(bench.Functions) - 1
|
2022-07-08 21:40:04 +02:00
|
|
|
// stats for the current algo (StochasticHillClimber).
|
|
|
|
algoStats := make([][]stats.Stats, funcCount)
|
2022-07-11 20:59:18 +02:00
|
|
|
// 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
|
|
|
|
2022-08-05 17:23:10 +02:00
|
|
|
defer close(ch)
|
|
|
|
|
2022-07-08 21:40:04 +02:00
|
|
|
for i := range algoStats {
|
2022-08-05 17:11:39 +02:00
|
|
|
// params:
|
|
|
|
// maxFES, benchMinIters, neighbours int,
|
|
|
|
// theD []int,
|
|
|
|
// benchFunc string,
|
|
|
|
// ch chan []stats.Stats
|
|
|
|
go HillClimb(10000, 30, 10, bench.Dimensions, bench.FuncNames[i], ch)
|
2022-07-11 20:59:18 +02:00
|
|
|
}
|
2022-07-09 16:19:56 +02:00
|
|
|
|
2022-07-11 20:59:18 +02:00
|
|
|
// get results.
|
|
|
|
for i := range algoStats {
|
|
|
|
s := <-ch
|
2022-07-09 16:19:56 +02:00
|
|
|
|
2022-07-11 20:59:18 +02:00
|
|
|
algoStats[i] = s
|
2022-07-08 21:40:04 +02:00
|
|
|
}
|
|
|
|
|
2022-07-13 23:47:37 +02:00
|
|
|
pCh := make(chan report.PicList, funcCount*len(bench.Dimensions))
|
2022-07-18 22:36:47 +02:00
|
|
|
pMeanCh := make(chan report.PicList, funcCount*len(bench.Dimensions))
|
2022-07-13 23:47:37 +02:00
|
|
|
|
2022-08-05 17:23:10 +02:00
|
|
|
defer close(pCh)
|
|
|
|
defer close(pMeanCh)
|
|
|
|
|
2022-07-08 21:40:04 +02:00
|
|
|
for _, algoStat := range algoStats {
|
2022-08-20 13:42:41 +02:00
|
|
|
go plotAllDims(algoStat, "plot", ".pdf", pCh, pMeanCh)
|
2022-07-13 23:47:37 +02:00
|
|
|
}
|
2022-07-08 22:51:31 +02:00
|
|
|
|
2022-07-16 21:40:53 +02:00
|
|
|
pLs := []report.PicList{}
|
2022-07-18 22:36:47 +02:00
|
|
|
pLsMean := []report.PicList{}
|
2022-07-16 21:40:53 +02:00
|
|
|
|
2022-07-13 23:47:37 +02:00
|
|
|
for range algoStats {
|
|
|
|
pL := <-pCh
|
2022-07-18 22:36:47 +02:00
|
|
|
pLMean := <-pMeanCh
|
2022-07-13 23:47:37 +02:00
|
|
|
|
2022-07-16 21:40:53 +02:00
|
|
|
pLs = append(pLs, pL)
|
2022-07-18 22:36:47 +02:00
|
|
|
pLsMean = append(pLsMean, pLMean)
|
2022-07-08 21:40:04 +02:00
|
|
|
}
|
|
|
|
|
2022-07-18 00:23:53 +02:00
|
|
|
algoName := "Stochastic Hill Climbing"
|
|
|
|
|
2022-07-16 22:05:15 +02:00
|
|
|
// protect access to shared data.
|
|
|
|
m.Lock()
|
2022-07-18 22:36:47 +02:00
|
|
|
report.SavePicsToFile(pLs, pLsMean, algoName)
|
2022-07-16 21:40:53 +02:00
|
|
|
|
2022-07-18 00:23:53 +02:00
|
|
|
stats.SaveTable(algoName, algoStats)
|
2022-07-16 23:30:56 +02:00
|
|
|
m.Unlock()
|
2022-06-14 22:34:52 +02:00
|
|
|
}
|
2022-12-24 11:30:22 +01:00
|
|
|
|
|
|
|
func DoStochasticHillClimbing100Neigh(wg *sync.WaitGroup, m *sync.Mutex) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
printSHC("starting...")
|
|
|
|
|
|
|
|
// funcCount is the number of bench functions available.
|
2022-12-27 00:56:54 +01:00
|
|
|
// without Rastrigin in active duty for the moment.
|
|
|
|
funcCount := len(bench.Functions) - 1
|
2022-12-24 11:30:22 +01:00
|
|
|
// 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, 100, bench.Dimensions, bench.FuncNames[i], ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get results.
|
|
|
|
for i := range algoStats {
|
|
|
|
s := <-ch
|
|
|
|
|
|
|
|
algoStats[i] = s
|
|
|
|
}
|
|
|
|
|
|
|
|
pCh := make(chan report.PicList, funcCount*len(bench.Dimensions))
|
|
|
|
pMeanCh := make(chan report.PicList, funcCount*len(bench.Dimensions))
|
|
|
|
|
|
|
|
for _, algoStat := range algoStats {
|
|
|
|
go plotAllDims(algoStat, "plot", ".pdf", pCh, pMeanCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
pLs := []report.PicList{}
|
|
|
|
pLsMean := []report.PicList{}
|
|
|
|
|
|
|
|
for range algoStats {
|
|
|
|
pL := <-pCh
|
|
|
|
pLMean := <-pMeanCh
|
|
|
|
|
|
|
|
pLs = append(pLs, pL)
|
|
|
|
pLsMean = append(pLsMean, pLMean)
|
|
|
|
}
|
|
|
|
|
|
|
|
algoName := "Stochastic Hill Climbing 100 Neighbours"
|
|
|
|
|
|
|
|
// protect access to shared data.
|
|
|
|
m.Lock()
|
|
|
|
report.SavePicsToFile(pLs, pLsMean, algoName)
|
|
|
|
// report.SavePicsToFile(pLsMean, pLs, algoName)
|
|
|
|
|
|
|
|
// stats.PrintStatisticTable(algoStats)
|
|
|
|
stats.SaveTable(algoName, algoStats)
|
|
|
|
m.Unlock()
|
|
|
|
}
|
2023-01-19 20:11:22 +01:00
|
|
|
|
|
|
|
func DojDE(wg *sync.WaitGroup, m *sync.Mutex) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
de.LogPrintln("starting")
|
|
|
|
|
|
|
|
// funcCount is the number of bench functions available and tested.
|
|
|
|
funcCount := len(bench.Functions)
|
|
|
|
// stats for the current algo.
|
|
|
|
algoStats := make([][]stats.Stats, funcCount)
|
|
|
|
// ch serves as a way to get the actual computed output.
|
|
|
|
ch := make(chan []stats.Stats, funcCount)
|
2023-01-21 02:35:29 +01:00
|
|
|
// chAlgoMeans := make(chan *stats.AlgoBenchMean, 1)
|
|
|
|
chAlgoMeans := make(chan *stats.AlgoBenchMean, funcCount)
|
2023-01-19 20:11:22 +01:00
|
|
|
|
|
|
|
defer close(ch)
|
2023-01-21 02:35:29 +01:00
|
|
|
defer close(chAlgoMeans)
|
2023-01-19 20:11:22 +01:00
|
|
|
|
|
|
|
// jDE params.
|
|
|
|
np := 50
|
|
|
|
f := 0.5
|
|
|
|
cr := 0.9
|
|
|
|
|
|
|
|
for i := range algoStats {
|
|
|
|
jDE := de.NewjDE()
|
|
|
|
|
|
|
|
// params:
|
2023-01-20 15:52:45 +01:00
|
|
|
// Generations, minimum bench iterations, mutation strategy, parameter
|
|
|
|
// self-adaptation scheme, initial population size, differential
|
|
|
|
// weight, mutation constant, dimensions, bench name and a
|
|
|
|
// synchronisation channel.
|
2023-01-19 20:11:22 +01:00
|
|
|
//
|
|
|
|
// -1 to disable generation limits,
|
2023-01-20 15:52:45 +01:00
|
|
|
// n > 0 for minimum bench iterations
|
2023-01-19 20:11:22 +01:00
|
|
|
// 0..17 to choose a mutation strategy,
|
|
|
|
// 0..1 to select a parameter self-adaptation scheme,
|
|
|
|
// np >= 4 as initial population size.
|
2023-01-21 02:35:29 +01:00
|
|
|
jDE.Init(-1, 30, 0, 0, np, f, cr, bench.DimensionsGA, bench.FuncNames[i], ch, chAlgoMeans)
|
2023-01-19 20:11:22 +01:00
|
|
|
|
|
|
|
go jDE.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
// get results.
|
|
|
|
for i := range algoStats {
|
|
|
|
s := <-ch
|
2023-01-21 02:35:29 +01:00
|
|
|
aM := <-chAlgoMeans
|
2023-01-19 20:11:22 +01:00
|
|
|
|
|
|
|
algoStats[i] = s
|
2023-01-21 02:35:29 +01:00
|
|
|
|
|
|
|
saveAlgoMeans(*aM)
|
2023-01-19 20:11:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pCh := make(chan report.PicList, funcCount*len(bench.DimensionsGA))
|
|
|
|
pMeanCh := make(chan report.PicList, funcCount*len(bench.DimensionsGA))
|
|
|
|
|
|
|
|
for _, algoStat := range algoStats {
|
|
|
|
go plotAllDims(algoStat, "plot", ".pdf", pCh, pMeanCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
pLs := []report.PicList{}
|
|
|
|
pLsMean := []report.PicList{}
|
|
|
|
|
|
|
|
for range algoStats {
|
|
|
|
pL := <-pCh
|
|
|
|
pLMean := <-pMeanCh
|
|
|
|
|
|
|
|
pLs = append(pLs, pL)
|
|
|
|
pLsMean = append(pLsMean, pLMean)
|
|
|
|
}
|
|
|
|
|
2023-02-09 03:46:07 +01:00
|
|
|
algoName := "Differential Evolution"
|
|
|
|
|
|
|
|
// protect access to shared data.
|
|
|
|
m.Lock()
|
|
|
|
report.SavePicsToFile(pLs, pLsMean, algoName)
|
|
|
|
|
|
|
|
stats.SaveTable(algoName, algoStats)
|
|
|
|
m.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func DoCEC2020jDE(wg *sync.WaitGroup, m *sync.Mutex) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
// funcCount is the number of bench functions available and tested.
|
2023-02-23 17:56:33 +01:00
|
|
|
funcCount := len(cec2020.Functions)
|
2023-02-09 03:46:07 +01:00
|
|
|
// stats for the current algo.
|
|
|
|
algoStats := make([][]stats.Stats, funcCount)
|
|
|
|
// ch serves as a way to get the actual computed output.
|
|
|
|
ch := make(chan []stats.Stats, funcCount)
|
|
|
|
// chAlgoMeans := make(chan *stats.AlgoBenchMean, 1)
|
|
|
|
chAlgoMeans := make(chan *stats.AlgoBenchMean, funcCount)
|
|
|
|
|
|
|
|
defer close(ch)
|
|
|
|
defer close(chAlgoMeans)
|
|
|
|
|
|
|
|
// jDE params.
|
|
|
|
np := 50
|
|
|
|
f := 0.5
|
|
|
|
cr := 0.9
|
|
|
|
|
|
|
|
for i := range algoStats {
|
2023-02-23 17:56:33 +01:00
|
|
|
jDE := ga.NewjDE()
|
2023-02-09 03:46:07 +01:00
|
|
|
|
|
|
|
// params:
|
|
|
|
// Generations, minimum bench iterations, mutation strategy, parameter
|
|
|
|
// self-adaptation scheme, initial population size, differential
|
|
|
|
// weight, mutation constant, dimensions, bench name and a
|
|
|
|
// synchronisation channel.
|
|
|
|
//
|
|
|
|
// -1 to disable generation limits,
|
|
|
|
// n > 0 for minimum bench iterations
|
|
|
|
// 0..17 to choose a mutation strategy,
|
|
|
|
// 0..1 to select a parameter self-adaptation scheme,
|
|
|
|
// np >= 4 as initial population size.
|
2023-02-23 17:56:33 +01:00
|
|
|
jDE.Init(-1, 30, 0, 0, np, f, cr, cec2020.Dimensions, cec2020.FuncNames[i], ch, chAlgoMeans)
|
2023-02-09 03:46:07 +01:00
|
|
|
|
|
|
|
go jDE.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
// get results.
|
|
|
|
for i := range algoStats {
|
|
|
|
s := <-ch
|
|
|
|
aM := <-chAlgoMeans
|
|
|
|
|
|
|
|
algoStats[i] = s
|
|
|
|
|
|
|
|
saveAlgoMeans(*aM)
|
|
|
|
}
|
|
|
|
|
2023-02-23 17:56:33 +01:00
|
|
|
pCh := make(chan report.PicList, funcCount*len(cec2020.Dimensions))
|
|
|
|
pMeanCh := make(chan report.PicList, funcCount*len(cec2020.Dimensions))
|
2023-02-09 03:46:07 +01:00
|
|
|
|
|
|
|
for _, algoStat := range algoStats {
|
|
|
|
go plotAllDims(algoStat, "plot", ".pdf", pCh, pMeanCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
pLs := []report.PicList{}
|
|
|
|
pLsMean := []report.PicList{}
|
|
|
|
|
|
|
|
for range algoStats {
|
|
|
|
pL := <-pCh
|
|
|
|
pLMean := <-pMeanCh
|
|
|
|
|
|
|
|
pLs = append(pLs, pL)
|
|
|
|
pLsMean = append(pLsMean, pLMean)
|
|
|
|
}
|
|
|
|
|
2023-01-19 20:11:22 +01:00
|
|
|
algoName := "Self-adapting Differential Evolution"
|
|
|
|
|
|
|
|
// protect access to shared data.
|
|
|
|
m.Lock()
|
|
|
|
report.SavePicsToFile(pLs, pLsMean, algoName)
|
|
|
|
|
|
|
|
stats.SaveTable(algoName, algoStats)
|
|
|
|
m.Unlock()
|
|
|
|
}
|
2023-02-23 18:05:18 +01:00
|
|
|
|
|
|
|
// DoCEC2020SOMAT3A performs a search using the SOMA T3A method.
|
|
|
|
func DoCEC2020SOMAT3A(wg *sync.WaitGroup, m *sync.Mutex) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
// funcCount is the number of bench functions available and tested.
|
|
|
|
funcCount := len(cec2020.Functions)
|
|
|
|
// stats for the current algo.
|
|
|
|
algoStats := make([][]stats.Stats, funcCount)
|
|
|
|
// ch serves as a way to get the actual computed output.
|
|
|
|
ch := make(chan []stats.Stats, funcCount)
|
|
|
|
chAlgoMeans := make(chan *stats.AlgoBenchMean, funcCount)
|
|
|
|
|
|
|
|
defer close(ch)
|
|
|
|
defer close(chAlgoMeans)
|
|
|
|
|
|
|
|
// somat3a params.
|
|
|
|
np := 50
|
|
|
|
k := 10
|
|
|
|
mSize := 10
|
|
|
|
n := 5
|
|
|
|
njumps := 7
|
|
|
|
|
|
|
|
for i := range algoStats {
|
|
|
|
somat3a := ga.NewSOMAT3A()
|
|
|
|
|
|
|
|
// params:
|
|
|
|
// Generations, minimum bench iterations, initial population size,
|
|
|
|
// leader candidates, migration candidates group size, number of
|
|
|
|
// migrants, number of jumps each migrant performs, dimensions, bench
|
|
|
|
// name and synchronisation channels.
|
|
|
|
//
|
|
|
|
// -1 to disable generation limits,
|
|
|
|
// n > 0 for minimum bench iterations,
|
|
|
|
// np >= k+mSize as initial population size,
|
|
|
|
// k, mSize, n and njumps >= 0.
|
|
|
|
err := somat3a.Init(-1, 30, np, k, mSize, n, njumps,
|
|
|
|
cec2020.Dimensions, cec2020.FuncNames[i],
|
|
|
|
ch, chAlgoMeans,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("Failed to initialise SOMA T3A, error: %q", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go somat3a.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
// get results.
|
|
|
|
for i := range algoStats {
|
|
|
|
s := <-ch
|
|
|
|
aM := <-chAlgoMeans
|
|
|
|
|
|
|
|
algoStats[i] = s
|
|
|
|
|
|
|
|
saveAlgoMeans(*aM)
|
|
|
|
}
|
|
|
|
|
|
|
|
pCh := make(chan report.PicList, funcCount*len(cec2020.Dimensions))
|
|
|
|
pMeanCh := make(chan report.PicList, funcCount*len(cec2020.Dimensions))
|
|
|
|
|
|
|
|
for _, algoStat := range algoStats {
|
|
|
|
go plotAllDims(algoStat, "plot", ".pdf", pCh, pMeanCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
pLs := []report.PicList{}
|
|
|
|
pLsMean := []report.PicList{}
|
|
|
|
|
|
|
|
for range algoStats {
|
|
|
|
pL := <-pCh
|
|
|
|
pLMean := <-pMeanCh
|
|
|
|
|
|
|
|
pLs = append(pLs, pL)
|
|
|
|
pLsMean = append(pLsMean, pLMean)
|
|
|
|
}
|
|
|
|
|
|
|
|
algoName := "SOMA T3A"
|
|
|
|
|
|
|
|
// protect access to shared data.
|
|
|
|
m.Lock()
|
|
|
|
report.SavePicsToFile(pLs, pLsMean, algoName)
|
|
|
|
|
|
|
|
stats.SaveTable(algoName, algoStats)
|
|
|
|
m.Unlock()
|
|
|
|
}
|