From 11807d046cb0d8844b1c0969a0d2ea1bb3d848ef Mon Sep 17 00:00:00 2001 From: fjzzq2002 Date: Mon, 1 May 2017 13:35:34 +0800 Subject: [PATCH 1/2] added hack_spfa & correct some bugs --- cyaron/graph.py | 37 +++++++++++++++++++++++++++++++++---- test2.py | 6 +----- test3.py | 19 +++++++++++++++++++ 3 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 test3.py diff --git a/cyaron/graph.py b/cyaron/graph.py index 8e54211..4dd2f98 100644 --- a/cyaron/graph.py +++ b/cyaron/graph.py @@ -132,16 +132,45 @@ def binary_tree(point_count, left=0, right=0, **kwargs): @staticmethod def graph(point_count, edge_count, **kwargs): - directed = kwargs.get("directed", True) + directed = kwargs.get("directed", False) weight_limit = kwargs.get("weight_limit", (1,1)) if not isinstance(weight_limit, tuple): weight_limit = (1, weight_limit) - - graph = Graph(point_count) + graph = Graph(point_count,directed) for i in range(edge_count): u = random.randint(1, point_count) v = random.randint(1, point_count) weight = random.randint(weight_limit[0], weight_limit[1]) - graph.add_edge(u, v, weight=weight, directed=directed) + graph.add_edge(u, v, weight=weight) + return graph + #hack spfa (maybe?) + @staticmethod + def hack_spfa(point_count, **kwargs): + directed = kwargs.get("directed", False) + extraedg = kwargs.get("extra_edge", 2) + weight_limit = kwargs.get("weight_limit", (1,1)) + if not isinstance(weight_limit, tuple): + weight_limit = (1, weight_limit) + skp=point_count+3 + graph=Graph(point_count,directed) + if point_count%2==1: + skp=point_count/2+1 + half=point_count/2 + for i in range(1,half): + (x,y)=(i,i+1) + weight = random.randint(weight_limit[0], weight_limit[1]) + graph.add_edge(x+(x>=skp),y+(y>=skp),weight=weight) + (x,y)=(i+half,i+half+1) + weight = random.randint(weight_limit[0], weight_limit[1]) + graph.add_edge(x+(x>=skp),y+(y>=skp),weight=weight) + for i in range(1,half+1): + (x,y)=(i,i+half) + weight = random.randint(weight_limit[0], weight_limit[1]) + graph.add_edge(x+(x>=skp),y+(y>=skp),weight=weight) + for i in range(extraedg): + u = random.randint(1, point_count) + v = random.randint(1, point_count) + weight = random.randint(weight_limit[0], weight_limit[1]) + graph.add_edge(u, v, weight=weight) return graph diff --git a/test2.py b/test2.py index 314694d..28895af 100644 --- a/test2.py +++ b/test2.py @@ -3,7 +3,6 @@ from cyaron import * _n = ati([0, 7, 50]) -_m = ati([0, 11, 100]) def oper(x): return "%d %d" % (x.start, x.end) @@ -11,10 +10,7 @@ def oper(x): for i in range(1, 3): test_data = IO(file_prefix="test", data_id=i) n = _n[i] - m = _m[i] - s = randint(1, n) - t = randint(1, n) - test_data.writeln(n, m, s, t) + test_data.writeln(n) graph = Graph.tree(n,0.3,0.3) test_data.writeln(graph.to_str(output=oper)) test_data.writeln('============Shuffle============') diff --git a/test3.py b/test3.py new file mode 100644 index 0000000..ca2e0b0 --- /dev/null +++ b/test3.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python + +from cyaron import * + +_n = ati([0, 7, 8]) + +def oper(x): + return "%d %d weight=%d" % (x.start, x.end, x.weight) + +for i in range(1, 3): + test_data = IO(file_prefix="test", data_id=i) + n = _n[i] + test_data.writeln(n) + graph = Graph.hack_spfa(n,weight_limit=(1,5)) + test_data.writeln(graph.to_str(output=oper)) + test_data.writeln('============Shuffle============') + test_data.writeln(graph.to_str(output=oper,shuffle=True)) + test_data.output_gen("~/Downloads/std_binary") + # A binary file or shell command that accepts input from stdin and outputs to stdout From c088ac4a4220bb7593e66fea49c90e658a8e3f88 Mon Sep 17 00:00:00 2001 From: fjzzq2002 Date: Mon, 1 May 2017 13:47:47 +0800 Subject: [PATCH 2/2] added support for custom weight generator --- cyaron/graph.py | 32 ++++++++++++++++---------------- test2.py | 2 +- test3.py | 8 ++++++-- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/cyaron/graph.py b/cyaron/graph.py index 4dd2f98..820e47e 100644 --- a/cyaron/graph.py +++ b/cyaron/graph.py @@ -67,6 +67,7 @@ def tree(point_count, chain=0, flower=0, **kwargs): weight_limit = kwargs.get("weight_limit", (1, 1)) if not isinstance(weight_limit, tuple): weight_limit = (1, weight_limit) + weight_gen = kwargs.get("weight_gen",lambda:random.randint(weight_limit[0],weight_limit[1])) if not 0 <= chain <= 1 or not 0 <= flower <= 1: raise Exception("chain and flower must be between 0 and 1") @@ -83,15 +84,15 @@ def tree(point_count, chain=0, flower=0, **kwargs): random_count = point_count - 1 - chain_count - flower_count for i in range(2, chain_count+2): - weight = random.randint(weight_limit[0], weight_limit[1]) + weight = weight_gen() graph.add_edge(i-1, i, weight=weight) for i in range(chain_count+2, chain_count+flower_count+2): - weight = random.randint(weight_limit[0], weight_limit[1]) + weight = weight_gen() graph.add_edge(1, i, weight=weight) for i in range(point_count-random_count+1, point_count+1): - weight = random.randint(weight_limit[0], weight_limit[1]) + weight = weight_gen() u = random.randrange(1, i) graph.add_edge(u, i, weight=weight) @@ -103,6 +104,7 @@ def binary_tree(point_count, left=0, right=0, **kwargs): weight_limit = kwargs.get("weight_limit", (1, 1)) if not isinstance(weight_limit, tuple): weight_limit = (1, weight_limit) + weight_gen = kwargs.get("weight_gen",lambda:random.randint(weight_limit[0],weight_limit[1])) if not 0 <= left <= 1 or not 0 <= right <= 1: raise Exception("left and right must be between 0 and 1") @@ -114,7 +116,7 @@ def binary_tree(point_count, left=0, right=0, **kwargs): graph = Graph(point_count,directed) for i in range(2, point_count+1): edge_pos = random.uniform(0, 1) - weight = random.randint(weight_limit[0], weight_limit[1]) + weight = weight_gen() node = 0 if edge_pos < left or left+right < edge_pos <= (1.0-left-right)/2: # Left @@ -136,11 +138,12 @@ def graph(point_count, edge_count, **kwargs): weight_limit = kwargs.get("weight_limit", (1,1)) if not isinstance(weight_limit, tuple): weight_limit = (1, weight_limit) + weight_gen = kwargs.get("weight_gen",lambda:random.randint(weight_limit[0],weight_limit[1])) graph = Graph(point_count,directed) for i in range(edge_count): u = random.randint(1, point_count) v = random.randint(1, point_count) - weight = random.randint(weight_limit[0], weight_limit[1]) + weight = weight_gen() graph.add_edge(u, v, weight=weight) return graph @@ -152,25 +155,22 @@ def hack_spfa(point_count, **kwargs): weight_limit = kwargs.get("weight_limit", (1,1)) if not isinstance(weight_limit, tuple): weight_limit = (1, weight_limit) + weight_gen = kwargs.get("weight_gen",lambda:random.randint(weight_limit[0],weight_limit[1])) skp=point_count+3 graph=Graph(point_count,directed) if point_count%2==1: skp=point_count/2+1 half=point_count/2 for i in range(1,half): - (x,y)=(i,i+1) - weight = random.randint(weight_limit[0], weight_limit[1]) - graph.add_edge(x+(x>=skp),y+(y>=skp),weight=weight) - (x,y)=(i+half,i+half+1) - weight = random.randint(weight_limit[0], weight_limit[1]) - graph.add_edge(x+(x>=skp),y+(y>=skp),weight=weight) + (x,y) = (i,i+1) + graph.add_edge(x+(x>=skp), y+(y>=skp), weight=weight_gen()) + (x,y) = (i+half,i+half+1) + graph.add_edge(x+(x>=skp), y+(y>=skp), weight=weight_gen()) for i in range(1,half+1): - (x,y)=(i,i+half) - weight = random.randint(weight_limit[0], weight_limit[1]) - graph.add_edge(x+(x>=skp),y+(y>=skp),weight=weight) + (x,y) = (i,i+half) + graph.add_edge(x+(x>=skp), y+(y>=skp), weight=weight_gen()) for i in range(extraedg): u = random.randint(1, point_count) v = random.randint(1, point_count) - weight = random.randint(weight_limit[0], weight_limit[1]) - graph.add_edge(u, v, weight=weight) + graph.add_edge(u, v, weight=weight_gen()) return graph diff --git a/test2.py b/test2.py index 28895af..2f7ed5c 100644 --- a/test2.py +++ b/test2.py @@ -5,7 +5,7 @@ _n = ati([0, 7, 50]) def oper(x): - return "%d %d" % (x.start, x.end) + return "%d %d" % (x.start, x.end) for i in range(1, 3): test_data = IO(file_prefix="test", data_id=i) diff --git a/test3.py b/test3.py index ca2e0b0..65f79b6 100644 --- a/test3.py +++ b/test3.py @@ -1,17 +1,21 @@ #!/usr/bin/env python from cyaron import * +import random _n = ati([0, 7, 8]) def oper(x): - return "%d %d weight=%d" % (x.start, x.end, x.weight) + return "%d %d weight=%d,%d" % (x.start, x.end, x.weight[0], x.weight[1]) + +def wg(): + return [random.randint(3,5),random.randint(1,10)] for i in range(1, 3): test_data = IO(file_prefix="test", data_id=i) n = _n[i] test_data.writeln(n) - graph = Graph.hack_spfa(n,weight_limit=(1,5)) + graph = Graph.hack_spfa(n,weight_gen=wg) test_data.writeln(graph.to_str(output=oper)) test_data.writeln('============Shuffle============') test_data.writeln(graph.to_str(output=oper,shuffle=True))