-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathusv.py
executable file
·2612 lines (1848 loc) · 89.8 KB
/
usv.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding=utf-8
from collections import namedtuple
from math import sin, cos, pi, atan, sqrt
from implementation import *
import random
import time
global DEBUGPrint
DEBUGPrint = False
class StaticUSV(object):
"""一个静态的USV类,move方法将会留空,这表示此类USV不可行动"""
def __init__(self, uid, x, y, env):
'''每艘USV的独立id,可以用来区分各舰'''
self.id = uid
'''env是指当前USV所在的环境,它指向当前游戏中这艘USV所在的Map类实例'''
self.env = env
self.x, self.y = x, y
self.speed = 0.0
self.angular_speed = 0.0
self.direction = 0.0
self.is_enemy = False
def decision_algorithm(self):
'''decision_algroithm是指导USV运动的方法,返回一个自定义的action字典'''
pass
def move(self):
'''USV运动的主方法,根据action来调用其它辅助函数完成下一时刻USV位置的计算'''
pass
def is_decision_legal(self, decisionX, decisionY):
'''判断USV决定要去的位置是否合法;在这个基本的函数里,所有舰艇不得走出地图范围,不得走到
其它舰艇已经占用的位置;友军舰艇不得走到被保护的目标点.'''
width, height = self.env.width, self.env.height
if(decisionX < 0 or decisionY < 0 or decisionX > width - 1 or decisionY > height - 1):
return False
occupied = False
for ship in self.env.ships:
if(ship.id == self.id):
continue
shipX, shipY = ship.coordinate()
if(shipX == decisionX and shipY == decisionY):
occupied = True
if(not self.is_enemy):
tX, tY = self.env.target_coordinate()
if(tX == decisionX and tY == decisionY):
occupied = True
if(occupied):
return False
return True
def turn(self, clockwise):
'''这一函数描绘本艘USV在一单位时间内如何改变自身方向,因此其作用是在顺时针或逆时针方向上
增加当前USV角速度的绝对值(角度变化=角速度*1时间单位=角速度的绝对值)'''
if(clockwise):
self.direction += self.angular_speed
if(self.direction >= 360):
self.direction -= 360
else:
self.direction -= self.angular_speed
if(self.direction < 0):
self.direction += 360
def coordinate(self):
'''返回本USV的位置'''
return self.x, self.y
def set_as_enemy(self):
'''将本USV定义为敌方(进攻方)'''
self.is_enemy = True
def set_as_friendly(self):
'''将本USV定义为友军(防守方)'''
self.is_enemy = False
class BasicPlaneUSV(StaticUSV):
"""基本平面USV, 这个USV可以在瞬间改变自己的角速度和速度, 转动后在对应方向上走动一帧时间*速度的距离"""
def __init__(self, uid, x, y, env):
super(BasicPlaneUSV, self).__init__(uid, x, y, env)
self.action_class = Action = namedtuple("action", ['stay', 'clockwise', 'angular_speed', 'speed'])
def decision_algorithm(self):
'''这种USV的action对象有四个属性:1.stay,如果设为True,代表USV决定不行动,后面的参数被忽略;
2.clockwise,转动方向是否是顺时针;3.angular_speed角速度;4.speed速度.
如果stay参数为False,USV将会根据clockwise的指示转动angular_speed*t(一帧时间)度,然后前进当前的速度*t的距离'''
Action = self.action_class
example_action = Action(False, False, 20.0, 10.0)
example_action1 = Action(True, False, 0.0, 0.0)
raise Exception("请覆盖decision_algorithm方法!")
def move(self):
action = self.decision_algorithm()
if(not action.stay):
self.update_direction(action)
self.update_speed(action)
self.update_coordinate()
def update_direction(self, action):
self.angular_speed = action.angular_speed
self.turn(action.clockwise)
def update_speed(self, action):
self.speed = action.speed
def update_coordinate(self): #x轴负方向是0度,y正方向是90度,所以如下计算
self.x -= cos(pi * self.direction / 180) * self.speed
self.y += sin(pi * self.direction / 180) * self.speed
class OneStepUSV(BasicPlaneUSV):
"""一个简单的USV类,在网格上它一次只能走动一步.每一时间单位,这种USV能够瞬时的改变自己的角速度,然后转动,最后向
转动后的方向上移动一格."""
def __init__(self, uid, x, y, env):
super(OneStepUSV, self).__init__(uid, x, y, env)
self.action_class = namedtuple("action", ['stay', 'clockwise', 'angular_speed'])
self.speed = 1
def decision_algorithm(self):
'''这种USV的action字典有三个参数:1.stay,如果设为True,代表USV决定不行动,后面的参数被忽略;
2.clockwise,转动方向是否是顺时针;3.angular_speed角速度.
如果stay参数为False,USV将会根据clockwise的指示转动angular_speed度,然后前进一步.注意由于
此模型下angular_speed只能为90的倍数'''
Action = self.action_class
example_action = Action(False, False, 20.0)
example_action1 = Action(True, False, 0.0)
raise Exception("请覆盖decision_algorithm方法!")
def move(self):
action = self.decision_algorithm()
'''print('USV中的action',action)'''
if(not action.stay):
self.update_direction(action)
self.update_coordinate()
def moverandom(self):
Action = self.action_class
action = Action(False, False, 90.0 * random.randint(0, 3))
self.update_direction(action)
self.update_coordinate_enemyship()
def update_coordinate_enemyship(self):
originalx, originaly = self.x, self.y
beforetemp = self.env.env_matrix()
if (self.direction == 0.0): # 向上移动一格,将direct置为0度
self.x -= self.speed
elif (self.direction == 90.0): # 向右移动一格,将direct置为90度
self.y += self.speed
elif (self.direction == 180.0): # 向下移动一格,将direct置为180度
self.x += self.speed
elif (self.direction == 270.0): # 向左移动一格,将direct置为270度
self.y -= self.speed
#越界处理
if self.x <0 or self.x >self.env.width-1 or self.y <0 or self.y >self.env.height-1:
if self.x <0: self.x = 0
if self.x >self.env.width-1: self.x = self.env.width-1
if self.y <0 : self.y = 0
if self.y >self.env.height-1: self.y = self.env.height-1
# 判断更新后的位置是否合法(是否越界或是该位置有值)
if beforetemp[self.x][self.y] != 0:
self.x, self.y = originalx, originaly
def update_coordinate(self): #左上角是(0,0)
#print('USV中位置更新之前:',self.x,self.y)
#print(self.direction)
if(self.direction == 0.0): #向上移动一格,将direct置为0度
self.x -= self.speed
elif(self.direction == 90.0): #向右移动一格,将direct置为90度
self.y += self.speed
elif(self.direction == 180.0): #向下移动一格,将direct置为180度
self.x += self.speed
elif(self.direction == 270.0): #向左移动一格,将direct置为270度
self.y -= self.speed
else:
raise Exception(
"OneStepUSV的direction属性应该是正交角度,然而,得到了 %f 度" % self.direction)
class MyUSV(OneStepUSV):
'''一个策略简单的USV,派生自OneStepUSV'''
def __init__(self, uid, x, y, env):
super(MyUSV, self).__init__(uid, x, y, env)
def finda(self):
findamap = self.env.str2() #A*方法的输入地图
#findamap 与 diagram的图是镜像对称的,x与y是相反的
diagram = GridWithWeights(len(findamap[0]),len(findamap))
obstacles = []
for kk in range(len(findamap)):
for hh in range(len(findamap[kk])):
if findamap[kk][hh] == 'S':
startpoint = (hh,kk)
if findamap[kk][hh] == 'E':
endpoint = (hh,kk)
if findamap[kk][hh] == '#':
obstacles.append((hh,kk))
diagram.walls = obstacles
#print('draw_grid')
#draw_grid(diagram, weights=2,start=startpoint, goal=endpoint)
came_from, cost_so_far = a_star_search(diagram, start = startpoint, goal = endpoint)
#draw_grid(diagram, width=1, path=reconstruct_path(came_from, start=startpoint, goal=endpoint))
pathtm = reconstruct_path(came_from, start=startpoint, goal=endpoint)
'''print(pathtm)'''
firstnode = (pathtm[0][1],pathtm[0][0])
secondnode = (pathtm[1][1],pathtm[1][0])
# 这里的地图形式是:左上角是(0,0),右下角是(n,n),,所以下面上下左右如下描述::
if secondnode[1] - firstnode[1] == 0 and secondnode[0] - firstnode[0] == -1:
string = 'up'
elif secondnode[1] - firstnode[1] == -1 and secondnode[0] - firstnode[0] == 0:
string = 'left'
elif secondnode[1] - firstnode[1] == 0 and secondnode[0] - firstnode[0] == 1:
string = 'down'
else:
string = 'right'
'''print('action是上下左右中的',string)'''
#print('USV当前位置:',self.x,self.y)
if (string =='left'):
direct = 270.0
elif(string =='down'):
direct = 180.0
elif(string =='right'):
direct = 90.0
else:
direct = 0.0
Action = self.action_class
# 这里要根据A*算法进行修改,只获取走一步,
next_action = Action(False, True, direct)
# print('使用A*算法的下一步:',next_action)
return next_action
def decision_algorithm(self):
act = self.finda()
return act
def update_direction(self, action):
self.direction = action.angular_speed
def recordenv(self):
curenv = self.env.env_matrix()
return curenv
def recordaction(self):
curaction = self.decision_algorithm()
return curaction
class MyContinueUSV(BasicPlaneUSV):
'''一个策略简单的USV,派生自BasicObsUSV,用于连续环境下USV,
认为USV可瞬间达到下一次角速度且按照speed走一帧时间的距离
'''
def __init__(self, uid, x, y, env):
super(MyContinueUSV, self).__init__(uid, x, y, env)
self.action_class = Action = namedtuple("action", ['stay', 'clockwise', 'angular_speed', 'speed'])
self.speed = 1
self.radius = 1
def getuid(self):
return self.id
def update_coordinate(self): #x轴负方向是0度,y正方向是90度,(按照左上角是0,0更新坐标),所以如下计算
self.y -= cos(pi * self.direction / 180) * self.speed
self.x -= sin(pi * self.direction / 180) * self.speed
def decision_algorithm(self):
'''这种USV的action对象有四个属性:
1.stay,如果设为True,代表USV决定不行动,后面的参数被忽略;
2.clockwise,转动方向是否是顺时针;
3.angular_speed角速度;
4.speed速度.
如果stay参数为False,USV将会根据clockwise的指示转动angular_speed*t(一帧时间)度,然后前进当前的速度*t的距离'''
#Action = self.action_class
#act = Action(False, False, 2.0, 1.0)
#act = self.pathGuide()
act = self.pathGuide2()
return act
#起点朝向终点进行导引-方法1
def pathGuide(self):
#difference_angular = self.next_angular_guide2() - self.direction
difference_angular = self.next_angular_guide3((self.x, self.y), self.env.target_coordinate()) - self.direction
#print('当前direction,当前difference_angular:',self.direction,difference_angular)
Action = self.action_class
act = Action(False, True, difference_angular, self.speed)
return act
def next_angular_guide(self):
target_x, target_y = self.env.target_coordinate()
#print('USV&终点', self.x, self.y, target_x, target_y)
# USV与终点在同一垂直线上:(USV最左侧值与终点最左侧值的差值是否在USV的2倍半径范围内)
if abs(round(self.y, 0) - round(target_y, 0)) <= self.radius * 2:
if self.x < target_x:
angle = 270
#print('斜率对应的角度angle-整数1:',angle)
return angle
else:
angle = 90
#print('斜率对应的角度angle-整数2:', angle)
return angle
# USV与终点在同一垂直线上:
if abs(round(self.x, 0) - round(target_x, 0)) <= self.radius * 2:
if self.y < target_y:
angle = 180
#print('斜率对应的角度angle-整数3:', angle)
return angle
else:
angle = 0
#print('斜率对应的角度angle-整数4:', angle)
return angle
# 斜率可计算(这里要适应左上角是(0,0)的状况,和传统左下角是(0,0)有差异)
slope = (self.x - target_x) / (target_y - self.y)
# 斜率转换为弧度
arc = atan(slope) / pi * 180
# 终点在起点的左区域
if target_y < self.y:
# 左上角区域
if target_x < self.x:
angle = - round(arc, 0)
#print('斜率对应的角度angle-小数1:', angle)
return angle
# 左下角区域
else:
angle = 360 - round(arc, 0)
#print('斜率对应的角度angle-小数2:', angle)
return angle
# 终点在起点的右区域
else:
angle = 180 - round(arc, 0)
#print('斜率对应的角度angle-小数3:', angle)
return angle
def next_angular_guide2(self):
target_x, target_y = self.env.target_coordinate()
#print('USV&终点',self.x,self.y,target_x, target_y)
#USV与终点在同一垂直线上:
if round(self.y, 0) - round(target_y, 0) == 0:
if self.x < target_x:
angle = 270
#print('斜率对应的角度angle-整数1:',angle)
return angle
else:
angle = 90
#print('斜率对应的角度angle-整数2:', angle)
return angle
# USV与终点在同一垂直线上:
if round(self.x, 0) - round(target_x, 0) == 0:
if self.y < target_y:
angle = 180
#print('斜率对应的角度angle-整数3:', angle)
return angle
else:
angle = 0
#print('斜率对应的角度angle-整数4:', angle)
return angle
# 假设斜率都存在(因为USV和终点都是浮点数,不可能完全相等,存在误差)
# 斜率可计算(这里要适应左上角是(0,0)的状况,和传统左下角是(0,0)有差异)
slope = (self.x - target_x) / (target_y - self.y)
# 斜率转换为弧度
arc = atan(slope) / pi * 180
#print('arc:', arc)
# 终点在起点的左区域
if target_y < self.y:
# 左上角区域
if target_x < self.x:
angle = - round(arc, 0)
#print('斜率对应的角度angle1-小数:', angle)
return angle
# 左下角区域
else:
angle = 360 - round(arc, 0)
#print('斜率对应的角度angle2-小数:', angle)
return angle
# 终点在起点的右区域
else:
angle = 180 - round(arc, 0)
#print('斜率对应的角度angle3-小数:', angle)
return angle
#含有输入参数startPoint[0]\ startPoint[1]表示:startPoint.x,startPoint.y(按照next_angular_guide2修改)
def next_angular_guide3(self, startPoint, endPoint):
#target_x, target_y = self.env.target_coordinate()
# print('USV&终点',self.x,self.y,target_x, target_y)
# USV与终点在同一垂直线上:
if round(startPoint[1], 0) - round(endPoint[1], 0) == 0:
if startPoint[0] < endPoint[0]:
angle = 270
# print('斜率对应的角度angle-整数1:',angle)
return angle
else:
angle = 90
# print('斜率对应的角度angle-整数2:', angle)
return angle
# USV与终点在同一垂直线上:
if round(startPoint[0], 0) - round(endPoint[0], 0) == 0:
if startPoint[1] < endPoint[1]:
angle = 180
# print('斜率对应的角度angle-整数3:', angle)
return angle
else:
angle = 0
# print('斜率对应的角度angle-整数4:', angle)
return angle
# 假设斜率都存在(因为USV和终点都是浮点数,不可能完全相等,存在误差)
# 斜率可计算(这里要适应左上角是(0,0)的状况,和传统左下角是(0,0)有差异)
slope = (startPoint[0] - endPoint[0]) / (endPoint[1] - startPoint[1])
# 斜率转换为弧度
arc = atan(slope) / pi * 180
# print('arc:', arc)
# 终点在起点的左区域
if endPoint[1] < startPoint[1]:
# 左上角区域
if endPoint[0] < startPoint[0]:
angle = - round(arc, 0)
# print('斜率对应的角度angle1-小数:', angle)
return angle
# 左下角区域
else:
angle = 360 - round(arc, 0)
# print('斜率对应的角度angle2-小数:', angle)
return angle
# 终点在起点的右区域
else:
angle = 180 - round(arc, 0)
# print('斜率对应的角度angle3-小数:', angle)
return angle
#起点与终点间添加一系列不与障碍物相交的中间点-方法2
def pathGuide2(self):
res = self.pathGuide_explore()
difference_angular = self.next_angular_guide3(res[0], res[1]) - self.direction
#print('当前direction,当前difference_angular:',self.direction,difference_angular)
Action = self.action_class
act = Action(False, True, difference_angular, self.speed)
return act
#迭代初始赋值
def pathGuide_explore(self):
target_x, target_y = self.env.target_coordinate()
toUseList = [(target_x, target_y),(self.x, self.y)]
pathList =[]
pathListRes = self.iter_explore(toUseList, pathList)
pathListRes.append((target_x, target_y))
return pathListRes
#迭代
def iter_explore(self, toUseList, pathList):
while len(toUseList)>=2:
#判断toUseList最后两点是否与障碍物相交
if ( self.pointToLine_Length(toUseList) ):
#不相交,pop和insert
pathList.append(toUseList.pop())
else:
#相交,找随机点
randx = round(random.uniform(0 + self.radius, self.env.width),4)
randy = round(random.uniform(0 + self.radius, self.env.height),4)
toUseList.insert(len(toUseList)-1, (randx, randy))
pathList = self.iter_explore(toUseList, pathList)
return pathList
#直线(a,b) point(x3);;a,b,x3是三点:构建Ax+By+c=0 但是没有考虑到障碍物与线段的垂线交点在线段延长线上
def pointToLine_Length2(self, toUseList):
a = toUseList[-1]
b = toUseList[-2]
# A = b.y - a.y
# B = a.x - b.x
# C = b.x * a.y - a.x * b.y
A = b[1] - a[1]
B = a[0] - b[0]
C = b[0] * a[1] - a[0] * b[1]
for obs in self.env.obs:
d = abs((A * obs.x + B * obs.y + C) / sqrt(A * A + B * B))
if ( d - obs.radius <= 0.05):
return False
#与障碍物相交
#与障碍物不相交
return True
#充分考虑了:障碍物与线段的距离(而不是障碍物与直线的距离,两者区别很大)
#https://www.cnblogs.com/lyggqm/p/4651979.html
def pointToLine_Length(self, toUseList):
A = toUseList[-1]
B = toUseList[-2]
AB = (B[0] - A[0], B[1] - A[1])
ABdic = sqrt(AB[0] * AB[0] + AB[1] * AB[1])
for obs in self.env.obs:
P = (obs.x, obs.y)
AP = (P[0] - A[0], P[1] - A[1])
dot = (AP[0] * AB[0] + AP[1] * AB[1]) / (ABdic * ABdic)
AC = (AB[0] * dot, AB[1] * dot)
C = (AC[0] + A[0], AC[1] + A[1])
if (dot > 1):
BP = (P[0] - B[0], P[1] - B[1])
leng = sqrt(BP[0] * BP[0] + BP[1] * BP[1])
elif dot < 0:
AP = (P[0] - A[0], P[1] - A[1])
leng = sqrt(AP[0] * AP[0] + AP[1] * AP[1])
else:
PC = (C[0] - P[0], C[1] - P[1])
leng = sqrt(PC[0] * PC[0] + PC[1] * PC[1])
if (leng - obs.radius <= 0.05):
return False
# 与障碍物相交
# 与障碍物不相交
return True
# 测试DDPG网络的有效性:调3*3地图上,固定angular_speed和固定speed值
# 对应continue_obsmap_test_smallmap
# CoutinuePyGame CoutinueNoPyGame MyContinueUSV_SmallMap
class MyContinueUSV_SmallMap(BasicPlaneUSV):
'''一个策略简单的USV,派生自BasicObsUSV,用于连续环境下USV,
认为USV可瞬间达到下一次角速度且按照speed走一帧时间的距离
'''
def __init__(self, uid, x, y, env):
super(MyContinueUSV_SmallMap, self).__init__(uid, x, y, env)
self.action_class = Action = namedtuple("action", ['stay', 'clockwise', 'angular_speed', 'speed'])
self.speed = 1
self.radius = 1
self.angular_speed_max = 6
self.speed_max = 0.3
def getuid(self):
return self.id
# 便于RL学习时获取相关信息
# a.计算当前船与目标点的距离
def getDistanceUSVTarget(self):
target_x, target_y = self.env.target_coordinate()
Dis = sqrt((self.x - target_x) * (self.x - target_x) + (self.y - target_y) * (self.y - target_y))
Dis = float("%.4f" % Dis)
return Dis
# b.当前船的位置
def getCurrentUSVPos(self):
return float("%.4f" % self.x), float("%.4f" % self.y)
# c.当前船的角加速度&速度
def getCurrentUSVAngularAndSpeed(self):
return self.angular_speed, self.speed
def set_usv_radius(self, radiusValue):
self.radius = radiusValue
def set_usv_speed(self, speedValue):
self.speed = speedValue
def update_coordinate(self): # x轴负方向是0度,y正方向是90度,(按照左上角是0,0更新坐标),所以如下计算
# self.y -= cos(pi * self.direction / 180) * self.speed
# self.x -= sin(pi * self.direction / 180) * self.speed
self.y += cos(pi * self.direction / 180) * self.speed
self.x -= sin(pi * self.direction / 180) * self.speed
self.y = float("%.4f" % self.y)
self.x = float("%.4f" % self.x)
def decision_algorithm(self):
'''这种USV的action对象有四个属性:
1.stay,如果设为True,代表USV决定不行动,后面的参数被忽略;
2.clockwise,转动方向是否是顺时针;
3.angular_speed角速度;
4.speed速度.
如果stay参数为False,USV将会根据clockwise的指示转动angular_speed*t(一帧时间)度,然后前进当前的速度*t的距离'''
# Action = self.action_class
# act = Action(False, False, 2.0, 1.0)
# 归一化后的
# decision_angular_speed, decision_speed = 2.9 / 5, - 0.1 / 0.3
decision_angular_speed, decision_speed = 5.8/6, 0.22 / 0.3
# 14步数:5.8/6, 0.22 / 0.3 #注意这里,要修改最大值限定:self.angular_speed_max = 6
# 17步数:4.85 / 5, 0.18 / 0.3
# 29步数:2.9 / 5, 0.1 / 0.3
angular_speed_value = decision_angular_speed * self.angular_speed_max
speed_value = decision_speed * self.speed_max
Action = self.action_class
# act = Action(False, False, angular_speed_value, speed_value)
act = Action(False, True, angular_speed_value, speed_value)
# act = self.pathGuide()
# act = self.pathGuide2()
return act
#第三次尝试:加入非线性阻尼部分,USV动力学文档上的参数
#(该部分目前的状况是:可少数点导引到终点,但是整个过程空间太复杂,RL难学习)
class MyContinueDynamicsUSV3(BasicPlaneUSV):
'''一个策略简单的USV,派生自BasicObsUSV,用于连续环境下USV,
认为USV可瞬间达到下一次角速度且按照speed走一帧时间的距离
'''
def __init__(self, uid, x, y, env, envDisturb, FTListValue):
super(MyContinueDynamicsUSV3, self).__init__(uid, x, y, env)
self.action_class = Action = namedtuple("action", ['F', 'T'])
self.radius = 1
self.x, self.y = x, y
self.heading = 0.0
self.u = 0.0#-10.0
self.v = 0.0
self.r = 0.0
#相关参数:m = 3980; X_u = -50; X_uu = -135; Y_v = -200; Y_vv = -2000; N_r = -3224; I = 19703; N_rrr = -3224
# 地球惯性系中x,y方向上的加速度(正负含方向值),根据动力学更新x和y可知:其适应左上角(0,0)的地图
# 所以:x向下是正方向,y向右是正方向
self.ax = 0.0
self.ay = 0.0
self.xyhList = [(self.x, self.y, self.heading)]
self.uvrList = [(self.u, self.v, self.r)]
self.expectStepLen = 2
self.F_max = 13100
self.T_max = 258000
self.FTList = []
self.FTLen = 0
self.envDisturb = envDisturb # 默认True,包含环境(风浪涌流干扰)
# 前段传入的参数,用于可视化观察
self.FTListValue = FTListValue
# 规划路径(该随机导引路径只产生一次),否则会造成过程中路径不断变化,影响角度的无效变化
# (路径的随机,造成角度一会大,一会小,造成多次迭代的失效)所以这里对随机导引路径只产生一次
self.pathguideList = []
# self.tUpdateCount记录按照更新位置的次数, self.RecordList记录:self.tUpdateCount%20==0 时的决策
self.tUpdateCount = 0
self.RecordList = []
#记录USV初始位置
self.startx = 0.0
self.starty = 0.0
def set_init_usv_pos(self, posx, posy):
self.startx = posx
self.starty = posy
def get_init_usv_pos(self):
return self.startx, self.starty
def set_init_xyh(self,x, y, heading):
self.x = x
self.y = y
self.heading = heading
def set_init_uvr(self,u, v, r):
self.u = u
self.v = v
self.r = r
def getuid(self):
return self.id
#查看目前进行了多少次决策
def getFTCurrentLen(self):
return self.FTLen
# 计算当前船与目标点的距离
def getDistanceUSVTarget(self):
target_x, target_y = self.env.target_coordinate()
Dis = sqrt((self.x - target_x) * (self.x - target_x) + (self.y - target_y) * (self.y - target_y))
Dis = float("%.4f" % Dis)
return Dis
def decision_algorithm(self):
'''这种USV的action对象有两个属性:
1.F:表示前进驱动力
2.T:表示转向'''
Action = self.action_class
#F= -13000;T= 2500
F, T = self.pathGuide33()
#F驱动力,T转向的范围限定
# if F < -6550: F = -6550
# if F > 13100: F = 13100
# if T < -2580: T = -2580
# if T > 2580: T = 2580
act = Action(F, T)#Action(30.0, 15.0)
return act
def update_xyduvr(self, F, T, t):
'''输入变量:驱动力F,转向T,更新时间t;;;根据动力学方程计算uvr的加速度'''
#这里可加一步判断F,T的范围**()
# step1:根据F,T计算uvr的加速度
if self.envDisturb == False: #无环境干扰(无风浪涌流)
au = self.v * self.r + (-50 / 3980) * self.u + (-135 / 3980) * self.u * abs(self.u) + (1/3980) * F
av = - self.u * self.r + (-200/3980) * self.v + (-2000/3980) * self.v * abs(self.v)
ar = (-3224/19703) * self.r + (-3224/19703) * self.r * self.r * self.r + (1/19703) * T
else: #环境干扰(风浪涌流)
timeexample = time.time()
disturbU = 0.08 * (sin(0.2 * timeexample)) + cos(0.2 * timeexample + pi / 4) + sin(0.2 * timeexample + pi / 6)
disturbV = disturbU
disturbR = 0.1 * (sin(0.2 * timeexample)) + cos(0.2 * timeexample + pi / 4) + sin(0.2 * timeexample + pi / 6)
if disturbU < -0.1:disturbU = -0.1
if disturbU > 0.1: disturbU = 0.1
if disturbV < -0.1:disturbV = -0.1
if disturbV > 0.1: disturbV = 0.1
if disturbR < -0.1:disturbR = -0.1
if disturbR > 0.1: disturbR = 0.1
au = self.v * self.r + (-50 / 3980) * self.u + (-135 / 3980) * self.u * abs(self.u) + (1/3980) * (F + disturbU)
av = - self.u * self.r + (-200/3980) * self.v + (-2000/3980) * self.v * abs(self.v) + (1/3980) * (disturbV)
ar = (-3224/19703) * self.r + (-3224/19703) * self.r * self.r * self.r + (1/19703) * (T + disturbR)
# step2:根据时间t,计算更新后的uvr
self.u += au * t
self.v += av * t
self.r += ar * t
self.uvrList.append((self.u, self.v, self.r))
# step3:uvr转换为x y heading #这里注意:self.heading*pi/180
ax = self.u * cos(self.heading*pi/180) - self.v * sin(self.heading*pi/180)
ay = self.u * sin(self.heading*pi/180) + self.v * cos(self.heading*pi/180)
self.ax = float("%.4f" % ax)
self.ay = float("%.4f" % ay)
aheading = self.r
tempx = self.y
tempy = self.env.width - 1 - self.x
tempx = tempx + float("%.4f" % (ax * t))
tempy = tempy + float("%.4f" % (ay * t))
self.x = self.env.width - 1 - tempy
self.y = tempx
# step4:根据时间t,计算更新后的x y heading
# self.x += float(ax * t)
# self.y += float(ay * t)
self.heading += aheading * t
self.heading = self.heading % 360 #380%360=20 (-50)%360=310
self.xyhList.append((self.x, self.y, self.heading))
#print('ax,ay,aheading:', list(map(float,[ax,ay,aheading])))
def move(self):
# 每1/20 * 5次 帧决策一次
if len(self.FTListValue)==0:
if self.tUpdateCount == 0 or self.tUpdateCount % 5 == 0:
action = self.decision_algorithm()
F, T = action.F, action.T
self.RecordList.append((F, T))
else:
F = self.RecordList[-1][0]
T = self.RecordList[-1][1]
# 每1/20帧决策一次
# if len(self.FTListValue) == 0:
# action = self.decision_algorithm()
# F, T = action.F, action.T
else:
F, T = self.FTListValue[self.FTLen][0], self.FTListValue[self.FTLen][1]
if (self.tUpdateCount + 1) % 5 == 0:
self.FTLen = self.FTLen + 1
F *= self.F_max
T *= self.T_max
target_x,target_y = self.env.target_coordinate()
#当前位置距离终点的距离
dis = sqrt( (self.x - target_x)*(self.x - target_x) + (self.y - target_y)*(self.y - target_y))
if DEBUGPrint == True:
print('update_before:',int(self.x), int(self.y), int(self.heading))
#如果当前位置距离终点在10范围内,大步伐更新;否则小步伐更新
if dis > 20:
self.update_xyduvr(F, T, 1/20)
self.tUpdateCount = self.tUpdateCount + 1
else:
self.update_xyduvr(F, T, 1/20)
self.tUpdateCount = self.tUpdateCount + 1
if DEBUGPrint == True:
print('update_after:', int(self.x), int(self.y), int(self.heading))
#修改引导算法::
def pathGuide33(self):
if len(self.pathguideList) == 0:
self.pathguideList = self.pathGuide_explore()
del self.pathguideList[0]
if DEBUGPrint == True:
print('计算出的路径', self.pathguideList)
if len(self.pathguideList) > 1:
disCharge = sqrt ((self.x - self.pathguideList[0][0])*(self.x - self.pathguideList[0][0]) + (self.y - self.pathguideList[0][1])*(self.y - self.pathguideList[0][1]))
if disCharge <= 10:
del self.pathguideList[0]
# 下一时刻期望的位置(x_res, y_res, heading_res)
heading_res = self.next_angular_guide4((self.x, self.y), self.pathguideList[0])
x_res = self.pathguideList[0][0]
y_res = self.pathguideList[0][1]
if DEBUGPrint == True:
print('路径导引下一坐标',float('%.4f' %x_res),float('%.4f'%y_res))
print('期望角度', heading_res)
print('当前的角度',self.heading)
#位置限定不出界
if x_res < self.radius:
x_res = self.radius
if x_res > self.env.width -1 - self.radius:
x_res = self.env.width -1 -self.radius
if y_res < self.radius:
y_res = self.radius
if y_res > self.env.height -1 -self.radius:
y_res = self.env.height -1 -self.radius
#第一种测试F,T方法
F = 13100
T = -2580*1000
# delta_heading = self.heading - heading_res
# if delta_heading < -180:
# delta_heading += 360
# if delta_heading >180:
# delta_heading -= 360
# T = delta_heading * T / 180
# 第三种测试F,T方法 u_res=x_dot*cos(heading) + y_dot*sin(heading)
# 根据期望位置求解,先将左上角00的期望位置转化为左下角00的位置
# x_trans_res = y_res
# y_trans_res = self.env.width - 1 - x_res
# u_res = (x_trans_res - self.x)/(1/20) * cos(heading_res * pi /180) + (y_trans_res - self.y)/(1/20) * sin(heading_res * pi /180)
# u_res = float("%.4f" % u_res)
# # 第二种测试F,T方法
# #求期望位置与当前位置的斜率
# # 斜率可计算(这里要适应左上角是(0,0)的状况 和 传统左下角是(0,0)有差异)
# #slope = (startPoint[0] - endPoint[0]) / (endPoint[1] - startPoint[1])
# if (y_res - self.y) != 0 :
# u_res = (self.x - x_res) / (y_res - self.y)
# else: