def simulate_and_dump_result(algorithm, graph_filename, out_filename): # parsing valid algorithm if algorithm == 'sun': opt_class = Sun config = dict(log_level=LOG_LEVEL, force_update=True, # update graph even if swap is not done max_trials=1000, # and continue even if swap is done Nt=6, Ng=10) n_steps = 30 elif algorithm == 'ichinose': opt_class = IchinoseSatotani config = dict(log_level=LOG_LEVEL, force_update=False) # does not accept if swap is not done n_steps = 500 elif algorithm == 'ichinose_greedy': opt_class = IchinoseSatotani config = dict(log_level=LOG_LEVEL, greedy=True, # greedy swapping force_update=False) # same as above n_steps = 500 else: raise ValueError('invalid algorithm: ', algorithm) # number of attacks (to calculate R) n_attacks = 10 # load graph graph = nx.read_gpickle(graph_filename) # define columns in csv columns = [ ('R', lambda G: R(G, n_attacks)), ('r', nx.degree_pearson_correlation_coefficient) ] # use TimeTracker to track R and r tracker = TimeTracker(opt_class, graph, columns, **config) tracker.track(steps=n_steps) tracker.dump(out_filename) # in this time, algorithm$i.ba.csv
G1 = scale_free_network() R1 = R(G1, n=10) # calculate 10 times to get more accurate result # use of optimizer print('----- optimizer test -----') print('original R =', R1) for opt_cls in [Schneider, WuHolme, IchinoseSatotani]: # still there are more options # see robust_graph/optimize document optimizer = opt_cls(G1, log_level=LOG_LEVEL, max_trials=10) G2 = optimizer.optimize(steps=100) R2 = R(G2, n=10) print('optimized by', opt_cls.__name__, 'R =', R2) # use of tracker print('----- time tracker test -----') # measurements used in timetracker cols = [('R', lambda G: R(G, n=10)), # R robustness ('r', nx.degree_pearson_correlation_coefficient), # r degree correlation ] # WuHolme is not iterative algorithm for opt_cls in [Schneider, IchinoseSatotani]: # options are given after columns(measurements) tracker = TimeTracker(opt_cls, G1, cols=cols, log_level=LOG_LEVEL, max_trials=5) tracker.track(steps=2) print('tracked by', opt_cls.__name__, 'algorithm...') print(tracker.dumps()) # tracker.dump('test.csv') # also, tracker can dump to file
import networkx as nx from robust_graph import R from robust_graph import LOG_LEVEL_INFO as LOG_LEVEL from robust_graph import TimeTracker # an algorithm from robust_graph import IchinoseSatotani # function to load US airline network from robust_graph import load_us # some important constants n_attacks = 10 n_steps = 100 filename = 'Randr.csv' graph = load_us() # define columns in csv columns = [ # R robustness ('R', lambda G: R(G, n_attacks)), # r degree correlation ('r', nx.degree_pearson_correlation_coefficient) ] # use TimeTracker to track R and r tracker = TimeTracker(IchinoseSatotani, graph, columns, log_level=LOG_LEVEL, force_update=False, greedy=True) tracker.track(steps=n_steps) tracker.dump(filename)
from robust_graph import R from robust_graph import LOG_LEVEL_INFO as LOG_LEVEL from robust_graph import TimeTracker # compare two algorithms from robust_graph import Schneider, IchinoseSatotani # function to load US airline network from robust_graph.util import load_us # some important constants n_attacks = 10 n_steps = 100 fileprefix = 'speed_test' graph = load_us() # define columns in csv columns = [ # R robustness ('R', lambda G: R(G, n_attacks)) ] for params in [(Schneider, dict(log_level=LOG_LEVEL, force_update=True)), (IchinoseSatotani, dict(log_level=LOG_LEVEL, force_update=False, greedy=True))]: opt_cls, config = params tracker = TimeTracker(opt_cls, graph, columns,**config) tracker.track(steps=n_steps) tracker.dump(fileprefix + '.' + opt_cls.__name__ + '.csv')
# time tracker to track simulation from robust_graph import TimeTracker # function to load US airline network from robust_graph import load_us # some important constants n_attacks = 10 n_steps = 30 filename = sys.argv[1] # load graph graph = load_us() #graph = nx.barabasi_albert_graph(20, 3) # define columns in csv columns = [ ('R', lambda G: R(G, n_attacks)), ('r', nx.degree_pearson_correlation_coefficient) ] # use TimeTracker to track R and r tracker = TimeTracker(Sun, graph, columns, # configurations: see robust_graph.optimize.Sun log_level=LOG_LEVEL, force_update=True, max_trials=1000, Nt=6, Ng=10) tracker.track(steps=n_steps) tracker.dump(filename) # in this time, tabu$i.us.csv