diff --git a/algo/de/jDE.go b/algo/de/jDE.go index 64e2751..2ad5635 100644 --- a/algo/de/jDE.go +++ b/algo/de/jDE.go @@ -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 { diff --git a/bench/bench.go b/bench/bench.go index 5a6cb92..8ae042e 100644 --- a/bench/bench.go +++ b/bench/bench.go @@ -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 +} diff --git a/bench/bench_test.go b/bench/bench_test.go index b241c67..cf45a09 100644 --- a/bench/bench_test.go +++ b/bench/bench_test.go @@ -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) + } +}