-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_ql.py
170 lines (132 loc) · 4.45 KB
/
main_ql.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
160
161
162
163
164
165
166
167
168
from cProfile import label
import random
from qlearning import *
from EnvRL import EnvRL_v0
import matplotlib.pyplot as plt
def evaluate_QL(step, nids,type):
print('Evalution for Q Learning:')
total_reward = 0
episodes = 100
attack_set = [random.randint(0,3) for i in range(step)]
env = EnvRL_v0(attack_set)
q_table,record,pdf,_,_ = train(step,nids,type)
actions = []
attacks = []
for _ in range(episodes):
state = env.attack_set[env.reset()]
epochs, reward = 0, 0
done = False
action_list = []
attack_list = []
while not done:
nids_success_rate = random.randrange(0, 100)
attack_list.append(env.attack_set[env.current_attack])
if nids_success_rate < nids:
action = np.argmax(q_table[state])
state, reward, done, info = env.step(action)
state = env.attack_set[state]
action_list.append(action)
else:
if env.current_attack+1 == len(env.attack_set)-1:
done = True
env.current_attack+=1
action_list.append(-1)
epochs += 1
actions.append(action_list)
attacks.append(attack_list)
total_reward+=reward
print(f"Results after {episodes} episodes:")
print(f"Average reward per episode: {total_reward / episodes}")
return q_table,record,actions,attacks
def evaluate_random():
print('Evalution for Random:')
total_reward = 0
episodes = 100
attack_set = [randint(0,3) for i in range(500)]
env = EnvRL_v0(attack_set)
actions = []
attacks = []
for _ in range(episodes):
state = env.attack_set[env.reset()]
epochs, reward = 0, 0
done = False
action_list = []
attack_list = []
while not done:
nids_success_rate = random.randrange(0, 100)
attack_list.append(env.attack_set[env.current_attack])
if nids_success_rate < 80:
action = randint(0,3)
action_list.append(action)
state, reward, done, info = env.step(action)
state = env.attack_set[state]
epochs += 1
actions.append(action_list)
attacks.append(attack_list)
total_reward+=reward
print(f"Results after {episodes} episodes:")
print(f"Average reward per episode: {total_reward / episodes}")
return actions,attacks
q_table,r0,act1,atk1 = evaluate_QL(1000,80,0) #1000 is number of steps in one eps, 80 is the nids success rate
q_table,r1,act2,atk2 = evaluate_QL(1000,50,1)
q_table,r2,act3,atk3 = evaluate_QL(1000,50,2)
q_table,r3,act4,atk4 = evaluate_QL(1000,60,3)
evaluate_random()
record = []
#Accuracy
count = 0
total = 0
for i,j in zip(act1,atk1):
for x in range(len(i)):
if i[x]==j[x]:
count+=1
if i[x]!=-1:
total+=1
record.append(count/total)
for i,j in zip(act2,atk2):
for x in range(len(i)):
if i[x]==j[x]:
count+=1
if i[x]!=-1:
total+=1
record.append(count/total)
for i,j in zip(act3,atk3):
for x in range(len(i)):
if i[x]==j[x]:
count+=1
if i[x]!=-1:
total+=1
record.append(count/total)
for i,j in zip(act4,atk4):
for x in range(len(i)):
if i[x]==j[x]:
count+=1
if i[x]!=-1:
total+=1
record.append(count/total)
print('Accuracy for each setting:')
print(record)
# print('Q table:')
# print(q_table)
# filter_length = 50
# avg0 = np.convolve(r0,np.ones((filter_length)),mode = 'same')
# avg0 /= filter_length
# avg1 = np.convolve(r1,np.ones((filter_length)),mode = 'same')
# avg1 /= filter_length
# avg2 = np.convolve(r2,np.ones((filter_length)),mode = 'same')
# avg2 /= filter_length
# avg3 = np.convolve(r3,np.ones((filter_length)),mode = 'same')
# avg3 /= filter_length
x = [i for i in range(len(r0))]
# # plt.plot(x[50:-50],avg0[50:-50],label = 'Epsilon-Vacuity')
# # plt.plot(x[50:-50],avg1[50:-50],label = 'Epsilon-Dissonance')
# # plt.plot(x[50:-50],avg2[50:-50],label = 'Epsilon-Entropy')
# # plt.plot(x[50:-50],avg3[50:-50],label = 'Epsilon-Greedy')
plt.plot(x,r0,label = 'Epsilon-Vacuity')
plt.plot(x,r1,label = 'Epsilon-Dissonance')
plt.plot(x,r2,label = 'Epsilon-Entropy')
plt.plot(x,r3,label = 'Epsilon-Greedy')
plt.xlabel('Number of episodes')
plt.ylabel('Average reward per episode')
plt.legend()
plt.show()