forked from DarrenYing/MultiAgentPathFinding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAStar_v2.js
176 lines (141 loc) · 5.46 KB
/
AStar_v2.js
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
class AStar_v2 {
constructor(env) {
this.env = env;
}
reconstructPath(parents, current) {
var path = [current];
while (parents[current] != undefined) {
current = parents[current];
path.push(current);
}
path.reverse();
return path;
}
calcTurn(parents, cur) {
// if (parents[cur] == undefined || parents[parents[cur]] == undefined) {
// return 0;
// }
//
// let curx = cur.location.x;
// let cury = cur.location.y;
// let ppx = parents[parents[cur]].location.x;
// let ppy = parents[parents[cur]].location.y;
// if (abs(curx - ppx) == 1 && abs(cury - ppy) == 1) {
// return 1;
// }
return 0;
}
checkIsSameDir(parents, prev, cur) {
if (parents[prev] == undefined) {
return false; //和原来保持一致
}
let curx = cur.location.x;
let cury = cur.location.y;
let ppx = parents[prev].location.x;
let ppy = parents[prev].location.y;
if (abs(curx - ppx) == 1 && abs(cury - ppy) == 1) {
return false;
}
return true;
}
search(agentName) {
var initialState = this.env.agent_dict[agentName]["start"];
var stepCost = 1;
var closeList = [];
var openList = [initialState];
var parents = {}; //记录路径
var gScore = {};
gScore[initialState] = 0;
var hScore = {};
hScore[initialState] = this.env.calcH(initialState, agentName);
var fScore = {};
fScore[initialState] = hScore[initialState];
// var visualDist = {};
// visualDist[initialState] = this.env.calcVisualDist(initialState, agentName);
var prev = 0; //记录上一步的选择
// 由于无路可走时,Agent可以选择一直停在原地,所以设定一个最大迭代轮数
var cnt = 0;
var maxCnt = 1000;
while (openList.length) {
cnt++;
if (cnt > maxCnt) {
break;
}
// var cur = 0;
// for (var i = 1; i < openList.length; i++) {
var cur = openList.length-1;
for (var i = openList.length-2; i >= 0; i--) {
if (fScore[openList[i]] < fScore[openList[cur]]) {
cur = i;
} else if (fScore[openList[i]] == fScore[openList[cur]]) { // f值相等时,优选选择g值大的,因为g大代表探索了更长的路径,但在直线卡墙时会多循环几轮
if (gScore[openList[i]] > gScore[openList[cur]]) {
cur = i;
}
else if (gScore[openList[i]] == gScore[openList[cur]]) { // 选择和上一步方向一致的
if (this.checkIsSameDir(parents, prev, openList[i])) {
cur = i;
}
}
// visualDist[openList[i]] > visualDist[openList[cur]]) { // f值和g值都相等时,选择视距大的,减少转弯的选择
// cur = i;
// }
}
}
var current = openList[cur];
// console.log("current:", current, fScore[current]);
// console.log(fScore);
if (this.env.isReachTarget(current, agentName)) {
console.log(cnt);
return this.reconstructPath(parents, current);
}
// this.removeFromArray(openList, current);
_.pull(openList, current);
closeList.push(current);
prev = current;
// console.log("open:", openList);
// console.log("close:", closeList);
var neighbors = this.env.getNeighbors(current);
// console.log(neighbors.length);
// console.log("neighbors:", neighbors);
for (var i = 0; i < neighbors.length; i++) {
var neighbor = neighbors[i];
if (this.isInArray(closeList, neighbor)) {
continue;
}
var tmpG = gScore[current] + this.env.calcG(current, neighbor);
if (!this.isInArray(openList, neighbor)) {
openList.push(neighbor);
} else if (tmpG >= gScore[neighbor]) {
continue;
}
//更新g、f和parent
parents[neighbor] = current;
gScore[neighbor] = tmpG;
hScore[neighbor] = this.env.calcH(neighbor, agentName);
let turnCost = this.calcTurn(parents, neighbor);
fScore[neighbor] = tmpG + hScore[neighbor] + turnCost;
// visualDist[neighbor] = this.env.calcVisualDist(neighbor, agentName);
}
// console.log(openList);
// console.log("fscore:", fScore);
// console.log('\n');
}
return false;
}
isInArray(arr, ele) {
for (let obj of arr) {
if (JSON.stringify(obj) == JSON.stringify(ele)) {
return true;
}
}
return false;
}
// removeFromArray(arr, elt) {
// // Could use indexOf here instead to be more efficient
// for (var i = arr.length - 1; i >= 0; i--) {
// if (arr[i].toString() == elt.toString()) {
// arr.splice(i, 1);
// }
// }
// }
}