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 """
215
217 self.points = points
218
219 b = self.points
220
221
222 b5 = numpy.array(b[5])
223 b34 = numpy.array(b[34])
224 b3 = numpy.array(b[3])
225
226 tmp = b34 - b5
227 self.bay1Line = tmp * 1.0 / linalg.norm(tmp)
228
229
230 self.normalBay1Line = numpy.array([self.bay1Line[1], -self.bay1Line[0]])
231
232
233 self.border1VerticalValueTop = dot(self.normalBay1Line, b5)
234 self.border1VerticalValueBottom = dot(self.normalBay1Line, b3)
235 self.border1VerticalValueMiddle = (self.border1VerticalValueTop +
236 self.border1VerticalValueBottom) * 1.0 / 2
237 self.border1HorizonalValueTop = dot(self.bay1Line, b34) + 50
238 self.border1HorizonalValueBottom = dot(self.bay1Line, b5) - 50
239 self.border1HorizonalValueMiddle = (self.border1HorizonalValueTop +
240 self.border1HorizonalValueBottom) * 1.0 / 2
241
242
243
244 b25 = numpy.array(b[25])
245 b24 = numpy.array(b[24])
246 b13 = numpy.array(b[13])
247 b14 = numpy.array(b[14])
248
249 tmp = b25 - b24
250 self.bay2Line = tmp * 1.0 / linalg.norm(tmp)
251
252
253 self.normalBay2Line = numpy.array([self.bay2Line[1], -self.bay2Line[0]])
254
255
256 self.border2VerticalValueTop = dot(self.normalBay2Line, b24) + 50
257 self.border2VerticalValueBottom = dot(self.normalBay2Line, b13) - 50
258 self.border2VerticalValueMiddle = (self.border2VerticalValueTop +
259 self.border2VerticalValueBottom) * 1.0 / 2
260 self.border2HorizonalValueTop = dot(self.bay2Line, b25)
261 self.border2HorizonalValueBottom = dot(self.bay2Line, b14)
262 self.border2HorizonalValueMiddle = (self.border2HorizonalValueTop +
263 self.border2HorizonalValueBottom) * 1.0 / 2
264
266 res = "\nIslandWaterCurrents:"
267 for member, value in vars(self).items():
268 res += " " + member + "=" + str(value)
269 res += "\n"
270 return res
271
273
274 p = numpy.array((x,y))
275 dot1 = dot(p, self.normalBay1Line)
276 dot2 = dot(p, self.bay1Line)
277 inSideBay1 = ((self.border1VerticalValueBottom <= dot1 <= self.border1VerticalValueTop) and
278 (self.border1HorizonalValueBottom <= dot2 <= self.border1HorizonalValueTop))
279 if inSideBay1:
280 return self.bay1Line * 8
281
282 dot1 = dot(p, self.normalBay2Line)
283 dot2 = dot(p, self.bay2Line)
284 inSideBay2 = (self.border2VerticalValueBottom <= dot1 <= self.border2VerticalValueTop and
285 self.border2HorizonalValueBottom <= dot2 <= self.border2HorizonalValueTop)
286 if inSideBay2:
287 return self.normalBay2Line * -8
288
289
290 waterSpeed = random.weibullvariate(1,2)
291 waterDirection = random.randint(0,360)
292 return numpy.array( waterSpeed * cos(radians(waterDirection)), waterSpeed * sin(radians(waterDirection)) )
293
295 """
296 Defines north->south or south->north currents.
297 Was used for the AAAI 11 experiments.
298 """
299
300 - def __init__(self, regionsAndCurrentsList):
301 """
302 @type regionsAndCurrentsList: list of 3-tuples
303 @param regionsAndCurrentsList: a list of 3-tuples, each defines a stripe: (minX, maxX, currentValue), where currentValue is signed to account for direction.
304 """
305 self.regionsAndCurrentsList = regionsAndCurrentsList
306
308 """
309 Check stripe and return corresponding current.
310 """
311 for p1, p2, current in self.regionsAndCurrentsList:
312 x1 = p1[0]
313 x2 = p2[0]
314 if x1 <= x <= x2:
315
316 return current * numpy.array([0,1])
317 return numpy.array([0, 0])
318
319
321 res = "\nVerticalWaterCurrents:"
322 for member, value in vars(self).items():
323 res += " " + member + "=" + str(value)
324 res += "\n"
325 return res
326
327
328
329
330
331
332
333
334
335
336
337
338
340 """
341 This is a base class for wave models.
342 Currently, we assume that waves just have a direction vector
343 in each location. We ignore the wave height, and the fact
344 that is has some period, and averaging everything out,
345 to assume a direction vector in each (x,y) point.
346 """
347
349 print 'Creating water-current model'
350
352 "Given a location, returns the avg speed vector, for this location"
353 abstract
354
356 """
357 This class implements waves that are constant in every point,
358 and never change.
359 """
360
361 - def __init__(self, directionX, directionY):
362 "Parameters define the constant wave speed vector"
363 self.directionX = directionX
364 self.directionY = directionY
365
367 res = "\nStaticConstantWaves:"
368 for member, value in vars(self).items():
369 res += " " + member + "=" + str(value)
370 res += "\n"
371 return res
372
374 "Given a location, returns the wave vector in it"
375 return numpy.array( [self.directionX, self.directionY] )
376
377
378
379
380
381
382
384 """
385 An obstacle in the sea. Currently an obstacle is just a
386 straight line. This class support general polygon but,
387 for now we only want line composed of 2 points. It is
388 possible to construct complex obstacles just from
389 straight lines.
390
391 Therfore, for now IGNORE the following:
392 [Currently implemented
393 as a list of points, where connecting each two
394 consecutive points by line defines the obstacle's
395 shape. The first point should be equal to the last one.]
396 """
398 """
399 @type points: a list of (x,y) tuples
400 @param points: a list of points - connecting each consecutive pair of points by line defines the obstacle's shape.
401 """
402 self.points = points
403
404
405 if len(points) != 2:
406 raise Exception("We currently support only straight line obstacles (though this could easily be changed)")
407
409 res = "\nObstacle:"
410
411
412 for p in self.points:
413 res += " " + str(point)
414 res += "\n"
415 return res
416
418 """
419 @rtype: tuple
420 @return: a list of points defining the shape of the obstacle
421 """
422 return self.points
423
424
425
427 """
428 This class models the obstacles part of the sea.
429 """
431 """
432 Initialize the set of obstacles.
433
434 @type obstacleList: a list of Obstacle.
435 @param obstacleList: a list of obstacles.
436 """
437 print 'Creating obstacles model'
438 self.obstacles = obstacleList
439
441 return self.obstacles
442
444 res = "\nObstacles:"
445
446
447 for o in self.obstacles:
448 res += str(o)
449 res += "\n"
450 return res
451