reservoir_sampling

AuthorFederico Bonfiglio
Submission date2018-05-10 16:13:18.107040
Rating5677
Matches played309
Win rate56.31

Use rpsrunner.py to play unranked matches on your computer.

Source code:

if input == "":
    import random, collections

    class RSPlayer():
        def __init__(self,k):
            self.res = []
            self.k = k
            self.t = 0.0
            self.moves = {1:"R",2:"P",3:"S"}

        def sostitute(self,p):
            return random.random() >= (1 - p)

        def sample(self,x):
            if x == "R":
                x = 1
            elif x == "P":
                x = 2
            elif x == "S":
                x = 3
            if self.t <= (self.k-1):
                self.res.append(x)
            else:
                if self.sostitute((self.k-1)/self.t):
                    self.res[random.randint(0,self.k-1)] = x
            self.t += 1.0

        def make_move(self):
            if self.t <= self.k:
                probs = {1:self.k/3.0,2:self.k/3.0,3:self.k/3.0}
            else:
                probs = collections.Counter(self.res)
            x,cn = random.random(),0.0
            for num in probs:
                cn+=probs[num]/self.k
                if cn >= x:
                    return self.moves[(num%3)+1]

    player = RSPlayer(100.0)
    output = player.make_move()
else:
    player.sample(input)
    output = player.make_move()