本文整理汇总了Python中edge.Edge.path方法的典型用法代码示例。如果您正苦于以下问题:Python Edge.path方法的具体用法?Python Edge.path怎么用?Python Edge.path使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类edge.Edge
的用法示例。
在下文中一共展示了Edge.path方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: swap_path_node
# 需要导入模块: from edge import Edge [as 别名]
# 或者: from edge.Edge import path [as 别名]
def swap_path_node(self, path, swap_from, swap_to):
"""
Swap the source node in the path to target
:param path:
:param source:
:param target:
:return:
"""
nodes = path.get_nodes()
assert swap_from in nodes
idx = nodes.index(swap_from)
if idx == 0:
# Remove the first edge and swap with a new one
old_edge = path.edges[0]
if old_edge.source == swap_from:
new_edge = Edge(source=swap_to, target=old_edge.target)
else:
new_edge = Edge(source=old_edge.source, target=swap_to)
new_edge.path = path
path.remove_edge(old_edge)
path.edges.insert(0, new_edge)
elif idx == len(nodes) - 1:
# Remove the last edge and swap with a new one
old_edge = path.edges[-1]
if old_edge.source == swap_from:
new_edge = Edge(source=swap_to, target=old_edge.target)
else:
new_edge = Edge(source=old_edge.source, target=swap_to)
new_edge.path = path
path.remove_edge(old_edge)
path.edges.append(new_edge)
else:
old_edge_1 = path.edges[idx - 1]
old_edge_2 = path.edges[idx]
for e_idx, old_edge in [(idx - 1, old_edge_1), (idx, old_edge_2)]:
if old_edge.source == swap_from:
new_edge = Edge(source=swap_to, target=old_edge.target)
else:
new_edge = Edge(source=old_edge.source, target=swap_to)
new_edge.path = path
path.remove_edge(old_edge)
path.edges.insert(e_idx, new_edge)
示例2: swap_edge
# 需要导入模块: from edge import Edge [as 别名]
# 或者: from edge.Edge import path [as 别名]
def swap_edge(self, edge, nodes_from, nodes_to):
"""
:param nodes_from:
:param nodes_to:
:return:
"""
assert len(nodes_from) == 2
assert len(nodes_to) == 2
assert edge in edge.path.edges
assert len(edge.path.edges) == 1
if edge.source == nodes_from[0]:
new_edge = Edge(source=nodes_to[0], target=nodes_to[1])
else:
new_edge = Edge(source=nodes_to[1], target=nodes_to[0])
path = edge.path
new_edge.path = path
path.remove_edge(edge)
path.edges.append(new_edge)
示例3: swap_edge_node
# 需要导入模块: from edge import Edge [as 别名]
# 或者: from edge.Edge import path [as 别名]
def swap_edge_node(self, edge, node_from, node_to):
"""
Swap a node
:param edge:
:param node_from:
:param node_to:
:return:
"""
assert hasattr(edge, 'path')
assert edge in edge.path.edges
path = edge.path
if edge.source == node_from:
new_edge = Edge(source=node_to, target=edge.target)
else:
new_edge = Edge(source=edge.source, target=node_to)
new_edge.path = path
idx = path.edges.index(edge)
path.remove_edge(edge)
path.edges.insert(idx, new_edge)
示例4: remove_node
# 需要导入模块: from edge import Edge [as 别名]
# 或者: from edge.Edge import path [as 别名]
def remove_node(self, node):
"""
This method removes a node from a path. This method should be called from GeometricGraph's method only.
:param node:
:return:
"""
assert len(self.edges) > 1
nodes = self.get_nodes()
if node == nodes[0]:
self.remove_edge(self.edges[0])
elif node == nodes[-1]:
self.remove_edge(self.edges[-1])
else:
idx = nodes.index(node)
new_edge = Edge(nodes[idx - 1], nodes[idx + 1])
new_edge.path = self
new_edges = self.edges[:idx - 1] + [new_edge] + self.edges[idx + 1:]
self.remove_edge(self.edges[idx])
self.edges = new_edges
示例5: merge_edges
# 需要导入模块: from edge import Edge [as 别名]
# 或者: from edge.Edge import path [as 别名]
def merge_edges(self, edge1, edge2):
"""
Merge two edges in this path
:param edge1:
:param edge2:
:return:
"""
assert len(self.edges) > 1
assert edge1 in self.edges
assert edge2 in self.edges
assert len({edge1.source, edge1.target} & {edge2.source, edge2.target}) == 1
shared_node = list({edge1.source, edge1.target} & {edge2.source, edge2.target})[0]
if edge1.source == shared_node:
node1 = edge1.target
else:
node1 = edge1.source
if edge2.source == shared_node:
node2 = edge2.target
else:
node2 = edge2.source
new_edge = Edge(node1, node2)
new_edge.path = self
self.edges.insert(self.edges.index(edge1), new_edge)
# Clean up
edge1.source.remove_edge(edge1)
edge1.target.remove_edge(edge1)
edge2.source.remove_edge(edge2)
edge2.target.remove_edge(edge2)
edge1.path = None
edge2.path = None
self.edges.remove(edge1)
self.edges.remove(edge2)
return new_edge