Exemplo n.º 1
0
// Partially mapped crossover.
func (a *GAOrderedIntGenome) Crossover(bi GAGenome, p1, p2 int) (GAGenome, GAGenome) {
	ca := a.Copy().(*GAOrderedIntGenome)
	b := bi.(*GAOrderedIntGenome)
	cb := b.Copy().(*GAOrderedIntGenome)
	copy(ca.Gene[p1:p2+1], b.Gene[p1:p2+1])
	copy(cb.Gene[p1:p2+1], a.Gene[p1:p2+1])
	//Proto child needs fixing
	amap := new(vector.IntVector)
	bmap := new(vector.IntVector)
	for i := p1; i <= p2; i++ {
		ma, found := ca.pmxmap(ca.Gene[i], p1, p2)
		if found {
			amap.Push(ma)
			if bmap.Len() > 0 {
				i1 := amap.Pop()
				i2 := bmap.Pop()
				ca.Gene[i1], cb.Gene[i2] = cb.Gene[i2], ca.Gene[i1]
			}
		}
		mb, found := cb.pmxmap(cb.Gene[i], p1, p2)
		if found {
			bmap.Push(mb)
			if amap.Len() > 0 {
				i1 := amap.Pop()
				i2 := bmap.Pop()
				ca.Gene[i1], cb.Gene[i2] = cb.Gene[i2], ca.Gene[i1]
			}
		}
	}
	ca.Reset()
	cb.Reset()
	return ca, cb
}
Exemplo n.º 2
0
func Qsort(list []interface{}, cmp func(a, b interface{}) int) (ret []interface{}, err os.Error) {
	if len(list) <= 0 {
		err = Error("len")
		return
	}
	ret = make([]interface{}, len(list))
	ret = list
	stack := new(vector.IntVector)
	stack.Push(0)
	stack.Push(len(list) - 1)
	for len(*stack) != 0 {
		tail := stack.Pop()
		head := stack.Pop()
		pivot := ret[head+((tail-head)>>1)]
		i := head - 1
		j := tail + 1
		for {
			for i++; cmp(ret[i], pivot) < 0; i++ {
			}
			for j--; cmp(ret[j], pivot) > 0; j-- {
			}
			if i >= j {
				break
			}
			tmp := ret[i]
			ret[i] = ret[j]
			ret[j] = tmp
		}
		if head < (i - 1) {
			stack.Push(head)
			stack.Push(i - 1)
		}
		if (j + 1) < tail {
			stack.Push(j + 1)
			stack.Push(tail)
		}
	}
	return
}
Exemplo n.º 3
0
// Sorts channels in random order
func (dbs *DBS) RandomChan() {

	var SortCh vector.IntVector

	for i := 0; i < NCh; i++ {
		SortCh.Push(i)
		dbs.RndCh[i] = i
	}

	//randomizesd reserved top canals
	/*	for i := 10; i > 1; i-- {
			j := dbs.Rgen.Intn(i) + NCh-10
			SortCh.Swap(NCh-10+i-1, j)
			dbs.RndCh[NCh-10+i-1] =SortCh.Pop()
		}
		dbs.RndCh[NCh-10]=SortCh.Pop()
	*/
	//randomizes other canals
	/*	for i := NCh - 11; i > NChRes; i-- {
			j := dbs.Rgen.Intn(i-NChRes) + NChRes
			SortCh.Swap(i, j)
			dbs.RndCh[i] =SortCh.Pop()
		}
		dbs.RndCh[NChRes] = SortCh.Pop()
		dbs.RndCh[0] = 0
	*/
	//fmt.Println(dbs.RndCh);

	for i := NCh - 1; i > NChRes; i-- {
		j := dbs.Rgen.Intn(i-NChRes) + NChRes
		SortCh.Swap(i, j)
		dbs.RndCh[i] = SortCh.Pop()
	}
	dbs.RndCh[NChRes] = SortCh.Pop()
	dbs.RndCh[0] = 0

}