def launch_tests(): for name in networks: network = network_from_file("graphs//" + name)[0] for b in examples.botnets(network, 0.9, nb_trials): tests.hyper_parameter_influence(b, network, nb_trials, hyper_parameter, values, redundancy)
def launch_tests(): for name in networks: print(name) network = network_from_file("graphs//" + name)[0] results = [] for b in examples.botnets(network, 0.9, nb_trials): print(b.type) real = [] exp = [] policy = [] action = [] perf = [] time = [] for i in range(redundancy): print("Trial ", i) real_rewards, expected_rewards, policy_rewards = tests.train( b, network, nb_trials) real.append(real_rewards) exp.append(expected_rewards) policy.append(policy_rewards) action.append(b.compute_policy()) p = Policy(network, b.compute_policy()) perf.append(p.expected_reward(b.gamma)) time.append(p.expected_time()) b.clear(all=True) results.append((b.type, real, exp, policy, action, perf, time)) tests.dump_actions("botnet_Compare", name, "all", results, nb_trials)
def show_results(nb): window_size = int(nb / 20) for name in networks: res = tests.load_file("results/botnet_Compare_%s_all_%d.out" % (name, nb)) network = network_from_file("graphs//" + name)[0] real_res = [] exp_res = [] pol_res = [] final_perf = [] for x in res: (b, real_rewards, expected_rewards, policy_rewards, actions, perf, time) = x real_rewards = np.array(real_rewards) expected_rewards = np.array(expected_rewards) policy_rewards = np.array(policy_rewards) perf = np.array(perf) time = np.array(time) real_rewards = np.mean(real_rewards, axis=0) expected_rewards = np.mean(expected_rewards, axis=0) policy_rewards = np.mean(policy_rewards, axis=0) real_res.append((b, real_rewards)) exp_res.append((b, expected_rewards)) pol_res.append((b, policy_rewards)) final_perf.append((b, np.mean(perf), np.max(perf), np.mean(time), np.min(time), actions)) subplot(2, 2, 1) for (name, perf) in real_res: tests.plot_perf(perf, window_size, name) ylabel("Real rewards") #f = figure(2) axis(sharex=True) subplot(2, 2, 3) for (name, perf) in exp_res: tests.plot_perf(perf, window_size, name) ylabel("Expected rewards") #f = figure(3) axis(sharex=True) subplot(1, 2, 2) for (name, perf) in pol_res: tests.plot_perf(perf, window_size, name) ylabel("Policy rewards") tests.show_with_legend(f) for (b_name, mean_p, max_p, mean_t, min_t, actions) in final_perf: print(b_name, "Mean perf:", mean_p, "Max perf:", max_p, "Mean time:", mean_t, "Min time:", min_t, "Actions:", actions, sep='\t') x = input("Enter something to exit :")
def show_results(): for name in networks: network = network_from_file("graphs//" + name)[0] b = "ThompsonSampling" [actions, times, rewards, values] = tests.load_file( "results/beta_%d_%s_%d.out" % (network.size, b, nb_trials)) tests.plot_with_legend(values, times, "Time") tests.plot_with_legend(values, rewards, "Reward") tests.show_with_legend()
def show_results(): botnets_names = [ "RewardTentative", "Sarsa", "QLearning", "QLearning - Potential", "ThompsonSampling", "ModelBasedThompson", "FullModelBasedThompson" ] for name in networks: network = network_from_file("graphs//" + name)[0] print(name) for b in botnets_names: print(b) [actions, times, rewards, values] = tests.load_file( "results/gamma_%d_%s_%d.out" % (network.size, b, nb_trials)) tests.plot_with_legend(values, times, b) tests.show_with_legend()
def show_results(): for name in networks: network = network_from_file("graphs//" + name)[0] b = FullModelBasedThompson(thompson_standard, network.graph, gamma=0.9, nb_trials=nb_trials, initial_nodes=network.initial_nodes) [actions, times, rewards, values] = tests.load_file( "results/alpha_p_%d_%s_%d.out" % (network.size, b.type, nb_trials)) values = [math.log(v, 10) for v in values] tests.plot_with_legend(values, times, "Time") tests.plot_with_legend(values, rewards, "Reward") tests.show_with_legend()
def launch_tests(): for name in networks: network = network_from_file("graphs//" + name)[0] botnet = Thompson(thompson_standard, network.graph, gamma=0.9, nb_trials=nb_trials, initial_nodes=network.initial_nodes) tests.hyper_parameter_influence(botnet, network, nb_trials, hyper_parameter, values, redundancy, is_log=False)
def show_results(): botnets_names = [ "Sarsa", "QLearning", "QLearning - Potential", "ThompsonSampling" ] for name in networks: network = network_from_file("graphs//" + name)[0] print(name) for b in botnets_names: print(b) [actions, times, rewards, values] = tests.load_file( "results/alpha_%d_%s_%d.out" % (network.size, b, nb_trials)) values = [math.log(v, 10) for v in values] tests.plot_with_legend(values, times, "Time") tests.plot_with_legend(values, rewards, "Reward") tests.show_with_legend()
def test(file_name): net = network.network_from_file(file_name) new_game = game.Game() score = new_game.play(net) print("Evaluated score: {0}".format(score))
import strategy import thompson_sampling as thom import time from qlearning import QLearning #n = 16 #difficulty = 2 ## ##net = network.Network(1) ## ##for i in range(n): ## net.add_node(i**1.5, i, i) ## ##net.generate_random_connected() #net = network.random_network(n, difficulty, big_nodes=log(n)/float(n), complete=False) net, _ = network.network_from_file("./graphs/W08atk.gr") q = thom.Thompson(strategy.thompson_standard, net.graph, 0.9, 0.01) #q = QLearning(strategy.full_exploration, net.graph, nb_trials=200) tests.train(q, net, 100) q.clear() l = tests.sample_optimal(q, net) net.viz.layout() #fig = plt.figure() #ax = fig.add_subplot(111) #plt.ion() #plt.show() #