【问题标题】:Stochastic Universal Sampling GA in pythonpython中的随机通用采样GA
【发布时间】:2014-03-30 20:04:51
【问题描述】:

我有一个遗传算法,目前正在使用轮盘赌选择来产生新的种群,我想将其更改为随机通用抽样。

我对这里的工作方式有一个粗略的概述:

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

但我正在努力解决如何准确实施随机通用采样。有谁知道一些伪代码的好来源,或者一个例子?

用一个例子简要说明什么是随机通用采样(但我不确定它是否有意义?):

http://en.wikipedia.org/wiki/Stochastic_universal_sampling

【问题讨论】:

  • 如果您添加一个关于什么是随机通用抽样的链接或描述可能会有所帮助
  • 我已经添加了一个指向关于它的 wiki 文章的链接。它确实有一些示例代码,但我不确定我是否理解它/我不相信它是正确的。
  • 什么是你不明白的:你不确定你的实现有多准确,或者方法本身是否有用?
  • 我觉得方法很有用,我觉得我的实现还不正确,也不确定wiki示例是否正确。
  • 看看this,我试试看

标签: python genetic-algorithm evolutionary-algorithm stochastic


【解决方案1】:
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

【讨论】:

  • 您能否添加一些 cmets 来解释您的逻辑? @inspectorG4dget ?
  • IndexError: 列表索引超出范围
  • @inspectorG4dget - 我也遇到了费尔南多的错误。然而,我对上面的代码 sn-p 做了一些小的改动(在下一条评论中描述),它最终成功了
  • 更改 elif low &gt; num: 并替换为 elif high &lt; num:
猜你喜欢
  • 1970-01-01
  • 2015-03-23
  • 1970-01-01
  • 2021-08-20
  • 2021-03-17
  • 1970-01-01
  • 1970-01-01
  • 2018-06-11
  • 1970-01-01
相关资源
最近更新 更多