2023-01-16 13:30:38 +01:00
|
|
|
// Copyright 2023 wanderer <a_mirre at utb dot cz>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
package de
|
|
|
|
|
2023-01-20 18:38:29 +01:00
|
|
|
import (
|
2023-01-20 20:33:23 +01:00
|
|
|
"git.dotya.ml/wanderer/math-optim/bench"
|
2023-01-20 18:38:29 +01:00
|
|
|
"golang.org/x/exp/rand"
|
|
|
|
|
|
|
|
"gonum.org/v1/gonum/stat/distuv"
|
|
|
|
)
|
|
|
|
|
2023-01-16 13:30:38 +01:00
|
|
|
type (
|
|
|
|
// DecisionVector is a []float64 abstraction representing the decision vector.
|
|
|
|
DecisionVector []float64
|
|
|
|
// FitnessVector is a []float64 abstraction representing the fitness vector.
|
|
|
|
FitnessVector []float64
|
|
|
|
// ConstraintVector is a []float64 abstraction representing the constraint vector.
|
|
|
|
ConstraintVector []float64
|
|
|
|
)
|
|
|
|
|
|
|
|
// PopulationIndividual representats a single population individual.
|
|
|
|
type PopulationIndividual struct {
|
|
|
|
CurX DecisionVector
|
|
|
|
CurV DecisionVector
|
|
|
|
CurC ConstraintVector
|
|
|
|
CurF FitnessVector
|
|
|
|
|
|
|
|
BestX DecisionVector
|
|
|
|
BestC ConstraintVector
|
|
|
|
BestF FitnessVector
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChampionIndividual is a representation of the best individual currently
|
|
|
|
// available in the population.
|
|
|
|
type ChampionIndividual struct {
|
|
|
|
X DecisionVector
|
|
|
|
C ConstraintVector
|
|
|
|
F FitnessVector
|
|
|
|
}
|
|
|
|
|
|
|
|
// Population groups population individuals (agents) with metadata about the population.
|
|
|
|
type Population struct {
|
|
|
|
// Population is a slice of population individuals.
|
|
|
|
Population []PopulationIndividual
|
|
|
|
// Problem is the current benchmarking function this population is attempting to optimise.
|
|
|
|
Problem string
|
2023-01-20 18:36:04 +01:00
|
|
|
// Dimen is the dimensionality of the problem being optimised.
|
|
|
|
Dimen int
|
2023-01-16 13:30:38 +01:00
|
|
|
// Seed is the value used to (re)init population.
|
|
|
|
Seed uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetIndividal returns a reference to individual at position n.
|
|
|
|
func (p *Population) GetIndividual(n uint) *PopulationIndividual { return &PopulationIndividual{} }
|
2023-01-20 20:33:23 +01:00
|
|
|
|
|
|
|
// 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) {}
|
2023-01-16 13:30:38 +01:00
|
|
|
|
2023-01-20 18:38:29 +01:00
|
|
|
// Init initialises all individuals to random values.
|
|
|
|
func (p *Population) Init() {
|
2023-01-21 02:35:29 +01:00
|
|
|
benchFuncParams := bench.FunctionParams[p.Problem]
|
|
|
|
|
|
|
|
uniform := distuv.Uniform{
|
|
|
|
Min: benchFuncParams.Min(),
|
|
|
|
Max: benchFuncParams.Max(),
|
|
|
|
}
|
2023-01-20 18:38:29 +01:00
|
|
|
uniform.Src = rand.NewSource(p.Seed)
|
|
|
|
|
2023-02-09 03:56:31 +01:00
|
|
|
// jDELogger.Printf("population initialisation - popCount: %d, seed: %d\n",
|
|
|
|
// len(p.Population), p.Seed,
|
|
|
|
// )
|
2023-01-21 02:35:29 +01:00
|
|
|
|
|
|
|
for i, v := range p.Population {
|
2023-01-20 18:38:29 +01:00
|
|
|
v.CurX = make([]float64, p.Dimen)
|
|
|
|
|
2023-01-21 02:35:29 +01:00
|
|
|
for j := 0; j < p.Dimen; j++ {
|
|
|
|
v.CurX[j] = uniform.Rand()
|
2023-01-20 18:38:29 +01:00
|
|
|
}
|
2023-01-21 02:35:29 +01:00
|
|
|
|
|
|
|
p.Population[i] = v
|
2023-01-20 18:38:29 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-16 13:30:38 +01:00
|
|
|
|
|
|
|
// Reinit reinitialises all individuals.
|
2023-01-20 18:38:29 +01:00
|
|
|
func (p *Population) Reinit() {
|
|
|
|
p.Init()
|
|
|
|
}
|
2023-01-16 13:30:38 +01:00
|
|
|
|
|
|
|
// ReinitN reinitialises the individual at position n.
|
|
|
|
func (p *Population) ReinitN(n uint) {}
|
2023-01-20 18:44:45 +01:00
|
|
|
|
|
|
|
// Clear sets all vectors to 0.
|
|
|
|
func (p *Population) Clear() {
|
|
|
|
if p.Population != nil {
|
|
|
|
for _, v := range p.Population {
|
|
|
|
v.CurX = make([]float64, p.Dimen)
|
|
|
|
v.CurC = make([]float64, p.Dimen)
|
|
|
|
v.CurF = make([]float64, p.Dimen)
|
|
|
|
v.CurV = make([]float64, p.Dimen)
|
|
|
|
v.BestX = make([]float64, p.Dimen)
|
|
|
|
v.BestC = make([]float64, p.Dimen)
|
|
|
|
v.BestF = make([]float64, p.Dimen)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-16 13:30:38 +01:00
|
|
|
|
|
|
|
// meanVelocity computes the mean current velocity of all individuals in the population.
|
|
|
|
func (p *Population) MeanVelocity() float64 { return 0.0 }
|
|
|
|
|
|
|
|
// newPopulation returns a pointer to a new, uninitialised population.
|
2023-01-20 18:36:04 +01:00
|
|
|
func newPopulation(benchProblem string, np, dimen int) *Population {
|
2023-01-16 13:30:38 +01:00
|
|
|
p := &Population{}
|
|
|
|
|
|
|
|
p.Problem = benchProblem
|
|
|
|
|
2023-01-20 18:36:04 +01:00
|
|
|
p.Dimen = dimen
|
|
|
|
|
2023-01-16 13:30:38 +01:00
|
|
|
// pre-alloc.
|
2023-01-21 02:35:29 +01:00
|
|
|
p.Population = make([]PopulationIndividual, np)
|
2023-01-16 13:30:38 +01:00
|
|
|
|
|
|
|
return p
|
|
|
|
}
|