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 import numpy
40 from numpy import *
41 import random
42
44 """
45 This is a base class for the sea model.
46 Different sea models can implement it,
47 and together with the ships states, they compose the world state.
48 """
49
50
51 - def __init__(self, wind, waterCurrents, waves, obstacles):
52 """
53 Initialize the sea model.
54
55 @type wind: Wind
56 @param wind: the Wind model
57
58 @type waterCurrents: WaterCurrents
59 @param waterCurrents: the water currents model
60
61 @type waves: Waves
62 @param waves: the waves model
63
64 @type obstacles: Obstacles
65 @param obstacles: the sea obstacles model
66 """
67
68
69 self.wind = wind
70 self.waterCurrents = waterCurrents
71 self.waves = waves
72 self.obstacles = obstacles
73
75 res = "\nSea:\n"
76 for member, value in vars(self).items():
77 res += " " + member + "=" + str(value)
78 res += "\n"
79 return res
80
81
82
83
84
85
86
87
89 """
90 This is a base class for wind models.
91 A wind is a mapping from location to a (2D) direction vector.
92 """
93
95 print 'Creating wind model'
96
98 "Given a location, returns the wind vector in it"
99 abstract
100
101
102
104 """
105 This class implements a wind that is constant in every point,
106 and never changes.
107 """
108
109 - def __init__(self, directionX, directionY):
110 "parameters define the constant wind direction vector"
111 self.directionX = directionX
112 self.directionY = directionY
113
115 res = "\nStaticConstantWind:"
116 for member, value in vars(self).items():
117 res += " " + member + "=" + str(value)
118 res += "\n"
119 return res
120
122 "Given a location, returns the wind vector in it"
123 return numpy.array( [self.directionX, self.directionY] )
124
125
126
127
128
129
130
131
133 """
134 This is a base class for water-currents models.
135 A water-current is a mapping from location to a (2D) direction vector.
136 """
137
139 print 'Creating water-current model'
140
142 "Given a location, returns the water-current vector in it"
143 abstract
144
146 """
147 This class implements a water-current that is constant in every point,
148 and never changes.
149 """
150
151 - def __init__(self, directionX, directionY):
152 "parameters define the constant water-current direction vector"
153 self.directionX = directionX
154 self.directionY = directionY
155
157 res = "\nStaticConstantWaterCurrents:"
158 for member, value in vars(self).items():
159 res += " " + member + "=" + str(value)
160 res += "\n"
161 return res
162
164 "Given a location, returns the wind vector in it"
165 return numpy.array( [self.directionX, self.directionY] )
166
167
169 """
170 This class implements a water-current that is constant and never changes,
171 except one vertical stripe (with top and bottom part)
172 that has different conditions.
173 """
174
175 - def __init__(self, waterDirectionX, waterDirectionY,
176 stripeXStart, stripeXEnd,
177 stripeYmiddle,
178 stripeTopWaterDirectionX, stripeTopWaterDirectionY,
179 stripeBottomWaterDirectionX, stripeBottomWaterDirectionY):
180 "parameters define the constant water-current direction vector"
181 self.waterDirectionX = waterDirectionX
182 self.waterDirectionY = waterDirectionY
183 self.stripeXStart = stripeXStart
184 self.stripeXEnd = stripeXEnd
185 self.stripeYmiddle = stripeYmiddle
186 self.stripeTopWaterDirectionX = stripeTopWaterDirectionX
187 self.stripeTopWaterDirectionY = stripeTopWaterDirectionY
188 self.stripeBottomWaterDirectionX = stripeBottomWaterDirectionX
189 self.stripeBottomWaterDirectionY = stripeBottomWaterDirectionY
190
191
193 res = "\nStripeWaterCurrents:"
194 for member, value in vars(self).items():
195 res += " " + member + "=" + str(value)
196 res += "\n"
197 return res
198
200 "Given a location, returns the wind vector in it"
201 if(x < self.stripeXStart or x > self.stripeXEnd):
202
203 return numpy.array( [self.waterDirectionX, self.waterDirectionY] )
204 elif( y > self.stripeYmiddle ):
205 return numpy.array( [self.stripeTopWaterDirectionX, self.stripeTopWaterDirectionY] )
206 else:
207 return numpy.array( [self.stripeBottomWaterDirectionX, self.stripeBottomWaterDirectionY] )
208
210 """
211 Hard coded values For the Island example.
212 We have a few islands, and two areas of strong currents
213 between them.
214 """
216 self.points = points
217
218 b = self.points
219
220
221 b5 = numpy.array(b[5])
222 b34 = numpy.array(b[34])
223 b3 = numpy.array(b[3])
224
225 tmp = b34 - b5
226 self.bay1Line = tmp * 1.0 / linalg.norm(tmp)
227
228
229 self.normalBay1Line = numpy.array([self.bay1Line[1], -self.bay1Line[0]])
230
231
232 self.border1VerticalValueTop = dot(self.normalBay1Line, b5)
233 self.border1VerticalValueBottom = dot(self.normalBay1Line, b3)
234 self.border1VerticalValueMiddle = (self.border1VerticalValueTop +
235 self.border1VerticalValueBottom) * 1.0 / 2
236 self.border1HorizonalValueTop = dot(self.bay1Line, b34) + 50
237 self.border1HorizonalValueBottom = dot(self.bay1Line, b5) - 50
238 self.border1HorizonalValueMiddle = (self.border1HorizonalValueTop +
239 self.border1HorizonalValueBottom) * 1.0 / 2
240
241
242
243 b25 = numpy.array(b[25])
244 b24 = numpy.array(b[24])
245 b13 = numpy.array(b[13])
246 b14 = numpy.array(b[14])
247
248 tmp = b25 - b24
249 self.bay2Line = tmp * 1.0 / linalg.norm(tmp)
250
251
252 self.normalBay2Line = numpy.array([self.bay2Line[1], -self.bay2Line[0]])
253
254
255 self.border2VerticalValueTop = dot(self.normalBay2Line, b24) + 50
256 self.border2VerticalValueBottom = dot(self.normalBay2Line, b13) - 50
257 self.border2VerticalValueMiddle = (self.border2VerticalValueTop +
258 self.border2VerticalValueBottom) * 1.0 / 2
259 self.border2HorizonalValueTop = dot(self.bay2Line, b25)
260 self.border2HorizonalValueBottom = dot(self.bay2Line, b14)
261 self.border2HorizonalValueMiddle = (self.border2HorizonalValueTop +
262 self.border2HorizonalValueBottom) * 1.0 / 2
263
265 res = "\nIslandWaterCurrents:"
266 for member, value in vars(self).items():
267 res += " " + member + "=" + str(value)
268 res += "\n"
269 return res
270
272
273 p = numpy.array((x,y))
274 dot1 = dot(p, self.normalBay1Line)
275 dot2 = dot(p, self.bay1Line)
276 inSideBay1 = ((self.border1VerticalValueBottom <= dot1 <= self.border1VerticalValueTop) and
277 (self.border1HorizonalValueBottom <= dot2 <= self.border1HorizonalValueTop))
278 if inSideBay1:
279 return self.bay1Line * 8
280
281 dot1 = dot(p, self.normalBay2Line)
282 dot2 = dot(p, self.bay2Line)
283 inSideBay2 = (self.border2VerticalValueBottom <= dot1 <= self.border2VerticalValueTop and
284 self.border2HorizonalValueBottom <= dot2 <= self.border2HorizonalValueTop)
285 if inSideBay2:
286 return self.normalBay2Line * -8
287
288
289 waterSpeed = random.weibullvariate(1,2)
290 waterDirection = random.randint(0,360)
291 return numpy.array( waterSpeed * cos(radians(waterDirection)), waterSpeed * sin(radians(waterDirection)) )
292
294 """
295 Defines north->south or south->north currents.
296 Was used for the AAAI 11 experiments.
297 """
298
299 - def __init__(self, regionsAndCurrentsList):
300 """
301 @type regionsAndCurrentsList: list of 3-tuples
302 @param regionsAndCurrentsList: a list of 3-tuples, each defines a stripe: (minX, maxX, currentValue), where currentValue is signed to account for direction.
303 """
304 self.regionsAndCurrentsList = regionsAndCurrentsList
305
307 """
308 Check stripe and return corresponding current.
309 """
310 for p1, p2, current in self.regionsAndCurrentsList:
311 x1 = p1[0]
312 x2 = p2[0]
313 if x1 <= x <= x2:
314
315 return current * numpy.array([0,1])
316 return numpy.array([0, 0])
317
318
320 res = "\nVerticalWaterCurrents:"
321 for member, value in vars(self).items():
322 res += " " + member + "=" + str(value)
323 res += "\n"
324 return res
325
326
327
328
329
330
331
332
333
334
335
336
337
339 """
340 This is a base class for wave models.
341 Currently, we assume that waves just have a direction vector
342 in each location. We ignore the wave height, and the fact
343 that is has some period, and averaging everything out,
344 to assume a direction vector in each (x,y) point.
345 """
346
348 print 'Creating water-current model'
349
351 "Given a location, returns the avg speed vector, for this location"
352 abstract
353
355 """
356 This class implements waves that are constant in every point,
357 and never change.
358 """
359
360 - def __init__(self, directionX, directionY):
361 "Parameters define the constant wave speed vector"
362 self.directionX = directionX
363 self.directionY = directionY
364
366 res = "\nStaticConstantWaves:"
367 for member, value in vars(self).items():
368 res += " " + member + "=" + str(value)
369 res += "\n"
370 return res
371
373 "Given a location, returns the wave vector in it"
374 return numpy.array( [self.directionX, self.directionY] )
375
376
377
378
379
380
381
383 """
384 An obstacle in the sea. Currently an obstacle is just a
385 straight line. This class support general polygon but,
386 for now we only want line composed of 2 points. It is
387 possible to construct complex obstacles just from
388 straight lines.
389
390 Therfore, for now IGNORE the following:
391 [Currently implemented
392 as a list of points, where connecting each two
393 consecutive points by line defines the obstacle's
394 shape. The first point should be equal to the last one.]
395 """
397 """
398 @type points: a list of (x,y) tuples
399 @param points: a list of points - connecting each consecutive pair of points by line defines the obstacle's shape.
400 """
401 self.points = points
402
403
404 if len(points) != 2:
405 raise Exception("We currently support only straight line obstacles (though this could easily be changed)")
406
408 res = "\nObstacle:"
409
410
411 for p in self.points:
412 res += " " + str(point)
413 res += "\n"
414 return res
415
417 """
418 @rtype: tuple
419 @return: a list of points defining the shape of the obstacle
420 """
421 return self.points
422
423
424
426 """
427 This class models the obstacles part of the sea.
428 """
430 """
431 Initialize the set of obstacles.
432
433 @type obstacleList: a list of Obstacle.
434 @param obstacleList: a list of obstacles.
435 """
436 print 'Creating obstacles model'
437 self.obstacles = obstacleList
438
440 return self.obstacles
441
443 res = "\nObstacles:"
444
445
446 for o in self.obstacles:
447 res += str(o)
448 res += "\n"
449 return res
450