7 Star 3 Fork 323

openKylin/cybersectookits

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
test.py 2.54 KB
一键复制 编辑 原始数据 按行查看 历史
张妞 提交于 2023-05-11 12:03 . fashionmnist
# -*- coding: utf-8 -*-
"""
@Time    : 2022/8/7 16:02
@Author  : Mr Wang
@FileName: test.py
@SoftWare: PyCharm
"""
# -*- coding: utf-8 -*-
"""
@Time    : 2022/5/15 17:56
@Author  : Mr Wang
@FileName: UE_new.py
@SoftWare: PyCharm
"""
# -*- coding: utf-8 -*-
"""
@Time    : 2022/1/16 15:33
@Author  : Mr Wang
@FileName: New.py
@SoftWare: PyCharm
"""
import random
import numpy as np
number =3 # 随机数数量
bits = 8 # 多少位
UB = 100.88
LB = 20.36 # 预估
def perturb1(epsilon, value, m, n=2):
a=5
p = 1 / (a+1) # 概率p
np.random.seed(m)
# 以p的概率生成value,1-p的概率非value
p = np.array([p, 1 - p])
index = np.random.choice([value, 1 - value], p=p.ravel())
return str(index)
def perturb2(epsilon, value, m, n=2):
a=5
p = a*np.e ** (epsilon / (number * bits)) / (a*np.e ** (epsilon / (number * bits)) + n - 1) # 概率p
np.random.seed(m)
# 以p的概率生成value,1-p的概率非value
p = np.array([p, 1 - p])
index = np.random.choice([value, 1 - value], p=p.ravel())
return str(index)
d = (UB-LB) / bits
def main(rnd_list):
binary_perturb = [0] * number
final = [0] * number
op_list = ['0'] * bits
binary_list = [0] * number
for i in range(0, number):
for j in range(0,bits):
if rnd_list[i] >= UB:
op_list = ['1'] * bits
if (rnd_list[i] >= LB+ d * j) & (rnd_list[i] < LB + d * (j + 1)):
k = j
for m in range(0, k):
op_list[m] = '1'
# print(op_list)
str1 = ''.join(op_list)
binary_list[i] = str1
op_list = ['0'] * bits
aa = ''.join(binary_list)
print(binary_list)
# print(aa)
bb = [0] * (bits*number)
for i in range(len(aa)):
if aa[i] == '1':
bb[i] = perturb1(1, int(aa[i]), i)
if aa[i] == '0':
bb[i] = perturb2(1, int(aa[i]), i)
# print(bb)
oplist = []
for i in range(0, number):
for j in range(i * bits, (i + 1) * bits):
oplist.append(bb[j])
# print(oplist)
str1 = ''.join(oplist)
binary_perturb[i] = str1
oplist = []
# print(binary_perturb)
for i in range(0, number):
for x in binary_perturb[i]:
if x == '1':
sum = binary_perturb[i].count(x)
final[i] = LB+ sum * d
sum = 0
print(final)
if __name__ == '__main__':
main([20,32.25,88.999])
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/openkylin/cybersectookits.git
git@gitee.com:openkylin/cybersectookits.git
openkylin
cybersectookits
cybersectookits
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385