def Play_TrueSkill_match(self, board, rounds, bot1, bot2): """Plays a tourney with the given bots for the given round. Prints results to screen. Args: rounds (int): number of rounds to be played bot1 (class object): object of bot1 bot2 (class object): object of bot2 Returns: bot1, bot2 (class objects): bot1 and bot2 objects with updated scores """ # Retrieve rating from the bot and put it in TrueSkill object r_bot1 = Rating(bot1.rating) r_bot2 = Rating(bot2.rating) # Play a single match and record its output outcome = self.Play_single_bot_match(bot1, bot2, board) if outcome == 0: # it is a draw r_bot1, r_bot2 = rate_1vs1(r_bot1, r_bot2, True) elif outcome == 1: # bot1 wins r_bot1, r_bot2 = rate_1vs1(r_bot1, r_bot2) elif outcome == 2: # bot2 wins r_bot2, r_bot1 = rate_1vs1(r_bot2, r_bot1) # Update rating bot1.rating = r_bot1 bot2.rating = r_bot2 return bot1, bot2
def calculate_scores(): env = TrueSkill(draw_probability=0.14) # trueskill.setup(draw_probability=0.14) scores = load_previous_scores() vote_count = 0 with open(FLAGS.votes_file, 'r') as votes_file: for line in votes_file: img_1, img_2, winner = line.strip().split(",") img_1_ts = scores[img_1] img_2_ts = scores[img_2] if winner == "0": #winner left img_1_ts_new, img_2_ts_new = rate_1vs1(img_1_ts, img_2_ts) else: #winner right img_2_ts_new, img_1_ts_new = rate_1vs1(img_2_ts, img_1_ts) scores[img_1] = img_1_ts_new scores[img_2] = img_2_ts_new vote_count += 1 if vote_count % 1000 == 0: print("Processed {} votes".format(vote_count)) get_highest_rated(scores) save_scores(scores)
def test_recalculate(self): """ Test the recalculate command. """ users = [UserFactory(), UserFactory()] default_competition = Competition.objects.get_default_competition() Game.objects.announce(users[0], users[1], default_competition) Game.objects.announce(users[0], users[1], default_competition) args = [25, 8] opts = {} stdout = StringIO() call_command('recalculate', stdout=stdout, *args, **opts) winner_score, loser_score = rate_1vs1( Rating(25, 8), Rating(25, 8) ) expected_winner_score, expected_loser_score = rate_1vs1( Rating(winner_score.mu, winner_score.sigma), Rating(loser_score.mu, loser_score.sigma) ) game = Game.objects.last() winner_score = game.winner.scores.get() loser_score = game.loser.scores.get() self.assertAlmostEqual(winner_score.score, expected_winner_score.mu) self.assertAlmostEqual(winner_score.stdev, expected_winner_score.sigma) self.assertAlmostEqual(loser_score.score, expected_loser_score.mu) self.assertAlmostEqual(loser_score.stdev, expected_loser_score.sigma)
def rate(ra, rb, score, env): if score == 1: return trueskill.rate_1vs1(ra, rb, env=env) elif score == -1: return reversed(trueskill.rate_1vs1(rb, ra, env=env)) else: return trueskill.rate_1vs1(ra, rb, env=env, drawn=True)
def play_round(): this_round = get_last_round() + 1 id1, id2 = pick_pair() name1 = get_id_name(id1) name2 = get_id_name(id2) m1, s1 = get_id_stats(id1) m2, s2 = get_id_stats(id2) r1 = TS.create_rating(m1, s1) r2 = TS.create_rating(m2, s2) present(name1, name2) response = get_input() if response == 'a': new_r1, new_r2 = trueskill.rate_1vs1(r1, r2, env=TS) elif response == 'b': new_r2, new_r1 = trueskill.rate_1vs1(r2, r1, env=TS) elif response == 'q': return False else: print("Invalid key pressed: {}".format(response)) return True update_name_stats(id1, new_r1.mu, new_r1.sigma, this_round) update_name_stats(id2, new_r2.mu, new_r2.sigma, this_round) return True
def rate(ra, rb, score, env): if score == 1: return trueskill.rate_1vs1(ra, rb, env=env) elif score == -1: return reversed(trueskill.rate_1vs1(rb, ra, env=env)) else: return trueskill.rate_1vs1(ra, rb, env=env, drawn=True)
def update_rating(player1, player2, win): """Update player ratings after a game Args: player1 (PlayerObject): player2 (PlayerObject): win (bool): whether player 1 won """ # Update Elo rating A, B = EloRating(player1.rating, player2.rating, K, win) player1.rating = A player2.rating = B # Update Glicko-2 rating player1.glicko.update_player([player2.glicko.getRating()], [player2.glicko.getRd()], [win]) player2.glicko.update_player([player1.glicko.getRating()], [player1.glicko.getRd()], [not win]) # Update Trueskill rating winner, loser = trueskill.rate_1vs1( player1.trueskill, player2.trueskill) if win == 1 else trueskill.rate_1vs1( player2.trueskill, player1.trueskill) player1.trueskill = winner if win else loser player2.trueskill = loser if win else winner
def rate_match(self): """Use TrueSkill to modify players skill""" # TrueSkill setup setup(mu=TRUESKILL_MU, sigma=TRUESKILL_SIGMA, beta=TRUESKILL_BETA, tau=TRUESKILL_TAU) if 'scipy' in backends.available_backends(): # scipy can be used in the current environment backends.choose_backend(backend='scipy') self.player_1_trueskill_mu_before_match = self.player_1.trueskill_mu self.player_1_trueskill_sigma_before_match = self.player_1.trueskill_sigma self.player_2_trueskill_mu_before_match = self.player_2.trueskill_mu self.player_2_trueskill_sigma_before_match = self.player_2.trueskill_sigma player_1_trueskill = Rating(self.player_1_trueskill_mu_before_match, self.player_1_trueskill_sigma_before_match) player_2_trueskill = Rating(self.player_2_trueskill_mu_before_match, self.player_2_trueskill_sigma_before_match) if self.winner == self.player_1: new_player_1_trueskill, new_player_2_trueskill = rate_1vs1( player_1_trueskill, player_2_trueskill) else: new_player_2_trueskill, new_player_1_trueskill = rate_1vs1( player_2_trueskill, player_1_trueskill) self.player_1_trueskill_mu_after_match = new_player_1_trueskill.mu self.player_1_trueskill_sigma_after_match = new_player_1_trueskill.sigma self.player_2_trueskill_mu_after_match = new_player_2_trueskill.mu self.player_2_trueskill_sigma_after_match = new_player_2_trueskill.sigma self.player_1.trueskill_mu = self.player_1_trueskill_mu_after_match self.player_1.trueskill_sigma = self.player_1_trueskill_sigma_after_match self.player_2.trueskill_mu = self.player_2_trueskill_mu_after_match self.player_2.trueskill_sigma = self.player_2_trueskill_sigma_after_match self.player_1.save() self.player_2.save()
def multiplayer_trueskill_applier(df, race_id_col, player1_col, player2_col, starting_mu=25.0, starting_std=25.0/3.0): env = trueskill.TrueSkill(draw_probability=0.015) # Get a list of unique players unique_players = df[player1_col].unique().tolist() + df[player2_col].unique().tolist() # Initialise a dictionary with default elos for each player ratings_dict = {team: Rating(mu=starting_mu, sigma=starting_std) for team in unique_players} # Create dict where we will update based on the race_id and horse_id before_game_ratings = [] after_game_ratings = [] # Loop over races in each group for idx, row in df.iterrows(): before_game_ratings.append([ratings_dict[row[player1_col]], ratings_dict[row[player2_col]]]) if row.winner_1 == 1: new_r1, new_r2 = trueskill.rate_1vs1(ratings_dict[row[player1_col]], ratings_dict[row[player2_col]]) ratings_dict[row[player1_col]] = new_r1 ratings_dict[row[player2_col]] = new_r2 elif row.winner_2 == 1: new_r1, new_r2 = trueskill.rate_1vs1(ratings_dict[row[player2_col]], ratings_dict[row[player1_col]]) ratings_dict[row[player1_col]] = new_r2 ratings_dict[row[player2_col]] = new_r1 elif row.draw == 1: new_r1, new_r2 = trueskill.rate_1vs1(ratings_dict[row[player1_col]], ratings_dict[row[player2_col]], drawn=True) ratings_dict[row[player1_col]] = new_r1 ratings_dict[row[player2_col]] = new_r2 else: print('error') after_game_ratings.append([ratings_dict[row[player1_col]], ratings_dict[row[player2_col]]]) return before_game_ratings, after_game_ratings, ratings_dict
def play_1v1(p1_move, p2_move, p1_rating, p2_rating, cur_round): if cur_round % 2 == 0: # arena = Arena(n1p, n2p, game) p1_color = HexBoard.BLUE p2_color = HexBoard.RED blue_ai_move = p1_move red_ai_move = p2_move else: # arena = Arena(n2p, n1p, game) p1_color = HexBoard.RED p2_color = HexBoard.BLUE blue_ai_move = p2_move red_ai_move = p1_move board = HB(BOARD_SIZE, n_players=0, enable_gui=False, interactive_text=False, ai_color=None, ai_move=None, blue_ai_move=blue_ai_move, red_ai_move=red_ai_move, move_list=[]) winning_color = board.get_winning_color() if winning_color == p1_color: p1_rating, p2_rating = ts.rate_1vs1(p1_rating, p2_rating) elif winning_color == p2_color: p2_rating, p1_rating = ts.rate_1vs1(p2_rating, p1_rating) else: raise ValueError("There was a draw or an unexpected winner: %d" % winning_color) return p1_rating, p2_rating
def cal_TrueSkill_demo(args): data, params, big_zero = args[0], args[1], args[2] user = defaultdict(ts.Rating) goal = defaultdict(ts.Rating) for i in data.iterrows(): rows = i[1] user[rows['uid']] = ts.Rating(rows['u_player'], 3.0) goal[rows['gid']] = ts.Rating(rows['g_player'], 3.0) for i in data.iterrows(): idx = i[0] item = i[1] # 对每一条迭代进来的记录,分别初始化uid和pid的能力值,然后用上式计算。 u_player = user[item['uid']] g_player = goal[item['gid']] # 核心句,计算一次做题提交之后,trueskill的变化。根据user做题的对错情况,决定这个delta数值是正还是负。 data.loc[idx, 'ts_pts'] = (g_player.mu / u_player.mu) * norm_duration( item, idx, u_player, g_player, data, params, big_zero) # 记录即时的goal与user值,是为了后面计算compensate data.loc[idx, 'temp_g'] = g_player.mu data.loc[idx, 'temp_u'] = u_player.mu if item['corr']: u_player, g_player = ts.rate_1vs1(u_player, g_player) else: g_player, u_player = ts.rate_1vs1(g_player, u_player) user[item['uid']] = u_player goal[item['gid']] = g_player return user, goal, data
def _rate_match(self, match): """ Take match dict and update elo for players -1 indicates DQ """ if (match['entrant_1_id'] is None or match['entrant_2_id'] is None or match['winner_id'] is None or (match['entrant_1_score'] is not None and match['entrant_1_score'] == -1) or (match['entrant_2_score'] is not None and match['entrant_2_score'] == -1)): return one = self._get_player_from_id(match['entrant_1_id']) two = self._get_player_from_id(match['entrant_2_id']) if match['entrant_1_id'] == match['winner_id']: one.rating, two.rating = trueskill.rate_1vs1( one.rating, two.rating) elif match['entrant_2_id'] == match['winner_id']: two.rating, one.rating = trueskill.rate_1vs1( two.rating, one.rating) else: raise ValueError( "Winner ID not one of the player's id. smash.gg error?")
def rate_uneven_match(self, win_team, lose_team): """Rates a match between differently-sized teams implementation, adjust each player as though they individually had won/lost against the average of the opposing team. TODO: Take into account that we might have really low confidence in the team. """ win_team_skill = self.get_team_skill(win_team) lose_team_skill = self.get_team_skill(lose_team) new_win_team = [] new_lose_team = [] try: new_win_team_skill, new_lose_team_skill =\ rate_1vs1(win_team_skill, lose_team_skill) for p in win_team: pskill = self.get_player_skill(p) new_skill, _ = rate_1vs1(pskill, lose_team_skill) self.__player_skills[p] = new_skill new_win_team.append(new_skill) for p in lose_team: pskill = self.get_player_skill(p) _, new_skill = rate_1vs1(win_team_skill, pskill) self.__player_skills[p] = new_skill new_lose_team.append(new_skill) except FloatingPointError: print("Skill difference too high: {} vs {}. Ratings not updated". format(win_team_skill, lose_team_skill))
def tournament(self, n_rounds, players): """ Function to determine the rating of a set of players over a given amount of rounds. To determine their rating, they play rounds of matches against random opponents after which their TrueSkill ranking is updated according to the results. The mean and standard deviations of their ratings is saved and returned. Every player plays 2 games each round. """ ratings = [Rating() for i in range(len(players))] mean, std = np.zeros((len(players), n_rounds + 1)), np.zeros( (len(players), n_rounds + 1)) mean[:, 0], std[:, 0] = 25, 25 / 3 for n in tqdm(range(n_rounds)): print(n / n_rounds) order = np.random.permutation(len(players)) for i in range(len(players)): p1, p2 = order[i], order[(i + 1) % len(players)] outcome = self.play_game((players[p1], players[p2])) if outcome == 0: ratings[p1], ratings[p2] = rate_1vs1( ratings[p1], ratings[p2]) elif outcome == 1: ratings[p2], ratings[p1] = rate_1vs1( ratings[p2], ratings[p1]) elif outcome == 2: ratings[p1], ratings[p2] = rate_1vs1(ratings[p1], ratings[p2], drawn=True) players[p1].reset() players[p2].reset() for i in range(len(players)): mean[i, n + 1], std[i, n + 1] = ratings[i].mu, ratings[i].sigma print(ratings) return mean, std
def update_ratings(self, agent0_wins, draw): # Update the ratings based on the result of the match # Need to pass winner as first argument to rate_1vs1 if self.playing_against_sepia: # print("Before ratings " + str(self.ratings[self.active_agents[0]]) + " " + str(self.sepia_rating)) # print("Agent0 won " + str(agent0_wins)) if agent0_wins: rating0, rating1 = trueskill.rate_1vs1( self.ratings[self.active_agents[0]], self.sepia_rating, drawn=draw) self.ratings[self.active_agents[0]] = rating0 self.sepia_rating = rating1 else: rating0, rating1 = trueskill.rate_1vs1( self.sepia_rating, self.ratings[self.active_agents[0]], drawn=draw) self.sepia_rating = rating0 self.ratings[self.active_agents[0]] = rating1 # print("After ratings " + str(self.ratings[self.active_agents[0]]) + " " + str(self.sepia_rating)) else: if agent0_wins: winner_idx = self.active_agents[0] loser_idx = self.active_agents[1] else: winner_idx = self.active_agents[1] loser_idx = self.active_agents[0] rating0, rating1 = trueskill.rate_1vs1(self.ratings[winner_idx], self.ratings[loser_idx], drawn=draw) self.ratings[winner_idx] = rating0 self.ratings[loser_idx] = rating1
def match(players, board_size, depths, r1, r2, amount, t_run, N, cp): """ Set up the Match""" # save ratings save = np.empty(0) #play game for i in range(amount): board = run(players[(i + 1) % 2], players[i % 2], board_size, depths[(i + 1) % 2], depths[i % 2], t_run, N, cp) #evaluate who won and update rating if board.check_win(board.BLUE) and (i + 1) % 2 == 0 or board.check_win( board.RED) and (i + 1) % 2 == 1: r1, r2 = ts.rate_1vs1(r1, r2) elif board.check_win( board.BLUE) and (i + 1) % 2 == 1 or board.check_win( board.RED) and (i + 1) % 2 == 0: r2, r1 = ts.rate_1vs1(r2, r1) else: #draw r1, r2 = ts.rate_1vs1(r1, r2, drawn=True) save = np.append(save, r1) save = np.append(save, r2) return r1, r2, save
def match(self, a, b, winner=None): """ a and b are ids of systems winners is either a, b or None `winner` as None means it's a draw """ error_message = "Please provide existing ids" if a in self.names2ids and b in self.names2ids and ( winner is None or winner in self.names2ids): a = self.names2ids[a] b = self.names2ids[b] if winner is not None: winner = self.names2ids[winner] error_message = "The winner must be either " + str( a) + " or " + str(b) if winner is None: na, nb = rate_1vs1(self.ratings[a], self.ratings[b], drawn=True) self.ratings[a], self.ratings[b] = na, nb elif winner == a: na, nb = rate_1vs1(self.ratings[a], self.ratings[b]) self.ratings[a], self.ratings[b] = na, nb elif winner == b: nb, na = rate_1vs1(self.ratings[b], self.ratings[a]) self.ratings[a], self.ratings[b] = na, nb elif self.allow_raise: raise Exception(error_message) else: print(error_message) elif self.allow_raise: raise Exception(error_message) else: print(error_message)
def PlayerVsPlayer(player1, player2, n, localPathToGame): r1 = player1.skill r2 = player2.skill for i in range(n): winnerID = int( subprocess.check_output([ os.path.dirname(os.path.realpath(__file__)) + localPathToGame, str(player1.ID), str(player2.ID) ]).decode("utf8").strip() ) # TODO Forward CLI arguments and allow for bot files if winnerID == player1.ID: r1, r2 = trueskill.rate_1vs1(r1, r2) else: r2, r1 = trueskill.rate_1vs1(r2, r1) # Update Trueskill level player1.skill = r1 player2.skill = r2 player1.rank = player1.skill.mu - 3 * player1.skill.sigma player2.rank = player2.skill.mu - 3 * player2.skill.sigma # Update History player1.history.append([player1.skill.mu, player1.skill.sigma]) player2.history.append([player2.skill.mu, player2.skill.sigma])
def handle_game_outcome(self, players, outcomes): # :players ~ [entrant_1, entrant_2] # Update skill rating # - First player passed to rate_1vs1 is the winner p1 = players[0] p2 = players[1] if outcomes[0] == 0: # Draw, order doesn't matter p1_new, p2_new = rate_1vs1(p1.skill_rating, p2.skill_rating, drawn=True) elif outcomes[0] == 1: # P1 won p1_new, p2_new = rate_1vs1(p1.skill_rating, p2.skill_rating) else: # P2 won p2_new, p1_new = rate_1vs1(p2.skill_rating, p1.skill_rating) p1.skill_rating = p1_new p2.skill_rating = p2_new # Update stats for entrant, outcome in zip(players, outcomes): opponent_entrant = [p for p in players if p != entrant][0] entrant.handle_outcome(opponent_entrant, outcome)
def calculate_score(self, comparison_pairs): """ Calculate scores for a set of comparison_pairs :param comparison_pairs: array of comparison_pairs :return: dictionary key -> ScoredObject """ self.storage = {} self.opponents = {} self.ratings = {} trueskill.setup() keys = self.get_keys_from_comparison_pairs(comparison_pairs) # create default ratings for every available key for key in keys: rating = trueskill.Rating() self.ratings[key] = rating self.opponents[key] = set() self.storage[key] = ScoredObject(key=key, score=trueskill.expose(rating), variable1=rating.mu, variable2=rating.sigma, rounds=0, opponents=0, wins=0, loses=0) # calculate rating by for every match for comparison_pair in comparison_pairs: key1 = comparison_pair.key1 key2 = comparison_pair.key2 winner = comparison_pair.winner # skip incomplete comparisosns if winner is None: self._update_rounds_only(key1) self._update_rounds_only(key2) continue r1 = self.ratings[key1] r2 = self.ratings[key2] key1_winner = winner == ComparisonWinner.key1 key2_winner = winner == ComparisonWinner.key2 if key1_winner: r1, r2 = trueskill.rate_1vs1(r1, r2) elif key2_winner: r2, r1 = trueskill.rate_1vs1(r2, r1) elif winner == ComparisonWinner.draw: r1, r2 = trueskill.rate_1vs1(r1, r2, drawn=True) else: raise InvalidWinnerException self._update_rating(key1, r1, key2, key1_winner, key2_winner) self._update_rating(key2, r2, key1, key2_winner, key1_winner) # return comparison results return self.storage
def _process_fixture(self, fixture): rating_1, rating_2, _, _ = self._get_player_ratings(fixture) if fixture[self.target] == 1: update_rating_1, update_rating_2 = trueskill.rate_1vs1(rating_1, rating_2) else: update_rating_2, update_rating_1 = trueskill.rate_1vs1(rating_2, rating_1) self.ratings[fixture[self.player_1]] = update_rating_1 self.ratings[fixture[self.player_2]] = update_rating_2
def updateRatings(self, color_blue=True): if self.starter == HexBoard.BLUE and color_blue: self.rating_p1, self.rating_p2 = rate_1vs1(self.rating_p1, self.rating_p2) else: self.rating_p2, self.rating_p1 = rate_1vs1(self.rating_p2, self.rating_p1) self.all_ratings_p1.append(self.rating_p1) self.all_ratings_p2.append(self.rating_p2)
def trueskill_layer(model_path, images_dir, csv_path): """ Compute a trueskill rating for each image of the directory from 100 comparisons predictions. Save the results in a csv file. :param model_path: path of the trained comparisons model :type model_path: str :param images_dir: images directory path :type images_dir: str :param csv_path: path of csv where results are stored :type csv_path: str """ # Variable initialization print("Loading model ...") model = load_model(model_path) images_path = glob.glob(images_dir + "/*jpg") pictures = [] images = [] # Build list of Image objects print("Done\nLoading images...") pbar = ProgressBar() for img_path in pbar(images_path): img = Image(img_path, build_array=True) img.get_info_from_filename() images.append(img) pictures.append(img.preprocess_image(224)) pictures = np.array(pictures) pbar2 = ProgressBar() print("Done\nComputing trueskill rating for each image ...") for i in pbar2(range(len(images))): # Create 100 contenders and predict a comparison for each contenders = [rd.randint(0, len(images) - 1) for _ in range(100)] predictions = model.predict([ np.array([pictures[i] for _ in range(100)]), np.array([pictures[contenders[k]] for k in range(100)]) ]) # Update image trueskill rating for each comparison for j in range(predictions.shape[0]): if predictions[j][0] > predictions[j][1]: images[i].trueskill_rating, images[ contenders[j]].trueskill_rating = tr.rate_1vs1( images[i].trueskill_rating, images[contenders[j]].trueskill_rating) else: images[contenders[j]].trueskill_rating, images[ i].trueskill_rating = tr.rate_1vs1( images[contenders[j]].trueskill_rating, images[i].trueskill_rating) # Save results in csv print("Done\nSaving results in csv ...") trueskill_csv(images, csv_path) print("Done")
def rate2p(self, r_a, r_b, score): if score == 1: return trueskill.rate_1vs1(r_a, r_b, env=self.env) elif score == 0: return list(reversed(self.rate2p(r_b, r_a, 1))) elif score == 0.5: return trueskill.rate_1vs1(r_a, r_b, env=self.env, drawn=True) else: raise 'Illegal score value: %s' % (score)
def rate2p(self, r_a, r_b, score): if score == 1: return trueskill.rate_1vs1(r_a, r_b, env=self.env) elif score == 0: return list(reversed(self.rate2p(r_b, r_a, 1))) elif score == 0.5: return trueskill.rate_1vs1(r_a, r_b, env=self.env, drawn=True) else: raise 'Illegal score value: %s' % (score)
def calculate_score(self, comparison_pairs): """ Calculate scores for a set of comparisons :param comparisons: array of :return: dictionary key -> ScoredObject """ self.storage = {} self.opponents = {} self.ratings = {} trueskill.setup() keys = self.get_keys_from_comparison_pairs(comparison_pairs) # create default ratings for every available key for key in keys: rating = trueskill.Rating() self.ratings[key] = rating self.opponents[key] = set() self.storage[key] = ScoredObject( key=key, score=trueskill.expose(rating), variable1=rating.mu, variable2=rating.sigma, rounds=0, opponents=0, wins=0, loses=0, ) # calculate rating by for every match for comparison_pair in comparison_pairs: key1 = comparison_pair.key1 key2 = comparison_pair.key2 winning_key = comparison_pair.winning_key # skip incomplete comparisosns if winning_key is None: self._update_rounds_only(key1) self._update_rounds_only(key2) continue r1 = self.ratings[key1] r2 = self.ratings[key2] if winning_key == comparison_pair.key1: r1, r2 = trueskill.rate_1vs1(r1, r2) elif winning_key == comparison_pair.key2: r2, r1 = trueskill.rate_1vs1(r2, r1) else: raise InvalidWinningKeyException self._update_rating(key1, r1, key2, winning_key) self._update_rating(key2, r2, key1, winning_key) # return comparison results return self.storage
def test_compatibility_with_another_rating_systems(): """All rating system modules should implement ``rate_1vs1`` and ``quality_1vs1`` to provide shortcuts for 1 vs 1 simple competition games. """ r1, r2 = Rating(30, 3), Rating(20, 2) assert quality_1vs1(r1, r2) == quality([(r1,), (r2,)]) rated = rate([(r1,), (r2,)]) assert rate_1vs1(r1, r2) == (rated[0][0], rated[1][0]) rated = rate([(r1,), (r2,)], [0, 0]) assert rate_1vs1(r1, r2, drawn=True) == (rated[0][0], rated[1][0])
def test_compatibility_with_another_rating_systems(): """All rating system modules should implement ``rate_1vs1`` and ``quality_1vs1`` to provide shortcuts for 1 vs 1 simple competition games. """ r1, r2 = Rating(30, 3), Rating(20, 2) assert quality_1vs1(r1, r2) == quality([(r1, ), (r2, )]) rated = rate([(r1, ), (r2, )]) assert rate_1vs1(r1, r2) == (rated[0][0], rated[1][0]) rated = rate([(r1, ), (r2, )], [0, 0]) assert rate_1vs1(r1, r2, drawn=True) == (rated[0][0], rated[1][0])
def updateRating(team1, team2): r1, s1 = team1 r2, s2 = team2 if s1 > s2: new_r1, new_r2 = ts.rate_1vs1(r1, r2, drawn=False) elif s2 < s1: new_r2, new_r1 = ts.rate_1vs1(r2, r1, drawn=False) else: # Draw new_r1, new_r2 = ts.rate_1vs1(r1, r2, drawn=True) return new_r1, new_r2
def update_ratings(self, r_1, r_2, outcome): r1_old = r_1 r2_old = r_2 if outcome == 1: r1_new, r2_new = rate_1vs1(r1_old, r2_old) elif outcome == -1: r2_new, r1_new = rate_1vs1(r2_old, r1_old) else: r1_new, r2_new = rate_1vs1(r1_old, r2_old, drawn=True) return r1_new, r2_new
def match(player1, player2, n_games, size): for _ in range(n_games): game = HexGame(size, player1, player2) game.play(['']) if game.win[1] == 1: player1.rating, player2.rating = rate_1vs1(player1.rating, player2.rating, drawn=False) elif game.win[1] == 2: player2.rating, player1.rating = rate_1vs1(player2.rating, player1.rating, drawn=False) return player1.rating, player2.rating
def play(player1, player2, board, verbose=True): global player1_ratings global player2_ratings turn = 0 # turn counter # while the game is not finished while not board.game_over: turn += 1 # increase turn after a round of the game searcher.d3_rt_lst.append(searcher.d3_run_time) searcher.d4_rt_lst.append(searcher.d4_run_time) searcher.d3_run_time = 0 # and reset run time searcher.d4_run_time = 0 if verbose == True: print("turn:{}".format(turn)) print("Player 1:{}[{}]".format(player1.playerType, player1.policy)) board.print() player1.move(board, verbose=verbose) # player_1 moves # if player _1 won game then : if board.check_win(player1.color): if verbose == True: board.print() print("The Game is Over. Player 1: {}[{}] won the game".format( player1.agent, player1.policy)) player1_ratings, player2_ratings = rate_1vs1( player1_ratings, player2_ratings) break if verbose == True: print("Player 2:{}[{}]".format(player2.agent, player2.policy)) board.print() player2.move(board, verbose=verbose) # player_2 moves # if player_2 won game then : if board.check_win(player2.color): if verbose == True: board.print() print("The Game is Over. Player 2: {}[{}] won the game".format( player2.agent, player2.policy)) player2_ratings, player1_ratings = rate_1vs1( player2_ratings, player1_ratings) break
def update_loop(): global DB_LOCK while True: sleep(1) if len(QUEUED_MATCHES.keys()) == 0: continue while DB_LOCK: sleep(0.1) DB_LOCK = True keys = [int(key) for key in QUEUED_MATCHES.keys()] cur.execute( "SELECT (id, status) FROM match_kube WHERE (status='redwon' or status='bluewon') and id=ANY(%s)", (keys, )) games = cur.fetchall() DB_LOCK = False for game in games: game = game[0][1:-1].split(",") game[0] = int(game[0]) print("Updating game " + str(game[0]) + ".") red_player = QUEUED_MATCHES[game[0]]['red'] blue_player = QUEUED_MATCHES[game[0]]['blue'] rs = Rating(mu=red_player['mu'], sigma=red_player['sigma']) bs = Rating(mu=blue_player['mu'], sigma=blue_player['sigma']) rn, bn = rate_1vs1(rs, bs) if game[1] == 'redwon' else rate_1vs1( bs, rs) print("New red mu: " + str(rn.mu) + ", sigma: " + str(rn.sigma)) print("New blue mu: " + str(bn.mu) + ", sigma: " + str(bn.sigma) + "\n") while DB_LOCK: sleep(0.1) DB_LOCK = True cur.execute( "UPDATE battlecode_teams SET (mu,sigma)=(%s,%s) WHERE id=%s", (rn.mu, rn.sigma, red_player['id'])) cur.execute( "UPDATE battlecode_teams SET (mu,sigma)=(%s,%s) WHERE id=%s", (bn.mu, bn.sigma, blue_player['id'])) pg.commit() DB_LOCK = False del QUEUED_MATCHES[game[0]]
def calculate_ratings(game_df, rating_object=Rating(), return_type='dataframe'): """ calculates player ratings and outputs a summary dict or dataframe of results :param rating_object: TrueSkill object :param return_type: 'dict' or 'dataframe' :type game_df: pd.DataFrame """ for col in game_df.columns: if 'player' in col: game_df[col] = game_df[col].apply(remove_whitespace) all_players = set( list(game_df.player_a.unique()) + list(game_df.player_b.unique())) ratings = {k: rating_object for k in all_players} for row in game_df.iterrows(): player_a = ratings[row[1]['player_a']] player_b = ratings[row[1]['player_b']] if row[1]['score_a'] > row[1]['score_b']: player_a, player_b = rate_1vs1(player_a, player_b) elif row[1]['score_a'] < row[1]['score_b']: player_b, player_a = rate_1vs1(player_b, player_a) else: player_a, player_b = rate_1vs1(player_a, player_b, drawn=True) ratings[row[1]['player_a']] = player_a ratings[row[1]['player_b']] = player_b if return_type == 'dict': return ratings elif return_type == 'dataframe': rating_df = pd.DataFrame() for k, v in ratings.iteritems(): rating_df.loc[k, 'rating'] = v.mu rating_df.loc[k, 'sigma'] = v.sigma rating_df.loc[k, 'tau'] = v.tau rating_df.loc[k, 'pi'] = v.pi rating_df.loc[k, 'trueskill'] = v.exposure rating_df.reset_index(inplace=True) return rating_df
def commitdata(labratID, sDog, sCat, bWinner): # LabratID, battledog dataclip, battleCat dataclip # Commiting LabRat Info bWinner = str(bWinner) labrat = LabRats.query.get_or_404(labratID) labrat.hero = sDog.id labrat.battle_order = labrat.battle_order + "" + (str(sCat.id)) + " " labrat.battle_winner = labrat.battle_winner + bWinner + " " db.session.commit() # Commit The Battles DogTS = Rating(mu=sDog.mean, sigma=sDog.deviation) tCat = Cats.query.get_or_404(sCat.id) CatTS = Rating(mu=tCat.mean, sigma=tCat.deviation) # Finding W/L status if bWinner == "1": new_DogTS, new_CatTS = rate_1vs1(DogTS, CatTS) dogResult = "W" catResult = "L" if bWinner == "0": new_DogTS, new_CatTS = rate_1vs1(CatTS, DogTS) dogResult = "L" catResult = "W" # Commiting all this data. bDog = Dogs.query.filter_by(id=sDog.id).first() # First we shall commit dogs. bDog.mean_history = bDog.mean_history + str(round(bDog.mean, 3)) + " " bDog.deviation_history = bDog.deviation_history + str( round(bDog.deviation, 2)) + " " bDog.mean = round(new_DogTS.mu, 3) bDog.deviation = round(new_DogTS.sigma, 2) bDogHistory = str(labratID) + " " + str(sCat.id) + " " + str( round(tCat.mean, 3)) + " " + str(round(tCat.deviation, 2)) + " " + dogResult + " " bDog.battle_history = bDog.battle_history + "" + bDogHistory print('bDogHistory', bDogHistory) # Now we shall commit cats bCat = Cats.query.filter_by(id=sCat.id).first() bCat.mean_history = bCat.mean_history + "" + str(round(tCat.mean, 3)) + " " bCat.deviation_history = bCat.deviation_history + str( round(tCat.deviation, 2)) + " " bCat.mean = round(new_CatTS.mu, 3) bCat.deviation = round(new_CatTS.sigma, 2) bCatHistory = str(labratID) + " " + str(bDog.id) + " " + str( round(DogTS.mu, 3)) + " " + str(round(DogTS.sigma, 2)) + " " + catResult + " " bCat.battle_history = bCat.battle_history + "" + bCatHistory print('bCatHistory', bCatHistory) db.session.commit() DogTS = new_DogTS
def get_new_ratings(players, teams): """ Affect new ratings to players from teams results """ nb_players_team0 = len(teams[0]) nb_players_team1 = len(teams[1]) winner = players[teams[0][0]] loser = players[teams[1][0]] if nb_players_team0 == 1 and nb_players_team1 == 1: new_r1, new_r3 = rate_1vs1(winner,loser) elif nb_players_team0 == 1 and nb_players_team1 > 1: team_loser = [loser, players[teams[1][1]]] (new_r1), (new_r3, new_r4) = rate([winner, team_loser], ranks=[0, 1]) elif nb_players_team0 > 1 and nb_players_team1 == 1: team_winner = [winner, players[teams[0][1]]] (new_r1, new_r2), (new_r3) = rate([team_winner, loser], ranks=[0, 1]) else: team_loser = [loser, players[teams[1][1]]] team_winner = [winner, players[teams[0][1]]] (new_r1, new_r2), (new_r3, new_r4) = rate([team_winner, team_loser], ranks=[0, 1]) player1 = {'name': teams[0][0], 'mu': new_r1.mu, 'sigma': new_r1.sigma} player3 = {'name': teams[1][0], 'mu': new_r3.mu, 'sigma': new_r3.sigma} if nb_players_team0 > 1: player2 = {'name': teams[0][1], 'mu': new_r2.mu, 'sigma': new_r2.sigma} if nb_players_team1 > 1: player4 = {'name': teams[1][1], 'mu': new_r4.mu, 'sigma': new_r4.sigma} if nb_players_team0 > 1: return [player1, player2, player3, player4] return [player1, player2, player4] return [player1, player3]
def calculate_ratings(self): outcomes = trueskill.rate_1vs1( self.winner.rating.ts_rating, self.loser.rating.ts_rating, ) self.winner.rating.update_rating(outcomes[0]) self.loser.rating.update_rating(outcomes[1])
def update_ts_scores(task_worker, winner_id): if task_worker.task.project.is_review: match = Match.objects.filter(task=task_worker.task).first() if match is not None: match_workers = MatchWorker.objects.prefetch_related('task_worker').filter(match=match) winner = [w for w in match_workers if w.task_worker_id == int(winner_id)][0] loser = [w for w in match_workers if w.task_worker_id != int(winner_id)][0] winner_project_ts = MatchWorker.objects.prefetch_related('task_worker').filter( task_worker__worker=winner.task_worker.worker, match__status=Match.STATUS_COMPLETED).order_by( '-match__submitted_at').first() loser_project_ts = MatchWorker.objects.prefetch_related('task_worker').filter( task_worker__worker=loser.task_worker.worker, match__status=Match.STATUS_COMPLETED).order_by( '-match__submitted_at').first() loser_ts = trueskill.Rating(mu=loser_project_ts.mu if loser_project_ts is not None else loser.old_mu, sigma=loser_project_ts.sigma if loser_project_ts is not None else loser.old_sigma) winner_ts = trueskill.Rating(mu=winner_project_ts.mu if winner_project_ts is not None else winner.old_mu, sigma=winner_project_ts.sigma if winner_project_ts is not None else winner.old_sigma) new_winner_ts, new_loser_ts = trueskill.rate_1vs1(winner_ts, loser_ts) winner.mu = new_winner_ts.mu winner.sigma = new_winner_ts.sigma winner.save() loser.mu = new_loser_ts.mu loser.sigma = new_loser_ts.sigma loser.save() match.status = Match.STATUS_COMPLETED match.submitted_at = timezone.now() match.save()
def calc_region_trueskill(winner_user, loser_user, region_num): """Recalculates regional TrueSkill association between Users depending on Region the Set was played in Args: winner_user: A User object representing winner of Set loser_user: A User object representing loser of Set region_num: An integer, index of User.trueskills (0 for Region and 1 for Global) Returns: None. """ # Create Rating objects using currently stored Region Trueskill attribute winner_user_rating = Rating(winner_user.trueskills[region_num].mu, winner_user.trueskills[region_num].sigma) loser_user_rating = Rating(loser_user.trueskills[region_num].mu, loser_user.trueskills[region_num].sigma) region_name = winner_user.trueskills[region_num].region # Print current TrueSkill values for both players print "CURRENT TrueSkill ({0}):".format(region_name), winner_user.tag, winner_user_rating, "VS.", loser_user.tag, loser_user_rating # Record set result, victory for winner_user and loss for loser_user new_winner_rating, new_loser_rating = rate_1vs1(winner_user_rating, loser_user_rating) print "UPDATED TrueSkill ({0}):".format(region_name), winner_user.tag, new_winner_rating, "VS.", loser_user.tag, new_loser_rating # Store and overwrite existing trueskill object with new Rating values winner_user.trueskills[region_num].region=region_name winner_user.trueskills[region_num].mu=new_winner_rating.mu winner_user.trueskills[region_num].sigma=new_winner_rating.sigma winner_user.trueskills[region_num].cons_mu=new_winner_rating.mu - STD*new_winner_rating.sigma loser_user.trueskills[region_num].region=region_name loser_user.trueskills[region_num].mu=new_loser_rating.mu loser_user.trueskills[region_num].sigma=new_loser_rating.sigma loser_user.trueskills[region_num].cons_mu=new_loser_rating.mu - STD*new_loser_rating.sigma
def update_ratings(self, game, winner): if game.player0 == winner: loser = game.player1 else: loser = game.player0 self.ratings[winner], self.ratings[loser] = trueskill.rate_1vs1(self.ratings[winner], self.ratings[loser]) self.save_ratings()
def _update_user_rating(user, user_winner): # create rating obj from user's mu and sigma user_rating = Rating(mu=user.mu, sigma=user.sigma) # create default rating obj default_rating = Rating() if user_winner: user_rating, default_rating = rate_1vs1(user_rating, default_rating) else: default_rating, user_rating = rate_1vs1(default_rating, user_rating) # update w/ new user rating user.mu = user_rating.mu user.sigma = user_rating.sigma # save the user user.put()
def update_trueskill_ratings(region_id, winner=None, loser=None): winner_ratings_dict = winner.ratings loser_ratings_dict = loser.ratings new_winner_rating, new_loser_rating = trueskill.rate_1vs1( winner_ratings_dict[region_id].trueskill_rating, loser_ratings_dict[region_id].trueskill_rating ) winner_ratings_dict[region_id] = TrueskillRating(trueskill_rating=new_winner_rating) loser_ratings_dict[region_id] = TrueskillRating(trueskill_rating=new_loser_rating)
def update_trueskill_ratings(region_id, winner=None, loser=None): winner_ratings_dict = winner.ratings loser_ratings_dict = loser.ratings new_winner_rating, new_loser_rating = trueskill.rate_1vs1( winner_ratings_dict[region_id].trueskill_rating(), loser_ratings_dict[region_id].trueskill_rating() ) winner_ratings_dict[region_id] = Rating.from_trueskill(new_winner_rating) loser_ratings_dict[region_id] = Rating.from_trueskill(new_loser_rating)
def update_true_skill(fight, true_skill_dict): """ incremental TrueSkill update function after one fight """ TrueSkill = true_skill_dict movie_1, movie_2 = fight.movie1.imdb_id, fight.movie2.imdb_id if movie_1 in TrueSkill and movie_2 in TrueSkill: if fight.isDraw(): id1, id2 = movie_1, movie_2 else: id1, id2 = fight.winner().imdb_id, fight.loser().imdb_id new_ratings = ts.rate_1vs1(TrueSkill[id1], TrueSkill[id2], drawn=fight.isDraw()) TrueSkill[id1], TrueSkill[id2] = new_ratings
def update_score(self, notify=True): winner = self.winner loser = self.loser for competition in self.competitions.all(): if notify: old_rankings = Score.objects.get_ranking_by_team(competition) winner_score = winner.get_or_create_score(competition) loser_score = loser.get_or_create_score(competition) winner_new_score, loser_new_score = rate_1vs1( Rating(winner_score.score, winner_score.stdev), Rating(loser_score.score, loser_score.stdev) ) winner_score.score = winner_new_score.mu winner_score.stdev = winner_new_score.sigma winner_score.save() loser_score.score = loser_new_score.mu loser_score.stdev = loser_new_score.sigma loser_score.save() HistoricalScore.objects.create( game=self, score=winner_score.score, team=winner, competition=competition ) HistoricalScore.objects.create( game=self, score=loser_score.score, team=loser, competition=competition ) if notify: new_rankings = Score.objects.get_ranking_by_team(competition) for team in [winner, loser]: if (team not in old_rankings or old_rankings[team] != new_rankings[team]): team_ranking_changed.send( sender=self, team=team, old_ranking=(old_rankings[team] if team in old_rankings else None), new_ranking=new_rankings[team], competition=competition )
def get(self, request, *args, **kwargs): try: data = signing.loads(self.kwargs['signed_data'], max_age=30) except signing.SignatureExpired: messages.info(request, _("Sorry, but your vote link expired. " "Feel free to vote again.")) return redirect('index') except signing.BadSignature: raise Http404 player_a = get_object_or_404(PlayerSeason, pk=data[0]) rating_a = player_a.get_rating() player_b = get_object_or_404(PlayerSeason, pk=data[1]) rating_b = player_b.get_rating() if player_a.season != player_b.season: raise Http404 if len(data) == 2: # First one is the winner rating_a, rating_b = rate_1vs1(rating_a, rating_b) player_a.votes_win += 1 else: # Tie rating_a, rating_b = rate_1vs1(rating_a, rating_b, drawn=True) player_a.votes_tie += 1 player_b.votes_tie += 1 # Save new ratings player_a.rating_mu = rating_a.mu player_a.rating_sigma = rating_a.sigma player_a.save() player_b.rating_mu = rating_b.mu player_b.rating_sigma = rating_b.sigma player_b.save() messages.success(request, _("Thanks for voting!")) return redirect('ranking', player_a.season)
def create_rankings(self): winner = self.winner loser = self.loser winner.rating, loser.rating = rate_1vs1(winner.rating, loser.rating) winner_ranking = Ranking(player=winner, game=self) winner_ranking.rating = winner.rating loser_ranking = Ranking(player=loser, game=self) loser_ranking.rating = loser.rating with transaction.atomic(): winner.save() loser.save() winner_ranking.save() loser_ranking.save()
def test_issue5(backend): """The `issue #5`_, opened by @warner121. This error occurs when a winner has too low rating than a loser. Basically Python cannot calculate correct result but mpmath_ can. I added ``backend`` option to :class:`TrueSkill` class. If it is set to 'mpmath' then the problem will have gone. The result of TrueSkill calculator by Microsoft is N(-273.092, 2.683) and N(-75.830, 2.080), of C# Skills by Moserware is N(NaN, 2.6826) and N(NaN, 2.0798). I choose Microsoft's result as an expectation for the test suite. .. _issue #5: https://github.com/sublee/trueskill/issues/5 .. _mpmath: http://mpmath.googlecode.com/ """ assert _quality_1vs1(Rating(-323.263, 2.965), Rating(-48.441, 2.190)) == 0 with raises(FloatingPointError): rate_1vs1(Rating(-323.263, 2.965), Rating(-48.441, 2.190)) assert _quality_1vs1(Rating(), Rating(1000)) == 0 with raises(FloatingPointError): rate_1vs1(Rating(), Rating(1000))
def update_rating(player_1, player_2, winner): # now let's to some complicated math...just kidding trueskill will do it for us player_one = player.objects.get(name=player_1) player_two = player.objects.get(name=player_2) player_1_mu = player_one.mu player_1_sigma = player_one.sigma player_2_mu = player_two.mu player_2_sigma = player_two.sigma # okay we got all of the variables because Django is f*****g stupid # now we get to recalculate the rating based on who the f**k won player_1_current_rating = Rating(float(player_1_mu), float(player_1_sigma)) player_2_current_rating = Rating(float(player_2_mu), float(player_2_sigma)) # we got ourselves some ratings here let's give them a match if winner == player_1: player_1_new_rating, player_2_new_rating = rate_1vs1(player_1_current_rating, player_2_current_rating) elif winner == player_2: player_2_new_rating, player_1_new_rating = rate_1vs1(player_2_current_rating, player_1_current_rating) player_one.mu = player_1_new_rating.mu player_one.sigma = player_1_new_rating.sigma player_two.mu = player_2_new_rating.mu player_two.sigma = player_2_new_rating.sigma player_one.save() player_two.save()
def get_best_comp(): env = TrueSkill(draw_probability = 0) comp_ratings = defaultdict(lambda: env.create_rating()) comp_counts = defaultdict(int) comp_win_rate = defaultdict(lambda: [0,0]) for i in xrange(100): games = get_games(i * NUM_GAMES, NUM_GAMES) for g in games: teamA_comp, teamB_comp, teamA_won = anaylze_match(g) if teamA_comp == teamB_comp: continue if tuple() in teamA_comp or tuple() in teamB_comp: continue teamA_rating = comp_ratings[teamA_comp] teamB_rating = comp_ratings[teamB_comp] comp_counts[teamA_comp] += 1 comp_counts[teamB_comp] += 1 comp_win_rate[teamA_comp][1] += 1 comp_win_rate[teamB_comp][1] += 1 if teamA_won: comp_win_rate[teamA_comp][0] += 1 teamA_rating, teamB_rating = rate_1vs1(teamA_rating, teamB_rating) else: comp_win_rate[teamB_comp][0] += 1 teamA_rating, teamB_rating = rate_1vs1(teamB_rating, teamA_rating) comp_ratings[teamA_comp] = teamA_rating comp_ratings[teamB_comp] = teamB_rating if i % 10 == 0: print i leaderboard = sorted([(comp_win_rate[k][0] / float(comp_win_rate[k][1]), v,k) for k,v in comp_ratings.items()], reverse = True) for l in leaderboard: print l, comp_counts[l[2]]
def confirm(self, confirmed): self.confirmed = confirmed self.reviewed = True if self.confirmed: winner_rating = Rating(self.winner.rating_mu, self.winner.rating_sigma) loser_rating = Rating(self.loser.rating_mu, self.loser.rating_sigma) winner_rating, loser_rating = rate_1vs1(winner_rating, loser_rating) self.winner.rating_mu = winner_rating.mu self.winner.rating_sigma = winner_rating.sigma self.loser.rating_mu = loser_rating.mu self.loser.rating_sigma = loser_rating.sigma self.winner.matches.append(self) self.loser.matches.append(self) db.commit() self.creator.add_notification('print', self.get_other_player().username + " has confirmed your match", "")
def forwards(self, orm): ids = orm['auth.User'].objects.values_list('id', flat=True) users = {i: trueskill.Rating() for i in ids} for game in orm.Game.objects.filter(confirmed=True).order_by('date_created'): wid = game.winner.id lid = game.loser.id users[wid], users[lid] = trueskill.rate_1vs1(users[wid], users[lid]) for id, rating in users.iteritems(): u = orm.Rating.objects.get(user__id=id) u.ts_rating = rating u.exposure = trueskill.expose(u.ts_rating) u.save()
def get_ratings(players=None, N=DEFAULTN): """ Run a tournment for all of the players, N times and return the ratings """ import trueskill allgames, players = tourney(players, N) ratings = {} for p in players: ratings[p] = trueskill.Rating() logging.info("Calculating ratings...") random.shuffle(allgames) for winner,loser in allgames: ratings[winner], ratings[loser] = trueskill.rate_1vs1(ratings[winner], ratings[loser]) return ratings, allgames, players
def update_player_trueskill(tournament, ratings_map): """ Update rating for each player by iterating over each match in chronological order and applying trueskill.""" # Calculate TrueSkill from each map for match in tournament['matches']: winner, loser = match['match']['winner_id'], match['match']['loser_id'] # If the user exists in our map, we will take their current rating. # Otherwise we will initialize a 0 Rating placeholder player. winner_rating = ratings_map.get(winner, trueskill.Rating(0)) loser_rating = ratings_map.get(loser, trueskill.Rating(0)) winner_rating, loser_rating = trueskill.rate_1vs1(winner_rating, loser_rating) # Update the dictionary with new ratings ratings_map[winner] = winner_rating ratings_map[loser] = loser_rating return ratings_map
def tournament(tourney): tList = TournamentList.load() for t in tList.tournaments.all(): if t.name == tourney: return None t = Tournament() t.name = tourney t.save() tList.tournaments.add(t) tList.save() participants = challonge.participants.index(tourney) for p in participants: addPlayer(p) # Retrieve a tournament by its id (or its url). matches = challonge.matches.index(tourney) for m in matches: score = re.findall(r'\d+', m["scores-csv"]) id1 = m["player1-id"] id2 = m["player2-id"] parts[id1].Gwins+=int(score[0]) parts[id1].Glosses+=int(score[1]) parts[id2].Gwins+=int(score[1]) parts[id2].Glosses+=int(score[0]) if m["winner-id"] == id2: temp = id1 id1 = id2 id2 = temp r1 = Rating(mu=float(parts[id1].mu),sigma=float(parts[id1].sigma)) r2 = Rating(mu=float(parts[id2].mu),sigma=float(parts[id2].sigma)) r1, r2 = rate_1vs1(r1, r2) parts[id1].mu = r1.mu parts[id1].sigma = r1.sigma parts[id2].mu = r2.mu parts[id1].safe = parts[id1].mu-(3*parts[id1].sigma) parts[id2].sigma = r2.sigma parts[id2].safe = parts[id2].mu-(3*parts[id1].sigma) parts[id1].Swins+=1 parts[id2].Slosses+=1
def update(name, scores, wins, tourneys): """Retrieves a tournament by name (url) and updates the global scores and wins dictionaries with new tournament data. name: The name of the tournament. scores: A dictionary that maps the player's name to his/her TrueSkill Rating. wins: A dictionary that maps the player's name to a list of players he/she has defeated. tourneys: A dictionary that maps the players name to a list of tournaments he/she has attended. """ tournament = challonge.tournaments.show(name) participants = challonge.participants.index(tournament["id"]) matches = challonge.matches.index(tournament["id"]) # Add each player to the database. players = {} for p in participants: tag = p["name"] if tag in names.keys(): tag = names[tag] players[p["id"]] = tag if tag not in wins.keys(): wins[tag] = [] if tag not in scores.keys(): scores[tag] = Rating() if tag not in tourneys.keys(): tourneys[tag] = [] tourneys[tag].append(name) # Update the 'wins' dictionary. for m in matches: if m["winner-id"] == None: pass else: winner = players[m["winner-id"]] loser = players[m["loser-id"]] wins[winner].append(loser) # Update the 'scores' dictionary. for w, ls in wins.items(): for l in ls: new_r1, new_r2 = rate_1vs1(scores[w], scores[l]) scores[w] = new_r1 scores[l] = new_r2 return (scores, wins, tourneys)
def update_rank(sender, **kwargs): if not kwargs['created']: return game = kwargs['instance'] winner_rank = Rank.objects.get(user=game.winner) loser_rank = Rank.objects.get(user=game.loser) winner_new_rating, loser_new_rating = rate_1vs1( Rating(winner_rank.rank, winner_rank.stdev), Rating(loser_rank.rank, loser_rank.stdev) ) winner_rank.rank = winner_new_rating.mu winner_rank.stdev = winner_new_rating.sigma winner_rank.save() loser_rank.rank = loser_new_rating.mu loser_rank.stdev = loser_new_rating.sigma loser_rank.save()
def main(): TrueSkill(backend='scipy') args = parseArgs() misspelled = loadMisspellings(args.misspelled) # map player name to rating object players = defaultdict(Rating) with open(args.record, 'r') as f: r = csv.reader(f) for i, row in enumerate(r): if i == 0: # header continue winner, loser, wasDraw = row wasDraw = bool(wasDraw) winner = misspelled.get(winner, winner).lower() loser = misspelled.get(loser, loser).lower() w_new, l_new = rate_1vs1(players[winner], players[loser]) players[winner] = w_new players[loser] = l_new # for p in sorted(players.items(), key=lambda p: (-p[1].mu, p[1].sigma)): for p in sorted(players.items(), key=lambda p: (p[1].mu - (3* p[1].sigma)), reverse=True): print(p, p[1].mu - (3 * p[1].sigma)) ratings = players.values() mus = np.asarray([r.mu for r in ratings]) sigmas = np.asarray([r.sigma for r in ratings]) coeffs = np.polyfit(mus, sigmas, 2) xp = np.linspace(np.min(mus), np.max(mus), 100) plt.plot(xp, np.poly1d(coeffs)(xp)) plt.scatter(mus, sigmas) plt.title('NorCal Store Champs 2016 TrueSkill') plt.xlabel('Player skill') plt.ylabel('Skill confidence') plt.savefig('trueskill')
def compileMatches(url, tourneyMatches, tourneyPlayers, tournament, listPlayers, listMatches): matches = challonge.matches.index(url); counter = 0; for match in matches: tourneyMatches.append(Match()); tourneyMatches[counter].setTourney(tournament["name"]); for player in tourneyPlayers: if match["winner-id"] == player[0]: tourneyMatches[counter].setWinner(player[1]); for person in listPlayers: if person.name == player[1]: winner = person; if match["loser-id"] == player[0]: tourneyMatches[counter].setLoser(player[1]); for person in listPlayers: if person.name == player[1]: loser = person; tourneyMatches[counter].setScore(match["scores-csv"]); tourneyMatches[counter].setRound(match["round"]); tourneyMatches[counter].setDate(match["updated-at"]) if tourneyMatches[counter] in listMatches: print ("Match already in database, skipping: "); continue; else: listMatches.append(tourneyMatches[counter]); new_r1, new_r2 = rate_1vs1(winner.rating, loser.rating) winner.updateRating(new_r1); loser.updateRating(new_r2); winner.wins = winner.wins + 1; loser.losses = loser.losses + 1; winner.matches.append(tourneyMatches[counter]); #change these eventually loser.matches.append(tourneyMatches[counter]); counter = counter + 1;