You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.7 KiB
Python

import lib, fiar
nets = lib.loadAll()
gen = 0
def NvN(net1, net2, printField=False):
game = fiar.FIAR();
while True:
game = lib.nextNNmove(net1, game, 1);
if(game.proveWinner()):
break
game = lib.nextNNmove(net2, game, 2);
if(game.proveWinner()):
break
if printField:
game.printField()
print(net1.synapses[0][0][0])
print()
return game.proveWinner()
try:
while True:
gen += 1
print("Gen:", gen)
# Reset the nets' score:
for net in nets:
net.score = 0
# Let the nets play against each other:
for i in range(len(nets)):
for j in range(i+1, len(nets)):
winner = NvN(nets[i], nets[j], i==0 and j==1)
#print("Net1:",i,"Net2:",j)
#print("Winner:",winner)
if(winner == 1):
nets[i].score += 1
if(winner == 2):
nets[j].score += 1
winner = NvN(nets[j], nets[i])
#print("Net1:",i,"Net2:",j)
#print("Winner:",winner)
if(winner == 1):
nets[i].score += 1
if(winner == 2):
nets[j].score += 1
# Sort the nets by their score:
nets.sort(key=lambda x: x.score, reverse=True)
for i in range(int(len(nets)/3)):
nets[i+int(len(nets)/3)] = nets[i];
nets[i+int(len(nets)/3)].mutate(0.001);
nets[i+int(len(nets)/3*2)] = nets[i];
nets[i+int(len(nets)/3*2)].mutate(0.1);
except KeyboardInterrupt:
print("Saving all Data...")
lib.saveAll(nets)