逻辑图形生成器,可用于各种可以建模为图形数据结构的问题
coopgraph的Python项目详细描述
坐标图
逻辑图形生成器,可用于各种可以建模为图形数据结构的问题
例如:
from Graphs import Graph, Node
from dataStructs import Vector2
a = Node(name='A', pos=Vector2(0, 0))
b = Node(name='B', pos=Vector2(3, 3))
c = Node(name='C', pos=Vector2(2, 0))
d = Node(name='D', pos=Vector2(2, 1))
e = Node(name='E', pos=Vector2(3, 4))
f = Node(name='F', pos=Vector2(5, 5))
g = { a: [d],
b: [c],
c: [b, d, e],
d: [a, c],
e: [c, f],
f: []
}
graph = Graph(g)
然后,图形结构可用于执行各种与图形相关的分析:
两个没有出站连接的查找节点
^{pr2}$寻找两个节点之间的最短路径
print(graph.astar(a, e))
注意,对于astar计算,可以针对一个或多个禁用器启用或禁用边。这有助于在以下情况下实施临时标准:
edges_to_disable = [value for key, value in graph.edges()][:3]
graph.disable_edges(edges_to_disable, "myDisabler")
path = graph.astar(a, e)
graph.disable_edges(edges_to_disable, "myDisabler")
也可以通过将禁用程序名称的列表传递给astar()方法直接忽略禁用程序
edges_to_disable = [value for key, value in graph.edges()][:3]
graph.disable_edges(edges_to_disable, "myIngoredDisabler")
ignored = ["myIngoredDisabler"]
path = graph.astar(a, e, ignored_disablers=ignored)
astar()调用还可以包含自定义g和h函数,以便更好地控制astar算法
def g(node1 Node, node2: Node) -> float:
if node1.pos - node2.pos > 10:
return 1
else
return .5
def h(node1 Node, node2: Node) -> float:
if node1.pos - node2.pos > 10:
return 100
else
return -100
path = graph.astar(a, e, g_func=g, h_func=h)
- 项目
标签: