我有一种遗传算法,目前正在使用轮盘赌选择来产生新的种群,我想将其改为随机通用抽样。
我粗略地概述了事情将如何发挥作用:
pointerDistance = sumFitness/popSize
start = rand.uniform(0, pointerDistance)
for i in xrange(popSize):
pointers.append(start + i*pointerDistance)
cumulativeFit = 0
newIndiv = 0
for p in pointers:
while cumulativeFit <= p:
cumulativeFit += pop[newIndiv].fitness
newPop[newIndiv] = copy.deepcopy(pop[newIndiv])
newIndiv += 1
但我正在努力实现如何实施随机通用抽样。有没有人知道一些伪代码的好来源,或者一个例子?
关于随机通用抽样的简要描述(但我不确定它是否有意义?):
答案 0 :(得分:2)
def makeWheel(population):
wheel = []
total = sum(fitness(p) for p in population)
top = 0
for p in population:
f = fitness(p)/total
wheel.append((top, top+f, p))
top += f
return wheel
def binSearch(wheel, num):
mid = len(wheel)//2
low, high, answer = wheel[mid]
if low<=num<=high:
return answer
elif low > num:
return binSearch(wheel[mid+1:], num)
else:
return binSearch(wheel[:mid], num)
def select(wheel, N):
stepSize = 1.0/N
answer = []
r = random.random()
answer.append(binSearch(wheel, r))
while len(answer) < N:
r += stepSize
if r>1:
r %= 1
answer.append(binSearch(wheel, r))
return answer