Created
September 13, 2015 05:19
-
-
Save seanlinehan/62019c42a8b490a10378 to your computer and use it in GitHub Desktop.
This file contains 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
def optimize(population, rounds): | |
population = sort_population(population) | |
# Set our seed set to a pretty good default | |
test_set = population[:10] | |
# To compare other sets against | |
highest_exposure = len(get_intersection(test_set)) | |
print "Baseline: %d" % highest_exposure | |
print "==============" | |
for i in range(rounds): | |
# Find somebody to remove | |
index = find_worst(test_set) | |
# Find somebody to replace them | |
replacement = get_replacement(population, test_set) | |
# Here we get our new test set | |
# We are using a form of simulated annealing to try | |
# to avoid local maximas | |
if random.random() < heat(i, rounds): | |
new_set = remove_at_index(test_set, random.randrange(len(test_set))) | |
new_set.append(replacement) | |
else: | |
new_set = remove_at_index(test_set, index) | |
new_set.append(replacement) | |
# Test it out | |
exposure = len(get_intersection(new_set)) | |
if exposure > highest_exposure: | |
highest_exposure = exposure | |
test_set = new_set | |
print "Conclusion: %d" % highest_exposure | |
return test_set |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment