Skip to content

Instantly share code, notes, and snippets.

@LeFreq
Created November 24, 2012 04:10
Show Gist options
  • Save LeFreq/4138312 to your computer and use it in GitHub Desktop.
Save LeFreq/4138312 to your computer and use it in GitHub Desktop.
Probabilistic Chooser
"""Given a list of items, each with a floating-point number representing popularity, bounded at the bottom at zero (no negative values), pick an item based on the weight of that popularity.
"""
#NOTE: A variant of this is used in the Network flow class in pangaia. <github/Social-Garden>\
#This was originally developed for genetic algorithms and the program called GA-SOLVE written in Pascal in 1992
# Zero-weighted values are given a value equal to *half* the lowest value.
from random import *
cityrank = {"Denver": 0.5, "Chicago": 1.0, "Houston": 0.0, "NYC": 2.9}
sumrank, ranksaccumulated = 0.0, {}
for city in cityrank:
sumrank += cityrank[city]
ranksaccumulated[city] = sumrank
for city in city
#Now pick a city based on the ranking.
throw = uniform(0.0,sumrank) #XXX need to check if uniform is right function, particularly at end-point
for city in cityrank:
if throw < ranksaccumulated[city]:
break
print city
#this is a crude mockup, untested to document the theory that actually was implemented in the program called GA-SOLVE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment