Commit 03fa0950 authored by yvonneanne's avatar yvonneanne

fix bugs, add comments

parent 999d1972
......@@ -186,9 +186,9 @@ def in_sp_to_r(g, v, u):
# swap greedily while a pair of nodes with a better objective function evaluation
# after the swap can be found
def greedy_swap_minobj(g, obj):
# default is to minimize the objective function, passing along max=True will
# maximize it
def greedy_swap_obj(g, obj, max=False):
arbs = get_arborescence_dict(g)
o = obj(g)
new_o = 0
......@@ -198,28 +198,29 @@ def greedy_swap_minobj(g, obj):
count = 0
while swapped:
swapped = False
#print('count, o', count,o)
for (u, v1) in test:
for v2 in [nbr for nbr in g[u] if nbr != v1]:
# print(u, v1, v2, g[u][v1]['arb'], g[u][v2]['arb'])
if swappable(u, v1, v2, arbs[g[u][v1]['arb']], arbs[g[u][v2]['arb']]):
# print('swappable')
swap(g, u, v1, u, v2)
new_o = obj(g)
if new_o < o:
# print('new o', new_o)
# sys.exit()
o = new_o
swapped = True
count += 1
if max:
if new_o> o:
o = new_o
swapped = True
count += 1
else:
swap(g, u, v1, u, v2)
else:
swap(g, u, v1, u, v2)
if new_o < o:
o = new_o
swapped = True
count += 1
else:
swap(g, u, v1, u, v2)
return count
# return the stretch of the arborescence with index i on g (how much longer the
# path to the root is in the arborescence than in the original graph)
def stretch_index(g, index):
arbs = get_arborescence_list(g)
dist = nx.shortest_path_length(g, target=g.graph['root'])
......@@ -431,8 +432,6 @@ def find_best_swap(g):
return e1, e2
# recursively swap best edges found (with respect to stretch) in g
def best_swap(g):
(e1, e2) = find_best_swap(g)
while e1 != None:
......@@ -440,14 +439,10 @@ def best_swap(g):
(e1, e2) = find_best_swap(g)
# return the edge connectivity of g between s and t
def TestCut(g, s, t):
return nx.edge_connectivity(g, s, t)
# return a random arborescence rooted at the root
def FindRandomTree(g, k):
T = nx.DiGraph()
T.add_node(g.graph['root'])
......@@ -482,8 +477,6 @@ def FindRandomTree(g, k):
return T
# associate random trees as arborescences with g
def RandomTrees(g):
gg = g.to_directed()
K = g.graph['k']
......@@ -498,8 +491,6 @@ def RandomTrees(g):
k = k-1
# compute the k^th arborescence of g greedily
def FindTree(g, k):
T = nx.DiGraph()
T.add_node(g.graph['root'])
......@@ -533,9 +524,41 @@ def FindTree(g, k):
sys.stdout.flush()
return T
# associate a greedy arborescence decomposition with g
# compute the k^th arborescence of g greedily without checking for remaining connectivity
def FindTreeNoTestCut(g, k):
T = nx.DiGraph()
T.add_node(g.graph['root'])
R = {g.graph['root']}
dist = dict()
dist[g.graph['root']] = 0
# heap of all border edges in form [(edge metric, (e[0], e[1])),...]
h = []
preds = sorted(g.predecessors(
g.graph['root']), key=lambda k: random.random())
for x in preds:
heappush(h, (0, (x, g.graph['root'])))
if k > 1:
continue
while len(h) > 0:
(d, e) = heappop(h)
g.remove_edge(*e)
if e[0] not in R:
dist[e[0]] = d+1
R.add(e[0])
preds = sorted(g.predecessors(e[0]), key=lambda k: random.random())
for x in preds:
if x not in R:
heappush(h, (d+1, (x, e[0])))
T.add_edge(*e)
else:
g.add_edge(*e)
if len(R) < len(g.nodes()):
print(
"Couldn't find next edge for tree with g.graph['root']")
sys.stdout.flush()
return T
# associate a greedy arborescence decomposition with g
def Trees(g):
reset_arb_attribute(g)
gg = g.to_directed()
......@@ -549,10 +572,9 @@ def Trees(g):
g[u][v]['arb'] = K-k
gg.remove_edges_from(T.edges())
k = k-1
return get_arborescence_list(g)
# XXX
# run one iteration for a greedy arborescence and then round robin
def BalanceLater(g):
reset_arb_attribute(g)
gg = g.to_directed()
......@@ -569,6 +591,7 @@ def BalanceLater(g):
round_robin(gg, swap=True)
for (u, v) in gg.edges():
g[u][v]['arb'] = gg[u][v]['arb']
return get_arborescence_list(g)
# associate a greedy arborescence decomposition with g and then swap edges
......@@ -576,27 +599,24 @@ def BalanceLater(g):
def OptimizeGreedyStretch(g):
Trees(g)
greedy_swap(g, stretchi=True)
return get_arborescence_list(g)
# associate a greedy arborescence decomposition with g and then swap edges
# greedily to optimize Depth
def OptimizeGreedyDepth(g):
Trees(g)
greedy_swap(g, stretchi=False)
return get_arborescence_list(g)
# associate a greedy arborescence decomposition with g and then swap edges
# trying to find the best swap to optimize stretch
def BestSwap(g):
Trees(g)
best_swap(g)
return get_arborescence_list(g)
# Helper class (some algorithms work with Network, others without),
# methods as above
class Network:
# initialize variables
def __init__(self, g, K, root):
......@@ -716,8 +736,6 @@ 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)
for i in range(n.K):
......@@ -730,8 +748,6 @@ def prepareDS(n, h, dist):
n.arbs[i].add_node(n.root)
# try to swap an edge on arborescence index for network with heap h
def trySwap(n, h, index):
ni = list(n.nodes_index(index))
for v1 in ni:
......@@ -756,8 +772,6 @@ def trySwap(n, h, index):
return False
# add a new items to the heap
def update_heap(n, h, index):
new = []
for (d, e) in list(h[index]):
......@@ -767,8 +781,6 @@ def update_heap(n, h, index):
h[index] = new
# add neighbors to heap
def add_neighbors_heap(n, h, nodes):
n.build_arbs()
for index in range(n.K):
......@@ -791,8 +803,6 @@ def add_neighbors_heap_index(n, h, index, nodes):
heappush(h[index], (stretch, (x, v)))
# Round robin version without testing for cuts and swaps
def RR(g):
return (round_robin(g, cut=False, swap=False))
......@@ -814,9 +824,7 @@ def RR_swap(g):
def RR_con_swap(g):
return (round_robin(g, cut=True, swap=True))
# basic round robin implementation of constructing arborences
# basic round robin implementation of constructing arborescences
def round_robin(g, cut=False, swap=False):
global swappy
reset_arb_attribute(g)
......@@ -843,7 +851,7 @@ def round_robin(g, cut=False, swap=False):
#drawArborescences(g, "balanced")
# sys.stdout.flush()
# plt.show()
return
return -1
(d, e) = heappop(h[index])
while e != None and n.g[e[0]][e[1]]['arb'] > -1: # in used_edges:
if len(h[index]) == 0:
......@@ -860,7 +868,7 @@ def round_robin(g, cut=False, swap=False):
#drawArborescences(g, "balanced")
# sys.stdout.flush()
# plt.show()
return
return -1
else:
(d, e) = heappop(h[index])
ni = n.nodes_index(index)
......@@ -876,4 +884,5 @@ def round_robin(g, cut=False, swap=False):
add_neighbors_heap_index(n, h, index, [e[0]])
index = (index + 1) % K
swappy.append(swaps)
g = n.g
\ No newline at end of file
g = n.g
return get_arborescence_list(g)
\ No newline at end of file
......@@ -82,7 +82,7 @@ def one_experiment(g, seed, out, algo):
# seed is used for pseudorandom number generation in this run
# rep denotes the number of repetitions in the shuffle for loop
def run_AS(out=None, seed=0, rep=5):
for i in range(4, 8):
for i in range(4, 5):
generate_trimmed_AS(i)
files = glob.glob('./benchmark_graphs/AS*.csv')
original_params = [n, rep, k, samplesize, f_num, seed, name]
......
......@@ -133,11 +133,11 @@ def experiment_objective_subset(obj_func, method, objstr=None, seed=11, gml=Fals
before = obj_func(g)
T1 = get_arborescence_list(g)
t_swap = time.time()
greedy_swap_minobj(g, obj_func)
count = greedy_swap_obj(g, obj_func)
t_swap = time.time() - t_swap
outtime.write("%i, %.6f, %.6f\n" % (n, t_arb, t_swap))
after = obj_func(g)
print(objstr, j, before, after)
print("objective",objstr, "repetition",j, "before", before, "after", after, "t_swap",t_swap, "number of swaps", count)
if before < after:
print('has not been optimized, line 119')
sys.exit()
......@@ -202,8 +202,12 @@ def experiment_objective(obj_func, method, objstr=None, seed=1):
outstretch = open(filename, 'a')
outstretch.write(
"#n= %d, connectivity= %d, repetitions= %d\n" % (n, k, rep))
outstretch.write(
"#graph, before/after, intensity, 'objective', success rate, switches, max load, mean load, max stretch, mean stretch, max hops, mean hops\n")
if "independent" in objstr:
outstretch.write(
"graph type, before, objective, after, objective\n")
else:
outstretch.write(
"#graph, before/after, intensity, 'objective', success rate, switches, max load, mean load, max stretch, mean stretch, max hops, mean hops\n")
stat = Statistic(RouteDetCirc, "DetCirc")
failure_range = [int(n / 10 * i) for i in range(1, 5 * k)]
data = {i: {'before': {'succ': [], 'hops': []}, 'after': {
......@@ -214,10 +218,16 @@ def experiment_objective(obj_func, method, objstr=None, seed=1):
if num_complete_nodes(g) == n:
before = obj_func(g)
T1 = get_arborescence_list(g)
greedy_swap_minobj(g, obj_func)
if "independent" in objstr:
greedy_swap_obj(g, obj_func, max=True)
else:
greedy_swap_obj(g, obj_func)
after = obj_func(g)
T2 = get_arborescence_list(g)
print(j, before, after)
print(j, before, after, obj_func)
if "independent" in objstr:
outstretch.write("regular, before, %d, after, %d\n" % (before, after))
continue
for f in failure_range:
stat.reset(g.nodes())
# , fails=fails) #replace True by False to use fails
......@@ -250,20 +260,23 @@ def experiment_objective(obj_func, method, objstr=None, seed=1):
print(objstr, j, before, after)
sys.stdout.flush()
outstretch.flush()
for f in failure_range:
brs = np.mean(data[f]['before']['succ'])
bsh = np.mean(data[f]['before']['hops'])
ars = np.mean(data[f]['after']['succ'])
arh = np.mean(data[f]['after']['hops'])
print('%d failures, avg before success hops, after success hops %.2f, %.2f, %.2f, %.2f' % (
f, brs, bsh, ars, arh))
brs = np.min(data[f]['before']['succ'])
bsh = np.min(data[f]['before']['hops'])
ars = np.min(data[f]['after']['succ'])
arh = np.min(data[f]['after']['hops'])
print('%d failures, min before success hops, after success hops %.2f, %.2f, %.2f, %.2f' % (
f, brs, bsh, ars, arh))
sys.stdout.flush()
if "independent" not in objstr:
for f in failure_range:
brs = np.mean(data[f]['before']['succ'])
bsh = np.mean(data[f]['before']['hops'])
ars = np.mean(data[f]['after']['succ'])
arh = np.mean(data[f]['after']['hops'])
print('%d failures, avg before success hops, after success hops %.2f, %.2f, %.2f, %.2f' % (
f, brs, bsh, ars, arh))
brs = np.min(data[f]['before']['succ'])
bsh = np.min(data[f]['before']['hops'])
ars = np.min(data[f]['after']['succ'])
arh = np.min(data[f]['after']['hops'])
print('%d failures, min before success hops, after success hops %.2f, %.2f, %.2f, %.2f' % (
f, brs, bsh, ars, arh))
sys.stdout.flush()
outstretch.close()
......@@ -305,8 +318,8 @@ def experiment_SRLG(method, name, seed=11):
index = g[u][v]['arb']
if index in range(k - 2) and v != g.graph['root']:
for vv in g[u]:
if vv != g.graph['root'] and (u, vv) not in SRLG and (vv, u) not in SRLG and g[u][vv][
'arb'] in [k - 1, k - 2]:
if vv != g.graph['root'] and (u, vv) not in SRLG and (vv, u) not in SRLG \
and g[u][vv]['arb'] in [k - 1, k - 2]:
swap(g, u, v, u, vv)
after = count_SRLG(g, k, SRLG)
......@@ -517,7 +530,7 @@ def read_zoo(j, min_connectivity):
if len(zoo_list) <= j:
return None
g1 = nx.Graph(nx.read_graphml(zoo_list[j]))
g2 = nx.convert_node_labels_to_integers(g1)
g2 = nx.convert_node_labels_to_integers(g1).to_directed()
# print(nx.edge_connectivity(g2),',', len(g2.nodes))
n_before = len(g2.nodes)
degree = 3
......@@ -529,8 +542,8 @@ def read_zoo(j, min_connectivity):
degree += 1
if len(g2.nodes) < 10:
return None
# print(zoo_list[j],n_before, len(g2.nodes), len(g2.edges), nx.edge_connectivity(g2),degree)
g = g2.to_directed()
#print(zoo_list[j],n_before, len(g.nodes), len(g.edges), nx.edge_connectivity(g2),degree)
g = nx.convert_node_labels_to_integers(g)
for (u, v) in g.edges():
g[u][v]['arb'] = -1
......
......@@ -25,7 +25,7 @@ def run_experiments():
k = nx.edge_connectivity(g)
n = len(g.nodes())
m = len(g.edges())
print('nodes, edges, connectivity', n, m, k)
#print('nodes, edges, connectivity', n, m, k)
samplesize = min(int(n/2), samplesize)
f_num = min(int(m/4), f_num)
set_parameters([n, rep, k, samplesize, f_num, seed, "zoo-srds2019-"])
......@@ -44,8 +44,7 @@ def run_experiments():
experiment_objective_subset(measure_load, method, str(
f_num)+"_load_for_subset_"+name, seed=seed)
if switch in ['independent', 'all']:
experiment_objective(num_independent_paths_in_arbs, method, str(
f_num)+"_independent_paths_"+name, seed=seed)
experiment_objective(num_independent_paths_in_arbs, method, "independent_paths_"+name, seed=seed)
if switch in ['SRLG', 'all']:
experiment_SRLG(method, str(f_num)+"_"+name, seed=seed)
experiment_SRLG_node_failures(
......
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