-
Notifications
You must be signed in to change notification settings - Fork 4
/
plot_complete.py
159 lines (125 loc) · 6.42 KB
/
plot_complete.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# -*- coding: utf-8 -*-
# %%
import matplotlib.pyplot as plt
import numpy as np
import seaborn
# %% SETTINGS
dfacts_choise = 0
col_choice = 0
ratio_choice = 0
name = 'case6'
suffix = f'choice_{dfacts_choise}_ratio_{ratio_choice}_column_{col_choice}'
BDD_threshold = 36.415
rc = {"font.family" : "serif",
"mathtext.fontset" : "stix",
}
plt.rcParams.update(rc)
plt.rcParams["font.serif"] = ["Times New Roman"] + plt.rcParams["font.serif"]
font = {'size' : 25}
plt.rc('font', **font)
attack_strength_under_test = [5,7,10,15,20]
x_axis_name = [str(i) for i in attack_strength_under_test]
# %% LOAD DATA
WORST_ROBUST = np.load(f'simulation_data/{name}/dc/{suffix}_WORST_ROBUST.npy', allow_pickle = True)
WORST_RANDOM = np.load(f'simulation_data/{name}/dc/{suffix}_WORST_RANDOM.npy', allow_pickle = True)
RANDOM_MAX = np.load(f'simulation_data/{name}/dc/{suffix}_RANDOM_MAX.npy', allow_pickle = True)
RANDOM_ROBUST = np.load(f'simulation_data/{name}/dc/{suffix}_RANDOM_ROBUST.npy', allow_pickle = True)
RANDOM_RANDOM = np.load(f'simulation_data/{name}/dc/{suffix}_RANDOM_RANDOM.npy', allow_pickle = True)
SINGLE_ROBUST = np.load(f'simulation_data/{name}/dc/{suffix}_SINGLE_ROBUST.npy', allow_pickle = True)
TIME_MAX = np.load(f'simulation_data/{name}/dc/{suffix}_TIME_MAX.npy', allow_pickle = True)
TIME_ROBUST = np.load(f'simulation_data/{name}/dc/{suffix}_TIME_ROBUST.npy', allow_pickle = True)
RANDOM_ROBUST_AC = np.load(f'simulation_data/{name}/ac/{suffix}_TP_RANDOM_ROBUST.npy', allow_pickle = True)
RANDOM_RANDOM_AC = np.load(f'simulation_data/{name}/ac/{suffix}_TP_RANDOM_RANDOM.npy', allow_pickle = True)
RESIDUAL = np.load(f'simulation_data/{name}/ac/{suffix}_RESIDUAL_ATTACKER_ROBUST.npy', allow_pickle = True)
COST_PRE = np.load(f'simulation_data/{name}/ac/{suffix}_COST_pre.npy', allow_pickle = True)
COST_AFTER = np.load(f'simulation_data/{name}/ac/{suffix}_COST_after.npy', allow_pickle = True)
# residual
RESIDUAL_ROBUST = np.load(f'simulation_data/{name}/ac/{suffix}_RESIDUAL_ROBUST.npy', allow_pickle = True)
RESIDUAL_RANDOM = np.load(f'simulation_data/{name}/ac/{suffix}_RESIDUAL_RANDOM.npy', allow_pickle = True)
RESIDUAL_BDD = np.load(f'simulation_data/{name}/ac/{suffix}_RESIDUAL_BDD.npy', allow_pickle = True)
WORST_RANDOM_MEAN = np.mean(WORST_RANDOM,1)
# %% dc
fig = plt.figure(figsize=(8, 6))
width = 0.1
plt.plot(WORST_ROBUST, color = plt.cm.tab10(0), linestyle = '-', marker = '^', lw = 2)
plt.plot(WORST_RANDOM_MEAN, color = plt.cm.tab10(1), linestyle = '-', marker = 'o', lw = 2)
for i in range(len(WORST_RANDOM)):
plt.scatter(x = i*np.ones(len(WORST_RANDOM[0,:500])) + (np.random.rand(len(WORST_RANDOM[0,:500]))*width-width/2.),
y = WORST_RANDOM[i,:500], color = plt.cm.tab10(2), s = 35, marker = "x")
plt.xticks(range(len(attack_strength_under_test)), x_axis_name)
plt.xlabel(r"Attack strength $\rho$")
plt.ylabel("Attack detection probability")
plt.grid()
plt.legend([r'Robust MTD',"Max-Rank MTD (Mean)", "Max-Rank MTD"], labelspacing=0.1, loc = 2)
plt.ylim([0,1.1])
plt.show()
fig.savefig(f"figure/{name}/{name}_dc_worst.pdf", bbox_inches="tight", dpi=400)
fig = plt.figure(figsize=(8, 6))
plt.plot(RANDOM_MAX, label = 'MAX MTD', color = plt.cm.tab10(1), linestyle = '-', marker = 'o', lw = 2)
plt.plot(RANDOM_ROBUST, label = 'ROBUST MTD', color = plt.cm.tab10(0), linestyle = '-', marker = '^', lw = 2)
plt.plot(RANDOM_RANDOM, label = 'Max-Rank MTD', color = plt.cm.tab10(2), linestyle = '-', marker = '*', lw = 2)
plt.xlabel(r"Attack strength $\rho$")
plt.ylabel("Attack detection probability")
plt.xticks(range(len(attack_strength_under_test)), x_axis_name)
plt.grid()
plt.legend(['Max MTD', 'Robust MTD', "Max-Rank MTD"], labelspacing=0.1, loc = 2)
plt.ylim([0,1.1])
plt.show()
fig.savefig(f"figure/{name}/{name}_dc_random.pdf", bbox_inches="tight", dpi=400)
print(f'TIME_ROBUST: {np.mean(TIME_ROBUST)}')
print(f'TIME_MAX: {np.mean(TIME_MAX)}')
# %% single state attack
width = 0.2
x = np.arange(6-1)
dfacts_choise = 0
col_choice = 0
ratio_choice = 0
suffix = f'choice_{dfacts_choise}_ratio_{ratio_choice}_column_{col_choice}'
SINGLE_ROBUST_ = np.load(f'simulation_data/{name}/dc/{suffix}_SINGLE_ROBUST.npy', allow_pickle = True)
fig = plt.figure(figsize=(8, 6))
plt.bar(x - width/2, SINGLE_ROBUST[2], width, label = 'with Principle 2')
plt.bar(x + width/2, SINGLE_ROBUST_[2], width, label = 'without Principle 2')
plt.xticks(range(6-1), np.arange(2,6+1))
plt.xlabel("Attack Position/Bus")
plt.ylabel("Attack detection probability")
plt.grid()
plt.ylim([0,1.1])
plt.legend(loc = 6, labelspacing=0.1)
plt.show()
fig.savefig(f"figure/{name}/{name}_single.pdf", bbox_inches="tight", dpi=400)
# %% AC
fig = plt.figure(figsize=(8, 6))
plt.plot(RANDOM_ROBUST_AC[:-1], color = plt.cm.tab10(0), linestyle = '-', marker = '^', lw = 2)
plt.plot(RANDOM_RANDOM_AC[:-1], color = plt.cm.tab10(2), linestyle = '-', marker = '*', lw = 2)
plt.xlabel(r"Attack strength $\rho$")
plt.ylabel("Attack detection probability")
plt.grid()
plt.legend(['Robust MTD', "Max-Rank MTD"], labelspacing=0.1, loc = 2)
plt.ylim([0,1.1])
plt.xticks(range(len(attack_strength_under_test)), x_axis_name)
plt.plot()
fig.savefig(f"figure/{name}/{name}_ac_random.pdf", bbox_inches="tight", dpi=400)
print(f'Attacker residual: {RESIDUAL}')
print(f'cost change: {(COST_AFTER - COST_PRE)/COST_PRE}')
# %%
kwargs = {'cumulative': True, 'linewidth' : 2}
for i in range(len(RESIDUAL_ROBUST)-1):
fig = plt.figure(figsize=(8, 6))
seaborn.distplot(RESIDUAL_BDD[i], kde = True, hist = False, kde_kws=kwargs, color = plt.cm.tab10(1))
seaborn.distplot(RESIDUAL_ROBUST[i], kde = True, hist = False, kde_kws=kwargs, color = plt.cm.tab10(0))
seaborn.distplot(RESIDUAL_RANDOM[i], kde = True, hist = False, kde_kws=kwargs, color = plt.cm.tab10(2))
plt.vlines(x = BDD_threshold, ymin = 0, ymax = 1, colors = 'r', linestyle = 'dashed')
plt.xlabel(r"Residual $\gamma$")
plt.ylabel("c.d.f")
plt.grid()
plt.legend(['No MTD', 'Robust MTD', "Max-Rank MTD", 'BDD Threshold'], labelspacing=0.1, loc = 4)
plt.ylim([0,1.])
plt.xlim([0,175])
plt.show()
fig.savefig(f"figure/{name}/{name}_ac_residual_{i}.pdf", bbox_inches="tight", dpi=400)
# %% state change
MAG_CHANGE = np.load(f'simulation_data/{name}/ac/{suffix}_MAG_CHANGE.npy', allow_pickle = True)
ANG_CHANGE = np.load(f'simulation_data/{name}/ac/{suffix}_ANG_CHANGE.npy', allow_pickle = True)
print(f'MAG_CHANGE_MEAN: {np.mean(np.abs(MAG_CHANGE))}')
print(f'ANG_CHANGE_MEAN: {np.mean(np.abs(np.nan_to_num(ANG_CHANGE, nan=0)))}')
# %%