-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwissRoundFitness.py
56 lines (51 loc) · 1.78 KB
/
SwissRoundFitness.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from swissround.tournament.Player import Player
from swissround.tournament.Tournament import Tournament
import neat
from sys import stdout
class Network(Player):
def __init__(self, g_id, genome, network):
self.name=str(g_id)
self.g_id=g_id
self.genome=genome
self.network=network
self.score=0
self.hasPlayed=[]
class SwissRoundFitness:
def __init__(self, gamefn):
class GameWrapper:
def __init__(self, p1, p2):
self.n1=p1.network
self.n2=p2.network
self.even=True
def play(self, ):
self.even=not self.even
if self.even:
self.results=gamefn(self.n1, self.n2)
else:
rst=gamefn(self.n2, self.n1)
if rst==1:
self.results=2
elif rst==2:
self.results=1
else:
self.results=-1
self.game=GameWrapper
def run(self, genomes, config):
players=[]
for g_id, genome in genomes:
network=neat.nn.recurrent.RecurrentNetwork.create(genome, config)
players.append(Network(g_id, genome, network))
tournament=Tournament(players, self.game, 3)
for i in range(tournament.nbRounds):
stdout.write(chr(27))
stdout.write('[2;1H')
stdout.write(chr(27))
stdout.write('[2K')
stdout.write("Round ")
stdout.write(str(i))
stdout.flush()
tournament.create_round()
tournament.play_round()
tournament.update_rank()
for network in tournament.ranking:
network.genome.fitness=network.score