コード例 #1
0
ファイル: ops_strings.go プロジェクト: postfix/goga-1
// StrMutation performs the mutation of genetic data from A
//  Output: modified individual 'A'
func StrMutation(A []string, time int, ops *OpsData) {
	size := len(A)
	if !rnd.FlipCoin(ops.Pm) || size < 1 {
		return
	}
	pos := rnd.IntGetUniqueN(0, size, ops.Nchanges)
	for _, i := range pos {
		A[i] = "TODO" // TODO: improve this
	}
}
コード例 #2
0
ファイル: ops_bytes.go プロジェクト: postfix/goga-1
// KeyMutation performs the mutation of genetic data from A
//  Output: modified individual 'A'
func KeyMutation(A []byte, time int, ops *OpsData) {
	size := len(A)
	if !rnd.FlipCoin(ops.Pm) || size < 1 {
		return
	}
	pos := rnd.IntGetUniqueN(0, size, ops.Nchanges)
	for _, i := range pos {
		v := rnd.Int(0, 100)
		A[i] = byte(v) // TODO: improve this
	}
}
コード例 #3
0
ファイル: ops_functions.go プロジェクト: postfix/goga-1
// FunMutation performs the mutation of genetic data from A
//  Output: modified individual 'A'
func FunMutation(A []Func_t, time int, ops *OpsData) {
	size := len(A)
	if !rnd.FlipCoin(ops.Pm) || size < 1 {
		return
	}
	pos := rnd.IntGetUniqueN(0, size, ops.Nchanges)
	for _, i := range pos {
		// TODO: improve this
		A[i] = func(ind *Individual) string { return "mutated" }
	}
}
コード例 #4
0
ファイル: intoperators.go プロジェクト: cpmech/goga
// MtIntBin performs the mutation of a binary chromosome
//  Output: modified individual 'A'
func MtIntBin(A []int, prms *Parameters) {
	size := len(A)
	if !rnd.FlipCoin(prms.IntPm) || size < 1 {
		return
	}
	pos := rnd.IntGetUniqueN(0, size, prms.IntNchanges)
	for _, i := range pos {
		if A[i] == 0 {
			A[i] = 1
		} else {
			A[i] = 0
		}
	}
}
コード例 #5
0
ファイル: ops_ints.go プロジェクト: postfix/goga-1
// IntBinMutation performs the mutation of a binary chromosome
//  Output: modified individual 'A'
func IntBinMutation(A []int, time int, ops *OpsData) {
	size := len(A)
	if !rnd.FlipCoin(ops.Pm) || size < 1 {
		return
	}
	pos := rnd.IntGetUniqueN(0, size, ops.Nchanges)
	for _, i := range pos {
		if A[i] == 0 {
			A[i] = 1
		} else {
			A[i] = 0
		}
	}
}
コード例 #6
0
ファイル: ops_ints.go プロジェクト: postfix/goga-1
// IntMutation performs the mutation of genetic data from A
//  Output: modified individual 'A'
func IntMutation(A []int, time int, ops *OpsData) {
	size := len(A)
	if !rnd.FlipCoin(ops.Pm) || size < 1 {
		return
	}
	pos := rnd.IntGetUniqueN(0, size, ops.Nchanges)
	for _, i := range pos {
		m := rnd.Int(1, int(ops.Mmax))
		if rnd.FlipCoin(0.5) {
			A[i] += m * A[i]
		} else {
			A[i] -= m * A[i]
		}
	}
}
コード例 #7
0
ファイル: operators.go プロジェクト: postfix/goga-1
// GenerateCxEnds randomly computes the end positions of cuts in chromosomes
//  Input:
//   size  -- size of chromosome
//   ncuts -- number of cuts to be used, unless cuts != nil
//   cuts  -- cut positions. can be nil => use ncuts instead
//  Output:
//   ends -- end positions where the last one equals size
//  Example:
//        0 1 2 3 4 5 6 7
//    A = a b c d e f g h    size = 8
//         ↑       ↑     ↑   cuts = [1, 5]
//         1       5     8   ends = [1, 5, 8]
func GenerateCxEnds(size, ncuts int, cuts []int) (ends []int) {

	// handle small slices
	if size < 2 {
		return
	}
	if size == 2 {
		return []int{1, size}
	}

	// cuts slice is given
	if len(cuts) > 0 {
		ncuts = len(cuts)
		ends = make([]int, ncuts+1)
		ends[ncuts] = size
		for i, cut := range cuts {
			if cut < 1 || cut >= size {
				chk.Panic("cut=%d is outside the allowed range: 1 ≤ cut ≤ size-1", cut)
			}
			if i > 0 {
				if cut == cuts[i-1] {
					chk.Panic("repeated cut values are not allowed: cuts=%v", cuts)
				}
			}
			ends[i] = cut
		}
		sort.Ints(ends)
		return
	}

	// randomly generate cuts
	if ncuts < 1 {
		ncuts = 1
	}
	if ncuts >= size {
		ncuts = size - 1
	}
	ends = make([]int, ncuts+1)
	ends[ncuts] = size

	// pool of values for selections
	pool := rnd.IntGetUniqueN(1, size, ncuts)
	sort.Ints(pool)
	for i := 0; i < ncuts; i++ {
		ends[i] = pool[i]
	}
	return
}
コード例 #8
0
ファイル: intoperators.go プロジェクト: cpmech/goga
// MtInt performs the mutation of genetic data from A
//  Output: modified individual 'A'
func MtInt(A []int, prms *Parameters) {
	size := len(A)
	if !rnd.FlipCoin(prms.IntPm) || size < 1 {
		return
	}
	mmax := 2
	pos := rnd.IntGetUniqueN(0, size, prms.IntNchanges)
	for _, i := range pos {
		m := rnd.Int(1, mmax)
		if rnd.FlipCoin(0.5) {
			A[i] += m * A[i]
		} else {
			A[i] -= m * A[i]
		}
	}
}
コード例 #9
0
ファイル: t_ints_test.go プロジェクト: postfix/goga-1
func Test_int01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("int01. organise sequence of ints")
	io.Pf("\n")

	// initialise random numbers generator
	rnd.Init(0) // 0 => use current time as seed

	// parameters
	C := NewConfParams()
	C.Nova = 1
	C.Noor = 0
	C.Nisl = 1
	C.Ninds = 20
	C.RegTol = 0
	C.NumInts = 20
	//C.GAtype = "crowd"
	C.CrowdSize = 2
	C.Tf = 50
	C.Verbose = chk.Verbose
	C.CalcDerived()

	// mutation function
	C.Ops.MtInt = func(A []int, time int, ops *OpsData) {
		size := len(A)
		if !rnd.FlipCoin(ops.Pm) || size < 1 {
			return
		}
		pos := rnd.IntGetUniqueN(0, size, ops.Nchanges)
		for _, i := range pos {
			if A[i] == 1 {
				A[i] = 0
			}
			if A[i] == 0 {
				A[i] = 1
			}
		}
	}

	// generation function
	C.PopIntGen = func(id int, cc *ConfParams) Population {
		o := make([]*Individual, cc.Ninds)
		genes := make([]int, cc.NumInts)
		for i := 0; i < cc.Ninds; i++ {
			for j := 0; j < cc.NumInts; j++ {
				genes[j] = rand.Intn(2)
			}
			o[i] = NewIndividual(cc.Nova, cc.Noor, cc.Nbases, genes)
		}
		return o
	}

	// objective function
	C.OvaOor = func(ind *Individual, idIsland, time int, report *bytes.Buffer) {
		score := 0.0
		count := 0
		for _, val := range ind.Ints {
			if val == 0 && count%2 == 0 {
				score += 1.0
			}
			if val == 1 && count%2 != 0 {
				score += 1.0
			}
			count++
		}
		ind.Ovas[0] = 1.0 / (1.0 + score)
		return
	}

	// run optimisation
	evo := NewEvolver(C)
	evo.Run()

	// results
	ideal := 1.0 / (1.0 + float64(C.NumInts))
	io.PfGreen("\nBest = %v\nBestOV = %v  (ideal=%v)\n", evo.Best.Ints, evo.Best.Ovas[0], ideal)
}