├── README.md ├── trust_value2.py └── trust_value.py /README.md: -------------------------------------------------------------------------------- 1 | # Welcome to GitHub Desktop! 2 | 3 | This is your README. READMEs are where you can communicate what your project is and how to use it. 4 | 5 | Write your name on line 6, save it, and then head back to GitHub Desktop. 6 | -------------------------------------------------------------------------------- /trust_value2.py: -------------------------------------------------------------------------------- 1 | import random 2 | import math 3 | 4 | class Node: 5 | ''' 6 | 定义一个节点类 7 | ''' 8 | R=0 9 | S=0 10 | def __init__(self,behavior,value,h,recomserver): 11 | ''' 12 | 初始化节点属性 13 | :param behavior: 节点行为序号,1为行为1,2为行为2,3为行为3,4为行为4 14 | :param value: 节点信任值 15 | :param h: 节点与服务器历史交互次数 16 | :param recomserver: 推荐服务器数量 17 | ''' 18 | self.behavior=behavior 19 | self.value=value 20 | self.h=h 21 | self.recomserver=recomserver 22 | pass 23 | def behav1(self,f): 24 | ''' 25 | 行为1:良好参与方,在服务器交互过程中保持正常稳定服务 26 | 异常信息acc在0.5-1之间 27 | 时延信息d在0-0.5之间 28 | :return: 29 | ''' 30 | acc=[] 31 | d=[] 32 | sum=0 33 | for i in range(10): 34 | acc.append(random.randint(5000,10000)/10000) 35 | d.append(random.randint(0,5000)/10000) 36 | pass 37 | abnormal_factor=ACC(acc) 38 | delay_factor=calculate_delay_factor(d) 39 | self.R=(1/(1+math.exp(-abnormal_factor)))*delay_factor 40 | for i in range(10): 41 | if i<9: 42 | sum+=abs(acc[i+1]-acc[i]) 43 | else: 44 | break 45 | self.S=sum/9 46 | print('异常信息 acc : {}'.format(acc), file=f) 47 | print('异常因子 abnormal_factor : {}'.format(abnormal_factor), file=f) 48 | print('时延信息 d : {}'.format(d), file=f) 49 | print('时延因子 delay_factor : {}'.format(delay_factor), file=f) 50 | print('行为可靠性 R : {}'.format(self.R), file=f) 51 | print('行为稳定性 S : {}'.format(self.S), file=f) 52 | pass 53 | def behav2(self, f): 54 | ''' 55 | 行为2:恶意参与方,行为一直差 56 | 异常信息acc在0-0.5之间 57 | 时延信息d在0.5-1之间 58 | :return: 59 | ''' 60 | acc=[] 61 | d=[] 62 | sum=0 63 | for i in range(10): 64 | acc.append(random.randint(0,5000)/10000) 65 | d.append(random.randint(5000,10000)/10000) 66 | pass 67 | abnormal_factor = ACC(acc) 68 | delay_factor = calculate_delay_factor(d) 69 | self.R=(1/(1+math.exp(-abnormal_factor)))*delay_factor 70 | for i in range(10): 71 | if i<9: 72 | sum+=abs(acc[i + 1]-acc[i]) 73 | else: 74 | break 75 | self.S=sum/9 76 | print('异常信息 acc : {}'.format(acc), file=f) 77 | print('异常因子 abnormal_factor : {}'.format(abnormal_factor), file=f) 78 | print('时延信息 d : {}'.format(d), file=f) 79 | print('时延因子 delay_factor : {}'.format(delay_factor), file=f) 80 | print('行为可靠性 R : {}'.format(self.R), file=f) 81 | print('行为稳定性 S : {}'.format(self.S), file=f) 82 | pass 83 | def behav3(self): 84 | ''' 85 | 行为3:恶意参与方,行为稳定性差,周期性的交替表现为良好行为和恶意行为 86 | 假设一次异常度信息和时延信息为行为1 87 | 一次异常度信息和时延信息为行为2 88 | :return: 89 | ''' 90 | acc=[] 91 | d=[] 92 | sum=0 93 | for i in range(10): 94 | if i%2==0: 95 | acc.append(random.randint(5000,10000)/10000) 96 | d.append(random.randint(0,5000)/10000) 97 | else: 98 | acc.append(random.randint(0,5000)/10000) 99 | d.append(random.randint(5000,10000)/10000) 100 | abnormal_factor=ACC(acc) 101 | delay_factor=calculate_delay_factor(d) 102 | self.R=(1/(1+math.exp(-abnormal_factor)))*delay_factor 103 | for i in range(10): 104 | if i<9: 105 | sum+=abs(acc[i+1]-acc[i]) 106 | else: 107 | break 108 | self.S=sum/9 109 | print('异常信息 acc : {}'.format(acc), file=f) 110 | print('异常因子 abnormal_factor : {}'.format(abnormal_factor), file=f) 111 | print('时延信息 d : {}'.format(d), file=f) 112 | print('时延因子 delay_factor : {}'.format(delay_factor), file=f) 113 | print('行为可靠性 R : {}'.format(self.R), file=f) 114 | print('行为稳定性 S : {}'.format(self.S), file=f) 115 | pass 116 | def behav4(self): 117 | ''' 118 | 行为4:恶意参与方,刚开始行为良好,让自己保持在一个较高水平的信任值下,进行恶意行为 119 | 后又行为良好,信任值积累到一定程度之后,再进行恶意行为。即间断性的进行攻击 120 | :return: 121 | ''' 122 | pass 123 | 124 | class Server: 125 | ''' 126 | 定义一个服务器类,直接计算得到节点的间接信任值 127 | ''' 128 | Text=0 129 | def __init__(self,amount): 130 | ''' 131 | :param amount: 推荐服务器数量 132 | ''' 133 | self.amount=amount 134 | def way1(self, f): 135 | ''' 136 | 方式1:对良好节点的推荐信任值高 0.5-1之间 137 | 节点与推荐服务器的交互次数 0-50之间 138 | :return: 139 | ''' 140 | valuelist=[] 141 | h_Server=[] 142 | h_all=0 143 | for i in range(self.amount): 144 | valuelist.append(random.randint(5000,10000)/10000) 145 | h_Server.append(random.randint(0,50)) 146 | h_all+=h_Server[i] 147 | for i in range(self.amount): 148 | self.Text+=(h_Server[i]/h_all)*valuelist[i] 149 | print('推荐信任值集合:{}'.format(valuelist), file=f) 150 | print('对应推荐服务器历史交互次数:{}'.format(h_Server), file=f) 151 | print('间接信任值:{}'.format(self.Text), file=f) 152 | pass 153 | def way2(self, f): 154 | ''' 155 | 方式2:对恶意节点的推荐信任值低 -1--0.5之间 156 | 节点与推荐服务器的交互次数0-50之间 157 | :return: 158 | ''' 159 | valuelist=[] 160 | h_Server=[] 161 | h_all=0 162 | for i in range(self.amount): 163 | valuelist.append(random.randint(-10000,-5000)/10000) 164 | h_Server.append(random.randint(0, 50)) 165 | h_all += h_Server[i] 166 | for i in range(self.amount): 167 | self.Text+=(h_Server[i]/h_all)*valuelist[i] 168 | print('推荐信任值集合:{}'.format(valuelist), file=f) 169 | print('对应推荐服务器历史交互次数:{}'.format(h_Server), file=f) 170 | print('间接信任值:{}'.format(self.Text), file=f) 171 | pass 172 | def way3(self, f): 173 | ''' 174 | 方式3:推荐信任值有低有高,假设一半高一半低 175 | 节点与推荐服务器的交互次数0-50之间 176 | :return: 177 | ''' 178 | valuelist=[] 179 | h_Server=[] 180 | h_all=0 181 | for i in range(self.amount): 182 | if i%2==0: 183 | valuelist.append(random.randint(0,10000)/10000) 184 | else: 185 | valuelist.append(random.randint(-10000,0)/10000) 186 | h_Server.append(random.randint(0, 50)) 187 | h_all += h_Server[i] 188 | for i in range(self.amount): 189 | self.Text+=(h_Server[i]/h_all)*valuelist[i] 190 | print('推荐信任值集合:{}'.format(valuelist), file=f) 191 | print('对应推荐服务器历史交互次数:{}'.format(h_Server), file=f) 192 | print('间接信任值:{}'.format(self.Text), file=f) 193 | pass 194 | 195 | 196 | #1、定义函数,计算异常因子 197 | def ACC(list): 198 | xx=0 199 | for i in range(10): 200 | xx+=pow(list[i],i+1)*pow(0.8,10-i) 201 | abnormal_factor=1-math.exp(-xx) 202 | return abnormal_factor 203 | 204 | #2、定义函数,计算时延因子 205 | def calculate_delay_factor(list): 206 | yy=0 207 | for i in range(10): 208 | yy+=pow(list[i],i+1)*pow(0.8,10-i) 209 | delay_factor=math.exp(-yy) 210 | return delay_factor 211 | 212 | #3、定义函数,计算时间衰减因子,参数t是当前时间,t0是最后一次信任建立更新时间 213 | #时间抽象为周期数 214 | def timeDecay(t,t0): 215 | return pow(2,-(t-t0)) 216 | pass 217 | 218 | #4、定义函数,计算熟悉度,参数是交互次数 219 | def famil(h): 220 | if h==0: 221 | return 0 222 | elif h>=1: 223 | return 1-(1/(pow(math.exp(h-1),0.5)+1)) 224 | pass 225 | 226 | #5、定义函数,计算稳定性度量权重因子,参数是节点的历史行为稳定性S的集合和历史交互次数 227 | def q(Node,h): 228 | S_all=0 229 | S_all+=Node.S 230 | Save=S_all/(h+1) #h+1目的保证分母不为0 231 | return Save 232 | pass 233 | 234 | #6、定义函数,计算交互满意度,参数是稳定性和可靠性,假设稳定性和可靠性的权重参数都为0.5 235 | def interSatis(Node): 236 | if Node.R==-1 or Node.S==-1: 237 | return -1 238 | else: 239 | return 0.5*Node.R+0.5*Node.S 240 | pass 241 | 242 | #7、定义函数,计算直接信任值,参数:1、历史信任值 2、历史交互次数 243 | def dirvalue(Node,Thist,h,t,t0): 244 | if Thist==-1: 245 | Tint=-1 246 | return Tint 247 | else: 248 | #r为历史信任值所占权重大小 249 | #g为实际当前交互满意度 250 | #历史交互次数阈值为1/pow(2*0.002,0.5) 251 | r=famil(h)*timeDecay(t,t0)*q(Node,h) 252 | if h>=0 and h<=(1/pow(2*0.002,0.5)): 253 | g=interSatis(Node)*(0.002*pow(h,2)+0.5) 254 | else: 255 | g=interSatis(Node) 256 | Tint=Thist*r+g*(1-r) 257 | return Tint 258 | 259 | #8、定义函数,计算最终信任值 参数1、直接信任值Tint 2、间接信任值Text 3、归一化历史交互次数 4、归一化推荐服务器个数 260 | def value(Tint,Text,h_Normalize,amount_Normalize): 261 | den=h_Normalize+amount_Normalize 262 | if h_Normalize==0 and amount_Normalize!=0: 263 | TTT=Text 264 | elif h_Normalize!=0 and amount_Normalize==0: 265 | TTT=Tint 266 | else: 267 | TTT=(amount_Normalize/den)*Text+(h_Normalize/den)*Tint 268 | return TTT 269 | 270 | #9、定义函数,归一化处理,参数为列表 271 | def normalize(list): 272 | deal=[] 273 | maxx=max(list) 274 | minn=min(list) 275 | mid=maxx-minn 276 | for i in range(len(list)): 277 | deal.append((list[i]-minn)/mid) 278 | return deal 279 | 280 | def main(f): 281 | # 模拟联邦学习 282 | # for循环共进行20个周期,每个周期进行10轮迭代 283 | # 存储1-100节点历史交互次数 284 | list_interaction_counts = [] 285 | for i in range(100): 286 | list_interaction_counts.append(0) 287 | # 存储1-100节点历史信任值 288 | list_Thist = [] 289 | for i in range(100): 290 | list_Thist.append(0) 291 | recomnum = 50 292 | mark1 = 0 293 | mark2 = 1 294 | Storage_behavior_stability = [0] # 存储行为稳定性 295 | for circul in range(20): 296 | print('第{}个周期'.format(circul + 1), file=f) 297 | print('-' * 80, file=f) 298 | # 每个周期100个节点全部作为参与者参与联邦学习 299 | # 100个节点中:1-40 行为1; 40-60 行为2; 60-80 行为3; 80-100 行为4; 300 | # 初始化节点类 301 | for i in range(100): 302 | print('{} ***'.format(i + 1), file=f) 303 | # 行为1 304 | if i + 1 < 40: 305 | p = Node(1, 0, list_interaction_counts[i], recomnum) # 行为 信任值 历史交互次数 推荐服务器数量 306 | p.behav1(f) # 行为稳定性和行为可靠性 307 | server = Server(recomnum) 308 | server.way1(f) # 间接信任度 309 | Text = server.Text 310 | Tint = dirvalue(p, list_Thist[i], list_interaction_counts[i], 10, 8) # 当前时间10,最后一次信任建立时间8 直接信任度 311 | print('直接信任值:{}'.format(Tint), file=f) 312 | TTT = value(Tint, Text, list_interaction_counts[i], recomnum) # 最终信任度 313 | print('信任值:{}'.format(TTT), file=f) 314 | list_Thist[i] = TTT 315 | list_interaction_counts[i] += 1 316 | # 行为2 317 | elif i + 1 >= 40 and i + 1 < 60: 318 | p = Node(2, 0, list_interaction_counts[i], recomnum) 319 | p.behav2(f) 320 | server = Server(recomnum) 321 | server.way2(f) 322 | Text = server.Text 323 | Tint = dirvalue(p, list_Thist[i], list_interaction_counts[i], 10, 8) # 当前时间10,最后一次信任建立时间8 直接信任度 324 | print('直接信任值:{}'.format(Tint), file=f) 325 | TTT = value(Tint, Text, list_interaction_counts[i], recomnum) # 最终信任度 326 | print('信任值:{}'.format(TTT), file=f) 327 | list_Thist[i] = TTT 328 | list_interaction_counts[i] += 1 329 | # 行为3 330 | elif i + 1 >= 60 and i + 1 < 80: 331 | p = Node(3, 0, list_interaction_counts[i], recomnum) 332 | if mark1 % 2 == 0: 333 | p.behav1(f) 334 | elif mark1 % 2 == 1: 335 | p.behav2(f) 336 | server = Server(recomnum) 337 | server.way3(f) 338 | Text = server.Text 339 | Tint = dirvalue(p, list_Thist[i], list_interaction_counts[i], 10, 8) # 当前时间10,最后一次信任建立时间8 直接信任度 340 | print('直接信任值:{}'.format(Tint), file=f) 341 | TTT = value(Tint, Text, list_interaction_counts[i], recomnum) # 最终信任度 342 | print('信任值:{}'.format(TTT), file=f) 343 | list_Thist[i] = TTT 344 | list_interaction_counts[i] += 1 345 | # 行为4 346 | else: 347 | p = Node(4, 0, list_interaction_counts[i], recomnum) 348 | if mark2 % 5 == 0: 349 | p.behav2(f) 350 | else: 351 | p.behav1(f) 352 | server = Server(recomnum) 353 | server.way3(f) 354 | Text = server.Text 355 | Tint = dirvalue(p, list_Thist[i], list_interaction_counts[i], 10, 8) # 当前时间10,最后一次信任建立时间8 直接信任度 356 | print('直接信任值:{}'.format(Tint), file=f) 357 | TTT = value(Tint, Text, list_interaction_counts[i], recomnum) # 最终信任度 358 | print('信任值:{}'.format(TTT), file=f) 359 | list_Thist[i] = TTT 360 | list_interaction_counts[i] += 1 361 | 362 | mark1 += 1 363 | mark2 += 1 364 | print('\n', file=f) 365 | pass 366 | 367 | if __name__ == '__main__': 368 | with open('C:\\Users\\tsy\\Desktop\\fout.txt', 'w') as f: 369 | main(f) -------------------------------------------------------------------------------- /trust_value.py: -------------------------------------------------------------------------------- 1 | import random 2 | import math 3 | 4 | 5 | class Node: 6 | ''' 7 | 定义一个节点类 8 | ''' 9 | id = -1 10 | R = 0 # 节点行为可靠性 11 | abnormal_factor = 0 # 异常因子 12 | delay_factor = 0 # 时延因子 13 | 14 | def __init__(self, behavior, value): 15 | ''' 16 | 初始化节点属性 17 | :param behavior: 节点行为序号,1为行为B1,2为行为B2,3为行为B3 18 | :param value: 节点信任值 19 | ''' 20 | self.behavior = behavior 21 | self.value = value 22 | 23 | def behav1(self): 24 | ''' 25 | 行为B1:良好参与方:行为良好、稳定性高(异常度低,时延低/异常因子和时延因子都高) 26 | 异常因子abnormal_factor 在0.9-1之间 27 | 时延因子delay_factor 在0.9-1之间 28 | :return: 29 | ''' 30 | self.abnormal_factor = random.randint(11000, 13000) / 10000 # 异常因子 31 | self.delay_factor = random.randint(11000, 13000) / 10000 # 时延因子 32 | if self.abnormal_factor >= 1.275 and self.delay_factor >= 1.275: 33 | self.R = 1 34 | else: 35 | self.R = (1 / (1 + math.exp(-self.abnormal_factor))) * self.delay_factor # 可靠性计算 36 | 37 | def behav2(self): 38 | ''' 39 | 行为B2:恶意参与方:可靠性差、稳定性高(异常度高、时延高/异常因子低,时延因子低) 40 | 异常因子abnormal_factor 在0-0.2之间 41 | 时延因子delay_factor 在0-0.2之间 42 | :return: 43 | ''' 44 | self.abnormal_factor = random.randint(0, 7000) / 10000 # 异常因子 45 | self.delay_factor = random.randint(0, 7000) / 10000 # 时延因子 46 | self.R = (1 / (1 + math.exp(-self.abnormal_factor))) * self.delay_factor # 可靠性计算 47 | 48 | def __str__(self): 49 | return 'abnormal_factor:' + str(self.abnormal_factor) + '; delay_factor:' + str(self.delay_factor) + '; R:' + str(self.R) + ';' 50 | 51 | 52 | # 计算当前信任值 53 | def current_trust(HL, interation_count): # 当前行为可靠性 历史交互次数(阈值70) 54 | Rcurr = HL[19][0] 55 | if Rcurr > 0.5: 56 | if interation_count <= 70 and interation_count >= 0: 57 | g = 0.0001 * interation_count * interation_count + 0.5 58 | else: 59 | g = 1 60 | return 0.5 + g * (Rcurr - 0.5) 61 | else: 62 | return 0 63 | 64 | 65 | # 计算历史信任值 66 | def historical_trust(HL): # 历史交互信息表(部分) 67 | hist = 0 68 | for i in range(19): 69 | hist += HL[i][0] * (math.pow(2, -(19 - i))) 70 | return hist 71 | 72 | 73 | # 计算直接信任值 74 | def direct_trust(current_trust_value, historical_trust_value, interation_count, current_stability, HL, Thist): # 当前信任值 历史信任值 历史交互次数 当前行为稳定性 历史交互信息表(部分) 历史信任值 75 | if interation_count > 1: 76 | shuxi = 1 - (1 / (pow((math.exp(interation_count - 1)), 0.1) + 1)) 77 | else: 78 | shuxi = 0 79 | whist = shuxi * current_stability 80 | wcurr = 1 - whist 81 | total = 0 82 | Rcurr = HL[19][0] 83 | if Rcurr > 0.5: 84 | for i in range(19): 85 | if HL[18 - i][0] > 0.5: 86 | total += 1 87 | else: 88 | break 89 | n = 0.001 * pow(2, total) 90 | if n > 1: 91 | n = 1 92 | return ((whist * historical_trust_value + wcurr * current_trust_value) - Thist) * n + Thist 93 | elif Rcurr <= 0.5: 94 | return whist * historical_trust_value + wcurr * current_trust_value 95 | 96 | 97 | # 计算推荐信任度 98 | def recommended_trust(RL): # 推荐信息表(部分) 99 | HR = 0 100 | sum = 0 101 | for i in range(20): 102 | HR += RL[i][0] 103 | if HR == 0: # 如果没有推荐服务器 104 | return 0 105 | else: 106 | for i in range(20): 107 | sum += (RL[i][0] / HR) * RL[i][1] 108 | return sum 109 | 110 | 111 | # 计算综合信任度 112 | def comprehensive_trust (recommended_trust_value, direct_trust_value, interation_count, RL, id): # 推荐信任度 直接信任度 历史交互次数列表 推荐信息列表 节点id 113 | if sum(interation_count) == 0: 114 | a = 0 115 | else: 116 | a = interation_count[id] / (sum(interation_count) / 100) 117 | addR = [] 118 | for i in range(100): 119 | total_R = 0 120 | for j in range(20): 121 | total_R += RL[i][j][0] 122 | addR.append(total_R) 123 | if sum(addR) == 0: 124 | b = 0 125 | else: 126 | b = addR[id] / (sum(addR) / 100) 127 | if a == 0 and b == 0: 128 | weight = 0 129 | elif a == 0 and b != 0: 130 | weight = 0.5 131 | else: 132 | weight = b / (a + b) 133 | wd = 1 - weight 134 | return weight * recommended_trust_value + wd * direct_trust_value 135 | 136 | 137 | # 计算综合信任度整体函数 138 | def function(R, list_interation_count, listHL, listRL, listThist, S, id): # 当前行为可靠性 历史交互次数(列表) 历史交互信息表(列表) 推荐信息表(列表) 行为稳定性 节点id 139 | Tcurr = current_trust(listHL[id], list_interation_count[id]) # 当前信任值 140 | Thist = historical_trust(listHL[id]) # 历史信任值 141 | Tdirect_trust = direct_trust(Tcurr, Thist, list_interation_count[id], S, listHL[id], listThist[id]) # 直接信任值 142 | Trecommended_trust = recommended_trust(listRL[id]) # 推荐信任度 143 | TTT = comprehensive_trust (Trecommended_trust, Tdirect_trust, list_interation_count, listRL, id) # 综合信任度 144 | return TTT 145 | 146 | 147 | B1 = B2 = B3 = B4 = B5 = B6 = B7 = B8 = 0 148 | count1 = [] 149 | for i in range(100): # 10 5 10 5 150 | if B1 == 10: 151 | B2 += 1 152 | count1.append(0) 153 | if B2 == 5: 154 | B1 = 0 155 | else: 156 | count1.append(1) 157 | B1 += 1 158 | B2 = 0 159 | count2 = [] 160 | for i in range(100): # 10 10 10 10 161 | if B3 == 10: 162 | B4 += 1 163 | count2.append(0) 164 | if B4 == 10: 165 | B3 = 0 166 | else: 167 | count2.append(1) 168 | B3 += 1 169 | B4 = 0 170 | count3 = [] 171 | for i in range(100): # 1 1 1 1 172 | if B5 == 1: 173 | B6 += 1 174 | count3.append(0) 175 | if B6 == 1: 176 | B5 = 0 177 | else: 178 | count3.append(1) 179 | B5 += 1 180 | B6 = 0 181 | count4 = [] 182 | for i in range(100): # 5 10 5 10 183 | if B7 == 5: 184 | B8 += 1 185 | count4.append(0) 186 | if B8 == 10: 187 | B7 = 0 188 | else: 189 | count4.append(1) 190 | B7 += 1 191 | B8 = 0 192 | 193 | # HL存储1-100节点的历史交互信息表,每个节点最多存储20个历史交互信息 194 | k1 = 0 195 | list_HL = [] 196 | for i in range(100): 197 | list_HL.append([[0.5, 0.5]]) 198 | for j in range(19): 199 | list_HL[k1].append([0.5, 0.5]) 200 | k1 += 1 201 | 202 | # RL存储1-100节点的推荐信息表,每个节点最多存储20个推荐信任值(交互次数随机给出1-100之间) 203 | k2 = 0 204 | list_RL = [] 205 | for i in range(100): 206 | list_RL.append([[0, 0]]) 207 | for j in range(19): 208 | list_RL[k2].append([0, 0]) 209 | k2 += 1 210 | 211 | # 存储1-100个节点的历史交互次数,最初1-100节点的历史交互次数都为0 212 | list_interation_count = [] 213 | for i in range(100): 214 | list_interation_count.append(0) 215 | 216 | # 存储1-100个节点的历史信任度值,最初1-100节点的历史信任度值都定义为0.5 217 | list_Thist = [] 218 | for i in range(100): 219 | list_Thist.append(0.5) 220 | 221 | # 存储1-100个节点的推荐服务器数量,最初1-100节点的推荐服务器数量都为99 222 | list_renum = [] 223 | for i in range(100): 224 | list_renum.append(99) 225 | 226 | 227 | def main(f): 228 | # 100个周期 229 | print('{},{},{},{},{},{}'.format(0, 0, 0, 0, 0, 0.5), file=f) 230 | for circul in range(100): 231 | # print('第{}个周期'.format(circul + 1),file=f) 232 | # print('-' * 80,file=f) 233 | # 每个周期100个节点全部作为候选参与者参与联邦学习,但是每次服务器S1只从100个节点中选取可靠性最高的20个本地模型进行聚合 234 | # 100个节点中:1-20 行为B1; 20-40 行为B2; 40-60 行为B3(1); 60-80 行为B3(2); 80-100 行为B3(3) 235 | # 遍历100个节点,初始化节点类 236 | for i in range(1): 237 | add = 0 # 10次B1 5次B2 238 | 239 | # 行为B3 240 | # 稳定性差(交替提供良好行为与恶意行为,进行x次良好行为后,再进行y次恶意行为) 241 | # ① x > y 10次行为B1,5次行为B2 242 | # ② x = y 10次行为B1,10次行为B2 243 | # ③ x < y 5次行为B1,10次行为B2 244 | 245 | # p = Node(2, list_Thist[i]) 246 | # p.behav2() 247 | # list_HL[i].append([p.R, 0]) 248 | # del list_HL[i][0] # 更新历史信息表 249 | # for j in range(19): 250 | # add += abs(list_HL[i][j + 1][0] - list_HL[i][j][0]) 251 | # S = add / 19 252 | # list_HL[i][19][1] = S 253 | # list_interation_count[i] += 1 254 | # for update_value in range(20): 255 | # recommended_trust_result = 0 256 | # recommended_trust_k1 = 1 257 | # recommended_trust_HL = [[[0.5, 0.5]]] # 推荐服务器 历史交互表 258 | # for nn in range(19): 259 | # recommended_trust_HL[0].append([0.5, 0.5]) 260 | # for ll in range(99): 261 | # recommended_trust_HL.append([[0, 0]]) 262 | # for nn in range(19): 263 | # recommended_trust_HL[recommended_trust_k1].append([0, 0]) 264 | # recommended_trust_k1 += 1 265 | # recommended_trust_k2 = 0 266 | # recommended_trust_RL = [] # 推荐服务器 推荐信息表 267 | # for nn in range(100): 268 | # recommended_trust_RL.append([[0, 0]]) 269 | # for ll in range(19): 270 | # recommended_trust_RL[recommended_trust_k2].append([0, 0]) 271 | # recommended_trust_k2 += 1 272 | # recommended_trust_interation_count = [] # 推荐服务器 历史交互次数 273 | # for ll in range(100): 274 | # recommended_trust_interation_count.append(0) 275 | # recommended_trust_Thist = [] # 推荐服务器 历史信任值 276 | # recommended_trust_Thist.append(0.5) 277 | # list_RL[i][update_value][0] = random.randint(0, 100) # 随机产生推荐服务器与节点的历史交互次数 278 | # for caculate in range(list_RL[i][update_value][0]): # 以实际交互为准 279 | # recommended_trust_add = 0 280 | # recommended_trust_p = Node(2, list_Thist[i]) 281 | # recommended_trust_p.behav2() 282 | # recommended_trust_HL[0].append([recommended_trust_p.R, 0]) 283 | # del recommended_trust_HL[0][0] 284 | # for j in range(19): 285 | # recommended_trust_add += abs(recommended_trust_HL[0][j + 1][0] - recommended_trust_HL[0][j][0]) 286 | # recommended_trust_S = recommended_trust_add / 19 287 | # recommended_trust_HL[0][19][1] = recommended_trust_S 288 | # recommended_trust_interation_count[0] += 1 # 更新历史交互次数列表 289 | # recommended_trust_direct_hist=direct_trust(current_trust(recommended_trust_HL[0], recommended_trust_interation_count[0]), historical_trust(recommended_trust_HL[0]), recommended_trust_interation_count[0], recommended_trust_HL[0][19][1], recommended_trust_HL[0],recommended_trust_Thist[0]) 290 | # recommended_trust_result = hanshhu(recommended_trust_p.R, recommended_trust_interation_count, recommended_trust_HL, recommended_trust_RL, recommended_trust_Thist,recommended_trust_S, 0) 291 | # recommended_trust_Thist[0]=recommended_trust_direct_hist 292 | # list_RL[i][update_value][1] = recommended_trust_result 293 | # direct_hist=direct_trust(current_trust(list_HL[i], list_interation_count[i]), historical_trust(list_HL[i]), list_interation_count[i], list_HL[i][19][1], list_HL[i],list_Thist[i]) 294 | # result = function(p.R, list_interation_count, list_HL, list_RL, list_Thist, list_HL[i][19][1], i) 295 | # print('{},{},{},{},{},{}'.format(circul+1,current_trust(list_HL[i], list_interation_count[i]),historical_trust(list_HL[i]),direct_hist,recommended_trust(list_RL[i]),result),file=f) 296 | # # print(list_HL[i], file=f) 297 | # print(list_RL[i], file=f) 298 | # print('S {} ; current_hist {} ; Rcurr: {} ;'.format(list_HL[i][19][1],list_Thist[i],p.R), file=f) 299 | # print('dq: {}'.format(current_trust(list_HL[i], list_interation_count[i])),file=f) 300 | # print('ls: {}'.format(historical_trust(list_HL[i])),file=f) 301 | # print('zj: {}'.format(direct_hist),file=f) 302 | # print('tj: {}'.format(recommended_trust(list_RL[i])),file=f) 303 | # print('T:{} '.format(result),file=f) 304 | # list_Thist[i] = direct_hist 305 | 306 | p = Node(3, list_Thist[i]) 307 | if count4[circul] == 1: 308 | p.behav1() 309 | list_HL[i].append([p.R, 0]) 310 | del list_HL[i][0] # 更新历史信息表 311 | for j in range(19): 312 | add += abs(list_HL[i][j + 1][0] - list_HL[i][j][0]) 313 | S = add / 19 314 | list_HL[i][19][1] = S 315 | elif count4[circul] == 0: 316 | p.behav2() 317 | list_HL[i].append([p.R, 0]) 318 | del list_HL[i][0] # 更新历史信息表 319 | for j in range(19): 320 | add += abs(list_HL[i][j + 1][0] - list_HL[i][j][0]) 321 | S = add / 19 322 | list_HL[i][19][1] = S 323 | list_interation_count[i] += 1 324 | 325 | # 更新推荐列表 326 | for update_value in range(20): 327 | recommended_trust_result = 0 328 | recommended_trust_k1 = 1 329 | recommended_trust_HL = [[[0.5, 0.5]]] # 推荐服务器 历史交互表 330 | for nn in range(19): 331 | recommended_trust_HL[0].append([0.5, 0.5]) 332 | for ll in range(99): 333 | recommended_trust_HL.append([[0, 0]]) 334 | for nn in range(19): 335 | recommended_trust_HL[recommended_trust_k1].append([0, 0]) 336 | recommended_trust_k1 += 1 337 | recommended_trust_k2 = 0 338 | recommended_trust_RL = [] # 推荐服务器 推荐信息表 339 | for nn in range(100): 340 | recommended_trust_RL.append([[0, 0]]) 341 | for ll in range(19): 342 | recommended_trust_RL[recommended_trust_k2].append([0, 0]) 343 | recommended_trust_k2 += 1 344 | recommended_trust_interation_count = [] # 推荐服务器 历史交互次数 345 | for ll in range(100): 346 | recommended_trust_interation_count.append(0) 347 | recommended_trust_Thist = [] # 推荐服务器 历史信任值 348 | recommended_trust_Thist.append(0.5) 349 | list_RL[i][update_value][0] = random.randint(0, 100) # 随机产生推荐服务器与节点的历史交互次数 350 | for caculate in range(list_RL[i][update_value][0]): # 以实际交互为准 351 | recommended_trust_add = 0 352 | if count4[caculate] == 1: 353 | recommended_trust_p = Node(1, list_Thist[i]) 354 | recommended_trust_p.behav1() 355 | recommended_trust_HL[0].append([recommended_trust_p.R, 0]) 356 | del recommended_trust_HL[0][0] 357 | for j in range(19): 358 | recommended_trust_add += abs(recommended_trust_HL[0][j + 1][0] - recommended_trust_HL[0][j][0]) 359 | recommended_trust_S = recommended_trust_add / 19 360 | recommended_trust_HL[0][19][1] = recommended_trust_S 361 | recommended_trust_interation_count[0] += 1 # 更新历史交互次数列表 362 | recommended_trust_direct_hist = direct_trust(current_trust(recommended_trust_HL[0], recommended_trust_interation_count[0]), historical_trust(recommended_trust_HL[0]), 363 | recommended_trust_interation_count[0], recommended_trust_HL[0][19][1], recommended_trust_HL[0], recommended_trust_Thist[0]) 364 | recommended_trust_result = function(recommended_trust_p.R, recommended_trust_interation_count, recommended_trust_HL, recommended_trust_RL, recommended_trust_Thist, 365 | recommended_trust_S, 0) 366 | recommended_trust_Thist[0] = recommended_trust_direct_hist 367 | elif count4[caculate] == 0: 368 | recommended_trust_p = Node(2, list_Thist[i]) 369 | recommended_trust_p.behav2() 370 | recommended_trust_HL[0].append([recommended_trust_p.R, 0]) 371 | del recommended_trust_HL[0][0] 372 | for j in range(19): 373 | recommended_trust_add += abs(recommended_trust_HL[0][j + 1][0] - recommended_trust_HL[0][j][0]) 374 | recommended_trust_S = recommended_trust_add / 19 375 | recommended_trust_HL[0][19][1] = recommended_trust_S 376 | recommended_trust_interation_count[0] += 1 # 更新历史交互次数列表 377 | recommended_trust_direct_hist = direct_trust(current_trust(recommended_trust_HL[0], recommended_trust_interation_count[0]), historical_trust(recommended_trust_HL[0]), 378 | recommended_trust_interation_count[0], recommended_trust_HL[0][19][1], recommended_trust_HL[0], recommended_trust_Thist[0]) 379 | recommended_trust_result = function(recommended_trust_p.R, recommended_trust_interation_count, recommended_trust_HL, recommended_trust_RL, recommended_trust_Thist, 380 | recommended_trust_S, 0) 381 | recommended_trust_Thist[0] = recommended_trust_direct_hist 382 | list_RL[i][update_value][1] = recommended_trust_result 383 | direct_hist = direct_trust(current_trust(list_HL[i], list_interation_count[i]), historical_trust(list_HL[i]), list_interation_count[i], list_HL[i][19][1], 384 | list_HL[i], list_Thist[i]) 385 | result = function(p.R, list_interation_count, list_HL, list_RL, list_Thist, list_HL[i][19][1], i) 386 | print('{},{},{},{},{},{}'.format(circul + 1, current_trust(list_HL[i], list_interation_count[i]), historical_trust(list_HL[i]), direct_hist, 387 | recommended_trust(list_RL[i]), result), file=f) 388 | list_Thist[i] = direct_hist 389 | 390 | # print('\n',file=f) 391 | 392 | 393 | if __name__ == '__main__': 394 | with open('C:\\Users\\12549\\Desktop\\fout.txt', 'w') as f: 395 | main(f) 396 | --------------------------------------------------------------------------------