1

I have a genetic algorithm that is currently using roulette wheel selection to produce a new population and I would like to change it to stochastic universal sampling.

I have a rough outline of how things are going to work here:

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

But i'm struggling with how exactly to implement stochastic universal sampling. Does anyone know of a good source for some pseudo code, or an example?

A brief description of what stochastic universal sampling is with an example (but i'm not sure if it makes sense?):

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

8
  • It might help if you added a link or description of what stochastic universal sampling is Mar 30, 2014 at 20:08
  • I've added a link to a wiki article about it. It does have some example code on there, but I'm not sure I understand it/I'm not convinced it is correct. Mar 30, 2014 at 20:11
  • What is it that you don't understand: are you unsure of how accurate you implementation is, or that the method itself is useful? Mar 30, 2014 at 20:13
  • I think the method is useful, I don't think my implementation is correct yet, and I'm not sure if the wiki example is correct either. Mar 30, 2014 at 20:14
  • 2
    Have a look at this, while I try something Mar 30, 2014 at 20:38

1 Answer 1

5
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
4
  • 1
    Would you be able to add some comments to explain your logic? @inspectorG4dget ?
    – Loligans
    Feb 14, 2018 at 19:32
  • IndexError: list index out of range
    – Fernando
    Mar 31, 2019 at 18:45
  • @inspectorG4dget - I had got Fernando's error, too. However, I made minor changes(described in the next comment) to the code snippet above and it eventually worked out
    – Geralt
    Nov 28, 2019 at 19:45
  • change elif low > num: and replace by elif high < num:
    – Geralt
    Nov 28, 2019 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.