def determine_if_winner(): print_points() # Pick a winner at random with a single random number. We divide the number # line up like: # # [ a_points | b_points | c_points | ... everything else ... ] # # and then we choose a place on the number line randomly: # # [ a_points | b_points | c_points | ... everything else ... ] # ^ # or: # # [ a_points | b_points | c_points | ... everything else ... ] # ^ # You can think of this as assigning a range to each player: # # A wins if random is [0, a_points) # B wins if random is [a_points, a_points + b_points) # C wins if random is [a_points + b_points, a_points + b_points + c_points) # no one wins if random is [a_points + b_points + c_points, 1) rnd = util.random() points_so_far = 0 for user, user_points in util.get_user_points().items(): if rnd < 0.00001 * (user_points + points_so_far): raise Exception('%s wins!' % user) points_so_far += user_points print('The game continues.')
def reproduce(population, top=0.7, determinism=0.8, crossover=lambda: random()): """ Returns a new population of candidates. Selects parent that are fit to reproduce. Recombines random pairs of parents to new child candidates. """ parents = select(population, top, determinism) children = [] for i in range(len(population)): i = random(len(parents)) j = choice( range(0,i) + range(i+1, len(parents)) ) k = crossover try: k = k() except: pass children.append(recombine(parents[i], parents[j], crossover=k)) return children
def determine_if_winner(): print_points() # Pick a winner at random with a single random number. We divide the number # line up like: # # [ a_points | b_points | c_points | ... everything else ... ] # # and then we choose a place on the number line randomly: # # [ a_points | b_points | c_points | ... everything else ... ] # ^ # or: # # [ a_points | b_points | c_points | ... everything else ... ] # ^ # You can think of this as assigning a range to each player: # # A wins if random is [0, a_points) # B wins if random is [a_points, a_points + b_points) # C wins if random is [a_points + b_points, a_points + b_points + c_points) # no one wins if random is [a_points + b_points + c_points, 1) # # The number line defaults to a range of 100000. # In order to ensure fairness with large numbers of points, # we extend the line if total points across all players exceed that value. # Relative chance per-player is still preserved. rnd = util.random() all_user_points = util.get_user_points() summed_user_points = [(user, util.total_user_points(user_points)) for user, user_points in all_user_points.items()] # Don't include negative values when summing user points, # since they have no chance to win anyway # There shouldn't be negative values, but just in case... total_points = sum([ user_points for user, user_points in summed_user_points if user_points > 0 ]) scalar = min(0.00001, 1.0 / total_points) points_so_far = 0 print('Probability of winning:') for user, user_points in summed_user_points: print('%s: %.3f%%' % (user, user_points * scalar * 100)) for user, user_points in summed_user_points: if rnd < scalar * (user_points + points_so_far): raise Exception('%s wins!' % user) points_so_far += user_points print('The game continues.')
def select(population, top=0.7, determinism=0.8): """ Returns a selection of fit candidates from the population. - top: roughly the fittest 70% candidates are allowed to reproduce. - determinism: there is a 20% chance that good candidates are ignored, this keeps the population diverse. """ population = sort_by_fitness(population) population = [candidate for (fitness, candidate) in population] parents = list(population) i = len(parents) while len(parents) > len(population)*top: i = (i-1) % len(parents) if random() < determinism: parents.pop(i) return parents
def random(rows: int, cols: int): return Matrix([[util.random() for _ in range(cols)] for _ in range(rows)])
def run(window): timer = pygame.time.Clock() width = window.get_width() height = window.get_height() font = pygame.freetype.SysFont('Comic Sans MS', 18) boundary = Rectangle((0, 0), (width, height)) bodies = [] using_quadtree = False for _ in range(0, 10): bodies.append( Body([random_zero_to_max(width), random_zero_to_max(height)], random(2, 10))) _, _, total_energy = calculate_energy(bodies) running = True frame_rate = [] while running: window.fill((0, 0, 0)) dt = timer.tick() qtree = Quadtree(boundary, 4) for body in bodies: qtree.insert(Point([body.x[0], body.x[1]])) for body in bodies: if not body.highlight: if using_quadtree: region = Circle([body.x[0], body.x[1]], body.r * 2 - 1) others = qtree.query(region) body.physics(others) else: body.physics(bodies) body.render(window) body.move(dt) ajust_speeds(bodies, total_energy, 0.001, 1) if len(frame_rate) > 10: frame_rate.pop(0) frame_rate.append(1000 / dt) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.MOUSEBUTTONUP: using_quadtree = not using_quadtree if using_quadtree: text_render, _ = font.render('Using QuadTree', (255, 255, 255)) else: text_render, _ = font.render('Not Using QuadTree', (255, 255, 255)) rect = text_render.get_rect(center=(400 + 100 / 2, 300 + 20 / 2)) window.blit(text_render, rect) text_render, _ = font.render( '{} FPS'.format(round(average(frame_rate), 1)), (255, 255, 255)) rect = text_render.get_rect(center=(400 + 100 / 2, 350 + 20 / 2)) window.blit(text_render, rect) pygame.display.update()