สัปดาห์ที่ 8 Basic Pathfinding, A* and Waypoint อ. เทพฤทธิ์ สินธำรงรักษ์ เรียบเรียง
Basic Pathfinding Random Movement Obstacle Tracing Around Obstacle Wall Tracing A* Pathfinding Waypoint
If (positionX > destinationX) positionX--; Else if (positionX < destinationX) positionX++; If (positionY>destinationY) positionY--; Else if (positionY < destinationY) positionY++;
อ่านว่า “ เอ - สตาร์ ” เป็นวิธีพัฒนาขึ้นมาเพื่อแก้ปัญหา Local Optimum โดยจะเปลี่ยนสมการ Fitness Function เป็น f(n) = g(n)+h(n)
ปัญหาการค้นหาเส้นทางในกรุงเทพมหานคร
เริ่มต้นจากเขตราชบูรณไปยังเขตมีนบุรี
A* เป็น algorithm ที่ทาการค้นหาเส้นทางที่ดีที่สุดจาก จุดเริ่มต้นไปยังจุดสุดท้าย จุดแต่ละจุดหรือ Node ที่ต้องใช้ในการคิดซึ่งอาจจะเป็น cell, tile, point ก็แล้วแต่จะเรียก Cost: ระดับของ Node ที่คำนวณได้ในแต่ละเส้นทางที่ เหมาะสม มีอยู่ด้วยกันสามส่วน f: Cost รวมของผลที่ได้จากการคานวณด้วย g + h ซึ่ง สุดท้ายจะเอามาตัดสินใจว่าเส้นทางใหนที่ดีกว่ากันยิ่งน้อย ยิ่งดี g: Cost ที่ได้จาก Node เริ่มต้นไปถึงโหนดที่กำลัง พิจารณา h: Cost ที่ได้จากการประมาณของ Node ที่กำลังพิจารณา ไปถึง Node สุดท้าย Node อันใหนที่มีค่า cost ของ Node น้อยที่สุดจะถูกเอา ไปคำนวณต่อไป
Initial StateGoal State ตัวอย่างปัญหาเกมส์ 8- Puzzle Initial state: สถานะเริ่มต้นให้มีตาแหน่งที่วาง ผิด 3 ตำแหน่ง Successor function: ขึ้น ลง ซ้าย และขวา Goal state: ตัวเลขทั้ง 8 ตัวที่วางอยู่ถูกต้องตาม กาหนด Path cost: การเคลื่อนที่ของช่องแต่ละช่อง นับเป็น 1
Goal State (a) f = g + h f = = 3 (b) (c) f = = 5 f = = 3
(d) (f) f = 2+ 3 = 5 f = = 3 Goal State (c) (e) f = 2+ 1 = 3f = 2+ 3 = 5
(g)(i) f = 3+ 2 = 5 f = = 3 Goal State (e) (h) f = 3+ 2 = 5f = 3+ 0 = 3 f = 3+ 2 = 5 (j)
แบบที่ 1 แบบที่ 2
Wall (1,5)(7,5)
Wall f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 (1,5) f = 6 g = 1 h =5 (7,5) f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =
Wall f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 (1,5) f = 6 g = 1 h =5 f = 6 g = 2 h = 4 (7,5) f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h =
Wall f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 (1,5) f = 6 g = 1 h =5 f = 6 g = 2 h = 4 (7,5) f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h =
Wall f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 (1,5) f = 6 g = 1 h =5 f = 6 g = 2 h = 4 (7,5) f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h =
Wall f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 (1,5) f = 6 g = 1 h =5 f = 6 g = 2 h = 4 (7,5) f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h =
Wall f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 (1,5) f = 6 g = 1 h =5 f = 6 g = 2 h = 4 (7,5) f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h =
f = 9.2 g = 3.4 h =5.4 f = 7.9 g = 2.8 h =5.1 Wall f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 (1,5) f = 6 g = 1 h =5 f = 6 g = 2 h = 4 (7,5) f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h =
f = 9.2 g = 3.4 h =5.4 f = 7.9 g = 2.8 h =5.1 Wall f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 (1,5) f = 6 g = 1 h =5 f = 6 g = 2 h = 4 (7,5) f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = จำค่าไว้ ปัญหาอยู่ตรงนี้แหละ
f = 9.2 g = 3.4 h =5.4 f = 7.9 g = 2.8 h =5.1 Wall f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 (1,5) f = 6 g = 1 h =5 f = 6 g = 2 h = 4 (7,5) f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 f = 9.2 g = 3.8 h =5.4 f = 7.9 g = 3.4 h =
f = 9.2 g = 3.4 h =5.4 f = 7.9 g = 2.8 h =5.1 Wall f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 (1,5) f = 6 g = 1 h =5 f = 6 g = 2 h = 4 (7,5) f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 f = 9.2 g = 3.8 h =5.4 f = 7.9 g = 3.4 h =4.5 f = 10.6 g = 4.8 h =5.8 f = 9.4 g = 4.4 h =5 f = 9 g = 4.8 h =
f = 9.2 g = 3.4 h =5.4 f = 7.9 g = 2.8 h =5.1 Wall f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 (1,5) f = 6 g = 1 h =5 f = 6 g = 2 h = 4 (7,5) f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 f = 9.2 g = 3.8 h =5.4 f = 7.9 g = 3.4 h =4.5 f = 10.6 g = 4.8 h =5.8 f = 9.4 g = 4.4 h =5 f = 9 g = 4.8 h =
Wall f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 (1,5) f = 6 g = 1 h =5 f = 6 g = 2 h = 4 (7,5) f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 f = 9.2 g = 3.8 h =5.4 f = 7.9 g = 3.4 h =4.5 f = 9 g = 6.2 h = 2.8 f = 10.6 g = 4.8 h =5.8 f = 9.4 g = 4.4 h =5 f = 9 g = 4.8 h = 4.2 f = 9.4 g = 5.8 h = 3.6 f = 10.9 g = 5.2 h = 5.7 f = 10.8 g = 5.8 h = 5 f = 10.7 g = 6.2 h =
Wall f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 (1,5) f = 6 g = 1 h =5 f = 6 g = 2 h = 4 (7,5) f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 f = 9.2 g = 3.8 h =5.4 f = 7.9 g = 3.4 h =4.5 f = 9 g = 6.2 h = 2.8 f = 10.6 g = 4.8 h =5.8 f = 9.4 g = 4.4 h =5 f = 9 g = 4.8 h = 4.2 f = 9.4 g = 5.8 h = 3.6 f = 10.9 g = 5.2 h = 5.7 f = 10.8 g = 5.8 h = 5 f = 10.7 g = 6.2 h =
Wall f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 (1,5) f = 6 g = 1 h =5 f = 6 g = 2 h = 4 (7,5) f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 f = 9.2 g = 3.8 h =5.4 f = 7.9 g = 3.4 h =4.5 f = 9 g = 6.2 h = 2.8 f = 10.6 g = 4.8 h =5.8 f = 9.4 g = 4.4 h =5 f = 9 g = 4.8 h = 4.2 f = 9.4 g = 5.8 h = 3.6 f = 10.9 g = 5.2 h = 5.7 f = 10.8 g = 5.8 h = 5 f = 10.7 g = 6.2 h =
f = 9.2 g = 3.4 h =5.4 f = 7.9 g = 2.8 h =5.1 Wall f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 (1,5) f = 6 g = 1 h =5 f = 6 g = 2 h = 4 (7,5) f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 f = 9.4 g = 7.2 h = 2.2 f = 9 g = 7.6 h = 1.4 f = 9.2 g = 3.8 h =5.4 f = 7.9 g = 3.4 h =4.5 f = 9 g = 6.2 h = 2.8 f = 9.9 g = 7.2 h = 2.2 f = 10.6 g = 4.8 h =5.8 f = 9.4 g = 4.4 h =5 f = 9 g = 4.8 h = 4.2 f = 9.4 g = 5.8 h = 3.6 f = 10.8 g = 7.6 h = 3.2 f = 10.9 g = 5.2 h = 5.7 f = 10.8 g = 5.8 h = 5 f = 10.7 g = 6.2 h =
f = 9.2 g = 3.4 h =5.4 f = 7.9 g = 2.8 h =5.1 Wall f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 (1,5) f = 6 g = 1 h =5 f = 6 g = 2 h = 4 (7,5) f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 f = 9.4 g = 7.2 h = 2.2 f = 9 g = 7.6 h = 1.4 f = 9.2 g = 3.8 h =5.4 f = 7.9 g = 3.4 h =4.5 f = 9 g = 6.2 h = 2.8 f = 9.9 g = 7.2 h = 2.2 f = 10.6 g = 4.8 h =5.8 f = 9.4 g = 4.4 h =5 f = 9 g = 4.8 h = 4.2 f = 9.4 g = 5.8 h = 3.6 f = 10.8 g = 7.6 h = 3.2 f = 10.9 g = 5.2 h = 5.7 f = 10.8 g = 5.8 h = 5 f = 10.7 g = 6.2 h =
f = 9.2 g = 3.4 h =5.4 f = 7.9 g = 2.8 h =5.1 Wall f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 (1,5) f = 6 g = 1 h =5 f = 6 g = 2 h = 4 f = 11 g = 9 h =2 f = 9.6 g = 8.6 h = 1 (7,5) f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 f = 9.4 g = 7.2 h = 2.2 f = 9 g = 7.6 h = 1.4 f = 9.6 g = 8.6 h = 1 f = 9.2 g = 3.8 h =5.4 f = 7.9 g = 3.4 h =4.5 f = 9 g = 6.2 h = 2.8 f = 9.9 g = 7.2 h = 2.2 f = 11 g =9 h =2 f = 10.6 g = 4.8 h =5.8 f = 9.4 g = 4.4 h =5 f = 9 g = 4.8 h = 4.2 f = 9.4 g = 5.8 h = 3.6 f = 10.8 g = 7.6 h = 3.2 f = 10.9 g = 5.2 h = 5.7 f = 10.8 g = 5.8 h = 5 f = 10.7 g = 6.2 h =
f = 9.2 g = 3.4 h =5.4 f = 7.9 g = 2.8 h =5.1 Wall f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 (1,5) f = 6 g = 1 h =5 f = 6 g = 2 h = 4 f = 11 g = 9 h =2 f = 9.6 g = 8.6 h = 1 (7,5) f = 7.1 g = 1 h =6.1 f = 6.5 g = 1.4 h =5.1 f = 6.5 g = 2.4 h = 4.1 f = 9.4 g = 7.2 h = 2.2 f = 9 g = 7.6 h = 1.4 f = 9.6 g = 8.6 h = 1 f = 9.2 g = 3.8 h =5.4 f = 7.9 g = 3.4 h =4.5 f = 9 g = 6.2 h = 2.8 f = 9.9 g = 7.2 h = 2.2 f = 1 g =9 h =2 f = 10.6 g = 4.8 h =5.8 f = 9.4 g = 4.4 h =5 f = 9 g = 4.8 h = 4.2 f = 9.4 g = 5.8 h = 3.6 f = 10.8 g = 7.6 h = 3.2 f = 10.9 g = 5.2 h = 5.7 f = 10.8 g = 5.8 h = 5 f = 10.7 g = 6.2 h =
ABCDEFG A B C D E F G Pathfinding can be a very time-comsuming and CPU-intensive operation. One way to reduce this problem is to precalcurate paths whenerver possible: Waypoint navigator.
ABCDEFG A- B- C- D- E- F- G-
ABCDEFG A-BBBBBB B- C- D- E- F- G-
ABCDEFG A-BBBBBB BA-CCCCC CBB-DEEE D- E- F- G-
ABCDEFG A-BBBBBB BA-CCCCC CBB-DEEE DCCC-CCC E- F- G-
ABCDEFG A-BBBBBB BA-CCCCC CBB-DEEE DCCC-CCC ECCCC-FF F- G-
ABCDEFG A-BBBBBB BA-CCCCC CBB-DEEE DCCC-CCC ECCCC-FF FEEEEE-G G-
ABCDEFG A-BBBBBB BA-CCCCC CBB-DEEE DCCC-CCC ECCCC-FF FEEEEE-G GFFFFFF-
ABCDEFG A-BBBBBB BA-CCCCC CBB-DEEE DCCC-CCC ECCCC-FF FEEEEE-G GFFFFFF- If we want to walk from A to G Round 1: A to B
ABCDEFG A-BBBBBB BA-CCCCC CBB-DEEE DCCC-CCC ECCCC-FF FEEEEE-G GFFFFFF- If we want to walk from A to G Round 2: B to C
ABCDEFG A-BBBBBB BA-CCCCC CBB-DEEE DCCC-CCC ECCCC-FF FEEEEE-G GFFFFFF- If we want to walk from A to G Round 3: C to E
ABCDEFG A-BBBBBB BA-CCCCC CBB-DEEE DCCC-CCC ECCCC-FF FEEEEE-G GFFFFFF- If we want to walk from A to G Round 4: E to F
ABCDEFG A-BBBBBB BA-CCCCC CBB-DEEE DCCC-CCC ECCCC-FF FEEEEE-G GFFFFFF- If we want to walk from A to G Round 5: E to G เจอแล้วหยุด !
ABCDEFG A-BBBBBB BA-CCCCC CBB-DEEE DCCC-CCC ECCCC-FF FEEEEE-G GFFFFFF- If we want to walk from A to G Round 5: E to G Round 1: A to B Round 2: B to C Round 3: C to E Round 4: E to F A-->B-->C-->E- ->E-->G
จบการนำเสนอ