-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
86 lines (69 loc) · 1.99 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import threading
import time
import random
import sys
import multiprocessing as mp
import os
# import agent types (positions)
from AI.soccerpy.agent import Agent as A0
# strikers
from AI.s import Agent as A1
# defenders
from AI.d import Agent as A2
# goalie
from AI.g import Agent as A3
# set team
TEAM_NAME = 'shobhit'
NUM_PLAYERS = 11
if __name__ == "__main__":
# return type of agent: midfield, striker etc.
def agent_type(position):
return {
2: A2,
3: A3,
4: A2,
6: A2,
7: A2,
8: A2,
}.get(position, A1)
# spawn an agent of team_name, with position
def spawn_agent(team_name, position):
"""
Used to run an agent in a seperate physical process.
"""
# return type of agent by position, construct
a = agent_type(position)()
a.connect("localhost", 6000, team_name)
a.play()
# we wait until we're killed
while 1:
# we sleep for a good while since we can only exit if terminated.
time.sleep(1)
# spawn all agents as seperate processes for maximum processing efficiency
agentthreads = []
for position in xrange(1, NUM_PLAYERS+1):
print " Spawning agent %d..." % position
at = mp.Process(target=spawn_agent, args=(TEAM_NAME, position))
at.daemon = True
at.start()
agentthreads.append(at)
print "Spawned %d agents." % len(agentthreads)
print
print "Playing soccer..."
# wait until killed to terminate agent processes
try:
while 1:
time.sleep(0.05)
except KeyboardInterrupt:
print
print "Killing agent threads..."
# terminate all agent processes
count = 0
for at in agentthreads:
print " Terminating agent %d..." % count
at.terminate()
count += 1
print "Killed %d agent threads." % (count - 1)
print
print "Exiting."
sys.exit()