Commit 85b38912 authored by yvonneanne's avatar yvonneanne

extend route det circ to imcomplete arborescence decompositions

parent 03fa0950
......@@ -519,8 +519,8 @@ def FindTree(g, k):
else:
g.add_edge(*e)
if len(R) < len(g.nodes()):
print(
"Couldn't find next edge for tree with g.graph['root']")
#print(
# "Couldn't find next edge for tree with g.graph['root'], ", k, len(R))
sys.stdout.flush()
return T
......@@ -736,8 +736,9 @@ class Network:
return self.g.predecessors(v)
# set up network data structures before using them
def prepareDS(n, h, dist):
reset_arb_attribute(n.g)
def prepareDS(n, h, dist, reset=True):
if reset:
reset_arb_attribute(n.g)
for i in range(n.K):
dist.append({n.root: 0})
preds = sorted(n.g.predecessors(n.root), key=lambda k: random.random())
......@@ -825,14 +826,15 @@ def RR_con_swap(g):
return (round_robin(g, cut=True, swap=True))
# basic round robin implementation of constructing arborescences
def round_robin(g, cut=False, swap=False):
def round_robin(g, cut=False, swap=False, reset=True):
global swappy
reset_arb_attribute(g)
if reset:
reset_arb_attribute(g)
n = Network(g, g.graph['k'], g.graph['root'])
K = n.K
h = []
dist = []
prepareDS(n, h, dist)
prepareDS(n, h, dist, reset)
index = 0
swaps = 0
count = 0
......@@ -885,4 +887,39 @@ def round_robin(g, cut=False, swap=False):
index = (index + 1) % K
swappy.append(swaps)
g = n.g
return get_arborescence_list(g)
# associate a greedy arborescence decomposition with g
# and the continue adding arborescences pieces as long as possible
def AdHocExtraLinks(g):
reset_arb_attribute(g)
gg = g.to_directed()
# K is set to degree of root
K = len(g.in_edges(g.graph['root']))
k = K
while k > 0:
T = FindTree(gg, k)
if T is None or len(T.edges()) == 0:
#print("no edges")
K = K-1
k = k-1
continue
for (u, v) in T.edges():
g[u][v]['arb'] = K-k
gg.remove_edges_from(T.edges())
k = k-1
#print('complete', num_complete_nodes(g))
count = 0
for (u, v) in g.edges():
if g[u][v]['arb'] == -1:
from_u = [d['arb'] for (i, j, d) in g.edges(data=True) if i == u]
for i in range(K):
if not (i in from_u):
g[u][v]['arb'] == i
count += 1
continue
#print('added edges', count)
#print('complete', num_complete_nodes(g))
return get_arborescence_list(g)
\ No newline at end of file
......@@ -8,6 +8,7 @@ import random
import time
import glob
from objective_function_experiments import *
DEBUG = False
# Data structure containing the algorithms under
# scrutiny. Each entry contains a name and a pair
......@@ -29,7 +30,8 @@ from objective_function_experiments import *
# In this example we compare Bonsai and Greedy. You can add more
# algorithms to this data structure to compare the performance
# of additional algorithms.
algos = {'Bonsai': [RR_swap, RouteDetCirc], 'Greedy': [Trees, RouteDetCirc]}
#algos = {'Bonsai': [RR_swap, RouteDetCirc], 'Greedy': [Trees, RouteDetCirc], 'Adhoc': [AdHocExtraLinks, RouteDetCirc]}
algos = {'Greedy': [Trees, RouteDetCirc], 'Adhoc': [AdHocExtraLinks, RouteDetCirc]}
# run one experiment with graph g
......@@ -41,6 +43,7 @@ algos = {'Bonsai': [RR_swap, RouteDetCirc], 'Greedy': [Trees, RouteDetCirc]}
# otherwise (2 - success_ratio) * (stretch + load)
def one_experiment(g, seed, out, algo):
[precomputation_algo, routing_algo] = algo[:2]
if DEBUG: print('experiment for ', algo[0])
# precomputation
reset_arb_attribute(g)
......@@ -64,6 +67,7 @@ def one_experiment(g, seed, out, algo):
success_ratio = stat.succ/ samplesize
# write results
if stat.succ > 0:
if DEBUG: print('success', stat.succ, algo[0])
# stretch, load, hops, success, routing time, precomputation time
out.write(', %i, %i, %i, %f, %f, %f\n' %
(np.max(stat.stretch), stat.load, np.max(stat.hops),
......@@ -71,6 +75,7 @@ def one_experiment(g, seed, out, algo):
score = (2 - success_ratio) * (np.max(stat.stretch) + stat.load)
else:
if DEBUG: print('no success_ratio', algo[0])
out.write(', %f, %f, %f, %f, %f, %f\n' %
(float('inf'), float('inf'), float('inf'), 0, rt, pt))
score = 1000*1000
......@@ -121,6 +126,7 @@ def run_zoo(out=None, seed=0, rep=5):
set_parameters([nn, rep, kk, ss, fn, seed, name + "zoo-"])
shuffle_and_run(g, out, seed, rep, str(i))
set_parameters(original_params)
#return #TODO remove this line
# shuffle root nodes and run algorithm
def shuffle_and_run(g, out, seed, rep, x):
......@@ -183,8 +189,10 @@ def experiments(switch="all", seed=0, rep=100):
run_AS(out=out, seed=seed, rep=rep)
out.close()
print()
for (algoname, algo) in algos.items():
print(algoname, np.mean(algo[2:]))
print('%s \t %.5E' % (algoname, np.mean(algo[2:])))
print("\nlower is better")
......@@ -208,6 +216,8 @@ if __name__ == "__main__":
rep = int(sys.argv[3])
if len(sys.argv) > 4:
n = int(sys.argv[4])
if len(sys.argv) > 4:
samplesize = int(sys.argv[5])
random.seed(seed)
set_parameters([n, rep, k, samplesize, f_num, seed, "benchmark-"])
experiments(switch=switch, seed=seed, rep=rep)
......
......@@ -34,6 +34,11 @@ def RouteDetCirc(s, d, fails, T):
n = len(T[0].nodes())
k = len(T)
while (s != d):
while (s not in T[curT].nodes()) and switches < k*n:
curT = (curT+1) % k
switches += 1
if switches >= k*n:
break
nxt = list(T[curT].neighbors(s))
if len(nxt) != 1:
print("Bug: too many or to few neighbours")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment