python尋路 A 尋路演算法 python實現

2021-10-11 17:24:04 字數 4997 閱讀 5579

# -*- coding: utf-8 -*-

import math

import cv2 as cv

class point(object):

def __init__(self, position, parent):

self.position = position

self.parent = parent

self.f = 0

self.g = 0

self.h = 0

# 全域性閾值

def threshold_demo(image):

gray = cv.cvtcolor(image, cv.color_rgb2gray) # 把輸入影象灰度化

# 直接閾值化是對輸入的單通道矩陣逐畫素進行閾值分割。

raise valueerror('無法找到可達路徑')

points = get_periphery_points(map, cur_point)

points = filter_not_reachables(map, points)

for point in points:

if is_point_in_list(point, open_list):

point.new_added = false

point.ignore = false

p = get_point_from_list(point, open_list)

point.parent = p.parent

point.f = p.f

point.g = p.g

point.h = p.h

elif is_point_in_list(point, close_list):

point.new_added = false

point.ignore = true

p = get_point_from_list(point, close_list)

point.parent = p.parent

point.f = p.f

point.g = p.g

point.h = p.h

else:

point.new_added = true

point.ignore = false

points = filter_ignored(points)

for point in points:

if point.new_added:

point.parent = cur_point

# 計算fgh

point.g = cur_point.g + 1

point.h = estimate_distance(point, target_point)

point.f = point.g + point.h

else:

# 計算fgh

old_f = point.g + point.h

new_f = cur_point.g + 1 + point.h

# 比較新的和老的f值哪個大

if new_f < old_f:

# 覆蓋新的fgh/parent

point.parent = cur_point

point.g = cur_point.g + 1

point.f = point.g + point.h

for point in points:

if is_same_node(point, target_point):

display_path(point)

return

open_list.remove(cur_point)

a_star(bi)

cv.waitkey(0)

cv.destroyallwindows()

迷宮尋路(A星尋路演算法)

題目 假設我們有乙個7 5大小的迷宮,如下圖所示,綠色格仔表示起點,紅色的格仔表示終點,中間的3個深灰色格仔表示障礙物。請找到一條從起點到終點最短的路徑。解題思路 需要引入兩個集合和乙個公式,如下 具體步驟 把起點放入openlist 檢查openlist中是否有值,如果沒有則無法到達終點,結束尋路...

python迷宮尋路 迷宮尋路問題 A 演算法

迷宮尋路問題 a 演算法 迷宮尋路問題是人工智慧中的有趣問題,如何表示狀態空間和搜尋路徑是尋路問題的重點,本文的主要內容是a 搜尋演算法的理解和應用,首先對基本知識和演算法思想進行了解,再通過其對迷宮問題求解應用,編寫 python 程式進行深入學習。1.搜尋區域 我們假設某個人要從 start 點...

A 尋路演算法

問題 由於遊戲中尋路出了個小問題 玩家尋路到乙個死角後在那邊不停的來回跑,就是無法越過障礙物,就研究了下a 尋路演算法以解決這個問題 研究了幾天,自己寫了個demo這裡給出總結 原理 a 演算法給出的是權值最優的路徑而不是最短路徑 權值有f g h來表示 啟發式函式如下 f p g p h p h值...