Пример #1
0
// PopOrdGen generates a population of individuals with ordered integers
// Notes: (1) ngenes = C.NumInts
func PopOrdGen(id int, C *ConfParams) Population {
	o := make([]*Individual, C.Ninds)
	ngenes := C.NumInts
	for i := 0; i < C.Ninds; i++ {
		o[i] = NewIndividual(C.Nova, C.Noor, C.Nbases, make([]int, ngenes))
		for j := 0; j < C.NumInts; j++ {
			o[i].Ints[j] = j
		}
		rnd.IntShuffle(o[i].Ints)
	}
	return o
}
Пример #2
0
// FilterPairs generates 2 lists with ninds/2 items each corresponding to selected pairs
// for reprodoction. Repeated indices in pairs are avoided.
//  Input:
//   selinds -- list of selected individuals len(selinds) == ninds
//  Output:
//   A and B -- [ninds/2] lists with pairs
func FilterPairs(A, B []int, selinds []int) {
	ninds := len(selinds)
	chk.IntAssert(len(A), ninds/2)
	chk.IntAssert(len(B), ninds/2)
	var a, b int
	var aux []int
	for i := 0; i < ninds/2; i++ {
		a, b = selinds[2*i], selinds[2*i+1]
		if a == b {
			if len(aux) == 0 {
				aux = rnd.IntGetShuffled(selinds)
			} else {
				rnd.IntShuffle(aux)
			}
			for _, s := range aux {
				if s != a {
					b = s
					break
				}
			}
		}
		A[i], B[i] = a, b
	}
}