go(de): add Run(), unexport Evolve()
All checks were successful
continuous-integration/drone/push Build is passing

* use evolve()
* add a way to calculate GAMaxFES (MaxFES for Genetic Algorithms) in the
  bench package
This commit is contained in:
leo 2023-01-19 19:55:41 +01:00
parent 525b30c38e
commit 2216b93426
Signed by: wanderer
SSH Key Fingerprint: SHA256:Dp8+iwKHSlrMEHzE3bJnPng70I7LEsa3IJXRH/U+idQ
3 changed files with 53 additions and 5 deletions

View File

@ -8,6 +8,7 @@
"os"
"sync"
"git.dotya.ml/wanderer/math-optim/bench"
"git.dotya.ml/wanderer/math-optim/stats"
)
@ -105,13 +106,38 @@ func (j *JDE) Init(generations, mutStrategy, adptScheme, np int, f, cr float64,
pop.Init()
}
// Evolve evolves a population by running the jDE (self-adapting Differential
// Run self-adapting differential evolution algorithm.
func (j *JDE) Run() {
if j == nil {
jDELogger.Fatalln("jDE is nil, NewjDE() needs to be called first. exiting...")
}
if !j.initialised {
jDELogger.Fatalln("jDE needs to be initialised before calling Run(), exiting...")
}
// have a wait group.
var wg sync.WaitGroup
// we're be spawning goroutines per dimension and that is the number of
// goroutines we need to wait for.
wg.Add(len(j.Dimensions))
// run Evolve for for all dimensions.
for _, dim := range j.Dimensions {
maxFES := bench.GetGAMaxFES(dim)
j.evolve(maxFES, &wg)
}
// wait for all.
wg.Wait()
}
// evolve evolves a population by running the jDE (self-adapting Differential
// Evolution) algorithm on the passed population until termination conditions
// are met.
func (j *JDE) Evolve(maxFES int, wg *sync.WaitGroup) {}
// Run self-adapting differential evolution algorithm.
func RunjDE(jDE *JDE) {}
func (j *JDE) evolve(maxFES int, wg *sync.WaitGroup) {}
// NewjDE returns a pointer to a new, uninitialised jDE instance.
func NewjDE() *JDE {

View File

@ -3,6 +3,8 @@
package bench
import "log"
type funcParams struct {
min float64
max float64
@ -46,3 +48,13 @@ func (f *funcParams) Min() float64 {
func (f *funcParams) Max() float64 {
return f.max
}
// GetGAMaxFES calculates the value of MaxFES for Genetic Algorithms. This is
// an arbitrary specification where MaxFES is 5000xD.
func GetGAMaxFES(dim int) int {
if dim <= 0 {
log.Fatalln("dim has to be greater than 0, got:", dim)
}
return dim * 5000
}

View File

@ -62,3 +62,13 @@ func TestDeJong2Params(t *testing.T) {
t.Errorf("wrong DeJong2Params, want: %+v, got: %+v", want, got)
}
}
func TestGetGAMaxFES(t *testing.T) {
dim := 3
want := 15000
got := GetGAMaxFES(dim)
if want != got {
t.Errorf("wrong GAMaxFES result, want: %d, got: %d", want, got)
}
}