Esempio n. 1
# robust measurement by schneider
from robust_graph import s, R
# log level: what kind of information are to be printed
# e.g. WARNING: print warning and error information
from robust_graph import LOG_LEVEL_QUIET as LOG_LEVEL
# 4 optimizers based on 4 different algorithms
from robust_graph import Schneider, WuHolme, Sun, IchinoseSatotani
# measures certain index through optimization process
from robust_graph import TimeTracker
# function to load united states airline network
# and make powerlaw-degree-distribution network
from robust_graph import load_us, scale_free_network

# initialize graph object and algorithm object
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 -----')
    'Schneider' : (Schneider,        LOG_LEVEL, True,
                                  dict(max_trials=100), 100),

    'Holme'     : (WuHolme,          LOG_LEVEL, True,
                                  dict(),                1),

    'Ichinose'  : (IchinoseSatotani, LOG_LEVEL, False,
                                  dict(greedy=True),    100)

result_s = dict(zip(optimizers.keys(),

for _ in range(n_graphs):
    # build graph and optimize with each algorithm
    G = scale_free_network(n_nodes)

    for name, params in optimizers.items():
        opt_cls, log_level, force_update, config, steps = params
        optimizer = opt_cls(G, log_level, force_update, **config)
        optimized = optimizer.optimize(steps)
        # the following three lines do:
        #    s[name] += s(optimized) * n_attacks
        result_s[name] = map(sum,
                map(lambda e: e*n_attacks, s(optimized, n=n_attacks))))

# convert result_s sum of s(q) -> average of s(q)
for k, v in result_s.items():
    result_s[k] = map(lambda s: s / (n_graphs*n_attacks), v)