Created
November 24, 2012 04:10
-
-
Save LeFreq/4138312 to your computer and use it in GitHub Desktop.
Probabilistic Chooser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """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