def random_aabb(self): aabb = AABB( lower_bound=(rand_float(-self.world_extent, self.world_extent), rand_float(0.0, 2.0 * self.world_extent))) w = (2.0 * self.proxy_extent, 2.0 * self.proxy_extent) aabb.upper_bound = aabb.lower_bound + w return aabb
def random_aabb(self): aabb = AABB( lower_bound=(rand_float(-self.world_extent, self.world_extent), rand_float(0.0, 2.0 * self.world_extent)) ) w = (2.0 * self.proxy_extent, 2.0 * self.proxy_extent) aabb.upper_bound = aabb.lower_bound + w return aabb
def move_aabb(self, aabb): d = Vec2(rand_float(-0.5, 0.5), rand_float(-0.5, 0.5)) aabb.lower_bound += d aabb.upper_bound += d c0 = 0.5 * (aabb.lower_bound + aabb.upper_bound) min_ = (-self.world_extent, 0.0) max_ = (self.world_extent, 2.0 * self.world_extent) c = clamp_vector(c0, min_, max_) aabb.lower_bound += c - c0 aabb.upper_bound += c - c0
def move_aabb(self, aabb): d = Vec2(rand_float(-.5, .5), rand_float(-.5, .5)) aabb.lower_bound += d aabb.upper_bound += d c0 = 0.5 * (aabb.lower_bound + aabb.upper_bound) min_ = (-self.world_extent, 0.0) max_ = (self.world_extent, 2.0 * self.world_extent) c = clamp_vector(c0, min_, max_) aabb.lower_bound += (c - c0) aabb.upper_bound += (c - c0)
def rand_dist(): while True: yield numpy.array([ rand_float() for dimensions in xrange(no_dimensions) ])
def update(self, best_neighbor_position): """Update the particle's position, velocity, and inertia.""" cognitive_mod = rand_float() social_mod = rand_float() current_inertia = self._inertial_dampening_schedule(self._initial_inertia, self._inertial_dampening, self._time) inertial_velocity = current_inertia * self._velocity cognitive_velocity = self._cognitive_comp * cognitive_mod * (self._best_position - self.position) social_velocity = self._social_comp * social_mod * (best_neighbor_position - self.position) self._velocity = inertial_velocity + cognitive_velocity + social_velocity numpy.clip(self._velocity, -self.MAX_VELOCITY, self.MAX_VELOCITY, out=self._velocity) self.position += self._velocity * self._velocity_dampening numpy.clip(self.position, self.MIN_POSITION, self.MAX_POSITION, out=self.position) self._time += 1
def break_two_way_tie(team_one: str, team_two: str, simulated_season: List): teams = {team_one, team_two} game = [ game for game in simulated_season if game['winner'] in teams and game['loser'] in teams ] if game: return game[0]['winner'] return team_one if rand_float() > 0.5 else team_two
def simulate_game(game): home_team, away_team = game['home_team'], game['away_team'] if game.get('home_points') is not None: winner, loser = ( home_team, away_team) if game['home_points'] > game['away_points'] else ( away_team, home_team) else: winner, loser = ( home_team, away_team) if game['home_team_win_pct'] > rand_float() else ( away_team, home_team) are_both_teams_division_one = home_team in TEAMS and away_team in TEAMS is_conf_game = are_both_teams_division_one and TEAMS[home_team][ 'conference'] == TEAMS[away_team]['conference'] return dict(winner=winner, loser=loser, is_conf_game=is_conf_game)
def evolve(roads, cars, counts, bonus, epochs, starting_vals, mutation_chance=0.001, pop_size=20, std=5, temp=15, delete_frac=0.5): solutions = [] for __ in range(pop_size): solution = {} solution["vals"] = starting_vals solution["score"] = 0 solution["delete_prob"] = 0 solutions.append(solution) score_curve = [] for __ in range(epochs): # delete 50% according to randomised scores # for i in range (50%) # copy a random solution # randomly change solution # add into list of solutions # get scores sum_scores = 0 for s in solutions: score = shitty_simulate(roads, cars, s["vals"], bonus, counts) s["score"] = score sum_scores += score score_curve.append(sum_scores) for s in solutions: s["delete_prob"] = np.random.normal(s["score"], temp) sorted_solutions = sorted(solutions, key=lambda s: s[ "delete_prob"]) # delete probability is actaully the random score solutions = sorted_solutions[int(delete_frac * len(sorted_solutions)):] for s in solutions.copy(): s_copy = s.copy() for schedule in s_copy["vals"]: for idx in range(len(schedule)): if rand_float() < mutation_chance: if rand_float() > 0.5: schedule[idx] = (schedule[idx][0], schedule[idx][1] + np.random.normal(0, std)) else: schedule[idx] = (schedule[idx][0], schedule[idx][1] - np.random.normal(0, std)) schedule[idx] = (schedule[idx][0], max(int(schedule[idx][1]), 0)) solutions.append(s_copy) for s in solutions: score = shitty_simulate(roads, cars, s["vals"], bonus, counts) s["score"] = score best_score = 0 best_solution = None for s in solutions: if s["score"] > best_score: best_score = s["score"] best_solution = s for schedule in best_solution["vals"]: schedule = sorted(schedule, key=lambda s: s[1], reverse=True) plt.plot(score_curve) plt.show() return best_solution["vals"], best_score
from random import randrange as rand_range, uniform as rand_float N = 1000 print("Running randomized trials (boolean)") avg_comp = None for i in range(10): # run this many trials timeseries = [(t / N, float(rand_range(0.0, 2.0))) for t in range(N)] assert_equal(uncompress(*compress(timeseries)), timeseries) comp = assert_positive_compression(compress(timeseries)[0], timeseries) avg_comp = comp if not avg_comp else (comp + i * avg_comp) / (i + 1) print(' Average compression: {:3.2f}%'.format(avg_comp)) print("Running randomized trials (integer [0, 9])") avg_comp = None for i in range(10): # run this many trials timeseries = [(t / N, float(rand_range(0.0, 10.0))) for t in range(N)] assert_equal(uncompress(*compress(timeseries)), timeseries) comp = assert_positive_compression(compress(timeseries)[0], timeseries) avg_comp = comp if not avg_comp else (comp + i * avg_comp) / (i + 1) print(' Average compression: {:3.2f}%'.format(avg_comp)) print("Running randomized trials (normal dist [-10.0, 10.0])") avg_comp = None for i in range(10): # run this many trials timeseries = [(t / N, rand_float(-10.0, 10.0)) for t in range(N)] assert_equal(uncompress(*compress(timeseries)), timeseries) comp = assert_positive_compression(compress(timeseries)[0], timeseries) avg_comp = comp if not avg_comp else (comp + i * avg_comp) / (i + 1) print(' Average compression: {:3.2f}%'.format(avg_comp)) print('compress()/uncompress() Testing Passed!')
def set_active_random(self): self.is_active = rand_float() < self.probability_buffer
num_acc = int(arguments[1]) host = arguments[2] rmi_port = int(arguments[3]) ReaderList = [] # Fetch the remote client handler object name_server = pyro.locateNS(host=host, port=rmi_port) client_handler_uri = name_server.lookup('client_handler') client_handler = pyro.Proxy(client_handler_uri) for iteration in range(0, num_acc): rSeq, sSeq, oVal = client_handler.handle_reader(rID) ReaderList.append((rSeq, sSeq, oVal)) sleep(rand_float(0, 10)) client_handler.close() # Create log file print(''.join( ["Client Type: Reader \nClient Name: ", str(rID), "\nrSeq\tsSeq\toVal"])) # Iterate through the Readerlist and write to file for row in ReaderList: row_string = '' for item in row: row_string += (str(item) + '\t' * 2) print(row_string)