PathAStar.cs
12 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
using XingYe_ACS.BaseStruct;
using XingYe_ACS.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace XingYe_ACS.Business
{
public class PathAStar
{
List<Point> OpenList = new List<Point>();
List<Point> CloseList = new List<Point>();
List<Point> SurPointsList = new List<Point>();
public const int CONER = 10;
public const int LINE = 4;
public const int TOEND = 4;
/// <summary>
/// 寻找路径
/// </summary>
/// <param name="listApoint">可用的点</param>
/// <param name="beginPoint">起点</param>
/// <param name="endPoint">终点</param>
/// <param name="agv"></param>
/// <returns></returns>
public Point GetPathPoint(List<Point> listApoint, Point beginPoint, Point endPoint, Agv agv,int flag)
{
if (beginPoint == null || endPoint == null)
throw new Exception(" 查找路径失败,起点终点不能为空");
OpenList.Add(beginPoint);
if (flag == 1) { CloseList.Clear(); }
int x = 0;
while (OpenList.Count != 0)
{
x++;
//按照F值从小到大排序
//将此点从Open表剔除,并加入Close表中
OpenList.Sort();
Point currentPoint = OpenList[0];
OpenList.RemoveAt(0);
CloseList.Add(currentPoint);
//找出它相邻的点
List<Point> aroundPoints = GetAroundPoint(listApoint, currentPoint);
if (aroundPoints.Count == 0 && OpenList.Count == 0) return currentPoint;
foreach (Point judgePoint in aroundPoints)
{
if (OpenList.Contains(judgePoint))
{
var G = currentPoint.Value_G + ConditionG(judgePoint, currentPoint);
if (G < judgePoint.Value_G)
{
judgePoint.ParentPoint = currentPoint;
judgePoint.Value_G = G;
judgePoint.Value_H = CalcH(judgePoint, endPoint);
judgePoint.Value_F = judgePoint.Value_G + judgePoint.Value_H;
}
}
else
{
//如果它们不在开始列表里, 就加入, 并设置父节点,并计算GHF
judgePoint.ParentPoint = currentPoint;
judgePoint.Value_G = currentPoint.Value_G + ConditionG(judgePoint, currentPoint);
judgePoint.Value_H = CalcH(judgePoint, endPoint);
judgePoint.Value_F = judgePoint.Value_G + judgePoint.Value_H;
OpenList.Add(judgePoint);
if (judgePoint.strBarcode == endPoint.strBarcode)
return judgePoint;
}
}
}
return OpenList.Last();
}
/// <summary>
/// 寻找四周点
/// </summary>
/// <param name="listApoint">可用点集合</param>
/// <param name="currentPoint"></param>
/// <returns></returns>
public List<Point> GetAroundPoint(List<Point> listApoint, Point currentPoint)
{
List<Point> listPoint = new List<Point>();
//找到当前点的X+方向点,判断是否在可用点里面,再判断X+的点是否可走
Point xPos = currentPoint.xPosPoint;
if (listApoint.Contains(xPos))
if (CanReach(xPos, currentPoint, EnumMsg.OriType.X正方向))
listPoint.Add(xPos);
Point xNeg = currentPoint.xNegPoint;
if (listApoint.Contains(xNeg))
if (CanReach(xNeg, currentPoint, EnumMsg.OriType.X负方向))
listPoint.Add(xNeg);
Point yPos = currentPoint.yPosPoint;
if (listApoint.Contains(yPos))
if (CanReach(yPos, currentPoint, EnumMsg.OriType.Y正方向))
listPoint.Add(yPos);
Point yNeg = currentPoint.yNegPoint;
if (listApoint.Contains(yNeg))
if (CanReach(yNeg, currentPoint, EnumMsg.OriType.Y负方向))
listPoint.Add(yNeg);
return listPoint;
}
/// <summary>
/// 能否选择此点
/// </summary>
/// <param name="judgePoint">待判定的点</param>
/// <param name="currentPoint"></param>
/// <param name="flag">待判定点与当前点方向标识</param>
/// <returns></returns>
public bool CanReach(Point judgePoint, Point currentPoint, EnumMsg.OriType pathDirection)
{
if (judgePoint == null)
return false;
//关掉的点,排除
if (CloseList.Exists(a => a.strBarcode == judgePoint.strBarcode))
return false;
switch (pathDirection)
{
case EnumMsg.OriType.X正方向:
//对于X+的四周点,若临时方向存在X-,则不能选择此点
if (judgePoint.tmpDirectionList.Exists(a => a.pathDirection == EnumMsg.OriType.X负方向))
return false;
//判断当前点的固定方向是否允许X+方向行走
if (!currentPoint.isXPos)
return false;
break;
case EnumMsg.OriType.X负方向:
if (judgePoint.tmpDirectionList.Exists(a => a.pathDirection == EnumMsg.OriType.X正方向))
return false;
if (!currentPoint.isXNeg)
return false;
break;
case EnumMsg.OriType.Y正方向:
if (judgePoint.tmpDirectionList.Exists(a => a.pathDirection == EnumMsg.OriType.Y负方向))
return false;
if (!currentPoint.isYPos)
return false;
break;
case EnumMsg.OriType.Y负方向:
if (judgePoint.tmpDirectionList.Exists(a => a.pathDirection == EnumMsg.OriType.Y正方向))
return false;
if (!currentPoint.isYNeg)
return false;
break;
default:
throw new Exception("创建路径失败:找不到此点的方向。");
}
return true;
}
/// <summary>
/// 获取方向
/// </summary>
/// <param name="judgePoint">要判断的点</param>
/// <param name="currentPoint">当前点</param>
/// <returns></returns>
public EnumMsg.OriType GetDircition(Point judgePoint, Point currentPoint)
{
if (judgePoint.intX == currentPoint.intX)
{
if (currentPoint.yPosPoint != null && judgePoint.intY == currentPoint.yPosPoint.intY)
return EnumMsg.OriType.Y正方向;
else if (currentPoint.yNegPoint != null && judgePoint.intY == currentPoint.yNegPoint.intY)
return EnumMsg.OriType.Y负方向;
}
if (judgePoint.intY == currentPoint.intY)
{
if (currentPoint.xPosPoint != null && judgePoint.intX == currentPoint.xPosPoint.intX)
return EnumMsg.OriType.X正方向;
else if (currentPoint.xNegPoint != null && judgePoint.intX == currentPoint.xNegPoint.intX)
return EnumMsg.OriType.X负方向;
}
throw new Exception("无法找到此两点的路径方向:" + judgePoint.strBarcode + "-" + currentPoint.strBarcode);
}
/// <summary>
/// G值权重的计算
/// </summary>
/// <param name="judgePoint"></param>
/// <param name="currentPoint"></param>
/// <returns></returns>
public double ConditionG(Point judgePoint, Point currentPoint)
{
double value = LINE;
Point parentPoint = null;
if (currentPoint.ParentPoint != null)
{
parentPoint = currentPoint.ParentPoint;
//如果是关键路段,则权重加大
if (judgePoint.strRegionName.Contains("Mutex"))
value = 4;
//如果三点在一个直线上,则等于4,否则等于8
if ((parentPoint.intX == currentPoint.intX && judgePoint.intX == parentPoint.intX)
|| (parentPoint.intY == currentPoint.intY && judgePoint.intY == parentPoint.intY))
value = LINE;
else value = CONER;
}
//返回值加上将要行走的小车数量
return value + judgePoint.tmpDirectionList.Count;
}
/// <summary>
/// H值权重的计算
/// </summary>
/// <param name="judgePoint"></param>
/// <param name="endPoint"></param>
/// <returns></returns>
public double CalcH(Point judgePoint, Point endPoint)
{
//查找距离目标点的距离(X+Y)*4
int dtX = Math.Abs(judgePoint.intX - endPoint.intX);
int dtY = Math.Abs(judgePoint.intY - endPoint.intY);
return (dtX + dtY) * TOEND;
//return (dtX + dtY) * 0;
}
/// <summary>
/// 寻找路径
/// </summary>
/// <param name="listApoint">可用的点</param>
/// <param name="beginPoint">起点</param>
/// <param name="endPoint">终点</param>
/// <param name="agv"></param>
/// <returns></returns>
public Point DemoGetPathPoint(List<Point> listApoint, Point beginPoint, Point endPoint, Agv agv)
{
if (beginPoint == null || endPoint == null)
throw new Exception(" 查找路径失败,起点终点不能为空");
OpenList.Add(beginPoint);
int x = 0;
while (OpenList.Count != 0)
{
x++;
//按照F值从小到大排序
//将此点从Open表剔除,并加入Close表中
OpenList.Sort();
Point currentPoint = OpenList[0];
OpenList.RemoveAt(0);
CloseList.Add(currentPoint);
//找出它相邻的点
List<Point> aroundPoints = GetAroundPoint(listApoint, currentPoint);
if (aroundPoints.Count == 0 && OpenList.Count == 0) return currentPoint;
foreach (Point judgePoint in aroundPoints)
{
if (OpenList.Contains(judgePoint))
{
var G = currentPoint.Value_G + ConditionG(judgePoint, currentPoint);
if (G < judgePoint.Value_G)
{
judgePoint.ParentPoint = currentPoint;
judgePoint.Value_G = G;
judgePoint.Value_H = CalcH(judgePoint, endPoint);
judgePoint.Value_F = judgePoint.Value_G + judgePoint.Value_H;
}
}
else
{
//如果它们不在开始列表里, 就加入, 并设置父节点,并计算GHF
judgePoint.ParentPoint = currentPoint;
judgePoint.Value_G = currentPoint.Value_G + ConditionG(judgePoint, currentPoint);
judgePoint.Value_H = CalcH(judgePoint, endPoint);
judgePoint.Value_F = judgePoint.Value_G + judgePoint.Value_H;
OpenList.Add(judgePoint);
if (judgePoint.strBarcode == endPoint.strBarcode)
return judgePoint;
}
}
}
return OpenList.Last();
}
}
}