智能控制作业
- 格式:docx
- 大小:157.30 KB
- 文档页数:17
智能控制作业Pleasure Group Office【T985AB-B866SYT-B182C-BS682T-STT18】一、A*寻路算法Attribute VB_Name = "mFindPath"Option ExplicitDim arrBap() As bPoint '地图状态Dim arrOpen() As fsPoint '开放列表Dim lOpenSize As Long '开放列表元素个数Dim TCount As Long '目标序号'※※※※A*寻路函数※※※※※※※※※'输入参数'arrMap() as fsPoint 地图障碍物数据'arrPath() as long 路径列表'lPathCount as long 路径长度'pStartPoint as fsPoint 起始点'pGotoPoint as fsPoint 目标点'lWidth as long 地图宽度'lHeight as long 地图高度'输出参数'类型:long 寻路结果'3 地图格式不正确'2 目标为障碍点'1 寻路成功'0 寻路失败无法到达'4 已经到达终点'5 目标被占'6 寻路过程中发生错误'※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※Public Function fPath(arrMap() As fsPoint, arrPath() As Long, lPathCount As Long, pStartPoint As fsPoint, pGotoPoint As fsPoint, lWidth As Long, lHeight As Long) As Long '寻路Dim i As Long '循环用变量Dim Count As Long '点的序号或者总数Dim pCurrentPoint As fsPointDim pSearchPoint As fsPointDim pSearchX As Long, pSearchY As LongDim tmpX As Long, tmpY As LongDim arrXofs(8) As Long, arrYofs(8) As Long 'XY变化常量数组Dim arrIofs(8) As Long '序号变化常量数组Dim tmpOfs As Long '序号变化量On Error GoTo PathErr:'判断地图If lWidth < 1 Or lHeight < 1 ThenfPath = 3 '地图数据不正确Exit FunctionEnd If'TCount = * lWidth + '目标序号TCount ='判断是否为障碍物If > lHeight - 1 Or > lWidth - 1 ThenfPath = 2Exit FunctionEnd IfIf arrMap(TCount).B Or arrMap(TCount).D Then'可以通过或者是门点fPath = 0Else '否则退出寻路fPath = 2Exit FunctionEnd IfIf arrMap(TCount).lNpcId <> 0 ThenfPath = 5Exit FunctionEnd IfIf = And = ThenfPath = 4Exit FunctionEnd If'设置常数数组该数组决定了寻路的优先顺序 arrXofs(0) = -1arrXofs(1) = 1arrXofs(2) = -1arrXofs(3) = 1arrXofs(4) = -1arrXofs(5) = 1arrXofs(6) = 0arrXofs(7) = 0arrYofs(0) = -1arrYofs(1) = 1arrYofs(2) = 1arrYofs(3) = -1arrYofs(4) = 0arrYofs(5) = 0arrYofs(6) = -1arrYofs(7) = 1tmpOfs = 0 - lWidtharrIofs(0) = tmpOfs - 1arrIofs(1) = lWidth + 1arrIofs(2) = lWidth - 1arrIofs(3) = tmpOfs + 1arrIofs(4) = -1arrIofs(5) = 1arrIofs(6) = tmpOfsarrIofs(7) = lWidth'初始化数据Count = lWidth * lHeight '地图总点数ReDim arrBap(Count)'清空所有需要用到的列表ReDim arrOpen(Count)'起始点进入开放列表Count = * lWidth += CounttmpX = Abs -tmpY = Abs -= 0If tmpX > tmpY Then= tmpXElse= tmpYEnd If= +arrBap(Count).Parent = 0arrBap(Count).aIndex = 1arrOpen(1) = pStartPointlOpenSize = 1Do While lOpenSize > 0pCurrentPoint = pBinaryHeapDelete()Count =arrBap(Count).bClose = TruearrBap(Count).aIndex = 0For i = 0 To 7= + arrXofs(i)= + arrYofs(i)Count = + arrIofs(i)= CountIf Count = TCount Then '就是终点arrBap(Count).Parent ='保存路径lPathCount = -1Do While arrBap(Count).Parent <> 0 lPathCount = lPathCount + 1arrPath(lPathCount) = CountCount = arrBap(Count).ParentLoop'起始点放入最后arrPath(lPathCount + 1) = CountfPath = 1Exit FunctionElseIf arrMap(Count).B And arrMap(Count).lNpcId = 0 Then '不是障碍点 If Not arrBap(Count).bClose Then '不在关闭列表= + 1tmpX = Abs -tmpY = Abs -= 0If tmpX > tmpY Then= tmpXElse= tmpYEnd If= +If arrBap(Count).aIndex <> 0 ThenIf < arrOpen(arrBap(Count).aIndex).F Then'更新开放列表arrBap(Count).Parent =pBinaryHeapUpdate arrBap(Count).aIndexEnd IfElse '加入到开放列表pBinaryHeapAdd pSearchPointarrBap(Count).Parent =End IfEnd IfEnd IfEnd IfNextLoopExit FunctionPathErr:fPath = 6End Function'增加一个值到末尾并重新排序Private Sub pBinaryHeapAdd(pAddPoint As fsPoint)Dim lParentIndex As LongDim lSelectIndex As LongDim tmpPoint As fsPointlOpenSize = lOpenSize + 1arrOpen(lOpenSize) = pAddPointarrBap(arrOpen(lOpenSize).Index).aIndex = lOpenSizelSelectIndex = lOpenSizeDo While TruelParentIndex = Int(lSelectIndex / 2)If lParentIndex > 0 ThenIf arrOpen(lSelectIndex).F <= arrOpen(lParentIndex).F Then '比父节点F值小交换tmpPoint = arrOpen(lSelectIndex)arrOpen(lSelectIndex) = arrOpen(lParentIndex)arrOpen(lParentIndex) = tmpPointarrBap(arrOpen(lSelectIndex).Index).aIndex = lParentIndex arrBap(arrOpen(lParentIndex).Index).aIndex = lSelectIndex lSelectIndex = lParentIndexElse '比父节点F值大不处理Exit DoEnd IfElse '无父节点走人Exit DoEnd IfLoopEnd Sub'取出第一个值并重新排序Private Function pBinaryHeapDelete() As fsPointDim tmpResult As fsPointDim tmpPoint As fsPointDim lSelectIndex As LongDim lLeftChildIndex As LongDim lRightChildIndex As LongtmpResult = arrOpen(1)lOpenSize = lOpenSize - 1'把最后一个点移动到最前面If lOpenSize > 0 ThenarrOpen(1) = arrOpen(lOpenSize + 1)lSelectIndex = 1arrBap(arrOpen(lSelectIndex).Index).aIndex = lSelectIndex'比较子节点Do While TruelLeftChildIndex = lSelectIndex * 2lRightChildIndex = lLeftChildIndex + 1If lLeftChildIndex > lOpenSize Then'没有左子节点则结束Exit DoElseIf lLeftChildIndex = lOpenSize Then '只有左子节点If arrOpen(lSelectIndex).F > arrOpen(lLeftChildIndex).F Then '父节点F值大则交换tmpPoint = arrOpen(lSelectIndex)arrOpen(lSelectIndex) = arrOpen(lLeftChildIndex)arrOpen(lLeftChildIndex) = tmpPointarrBap(arrOpen(lSelectIndex).Index).aIndex = lLeftChildIndex arrBap(arrOpen(lLeftChildIndex).Index).aIndex = lSelectIndex lSelectIndex = lLeftChildIndexElse '父节点比子节点F值小则结束Exit DoEnd IfElseIf lRightChildIndex <= lOpenSize ThenIf arrOpen(lLeftChildIndex).F <= arrOpen(lRightChildIndex).F Then '左边F值小If arrOpen(lSelectIndex).F > arrOpen(lLeftChildIndex).F Then '父节点F值大则交换tmpPoint = arrOpen(lSelectIndex)arrOpen(lSelectIndex) = arrOpen(lLeftChildIndex)arrOpen(lLeftChildIndex) = tmpPointarrBap(arrOpen(lSelectIndex).Index).aIndex = lLeftChildIndexarrBap(arrOpen(lLeftChildIndex).Index).aIndex = lSelectIndexlSelectIndex = lLeftChildIndexElse '父节点比子节点F值小则结束Exit DoEnd IfElseIf arrOpen(lSelectIndex).F > arrOpen(lRightChildIndex).F Then '父节点F值大则交换tmpPoint = arrOpen(lSelectIndex)arrOpen(lSelectIndex) = arrOpen(lRightChildIndex)arrOpen(lRightChildIndex) = tmpPointarrBap(arrOpen(lSelectIndex).Index).aIndex = lRightChildIndexarrBap(arrOpen(lRightChildIndex).Index).aIndex = lSelectIndexlSelectIndex = lRightChildIndexElse '父节点比子节点F值小则结束Exit DoEnd IfEnd IfEnd IfEnd IfEnd IfLoopEnd IfpBinaryHeapDelete = tmpResultEnd Function'更新一个点并重新排序Private Sub pBinaryHeapUpdate(Index As Long)Dim lParentIndex As LongDim lSelectIndex As LongDim tmpPoint As fsPointlSelectIndex = IndexDo While TruelParentIndex = Int(lSelectIndex / 2)If lParentIndex > 0 ThenIf arrOpen(lSelectIndex).F < arrOpen(lParentIndex).F Then'比父节点F值小交换tmpPoint = arrOpen(lSelectIndex)arrOpen(lSelectIndex) = arrOpen(lParentIndex)arrOpen(lParentIndex) = tmpPointarrBap(arrOpen(lSelectIndex).Index).aIndex = lParentIndexarrBap(arrOpen(lParentIndex).Index).aIndex = lSelectIndexlSelectIndex = lParentIndexElse '比父节点F值大不处理Exit DoEnd IfElse '无父节点走人Exit DoEnd IfLoopEnd Sub'加载地图数据Public Sub pAddMapData(lMapDataID As Long, strMsg As String)Dim bytChar() As ByteDim MapFile As StringDim iLen As LongDim i As Long, j As LongDim m As Long, iCount As LongDim iMapWidth As LongDim iMapHeight As LongMapFile = & "\Data\cMap\" & strMsg & ".dat"Open MapFile For Binary As #2iLen = LOF(2)If iLen > 0 ThenReDim bytChar(iLen - 1)Get #2, 1, bytChar()Close #2iMapHeight = Val(GetValue("Index_" & strMsg, "Height", fMapSet))iMapWidth = Val(GetValue("Index_" & strMsg, "Width", fMapSet))nMap(lMapDataID).lWidth = iMapWidthnMap(lMapDataID).lHeight = iMapHeightnMap(lMapDataID).lDataSize = iMapWidth * iMapHeightIf nMap(lMapDataID).lDataSize <> iLen Then Exit Sub '地图数据不正确ReDim nMap(lMapDataID).cMapData(nMap(lMapDataID).lDataSize - 1) iCount = -1For i = 0 To iMapHeight - 1For j = 0 To iMapWidth - 1iCount = iCount + 1nMap(lMapDataID).cMapData(iCount).X = jnMap(lMapDataID).cMapData(iCount).Y = iIf bytChar(iCount) = 0 ThennMap(lMapDataID).cMapData(iCount).B = FalseElsenMap(lMapDataID).cMapData(iCount).B = TrueEnd IfNextNextnMap(lMapDataID).bData = True'读取固定门点Dim arrData() As StringDim brrData() As StringstrMsg = GetValue("bDoor", CStr(lMapDataID), fMapSet)arrData = Split(strMsg, "_")For m = 0 To UBound(arrData)brrData = Split(arrData(m), ",")If UBound(brrData) > 0 Theni = CLng(brrData(1))j = CLng(brrData(0))iCount = iMapWidth * i + jnMap(lMapDataID).cMapData(iCount).B = FalsenMap(lMapDataID).cMapData(iCount).D = TrueEnd IfNext'读取死点strMsg = GetValue("bFault", CStr(lMapDataID), fMapSet)arrData = Split(strMsg, "_")For m = 0 To UBound(arrData)brrData = Split(arrData(m), ",")If UBound(brrData) > 0 Theni = CLng(brrData(1))j = CLng(brrData(0))iCount = iMapWidth * i + jnMap(lMapDataID).cMapData(iCount).B = FalseEnd IfNext'读取活点strMsg = GetValue("bThrough", CStr(lMapDataID), fMapSet) arrData = Split(strMsg, "_")For m = 0 To UBound(arrData)brrData = Split(arrData(m), ",")If UBound(brrData) > 0 Theni = CLng(brrData(1))j = CLng(brrData(0))iCount = iMapWidth * i + jnMap(lMapDataID).cMapData(iCount).B = TrueEnd IfNextElseClose #2Exit SubEnd IfEnd Sub一种改进的移动Agent通信算法摘要:通信机制是Agent技术研究的重要课题,该文在移动Agent主动通信算法的基础上,提出了,解决了可靠通信问题,通过设置时间变量和超速等待,在Agent高速移动的情况下,强迫Agent停止移动,避免消息长时间不能传递的问题,完成可靠的消息传递。