From 3e78332869e1530858e114ead2f48ab267dca496 Mon Sep 17 00:00:00 2001 From: leo Date: Fri, 20 Jan 2023 20:33:23 +0100 Subject: [PATCH] go: impl. Get{Best,Worst}Idx Population methods --- algo/de/population.go | 46 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/algo/de/population.go b/algo/de/population.go index aaaa200..5bf9ac8 100644 --- a/algo/de/population.go +++ b/algo/de/population.go @@ -4,6 +4,7 @@ package de import ( + "git.dotya.ml/wanderer/math-optim/bench" "golang.org/x/exp/rand" "gonum.org/v1/gonum/stat/distuv" @@ -52,10 +53,47 @@ type Population struct { // GetIndividal returns a reference to individual at position n. func (p *Population) GetIndividual(n uint) *PopulationIndividual { return &PopulationIndividual{} } -func (p *Population) GetBestIdx() int { return 0 } -func (p *Population) GetWorstIdx() int { return 0 } -func (p *Population) SetX(n int, nuX DecisionVector) {} -func (p *Population) SetV(n int, nuV DecisionVector) {} + +// GetBestIdx returns the index of the best population individual. +func (p *Population) GetBestIdx() int { + f := bench.Functions[p.Problem] + + bestIndividual := 0 + // the first one is the best one. + bestVal := f(p.Population[0].CurX) + + for i, v := range p.Population { + current := f(v.CurX) + + if current < bestVal { + bestIndividual = i + } + } + + return bestIndividual +} + +// GetWorstIdx returns the index of the worst population individual. +func (p *Population) GetWorstIdx() int { + f := bench.Functions[p.Problem] + + worstIndividual := 0 + // the first one is the worst one. + worstVal := f(p.Population[0].CurX) + + for i, v := range p.Population { + current := f(v.CurX) + + if current > worstVal { + worstIndividual = i + } + } + + return worstIndividual +} + +func (p *Population) SetX(n int, nuX DecisionVector) {} +func (p *Population) SetV(n int, nuV DecisionVector) {} // Init initialises all individuals to random values. func (p *Population) Init() {