本文整理汇总了Python中halp.directed_hypergraph.DirectedHypergraph.add_hyperedge方法的典型用法代码示例。如果您正苦于以下问题:Python DirectedHypergraph.add_hyperedge方法的具体用法?Python DirectedHypergraph.add_hyperedge怎么用?Python DirectedHypergraph.add_hyperedge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类halp.directed_hypergraph.DirectedHypergraph
的用法示例。
在下文中一共展示了DirectedHypergraph.add_hyperedge方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_raises_exception_if_H_not_B_hypegraph
# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_hyperedge [as 别名]
def test_raises_exception_if_H_not_B_hypegraph(self):
H = DirectedHypergraph()
H.add_nodes([1, 2, 3])
H.add_hyperedge([1], [2, 3])
source, destination = 1, 2
self.assertRaises(TypeError,
ksh.k_shortest_hyperpaths, H, source, destination, 1)
示例2: test_stationary_distribution
# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_hyperedge [as 别名]
def test_stationary_distribution():
H = DirectedHypergraph()
H.read("./tests/data/basic_directed_hypergraph.txt")
# Try random-walking a directed hypergraph with a node
# with no outgoing hyperedges
try:
pi = rw.stationary_distribution(H)
assert False
except AssertionError:
pass
except BaseException as e:
assert False, e
# Try random-walking a valid directed hypergraph
H.add_hyperedge(["u"], ["u"], weight=1)
pi = rw.stationary_distribution(H)
# Correctness tests go here
# Try partitioning an invalid directed hypergraph
try:
pi = rw.stationary_distribution("H")
assert False
except TypeError:
pass
except BaseException as e:
assert False, e
示例3: from_networkx_digraph
# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_hyperedge [as 别名]
def from_networkx_digraph(nx_digraph):
"""Returns a DirectedHypergraph object that is the graph equivalent of the
given NetworkX DiGraph object.
:param nx_digraph: the NetworkX directed graph object to transform.
:returns: DirectedHypergraph -- hypergraph object equivalent to the
NetworkX directed graph.
:raises: TypeError -- Transformation only applicable to directed
NetworkX graphs
"""
import networkx as nx
if not isinstance(nx_digraph, nx.DiGraph):
raise TypeError("Transformation only applicable to directed \
NetworkX graphs")
G = DirectedHypergraph()
for node in nx_digraph.nodes_iter():
G.add_node(node, copy.copy(nx_digraph.node[node]))
for edge in nx_digraph.edges_iter():
tail_node = edge[0]
head_node = edge[1]
G.add_hyperedge(tail_node,
head_node,
copy.copy(nx_digraph[tail_node][head_node]))
return G
示例4: test_returns_empty_list_if_no_s_t_path
# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_hyperedge [as 别名]
def test_returns_empty_list_if_no_s_t_path(self):
H = DirectedHypergraph()
H.add_node('s')
H.add_node('1')
H.add_node('2')
H.add_node('t')
H.add_hyperedge({'s'}, {'1'}, weight=1)
H.add_hyperedge({'1', '2'}, {'t'}, weight=1)
output = ksh.k_shortest_hyperpaths(H, 's', 't', 1)
self.assertEqual(output, [])
示例5: test_add_hyperedge
# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_hyperedge [as 别名]
def test_add_hyperedge():
node_a = 'A'
node_b = 'B'
node_c = 'C'
node_d = 'D'
tail = set([node_a, node_b])
head = set([node_c, node_d])
frozen_tail = frozenset(tail)
frozen_head = frozenset(head)
attrib = {'weight': 6, 'color': 'black'}
H = DirectedHypergraph()
H.add_node(node_a, label=1337)
hyperedge_name = H.add_hyperedge(tail, head, attrib, weight=5)
assert hyperedge_name == 'e1'
# Test that all hyperedge attributes are correct
assert H._hyperedge_attributes[hyperedge_name]['tail'] == tail
assert H._hyperedge_attributes[hyperedge_name]['head'] == head
assert H._hyperedge_attributes[hyperedge_name]['weight'] == 5
assert H._hyperedge_attributes[hyperedge_name]['color'] == 'black'
# Test that successor list contains the correct info
assert frozen_head in H._successors[frozen_tail]
assert hyperedge_name in H._successors[frozen_tail][frozen_head]
# Test that the precessor list contains the correct info
assert frozen_tail in H._predecessors[frozen_head]
assert hyperedge_name in H._predecessors[frozen_head][frozen_tail]
# Test that forward-stars and backward-stars contain the correct info
for node in frozen_tail:
assert hyperedge_name in H._forward_star[node]
for node in frozen_head:
assert hyperedge_name in H._backward_star[node]
# Test that adding same hyperedge will only update attributes
new_attrib = {'weight': 10}
H.add_hyperedge(tail, head, new_attrib)
assert H._hyperedge_attributes[hyperedge_name]['weight'] == 10
assert H._hyperedge_attributes[hyperedge_name]['color'] == 'black'
try:
H.add_hyperedge(set(), set())
assert False
except ValueError:
pass
except BaseException as e:
assert False, e
示例6: test_returns_hyperpath_for_simple_tree
# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_hyperedge [as 别名]
def test_returns_hyperpath_for_simple_tree(self):
s1, s2, s3, s4 = 1, 2, 3, 4
H = DirectedHypergraph()
H.add_nodes([s1, s2, s3, s4])
e1 = H.add_hyperedge([s1], [s2])
e2 = H.add_hyperedge([s1], [s3])
e3 = H.add_hyperedge([s3], [s4])
T = {s4: e3, s3: e2, s2: e1, s1: None}
path = directed_paths.get_hyperpath_from_predecessors(H, T, s1, s4)
# validate nodes
self.assertEqual(path.get_node_set(), {s1, s3, s4})
# validate hyperedges
self.assertEqual(len(path.get_hyperedge_id_set()), 2)
self.assertTrue(path.get_hyperedge_id([1], [3]))
self.assertTrue(path.get_hyperedge_id([3], [4]))
示例7: test_get_hyperedge_attributes
# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_hyperedge [as 别名]
def test_get_hyperedge_attributes():
node_a = 'A'
node_b = 'B'
node_c = 'C'
node_d = 'D'
tail = set([node_a, node_b])
head = set([node_c, node_d])
frozen_tail = frozenset(tail)
frozen_head = frozenset(head)
attrib = {'weight': 6, 'color': 'black'}
H = DirectedHypergraph()
H.add_node(node_a, label=1337)
hyperedge_name = H.add_hyperedge(tail, head, attrib, weight=5)
assert H.get_hyperedge_attributes(hyperedge_name) == \
{'tail': tail, 'head': head, 'weight': 5, 'color': 'black'}
# Test getting non-existent hyperedge's attributes
try:
H.get_hyperedge_attributes("e10")
assert False
except ValueError:
pass
except BaseException as e:
assert False, e
示例8: test_returns_hyperpath_when_node_is_in_tail_of_two_edges
# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_hyperedge [as 别名]
def test_returns_hyperpath_when_node_is_in_tail_of_two_edges(self):
s1, s2, s3 = 1, 2, 3
s4 = 4
H = DirectedHypergraph()
e1 = H.add_hyperedge([s1], [s2])
e2 = H.add_hyperedge([s2], [s3])
e3 = H.add_hyperedge([s2, s3], [s4])
T = {s4: e3, s3: e2, s2: e1, s1: None}
path = directed_paths.get_hyperpath_from_predecessors(H, T, s1, s4)
# validate nodes
self.assertEqual(path.get_node_set(), {s1, s2, s3, s4})
# validate hyperedges
self.assertEqual(len(path.get_hyperedge_id_set()), 3)
self.assertTrue(path.get_hyperedge_id([2, 3], [4]))
self.assertTrue(path.get_hyperedge_id([2], [3]))
self.assertTrue(path.get_hyperedge_id([1], [2]))
示例9: test_raises_exception_if_all_nodes_have_predecessors
# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_hyperedge [as 别名]
def test_raises_exception_if_all_nodes_have_predecessors(self):
s1, s2, s3 = 1, 2, 3
H = DirectedHypergraph()
H.add_nodes([s1, s2, s3])
e1 = H.add_hyperedge([s1], [s2])
T = {s1: e1, s2: e1, s3: e1}
self.assertRaises(ValueError,
directed_paths.get_hyperpath_from_predecessors,
H, T, s1, s2)
示例10: test_raises_exception_if_values_of_function_are_not__in_hypergraph
# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_hyperedge [as 别名]
def test_raises_exception_if_values_of_function_are_not__in_hypergraph(
self):
s1, s2, s3 = 1, 2, 3
H = DirectedHypergraph()
H.add_nodes([s1, s2])
e1 = H.add_hyperedge([s1], [s2])
T = {s1: None, s2: 'e2'}
self.assertRaises(KeyError,
directed_paths.get_hyperpath_from_predecessors,
H, T, s1, s2)
示例11: test_returns_disconnected_nodes_on_graph_with_two_nodes
# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_hyperedge [as 别名]
def test_returns_disconnected_nodes_on_graph_with_two_nodes(self):
H = DirectedHypergraph()
s, t = 's', 't'
H.add_node(s)
H.add_node(t)
e1 = H.add_hyperedge({s}, {t})
predecessor = {s: None, t: e1}
ordering = [s, t]
branch = ksh._branching_step(H, predecessor, ordering)[0]
self.assertEqual(branch.get_hyperedge_id_set(), set([]))
self.assertEqual(branch.get_node_set(), {'s', 't'})
示例12: test_returns_hyperpath_for_tree_with_multiple_nodes_in_tail
# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_hyperedge [as 别名]
def test_returns_hyperpath_for_tree_with_multiple_nodes_in_tail(self):
s1, s2, s3 = 1, 2, 3
s4, s5, s6 = 4, 5, 6
H = DirectedHypergraph()
H.add_nodes([s1, s2, s3, s4, s5, s6])
e1 = H.add_hyperedge([s1], [s2])
e2 = H.add_hyperedge([s1], [s3])
e3 = H.add_hyperedge([s1], [s4])
e4 = H.add_hyperedge([s2, s3], [s5])
e5 = H.add_hyperedge([s5], [s6])
T = {s6: e5, s5: e4, s4: e3, s3: e2, s2: e1, s1: None}
path = directed_paths.get_hyperpath_from_predecessors(H, T, s1, s6)
# validate nodes
self.assertEqual(path.get_node_set(), {s1, s2, s3, s5, s6})
# validate hyperedges
self.assertEqual(len(path.get_hyperedge_id_set()), 4)
self.assertTrue(path.get_hyperedge_id([5], [6]))
self.assertTrue(path.get_hyperedge_id([2, 3], [5]))
self.assertTrue(path.get_hyperedge_id([1], [3]))
self.assertTrue(path.get_hyperedge_id([1], [2]))
示例13: convert_pnet_to_hypergraph_andgatewayonly
# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_hyperedge [as 别名]
def convert_pnet_to_hypergraph_andgatewayonly(pnet):
hg = DirectedHypergraph()
#scan all transitions and create hyperedges
transitions = get_transitions(pnet)
for transition in transitions:
#get all incoming arcs, the source of these become the tail of hyperedge
inc_arcs = get_incoming_arcs(transition,pnet)
tail = []
for inc_arc in inc_arcs:
source = str(get_arc_source(inc_arc))
tail.append(source)
#get all outgoing arcs, the target of these become the head of the hyperedge
out_arcs = get_outgoing_arcs(transition,pnet)
head = []
for out_arc in out_arcs:
target = str(get_arc_target(out_arc))
head.append(target)
name = get_transition_name(transition)
hg.add_hyperedge(tail, head, name = name, phero = 0.5, cost = 0.4, avail = 0.6, qual = 0.2, time = 0.99)
#print the result before exit
print_hg_std_out_only(hg)
return hg
示例14: test_returns_only_one_hyperpath_for_k_equals_one
# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_hyperedge [as 别名]
def test_returns_only_one_hyperpath_for_k_equals_one(self):
H = DirectedHypergraph()
H.add_node('s')
H.add_node('1')
H.add_node('2')
H.add_node('3')
H.add_node('t')
H.add_hyperedge({'s'}, {'1'}, weight=1)
H.add_hyperedge({'s'}, {'2'}, weight=1)
H.add_hyperedge({'s'}, {'3'}, weight=1)
H.add_hyperedge({'1'}, {'t'}, weight=1)
H.add_hyperedge({'2', '3'}, {'t'}, weight=1)
output = ksh.k_shortest_hyperpaths(H, 's', 't', 1)
self.assertEqual(len(output), 1)
示例15: test_is_BF_hypergraph
# 需要导入模块: from halp.directed_hypergraph import DirectedHypergraph [as 别名]
# 或者: from halp.directed_hypergraph.DirectedHypergraph import add_hyperedge [as 别名]
def test_is_BF_hypergraph():
H = DirectedHypergraph()
H.read("tests/data/basic_directed_hypergraph.txt")
assert not H.is_BF_hypergraph()
H = DirectedHypergraph()
H.add_hyperedge(['a', 'b'], ['c'])
assert H.is_BF_hypergraph()
H = DirectedHypergraph()
H.add_hyperedge(['x'], ['y', 'z'])
assert H.is_BF_hypergraph()
H = DirectedHypergraph()
H.add_hyperedge(['a', 'b'], ['c'])
H.add_hyperedge(['x'], ['y', 'z'])
assert H.is_BF_hypergraph()