-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParameter_Sweeps.py
151 lines (125 loc) · 5.73 KB
/
Parameter_Sweeps.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
from dandelion_utils.generateStemLength import generateStemLength
from dandelion_utils.reviseStemLength import reviseStemLength_uniform
from dataclasses import dataclass
import numpy as np
import pandas as pd
from scipy.stats import norm
import matplotlib.pyplot as plt
import random
class Params:
def __init__(self, stemMin: int, stemMax: int, numberOfStemPeers: int, StemReductionMin: float, StemReductionMax: float) -> None:
self.stemMin = stemMin
self.stemMax = stemMax
self.numberOfStemPeers = numberOfStemPeers
self.StemReductionMin = StemReductionMin
self.StemReductionMax = StemReductionMax
def __repr__(self):
return self.long_description()
def long_description(self):
return f'StemMin: {self.stemMin}, StemMax: {self.stemMax}, StemPeers: {self.numberOfStemPeers}, ReductionMin: {self.StemReductionMin}, ReductionMax: {self.StemReductionMax}'
def short_description(self):
return f'{self.stemMin}-{self.stemMax}, {self.numberOfStemPeers}p, {self.StemReductionMin}-{self.StemReductionMax}r'
def data_description(self):
return {"Min": self.stemMin,
"Max": self.stemMax,
"Peers": self.numberOfStemPeers,
"ReductionMin": self.StemReductionMin,
"ReductionMax": self.StemReductionMax}
class Sweep:
def __init__(self, params: Params, num_runs: int, hops: list[int], startingStemLengths: list[int]) -> None:
self.params = params.data_description(),
self.num_runs = num_runs
self.startingStemLengths = startingStemLengths
self.hops = hops
self.hops_mean = np.mean(self.hops)
self.hops_median = np.median(self.hops)
self.hops_stdd = np.std(self.hops)
self.hops_min = np.min(self.hops)
self.hops_max = np.max(self.hops)
self.starting_stem_mean = np.mean(self.startingStemLengths)
self.starting_stem_median = np.median(self.startingStemLengths)
self.starting_stem_stdd = np.std(self.startingStemLengths)
self.starting_stem_min = np.min(self.startingStemLengths)
self.starting_stem_max = np.max(self.startingStemLengths)
def describe_sweep(self):
print(f"NUMBER RUNS: {self.num_runs}")
print(f"HOPS:")
print(f" - Mean: {self.hops_mean}")
print(f" - Median: {self.hops_median}")
print(f" - StdD: {self.hops_stdd}")
print(f" - Min: {self.hops_min}")
print(f" - Max: {self.hops_max}")
def plot_sweep(self):
_title = self.params
plt.hist(self.hops, bins=[1, 2, 3, 4, 5, 6, 7, 8])
plt.gca().set(title=_title, ylabel='count')
def export(self):
e = {"stemMin": self.params[0]["Min"],
"stemMax": self.params[0]["Max"],
"numberOfStemPeers": self.params[0]["Peers"],
"stemReductionMin": self.params[0]["ReductionMin"],
"stemReductionMax": self.params[0]["ReductionMax"],
"hops_mean": self.hops_mean,
"hops_median": self.hops_median,
"hops_stdd": self.hops_stdd,
"hops_min": self.hops_min,
"hops_max": self.hops_max,
"starting_stem_mean": self.starting_stem_mean,
"starting_stem_median": self.starting_stem_median,
"starting_stem_stdd": self.starting_stem_stdd,
"starting_stem_min": self.starting_stem_min,
"starting_stem_max": self.starting_stem_max
}
return e
def printConstants(params: Params):
print("USING CONSTANTS:")
print(f" - StemMin: {params.stemMin}")
print(f" - StemMax: {params.stemMax}")
print(f" - numberOfStemPeers: {params.numberOfStemPeers}")
print(f" - StemReductionMin: {params.StemReductionMin}")
print(f" - StemReductionMax: {params.StemReductionMax}")
def random_stem_hops(params: Params):
# Generate a random initial stem length
# stemLength = generateStemLength(params.stemMin, params.stemMax)
stemLength = params.stemMin
startingStemLength = stemLength
hops = 0
while stemLength > 0:
hops += 1
temp_stems = []
# This simulates the message being passed to multiple "stem peers",
# of the multiple paths that the message takes, we are always going
# to chose the shortest path.
for x in range(params.numberOfStemPeers):
temp_stems.append(reviseStemLength_uniform(stemLength, params.StemReductionMin, params.StemReductionMax))
stemLength = min(temp_stems)
return hops, startingStemLength
def single_sweep(params: Params, num_runs=10000):
hops = []
startingStemLengths = []
while len(hops) < num_runs:
num_hops, startingStemLength = random_stem_hops(params)
hops.append(num_hops)
startingStemLengths.append(startingStemLength)
return Sweep(params, num_runs, hops, startingStemLengths)
def generate_data():
width = 3 # variance
additional_privacy = 1
stemlength = range(3 * width, (3 + additional_privacy) * width)
delta_width = range(0, width) # stemRevision = 0 to delta_width sampled uniformly
results = []
_stemMin = random.choice(stemlength)
_stemReductionMin = random.choice(delta_width)
params = Params(stemMin=_stemMin, stemMax=_stemMin, numberOfStemPeers=2, StemReductionMin=_stemReductionMin, StemReductionMax=_stemReductionMin)
print(params)
x = single_sweep(params).export()
results.append(x)
# params = Params(stemMin = 3, stemMax = 7, numberOfStemPeers = 2, StemReductionMin = 1.25, StemReductionMax = 1.33)
# x = single_sweep(params)
# print(params)
# x.describe_sweep()
# x.plot_sweep()
df = pd.DataFrame.from_records(results)
df.to_csv("results.csv")
if __name__ == "__main__":
generate_data()