Attempt at fixing memory mess.
[progcomp10.git] / src / simulate.py
1 #!/usr/bin/python2.5
2 '''simulate.py - Runs a full simulation of the UCC Programming Competition with the provided agents.
3 Written by Luke Williams <[email protected]> for the UCC Programming Competition in 2008.
4
5 Licensed under an MIT-style license: see the LICENSE file for details.
6 '''
7
8 # this is the default arena. To chose a different one - for example to run your
9 # own bots - use -a arenaName on the command line, or change this variable.
10 arenaName = "PythonSampleAgents"
11
12 ####################################
13 # Developers only past this point! #
14 ####################################
15
16 import sys
17 from conf import *
18 from uccProgComp import Supervisor
19
20 usage = "Usage: rps [-v] [-i iterations=150] [-n starting_populations=10] [-t trials=1]"
21 for i in range (1,len(sys.argv)):
22         if sys.argv[i] == "-i":
23                 try:
24                         MAX_ITERATIONS = int(sys.argv[i+1])
25                         i += 1
26                         continue
27                 except:
28                         print usage
29                         sys.exit(1)
30         elif sys.argv[i] == "-n":
31                 try:
32                         startingPopulations = int(sys.argv[i+1])
33                         i += 1
34                         continue
35                 except:
36                         print usage
37                         sys.exit(1)
38         elif sys.argv[i] == "-t":
39                 try:
40                         TRIALS = int(sys.argv[i+1])
41                         i += 1
42                         continue
43                 except:
44                         print usage
45                         sys.exit(1)
46         elif sys.argv[i] == "-v":
47                 VERBOSE = True
48         elif sys.argv[i] == "-a":
49                 arenaName = sys.argv[i+1]
50                 i += 1
51
52
53 #import the arena - NOTE THAT THIS IS A POTENTIAL SECURITY HOLE,
54 # AS INPUT IS NOT SANITY CHECKED!
55 importString = "from arenas." + arenaName + " import arena"
56 exec importString
57
58 iteration = 0
59 trial = 0
60 winners = {}
61 while trial < TRIALS:
62         sup = Supervisor ()
63         for Agent in arena.Agents: sup.RegisterAgent (Agent)
64         sup.GeneratePopulation (STARTING_POPULATION)
65
66         trial += 1
67         iteration = 0
68         while iteration < MAX_ITERATIONS and not sup.IsGameOver ():
69                 iteration += 1
70                 sup.Iterate ()
71                 if not VERBOSE: continue
72                 print "Iteration %d:" % iteration
73                 for key, value in sup.GetStats ().iteritems():
74                         print "%s: Population=%d, Newborns=%d, Deaths=%d" % (key, value[0], value[1], value[2])
75         winner = ("Error", -1)
76         for key, value in sup.GetStats ().iteritems ():
77                 #print key, value
78                 if value[0] > winner[1]:
79                         winner = (key, value[0])
80         if winner[0] in winners: winners[winner[0]] += 1
81         else: winners[winner[0]] = 1
82         #print "Winner: %s" % winner[0]
83
84 print "SCOREBOARD OVER %d TRIALS OF %d ROUNDS EACH" % (TRIALS, MAX_ITERATIONS)
85 rawscoreboard = sorted ( [(score,player) for (player,score) in winners.items ()] , reverse=True )
86 scoreboard = []
87 for score, player in rawscoreboard:
88         print "%s: %s" % (player, score)
89

UCC git Repository :: git.ucc.asn.au