// SetFloat returns the float corresponding to gene 'i' // igene -- is the index of gene/float in [0, Nfltgenes] func (o *Individual) SetFloat(igene int, x float64) { if o.Nbases > 1 { values := make([]float64, o.Nbases) rnd.Float64s(values, 0, 1) sum := la.VecAccum(values) for j := 0; j < o.Nbases; j++ { o.Floats[igene*o.Nbases+j] = x * values[j] / sum } return } o.Floats[igene] = x }
// SimpleChromo splits 'genes' into 'nbases' unequal parts // Input: // genes -- a slice whose size equals to the number of genes // nbases -- number of bases used to split 'genes' // Output: // chromo -- the chromosome // // Example: // // genes = [0, 1, 2, ... nbases-1, 0, 1, 2, ... nbases-1] // \___________________/ \___________________/ // gene # 0 gene # 1 // func SimpleChromo(genes []float64, nbases int) (chromo []float64) { ngenes := len(genes) chromo = make([]float64, ngenes*nbases) values := make([]float64, nbases) var sumv float64 for i, g := range genes { rnd.Float64s(values, 0, 1) sumv = la.VecAccum(values) for j := 0; j < nbases; j++ { chromo[i*nbases+j] = g * values[j] / sumv } } return }
func Test_sus01(tst *testing.T) { //verbose() chk.PrintTitle("sus01. stochastic-universal-sampling") f := []float64{2.0, 1.8, 1.6, 1.4, 1.2, 1.0, 0.8, 0.6, 0.4, 0.2, 0.0} n := len(f) p := make([]float64, n) sum := la.VecAccum(f) for i := 0; i < n; i++ { p[i] = f[i] / sum } cs := make([]float64, len(p)) utl.CumSum(cs, p) selinds := make([]int, 6) SUSselect(selinds, cs, 0.1) io.Pforan("selinds = %v\n", selinds) chk.Ints(tst, "selinds", selinds, []int{0, 1, 2, 3, 5, 7}) }
func Test_rws01(tst *testing.T) { //verbose() chk.PrintTitle("rws01. roulette whell selection") f := []float64{2.0, 1.8, 1.6, 1.4, 1.2, 1.0, 0.8, 0.6, 0.4, 0.2, 0.0} n := len(f) p := make([]float64, n) sum := la.VecAccum(f) for i := 0; i < n; i++ { p[i] = f[i] / sum } cs := make([]float64, len(p)) utl.CumSum(cs, p) selinds := make([]int, 6) RouletteSelect(selinds, cs, []float64{0.81, 0.32, 0.96, 0.01, 0.65, 0.42}) io.Pforan("selinds = %v\n", selinds) chk.Ints(tst, "selinds", selinds, []int{5, 1, 8, 0, 4, 2}) }
// Balance computes load balance func (o System) Balance(P []float64) float64 { return math.Abs(la.VecAccum(P) - o.Pdemand - o.Ploss(P)) }