forked from curds01/MengeUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
323 lines (285 loc) · 11.2 KB
/
Copy pathagent.py
File metadata and controls
323 lines (285 loc) · 11.2 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
from xml.sax import make_parser, handler
from OpenGL.GL import *
from math import cos, sin, pi
import os
class Agent:
"""Agent object for drawing"""
ID = 0
INACTIVE = 0
AGENT = 1
def __init__( self, radius, pos ):
self.radius = radius
self.pos = pos
self.id = Agent.ID
self.active = Agent.INACTIVE
Agent.ID += 1
def __str__( self ):
return "Agent %d of %d - %s" % ( self.id, Agent.ID, self.pos )
def xml( self ):
return '<Agent p_x="{0}" p_y="{1}" />'.format( self.pos[0], self.pos[1] )
def getActivePos( self ):
if ( self.active == Agent.AGENT ):
return self.pos
else:
raise AttributeError, "Can't get active position for an inactive agent"
def setActivePos( self, p ):
if ( self.active == Agent.AGENT ):
self.pos = p
else:
raise AttributeError, "Can't set active position for an inactive agent"
def setPosition( self, pos ):
self.pos = pos
def activateAgent( self ):
self.active = Agent.AGENT
def deactivate( self ):
self.active = False
def drawGL( self, select=False, editable=False ):
# draw the agent
if ( editable ):
glColor3f( 0, 0, 0.95 )
else:
glColor3f( 0, 0, 0.4 )
scale = self.radius
if ( self.active == Agent.AGENT ):
scale *= 1.5
glPushMatrix()
glTranslate( self.pos[0], self.pos[1], 0 )
glScale( scale, scale, scale )
SEGMENTS = 10
dTheta = 2 * pi / SEGMENTS
if ( select ):
glLoadName( self.id )
glBegin( GL_TRIANGLE_FAN )
glVertex3f( 0, 0, 0 )
for i in range( SEGMENTS + 1 ):
theta = dTheta * i
c = cos( theta )
s = sin( theta )
glVertex3f( c, s, 0 )
glEnd()
glPopMatrix()
class AgentProfile:
'''A simple approximatino of the Menge profile. Currently only supports varying
agent radii.'''
def __init__( self, name, radius, parent=None ):
'''Constructor
@param name A string. The name of this profile group.
@param radius A number. The radius for all agents in this group.
@param parent An AgentProfile reference -- the group that this group
inherits from.
'''
self.name = name
self.radius = radius
self.parent = parent
def xml( self, indent=0 ):
'''Creates the xml for this group.
@param indent The white space to inject before the xml.
@returns A string. The indented XML for this group.
'''
if ( self.parent is None ):
return self.fullXml( indent )
else:
return self.inheritXml( indent )
def fullXml( self, indent ):
'''Creates the xml for a "fully" specified agent profile.
@param indent The white space to inject before the xml.
@returns A string. The indented XML for this group.
'''
return '''{0}<AgentProfile name="{1}" >
{0} <Common max_angle_vel="360" class="1" max_neighbors="10" obstacleSet="1" neighbor_dist="5" r="{2}" pref_speed="1.34" max_speed="2" max_accel="5" />'.format(indent, self.radius)
{0} <PedVO factor="1.57" buffer="0.9" tau="3" tauObst="0.1" turningBias="1.0" />
{0} <Helbing mass="80" />
{0} <ORCA tau="3.0" tauObst="0.15" />
{0}</AgentProfile>'''.format( indent, self.name, self.radius )
def inheritXml( self, indent ):
'''Creates the xml for an inherited agent profile.
@param indent The white space to inject before the xml.
@returns A string. The indented XML for this group.
'''
return '''{0}<AgentProfile name="{1}" inherits="{3}" >
{0} <Common r="{2}" />
{0} </AgentProfile>'''.format(indent, self.name, self.radius, self.parent.name )
class AgentSet:
def __init__( self, defRadius ):
self.defRadius = defRadius
self.agents = []
self.activeAgent = None
self.editable = False
def setRadius( self, radius ):
'''Sets the default radius'''
self.defRadius = radius
def count( self ):
'''Returns the number of agents in the set'''
return len( self.agents )
def selectAgent( self, id ):
"""Given the agent id, returns the agent with activity set correctly"""
agt = self.agents[ id ]
agt.activateAgent()
return agt
def initFromFile( self, file ):
print "Reading", file
base, ext = os.path.splitext( file )
if ( ext == '.xml' ):
parser = make_parser()
agtHandler = AgentXMLParser( self )
parser.setContentHandler( agtHandler )
parser.parse( file )
else:
raise RuntimeError('Only reads xml files')
def groupAgents( self ):
'''Partitions the agents into distinct agent groups by radius size.
@returns A list of tuples: (AgentGroup, list of agents) such that each
list belongs to its corresponding agent group.
'''
groupSet = {}
defProfile = None
for agent in self.agents:
if ( not groupSet.has_key( agent.radius ) ):
if ( defProfile is None ):
profile = AgentProfile( 'group1', agent.radius )
groupSet[ agent.radius ] = (profile, [])
defProfile = profile
else:
profile = AgentProfile( 'group%d' % (len(groupSet) + 1), agent.radius, defProfile )
groupSet[ agent.radius ] = (profile, [] )
else:
profile = groupSet[ agent.radius ]
groupSet[ agent.radius ][1].append( agent )
profiles = groupSet.values()
def compare( a, b ):
if ( a[0].parent is None ):
return -1
elif ( b[0].parent is None ):
return 1
else:
return cmp(a[0].name, b[0].name)
profiles.sort(compare)
return profiles
def xml( self ):
'''Returns the xml-formatted agent group'''
#TODO: I need to partition them based on agent radius
profiles = self.groupAgents()
s = '''<?xml version="1.0"?>
<Experiment version="2.0" >'''
for profile, agentList in profiles:
s += '\n\n' + profile.xml('\t')
for profile, agentList in profiles:
s += '''
<AgentGroup>
<ProfileSelector type="const" name="{0}" />
<StateSelector type="const" name="???" />
<Generator type="explicit" >'''.format(profile.name)
for a in agentList:
s += '\n ' + a.xml()
s += '''
</Generator>
</AgentGroup>'''
s += '''
</Experiment>'''
return s
def addAgent( self, pos, radius = None ):
if ( radius is None ):
radius = self.defRadius
a = Agent( radius, pos )
self.agents.append( a )
return a
def drawGL( self, select=False, dummy=None ):
glPushAttrib( GL_POLYGON_BIT )
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL )
for agent in self.agents:
agent.drawGL( select, self.editable )
glPopAttrib()
def deleteActiveAgent( self ):
"""Removes the active agent"""
if self.activeAgent is not None:
agt = self.activeAgent
activeID = agt.id
Agent.ID -= 1
popped = self.agents.pop(activeID)
assert(popped == agt)
# Decrement the ids of all agents following the active agent.
for a in self.agents[activeID:]:
a.id -= 1
self.activeAgent = None
return True
return False
def clear( self ):
'''Clears all the agents from the set'''
self.agents = []
self.activeAgent = None
self.editable = False
class AgentXMLParser( handler.ContentHandler ):
def __init__( self, agentSet ):
self.agentSet = agentSet
self.version = (1,0)
self.inAgentGroup = False
self.currRadius = 1.0
self.profileRadii = {}
self.currProfile = None
def startElement( self, name, attrs ):
if ( name == 'Experiment' ):
try:
vStr = attrs[ 'version' ]
except:
pass
else:
self.version = map( lambda x: int(x), vStr.split('.') )
elif ( name == 'AgentProfile' ):
try:
self.currProfile = attrs[ 'name' ]
self.profileRadii[ self.currProfile ] = 1.0
except:
raise RuntimeError("Found <AgentProfile> without a name")
try:
parentName = attrs['inherits']
self.profileRadii[ self.currProfile ] = self.profileRadii[ parentName ]
except:
pass
elif ( name == 'Common' ):
if ( self.currProfile is not None ):
try:
r = attrs['r']
self.profileRadii[ self.currProfile ] = float( attrs['r'] )
except:
pass
elif ( name == 'Property' ):
if ( self.currProfile is not None ):
try:
name = attrs['name']
if ( name == 'r' ):
dist = attrs['dist']
if ( dist == 'u' ):
print 'Uniform radius distribution not supported. Using mid-point value'
self.profileRadii[ self.currProfile ] = ( float(attrs['min']) + float(attrs['max']) ) * 0.5
elif ( dist == 'c' ):
self.profileRadii[ self.currProfile ] = float(attrs['value'])
elif ( dist == 'n' ):
print 'Normal radius distributino not supported. Using mean value'
self.profileRadii[ self.currProfile ] = float(attrs['mean'])
except:
pass
elif ( name == 'Agent' ):
if ( not self.inAgentGroup ):
raise RuntimeError( "Found Agent definition outside of AgentGroup" )
x = float( attrs[ 'p_x' ] )
y = float( attrs[ 'p_y' ] )
self.agentSet.addAgent( (x, y), self.profileRadii[ self.currProfile ] )
elif ( name == 'AgentGroup' ):
self.inAgentGroup = True
elif ( name == 'ProfileSelector' ):
try:
self.currProfile = attrs[ 'name' ]
except:
raise RuntimeError("Profile selector doesn't have a profile name reference")
elif ( name == 'Generator' ):
try:
type = attrs['type']
if ( type != 'explicit' ):
print 'Agent group of unsupported type found: %s. These agents will *not* be instantiated' % type
except:
pass
def endElement( self, name ):
if ( name == 'AgentGroup' ):
self.inAgentGroup = False
elif ( name == 'AgentProfile' ):
self.currProfile = None