def glicko_test():
    env = Glicko2(tau=0.5)
    r1 = env.create_rating(1500, 200, 0.06)
    r2 = env.create_rating(1400, 30)
    r3 = env.create_rating(1550, 100)
    r4 = env.create_rating(1700, 300)
    
    
    rated = env.rate(r1, [(WIN, r2), (LOSS, r3), (LOSS, r4)])
    
    print ('RATED:')
    
    print(rated)
    print(env.create_rating(1464.051, 151.515, 0.05999))
    
    r1 = rated
Exemple #2
0
 async def setUpAsync(self):
     self.gl2 = Glicko2(tau=0.5)
Exemple #3
0
    print("No pyffish module installed!")

from fairy import FairyBoard, WHITE, BLACK, STANDARD_FEN, SHOGI_FEN, MINISHOGI_FEN, KYOTOSHOGI_FEN

from settings import URI
from compress import encode_moves, decode_moves, R2C, C2R, V2C, C2V
from glicko2.glicko2 import Glicko2, PROVISIONAL_PHI

log = logging.getLogger(__name__)


class MissingRatingsException(Exception):
    pass


gl2 = Glicko2()
rating = gl2.create_rating()
DEFAULT_PERF = {
    "gl": {
        "r": rating.mu,
        "d": rating.phi,
        "v": rating.sigma
    },
    "la": datetime.utcnow(),
    "nb": 0
}

MAX_HIGH_SCORE = 10
MAX_USER_SEEKS = 10
MORE_TIME = 15 * 1000
MOVE, ANALYSIS = 0, 1
Exemple #4
0
 def setUp(self):
     self.loop = asyncio.get_event_loop()
     self.gl2 = Glicko2(tau=0.5)
def setup_ladder(team_info,previous_matches,match_size=2,additional_costs=None):
    # team_info: team name, starting rating, updated ranking
    # previous_matches: round number, [team, score], [team, score] etc.
    
    # dominion.online use tau=0.4 for 2p, and tau=0.3 for 3p & 4p
    # starting rating 1500, volatility 0.06
    glicko_env = Glicko2(tau=0.5)
    
    # glicko ratings:
    # mu = rating, phi = deviation, sigma = volatility
    sigma = 0.15
    teams = [glicko_env.create_rating(t[1],t[2],sigma=sigma) for t in team_info]
    teams_playing = [t[3] for t in team_info]
        
    #for i in range(len(team_info)):
    #    print (team_info[i][0],teams[i].mu,teams[i].phi)
    
    replay_cost = 1.0*10.0
    replay_recent_cost = 1.0*100.0 #how much to add for the previous X rounds
    replay_rounds = 3
    
    costs = [[0.0 for i in range(len(team_info))] for j in range(len(team_info))]
    
    
    print ('Initial ratings:')
    for t in team_info:
        print(*t[0:2],sep=",")
        
    #update ratings using glicko system:
    # rated = env.rate(r1, [(WIN, r2),
    
    temp_teams = copy.deepcopy(teams)
    
    bye_teams = []
    
    team_dict = {}
    
    for i in range(len(team_info)):
        team_dict[team_info[i][0]] = i
        if team_info[i][0][:3] == 'bye':
            bye_teams.append(i)
    
    # preset costs:
    
    if not (additional_costs==None):
        for ac in additional_costs:
            teami = team_dict.get(ac[0])
            teamj = team_dict.get(ac[1])
            if teami==None:
                print('---- TEAM NOT FOUND:',ac[0])
            if teamj==None:
                print('---- TEAM NOT FOUND:',ac[0])
            
            if (teamj!=None) and (teami!=None):
                costs[teami][teamj] = ac[2]
                costs[teamj][teami] = ac[2]
                
    for c in costs:
        print (c)        
    
    #print('bye teams: ',bye_teams)
    

    
    max_rounds = 0
    for r in previous_matches:
        if r[0] > max_rounds:
            max_rounds = r[0]
    
    print('Rounds so far',max_rounds)
    
    cur = 1

    #print ('Previous matches')
    for r in previous_matches:
        n_teams = len(r)-1
        
        #print(r)
        #if r[0]>cur:
        #    cur = r[0]
        #    print ('Latest ratings: r',cur-1)
        #    for t in team_info:
        #        print(*t[0:2],sep=",")
        if r[0]>cur:
            cur = r[0]
            print ('After Round:',r[0]-1)
            for i in range(len(team_info)):
                print (team_info[i][0],int(teams[i].mu),int(teams[i].phi))
                
        for i in range(n_teams):
            # team number:
            teami = team_dict[r[i+1][0]]
            for j in range(n_teams):
                # team number:
                teamj = team_dict[r[j+1][0]]
                if i!=j:
                    # handle byes: add replay_cost for all bye teams
                    if (teami in bye_teams):
                        for bt in bye_teams:
                            costs[bt][teamj] += replay_cost*20
                    elif (teamj in bye_teams):
                        for bt in bye_teams:
                            costs[teami][bt] += replay_cost*20       
                    else:
                        # add replay cost - to avoid playing same teams again
                        costs[teami][teamj] += replay_cost
                        if (max_rounds - r[0])<(replay_rounds+0.5):
                            costs[teami][teamj] += replay_recent_cost
                        
                        # now update team i rating, ignoring byes
                        if (r[i+1][1] > r[j+1][1]):
                            game_result = min(r[i+1][1] / max(r[j+1][1],1.0),2.0)/2.0 #WIN
                        elif (r[i+1][1] < r[j+1][1]):
                            game_result = r[i+1][1] / (max(r[j+1][1],1.0)*2.0) #LOSS
                        else:
                            game_result = DRAW
                        
                        #print(r[i+1][1] , r[j+1][1] , game_result)
                        #print(r[i+1][0],r[j+1][0],temp_teams[teami].mu,teams[teamj].mu)
                        
                        temp_teams[teami] = glicko_env.rate(temp_teams[teami],[(game_result,teams[teamj])])
                        
                        #print(temp_teams[teami].mu,teams[teamj].mu)
        
        # only after round calculated, update actual ratings:
        for i in range(len(teams)):
            teams[i] = temp_teams[i]
        
    print ('Updated ratings:')
    for i in range(len(team_info)):
        print ('[\'',team_info[i][0],'\',',int(teams[i].mu),',variability,True],',sep='')    
    
    # now update cost matrix with rating differences:
    for i in range(len(team_info)):
        if team_info[i][0][:3]=='bye':
            for j in range(len(team_info)):
                if team_info[j][0][:3]=='bye':
                    costs[i][j] = 99999.0
        else:
            for j in range(len(team_info)):
                if team_info[j][0][:3]!='bye':
                    costs[i][j] += abs(teams[i].mu-teams[j].mu)
    
    print(*[team_info[j][0] for j in range(len(costs[0]))])
    
    for i in range(len(costs)):
        print(team_info[i][0],'costs:')
        for j in range(len(costs[i])):
            costs[i][j] = round(costs[i][j],1)
            #print('    ',costs[i][j],team_info[j][0])
        print(*[costs[i][j] for j in range(len(costs[i]))])
    
    print ('Ratings, for pasting into spreadsheet')
    for i in range(len(team_info)):
        if (teams_playing[i]):
            print (team_info[i][0],',',int(teams[i].mu))
                
    # CUT OUT TEAMS NOT COMPETING IN THIS ROUND:
    # from team_info and costs:
    for i in range(len(teams_playing)):
        j = len(teams_playing)-i-1
        if not (teams_playing[j]):
            team_info.pop(j)
            for k in range(len(costs)):
                costs[k].pop(j)
            costs.pop(j)
        
    #print(len(team_info),len(costs))
    
    
    results = best_matchups([i for i in range(len(team_info))],costs,match_size,quiet=True)
    
    print ('Next round of matches: (formatted for pasting into spreadsheet)')
    costt = 0.0
    
    for r in results:
        print (*[team_info[i][0] for i in r[1]], sep = ", ,")
    
    print ('Match costs:')
    for r in results:
        print(r[0])
        costt+=r[0]
    print('Total cost:',costt)
Exemple #6
0
 async def startup(self, app):
     self.gl2 = Glicko2(tau=0.5)
Exemple #7
0
 def setUp(self):
     self.loop = asyncio.get_event_loop()
     self.gl2 = Glicko2(tau=0.5)
     self.app = {}
     self.app["db"] = None