Created
July 18, 2013 02:50
-
-
Save mutoo/6026346 to your computer and use it in GitHub Desktop.
a simple simulator of ELO
This file contains hidden or 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
| import java.util.*; | |
| static int count = 0; | |
| class Player implements Comparable<Player> { | |
| int id; | |
| float score; | |
| int win; | |
| int lose; | |
| Player() { | |
| id = count++; | |
| score = 1000; | |
| win = 0; | |
| lose = 0; | |
| } | |
| public int compareTo(final Player p) { | |
| return score<p.score?1:-1; | |
| } | |
| } | |
| Vector<Player> players; | |
| int total = 100; | |
| float h; | |
| float maxw; | |
| float ELO_K = 16; | |
| int times = 0; | |
| int range = 10; | |
| void setup() { | |
| size(1024, 768); | |
| players = new Vector<Player>(); | |
| for (int i=0; i<total;i++) { | |
| players.add(new Player()); | |
| } | |
| h = (float)height / count; | |
| maxw = 1000; | |
| // frameRate(1); | |
| } | |
| void draw() { | |
| //void mousePressed() { | |
| Collections.sort(players); | |
| redraw(); | |
| // for (int i=0;i<20;i++) { | |
| nearPlayersKill(); | |
| // } | |
| fill(0); | |
| textSize(32); | |
| text(times, width/2, height/2); | |
| } | |
| int r = 0; | |
| void redraw() { | |
| background(200); | |
| for (int i=0;i<count;i++) { | |
| Player p = players.get(i); | |
| maxw = max(maxw, p.score); | |
| if (p.id==13) { | |
| r = i; | |
| fill(255, 255, 0); | |
| } | |
| else | |
| fill(255); | |
| rect(0, i*h, width*p.score/maxw, h); | |
| textSize(h); | |
| fill(128); | |
| text("id:"+p.id+" "+(int)p.score+" "+p.win+" "+p.lose, 10, (i+1)*h); | |
| } | |
| } | |
| void randomPlayersKill() { | |
| int r1 = (int)random(count); | |
| int r2 = (int)(r1 + random(count-1))%count; | |
| Player a = players.get(r1); | |
| Player b = players.get(r2); | |
| float exp = getExpect(a, b); | |
| float result = exp<random(1)?0:1; | |
| updateScore(a, b, result); | |
| times++; | |
| } | |
| void nearPlayersKill() { | |
| int r1 = (int)random(count); | |
| // int r1=r; | |
| // float offset = random(count/10.0); | |
| float f1 = min(r1, range); | |
| float f2 = min(count-(r1+1), range); | |
| float f = f1+f2; | |
| float offset = random(f) - f1; | |
| offset += offset<0?0:1; | |
| // println(f1+" "+f2+" "+(int)offset); | |
| int r2 = r1+(int)offset; | |
| fill(200); | |
| ellipse(width/2, (r1+0.5)*h, h/2, h/2); | |
| fill(100); | |
| ellipse(width/2, (r2+0.5)*h, h/2, h/2); | |
| Player a = players.get(r1); | |
| Player b = players.get(r2); | |
| float exp = getExpect(a, b); | |
| // println((int)a.score+" "+(int)b.score+" "+(int)(exp*100)); | |
| float result = exp<random(1)?0:1; | |
| if (result==0) { | |
| a.lose++; | |
| b.win++; | |
| } | |
| else { | |
| a.win++; | |
| b.lose++; | |
| } | |
| updateScore(a, b, result); | |
| times++; | |
| } | |
| //void mousePressed() { | |
| // randomPlayersKill(); | |
| //} | |
| void updateScore(Player a, Player b, float result) { | |
| a.score = a.score + ELO_K * (result - getExpect(a, b)); | |
| b.score = b.score + ELO_K * ((1-result) - getExpect(b, a)); | |
| } | |
| float getExpect(Player a, Player b) { | |
| return 1.0 / (1.0 + pow(10, (b.score - a.score) / 400.0)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment