再帰的なパス検索アルゴリズムを実装しました。この再帰アルゴリズムは、互いに接続されたノードの事前設定セットに基づいて機能します。各ノードには、上、ボタン、左、右の方向を含む 4 つのポインターがあります。再帰アルゴリズムは、単純に各ノードをウォークスルーし、最終目的地に到達するためにこれら 4 つの方向を 1 つずつ探します。例として、A、B、C、D、E、F、G、H の 7 つのノードを考えてみましょう。
A (Button->D, Right->B)
B (Right->C, Left->B)
C (Left->B)
D (Button->G, Right->E, Top->A)
E (Right->F, Left->D)
F (Left->E)
G (Right->H, Top->D)
H (Left->G)
これらのノードを全体的に見ると、次の図が表示されます。
A—B—C
|
D—E—F
|
G—H
この例では、ウォーカーが開始したノードがノード A であり、最終目的地としてノード H に行きたいとします。ノード A は、自身の右、ボタン、左、および上を順番に調べます。その権利は、ノード B に行くことを選択した結果、ノード B を指していました。同じパターンのノード B は、右側のノード C に移動することを選択します。右、上、ボタンがブロックされているため、ノード C はノード B に戻ります。同様に、ノード B はノード A に戻ります。ウォーカーは再び開始点に戻ります。次に、ノード A は、順序に基づいてボタン ノードに移動します。これは、ノード D に移動することを意味します。ノード D は、右側のノード E に移動し、次にノード F に移動します。ノード E とノード D に戻ります。その後、ノード D は、ウォーカーの順序に従って、そのボタンであるノード G に移動することを選択します。そこからノード G がノード H に移動します。最後に、ウォーカーは最終目的地に到達します。
Pseudocode: Recursive Path Finding Algorithm
ArrayList findPath(GameObject currentPoint , GameObject targetPoint , ArrayList InputArrayList)
{
1-Duplicate InputArrayList as tempArrayList
2-If the currentPoint equals to target Point return inputArrayList
//*** End Condition found target
3-If the Right side of the currentPoint is empty goto step 4
3.1- Add currentPoint to tempArrayList
//*** Call Right
3.2- tempArrayList = findPath(currentpoint.Right, targetPoint, tempArrayList);
3.3- If tempArrayList is not null return tempArrayList
4-If the Button side of the currentPoint is empty goto step 5
4.1- Add currentPoint to tempArrayList
//*** Call Button
4.2- tempArrayList = findPath(currentpoint.Button, targetPoint, tempArrayList);
4.3- If tempArrayList is not null return tempArrayList
5-If the Left side of the currentPoint is empty goto step 6
5.1- Add currentPoint to tempArrayList
//*** Call Left
5.2- tempArrayList = findPath(currentpoint.Left, targetPoint, tempArrayList);
5.3- If tempArrayList is not null return tempArrayList
6-If the Top side of the currentPoint is empty goto step 7
6.1- Add currentPoint to tempArrayList
//*** Call Top
6.2- tempArrayList = findPath(currentpoint.Top, targetPoint, tempArrayList);
6.3- If tempArrayList is not null return tempArrayList
7-Return null;
//*** End Condition does not found target
}
注: 実際のコードは C# です。このリンクからダウンロードできます。
ケーススタディでの問題の発生: このコードを理解すると; それを説明するために、それには弱点があります。以下のノードの全体像を考慮して、開始ノードがノード A であり、最終目的地がノード H であると仮定します。
A—B—C
|
D—E—F—I
| | |
G—H—J—K
最適なパスの解は (A, D, G, H) ですが、説明した再帰的なパス検索アルゴリズムは、その解として (A, D, E, F, I, K, J, H) を見つけます。これは本当にロボットが愚かなロボットのようです :D !
図 1: 再帰的なパス検索アルゴリズム
図 2: 学習機能を備えた再帰的経路探索アルゴリズム
ノードに学習機能を追加することで問題を解決しました。このリンクから問題の詳細を確認できます。しかし、誰かが再帰アルゴリズムを修正して最短経路を見つけることができるかどうか疑問に思います。
ありがとうございました、