Commit d78751ba authored by yvonneanne's avatar yvonneanne

rename and improve

parent 85b38912
......@@ -559,7 +559,7 @@ def FindTreeNoTestCut(g, k):
return T
# associate a greedy arborescence decomposition with g
def Trees(g):
def GreedyArborescenceDecomposition(g):
reset_arb_attribute(g)
gg = g.to_directed()
K = g.graph['k']
......@@ -597,21 +597,21 @@ def BalanceLater(g):
# associate a greedy arborescence decomposition with g and then swap edges
# greedily to optimize stretch
def OptimizeGreedyStretch(g):
Trees(g)
GreedyArborescenceDecomposition(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)
GreedyArborescenceDecomposition(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)
GreedyArborescenceDecomposition(g)
best_swap(g)
return get_arborescence_list(g)
......@@ -887,39 +887,4 @@ def round_robin(g, cut=False, swap=False, reset=True):
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
......@@ -30,8 +30,8 @@ DEBUG = False
# 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], 'Adhoc': [AdHocExtraLinks, RouteDetCirc]}
algos = {'Greedy': [Trees, RouteDetCirc], 'Adhoc': [AdHocExtraLinks, RouteDetCirc]}
#algos = {'Bonsai': [RR_swap, RouteDetCirc], 'Greedy': [GreedyArborescenceDecomposition, RouteDetCirc], 'Adhoc': [AdHocExtraLinks, RouteDetCirc]}
algos = {'Greedy': [GreedyArborescenceDecomposition, RouteDetCirc], 'Adhoc': [AdHocExtraLinks, RouteDetCirc]}
# run one experiment with graph g
......@@ -113,6 +113,8 @@ def run_AS(out=None, seed=0, rep=5):
def run_zoo(out=None, seed=0, rep=5):
min_connectivity = 4
original_params = [n, rep, k, samplesize, f_num, seed, name]
if DEBUG:
print('n_before, n_after, m_after, connectivity, degree')
for i in range(261):
random.seed(seed)
g = read_zoo(i, min_connectivity)
......@@ -124,9 +126,13 @@ def run_zoo(out=None, seed=0, rep=5):
ss = min(int(nn / 2), samplesize)
fn = min(int(mm / 4), f_num)
set_parameters([nn, rep, kk, ss, fn, seed, name + "zoo-"])
#print('parameters', nn, rep, kk, ss, fn, seed)
shuffle_and_run(g, out, seed, rep, str(i))
set_parameters(original_params)
#return #TODO remove this line
for (algoname, algo) in algos.items():
index_1 = len(algo) - rep
index_2 = len(algo)
print('intermediate result: %s \t %.5E' % (algoname, np.mean(algo[index_1:index_2])))
# shuffle root nodes and run algorithm
def shuffle_and_run(g, out, seed, rep, x):
......
......@@ -14,7 +14,7 @@ import glob
def run_AS(outstretch=None, outtime=None, seed=0, rep=5):
global swappy
astr = ['RR-swap'] # , 'Greedy', 'Random']
algos = {'RR-swap': RR_swap, 'Greedy': Trees, 'Random': RandomTrees}
algos = {'RR-swap': RR_swap, 'Greedy': GreedyArborescenceDecomposition, 'Random': RandomTrees}
swappy = [0]
files = glob.glob('./benchmark_graphs/AS*.csv')
for x in files:
......@@ -97,7 +97,7 @@ def run_regular(k=4, n=50, rep=100, outstretch=None, outtime=None, seed=0):
edge_labels[-1] = {}
astr = ['RR', 'RR-con', 'RR-swap', 'RR-swap-con', 'Greedy', 'random']
astr = ['RR', 'RR-swap', 'Greedy', 'random']
algos = {'Later': BalanceLater, 'RR': RR, 'RR-con': RR_con, 'RR-swap': RR_swap, 'RR-swap-con': RR_con_swap, 'Greedy': Trees,
algos = {'Later': BalanceLater, 'RR': RR, 'RR-con': RR_con, 'RR-swap': RR_swap, 'RR-swap-con': RR_con_swap, 'Greedy': GreedyArborescenceDecomposition,
'Greedy-swap-stretch': OptimizeGreedyStretch, 'Greedy-swap-depth': OptimizeGreedyDepth, 'bestSw': BestSwap, 'random': RandomTrees}
data = {v: {'complete': 0, 'stretch': [0 for i in range(rep)], 'depth': [0 for i in range(
rep)], 'time': [0.0 for i in range(rep)]} for (v, k) in algos.items()}
......
......@@ -12,7 +12,7 @@ from objective_function_experiments import *
def infocom_experiments(switch):
write_graphs()
print('generated graphs')
for (method, name) in [(RandomTrees, 'Random'), (Trees, 'Greedy')]:
for (method, name) in [(RandomTrees, 'Random'), (GreedyArborescenceDecomposition, 'Greedy')]:
if switch in ['subset', 'all']:
experiment_objective_subset(
measure_stretch, method, "stretch_for_subset_"+name)
......
......@@ -481,7 +481,7 @@ def write_graphs():
g.graph['seed'] = 0
g.graph['k'] = k
g.graph['root'] = 0
Trees(g)
GreedyArborescenceDecomposition(g)
d.append(depth(g))
ecc.append(nx.eccentricity(g, 0))
sp.append(nx.average_shortest_path_length(g))
......@@ -533,7 +533,7 @@ def read_zoo(j, min_connectivity):
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
degree = min(3, min_connectivity)
while nx.edge_connectivity(g2) < min_connectivity:
g2 = trim2(g2, degree)
if len(g2.nodes) == 0:
......@@ -543,7 +543,7 @@ def read_zoo(j, min_connectivity):
if len(g2.nodes) < 10:
return None
g = g2.to_directed()
#print(zoo_list[j],n_before, len(g.nodes), len(g.edges), nx.edge_connectivity(g2),degree)
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
......
......@@ -5,6 +5,7 @@ import itertools
import random
import time
from arborescences import *
from extra_links import *
import glob
#global variables in this file
......@@ -347,7 +348,7 @@ def SimulateGraph(g, RANDOM, stats, f, samplesize, precomputation=None, dest=Non
if precomputation is None:
precomputation = tree
if precomputation is None:
precomputation = Trees(g)
precomputation = GreedyArborescenceDecomposition(g)
if precomputation is None:
return -1
if RANDOM:
......
......@@ -10,7 +10,7 @@ def run_experiments():
global name, n, rep, k, samplesize, f_num, seed
write_graphs()
print('generated graphs')
for (method, name) in [(Trees, 'Greedy'), (RR_swap, 'RR-swap')]:
for (method, name) in [(GreedyArborescenceDecomposition, 'Greedy'), (RR_swap, 'RR-swap')]:
if switch in ['zoo', 'all']:
original_params = [n, rep, k, samplesize, f_num, seed, "srds2019-"]
zoo_count = 0
......
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