The following code generates a CSV in which a series of columns are displayed (PLAYER, SIGNAL, ROUND1, ROUND2, ROUND3). In the columns "RONDAx" appear the times that each player has shown a determined signal in each round throughout 1000 simulations of the game (see for _ in range(1000)
). The code is configured for 3 rounds and 4 players.
from random import random, sample
from bisect import bisect
import csv
class Partida():
def __init__(self, jugadores, emparejamientos, senales, s, b, x, m):
self.emparejamientos = emparejamientos
self.senales = senales
self.s = s
self.b = b
self.x = x
self.m = m
self.jugadores = {nombre: Partida.Jugador(senales)
for pareja in emparejamientos[0]
for nombre in pareja}
self.memoria = list()
def generar_senales(self):
def with_b(muestra, observa, s, r):
if not (muestra == observa == 0):
result = ((0.98) * (1.0 - self.b) * (1.0 - self.x) * muestra/r) + ((0.98) * (1.0 - self.b) * (self.x) * observa/r) + ((0.98) * self.b * s) + ((self.m / 8))
else:
result = ((0.98) * (1.0 - 0) * (1.0 - self.x) * muestra/r) + ((0.98) * (1.0 - 0) * (self.x) * observa/r) + ((0.98) * 0 * s) + ((self.m / 8))
return result
def choice(opciones, probs):
probAcumuladas = list()
aux = 0
for p in probs:
aux += p
probAcumuladas.append(aux)
r = random() * probAcumuladas[-1]
op = bisect(probAcumuladas, r)
return opciones[op]
yield dict(zip(self.jugadores.keys(), self.senales))
r = 1
while True:
eleccs = dict.fromkeys(self.jugadores.keys())
for nombre, inst in self.jugadores.items():
probs = [with_b(inst.mem_mostradas[op], inst.men_observadas[op], self.s[indx], r)
for indx, op in enumerate(self.senales)]
eleccs[nombre] = choice(self.senales, probs)
r += 1
yield eleccs
def jugar(self):
gen_sens = self.generar_senales()
for n, ronda in enumerate(self.emparejamientos):
senales = next(gen_sens)
self.memoria.append(senales)
for jugador1, jugador2 in ronda:
self.jugadores[jugador1].men_observadas[senales[jugador2]] += 1
self.jugadores[jugador2].men_observadas[senales[jugador1]] += 1
self.jugadores[jugador1].mem_mostradas[senales[jugador1]] += 1
self.jugadores[jugador2].mem_mostradas[senales[jugador2]] += 1
class Jugador():
def __init__(self, senales):
self.mem_mostradas = {senal: 0 for senal in senales}
self.men_observadas = {senal: 0 for senal in senales}
def main():
jugadores = [1, 2, 3, 4]
senales = ['S1', 'S2', 'S3', 'S4', ]
emparejamientos = [[(1,2),(3,4)],
[(1,3),(2,4)],
[(1,4),(2,3)]]
s=[1,0,0,0]
b=0.5
x=0.5
m=0.02
rondas = ['RONDA' + str(n+1) for n in range(len(emparejamientos))]
estadisticas = {jugador:{senal:[0 for ronda in rondas]
for senal in senales}
for jugador in jugadores}
for _ in range(1000):
juego = Partida(jugadores, emparejamientos, senales, s, b, x, m)
juego.jugar()
for n, ronda in enumerate(juego.memoria):
for jugador, senal in ronda.items():
estadisticas[jugador][senal][n] += 1
with open('salida.csv', 'wb') as csvfile:
writer =csv.writer(csvfile, delimiter=',',
quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(['JUGADOR', 'SENAL'] + rondas)
for jugador in jugadores:
for senal in senales:
writer.writerow([jugador, senal]+estadisticas[jugador][senal])
if __name__ == '__main__':
main()
We now want to use this same code to generate x samples of x simulations and collect the data in a CSV as in the following example:
That is to say, as shown, it is intended that the code makes 3 times 1000 simulations of the complete game, and that the results of the 3 times (3 samples) are stored as shown in the image. Where in "sample" the number "1" refers to the first 1000 simulations, "2" refers to the second 1000 simulations and so on. Where "round" refers to the round number in the game. Where "player" refers to the player number.
Note that in this case the parameters b, x and m are included in the table, and that the numbers in the "Senalx" columns should refer to the times that each signal has been chosen in each round.