-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
5c46e4e
commit 339eb45
Showing
7 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class Dog(): | ||
def __init__(self,name,breed,age): | ||
"""初始化属性name、breed和age""" | ||
self.name = name | ||
self.breed = breed | ||
self.age = age | ||
def eat(self): | ||
"""小狗正在吃狗粮""" | ||
print(f"{self.name}正在吃狗粮") | ||
def run(self): | ||
"""小狗正在奔跑""" | ||
print(f"{self.name}在奔跑玩耍") | ||
|
||
|
||
if __name__ == '__main__': | ||
d1 = Dog("小巴","哈巴狗",4) #创建实例对象 | ||
|
||
print(f"狗狗的姓名是{d1.name}") #访问name属性 | ||
print(f"狗狗的品种是{d1.breed}") #访问breed属性 | ||
print(f"狗狗的品种是{d1.age}") #访问age属性 | ||
d1.eat() | ||
d1.run() | ||
|
||
d2 = Dog("小柴","柴犬",1) #创建一个实例对象d2 | ||
print(f"{d2.name}的品种是{d2.breed},年龄是{d2.age}") | ||
d2.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from data import Node, graph, solution_path | ||
|
||
# BFS 搜索 | ||
def BFS(graph, initState, goalState ): | ||
|
||
# open 表 | ||
open_table = [] | ||
closed_table = [] # closed表 | ||
print_data(open_table, closed_table) | ||
|
||
open_table.append(initState) | ||
print_data(open_table, closed_table) | ||
|
||
while len(open_table) != 0: | ||
currentNode = open_table.pop(0) | ||
closed_table.append(currentNode) | ||
print_data(open_table, closed_table) | ||
|
||
if graph[currentNode].state == goalState: | ||
return solution_path(graph, initState, goalState) | ||
|
||
for sub_node in graph[currentNode].actions: | ||
if sub_node not in open_table and sub_node not in closed_table: | ||
graph[sub_node].parent = currentNode | ||
open_table.append(sub_node) | ||
print_data(open_table, closed_table) | ||
|
||
|
||
def print_data(open, closed): | ||
print('open : {0}'.format(open)) | ||
# print(open ) | ||
print('closed : {0}'.format(closed)) | ||
# print(closed) | ||
print('='*50) | ||
|
||
if __name__ == '__main__': | ||
# 用字典来表示图 | ||
# graph = { 'S': Node('S', None, ['A', 'D']), | ||
# 'A':Node('A',None,['B']), | ||
# 'D':Node('D',None,['G']), | ||
# 'B':Node('B',None,['C','G']), | ||
# 'G':Node('G',None,[]), | ||
# 'C':Node('C',None,['G'])} | ||
solution = BFS(graph, 'S', 'G') | ||
print(solution) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# 定一个类表示状态空间的节点。 | ||
|
||
class Node: | ||
|
||
def __init__(self, state, parent, actions): | ||
self.state = state | ||
self.parent = parent | ||
self.actions = actions | ||
|
||
|
||
def solution_path(graph, init, goal): | ||
#返回解路径。 从goal节点反溯parent,直到到达init节点。 | ||
path = [goal] | ||
current_parent = graph[goal].parent | ||
while current_parent!= None: | ||
path.append(current_parent) | ||
current_parent = graph[current_parent].parent | ||
path.reverse() | ||
|
||
return path | ||
|
||
graph = { 'S': Node('S', None, ['A', 'D']), | ||
'A':Node('A',None,['B']), | ||
'D':Node('D',None,['G']), | ||
'B':Node('B',None,['C','G']), | ||
'G':Node('G',None,[]), | ||
'C':Node('C',None,['G'])} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.