Testing, not recommended parameters, working on probability distributions; this is just a backup

This commit is contained in:
AtHeartEngineer
2022-09-16 22:20:37 +00:00
parent 8f3a5d623c
commit 1b363c474a
7 changed files with 8406 additions and 195 deletions

151
Parameter_Sweeps.py Normal file
View File

@@ -0,0 +1,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()

219
analysis.ipynb Normal file
View File

@@ -0,0 +1,219 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 214,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.3126925639607743\n"
]
}
],
"source": [
"from dandelion_utils.generateStemLength import generateStemLength\n",
"from dandelion_utils.reviseStemLength import reviseStemLength_uniform\n",
"\n",
"from dataclasses import dataclass\n",
"import numpy as np\n",
"import pandas as pd\n",
"from scipy.stats import norm\n",
"import matplotlib.pyplot as plt\n",
"import random\n",
"\n",
"\n",
"class Params:\n",
" def __init__(self, stemMin: int, stemMax: int, numberOfStemPeers: int, Peer1, Peer2) -> None:\n",
" self.stemMin = stemMin\n",
" self.stemMax = stemMax\n",
" self.numberOfStemPeers = numberOfStemPeers\n",
" self.Peer1 = Peer1\n",
" self.Peer2 = Peer2\n",
"\n",
" def __repr__(self):\n",
" return self.long_description()\n",
"\n",
" def long_description(self):\n",
" return f'StemMin: {self.stemMin}, StemMax: {self.stemMax}, StemPeers: {self.numberOfStemPeers}, ReductionMin: {self.Peer1}, ReductionMax: {self.Peer2}'\n",
"\n",
" def short_description(self):\n",
" return f'{self.stemMin}-{self.stemMax}, {self.numberOfStemPeers}p, {self.Peer1}-{self.Peer2}r'\n",
"\n",
" def data_description(self):\n",
" return {\"Min\": self.stemMin,\n",
" \"Max\": self.stemMax,\n",
" \"Peers\": self.numberOfStemPeers,\n",
" \"Peer1\": self.Peer1,\n",
" \"Peer2\": self.Peer2}\n",
"\n",
"\n",
"class Sweep:\n",
" def __init__(self, params: Params, num_runs: int, hops: list[int], startingStemLengths: list[int]) -> None:\n",
" self.params = params.data_description(),\n",
" self.num_runs = num_runs\n",
" self.startingStemLengths = startingStemLengths\n",
" self.hops = hops\n",
" self.hops_mean = np.mean(self.hops)\n",
" self.hops_median = np.median(self.hops)\n",
" self.hops_stdd = np.std(self.hops)\n",
" self.hops_min = np.min(self.hops)\n",
" self.hops_max = np.max(self.hops)\n",
" self.starting_stem_mean = np.mean(self.startingStemLengths)\n",
" self.starting_stem_median = np.median(self.startingStemLengths)\n",
" self.starting_stem_stdd = np.std(self.startingStemLengths)\n",
" self.starting_stem_min = np.min(self.startingStemLengths)\n",
" self.starting_stem_max = np.max(self.startingStemLengths)\n",
"\n",
" def describe_sweep(self):\n",
" print(f\"NUMBER RUNS: {self.num_runs}\")\n",
" print(f\"HOPS:\")\n",
" print(f\" - Mean: {self.hops_mean}\")\n",
" print(f\" - Median: {self.hops_median}\")\n",
" print(f\" - StdD: {self.hops_stdd}\")\n",
" print(f\" - Min: {self.hops_min}\")\n",
" print(f\" - Max: {self.hops_max}\")\n",
"\n",
" def plot_sweep(self):\n",
" _title = self.params\n",
" plt.hist(self.hops, bins=[3,4,5,6,7,8,9,10])\n",
" plt.gca().set(title=_title, ylabel='count')\n",
"\n",
" def export(self):\n",
" e = {\"stemMin\": self.params[0][\"Min\"],\n",
" \"stemMax\": self.params[0][\"Max\"],\n",
" \"numberOfStemPeers\": self.params[0][\"Peers\"],\n",
" \"stemReductionMin\": self.params[0][\"ReductionMin\"],\n",
" \"stemReductionMax\": self.params[0][\"ReductionMax\"],\n",
" \"hops_mean\": self.hops_mean,\n",
" \"hops_median\": self.hops_median,\n",
" \"hops_stdd\": self.hops_stdd,\n",
" \"hops_min\": self.hops_min,\n",
" \"hops_max\": self.hops_max,\n",
" \"starting_stem_mean\": self.starting_stem_mean,\n",
" \"starting_stem_median\": self.starting_stem_median,\n",
" \"starting_stem_stdd\": self.starting_stem_stdd,\n",
" \"starting_stem_min\": self.starting_stem_min,\n",
" \"starting_stem_max\": self.starting_stem_max\n",
" }\n",
" return e\n",
"\n",
"\n",
"def printConstants(params: Params):\n",
" print(\"USING CONSTANTS:\")\n",
" print(f\" - StemMin: {params.stemMin}\")\n",
" print(f\" - StemMax: {params.stemMax}\")\n",
" print(f\" - numberOfStemPeers: {params.numberOfStemPeers}\")\n",
" print(f\" - p1: {params.Peer1}\")\n",
" print(f\" - p2: {params.Peer2}\")\n",
"\n",
"\n",
"def random_stem_hops(params: Params):\n",
" # Generate a random initial stem length\n",
" # stemLength = generateStemLength(params.stemMin, params.stemMax)\n",
" stemLength = params.stemMin\n",
" startingStemLength = stemLength\n",
" hops = 0\n",
" while stemLength > 0:\n",
" hops += 1\n",
" temp_stems = []\n",
" # This simulates the message being passed to multiple \"stem peers\",\n",
" # of the multiple paths that the message takes, we are always going\n",
" # to chose the shortest path.\n",
" random_reduction = [params.Peer1, params.Peer2]\n",
" for x in range(params.numberOfStemPeers):\n",
" temp_stems.append(stemLength - random.choice(random_reduction))\n",
" stemLength = min(temp_stems)\n",
" return hops, startingStemLength\n",
"\n",
"\n",
"def single_sweep(params: Params, num_runs=10000):\n",
" hops = []\n",
" startingStemLengths = []\n",
" while len(hops) < num_runs:\n",
" num_hops, startingStemLength = stem_hops(params)\n",
" hops.append(num_hops)\n",
" startingStemLengths.append(startingStemLength)\n",
" return Sweep(params, num_runs, hops, startingStemLengths)\n",
"\n",
"\n",
"def stem_hops(params: Params):\n",
" # Generate a random initial stem length\n",
" # stemLength = generateStemLength(params.stemMin, params.stemMax)\n",
" stemLength = params.stemMin\n",
" startingStemLength = stemLength\n",
" hops = 0\n",
" while stemLength > 0:\n",
" hops += 1\n",
" temp_stems = []\n",
" # This simulates the message being passed to multiple \"stem peers\",\n",
" # of the multiple paths that the message takes, we are always going\n",
" # to chose the shortest path.\n",
" \n",
" for x in range(params.numberOfStemPeers):\n",
" temp_stems.append(stemLength - random.choice(params.Peer1))\n",
" stemLength = min(temp_stems)\n",
" return hops, startingStemLength\n",
"\n",
"def generate_data():\n",
" width = 2 # variance\n",
" min_hops = 3\n",
" additonal_hops = 1\n",
" stemlength_min = min_hops * (width)\n",
" stemlength_max = (min_hops + additonal_hops) * (width)\n",
" stemlength_choices = range(stemlength_min, stemlength_max)\n",
" delta = range(1, width+1) # stemRevision = 0 to delta_width sampled uniformly\n",
" print(f'stemLength = {min_hops* (width)} - {(min_hops + additonal_hops) * (width)}')\n",
" results = []\n",
"\n",
" stemlength = random.choice(stemlength_choices)\n",
"\n",
" params = Params(stemMin=stemlength, stemMax=stemlength, numberOfStemPeers=2, Peer1=delta, Peer2=delta)\n",
" #print(params)\n",
" x = single_sweep(params)\n",
" #results.append(x)\n",
"\n",
" # params = Params(stemMin = 3, stemMax = 7, numberOfStemPeers = 2, StemReductionMin = 1.25, StemReductionMax = 1.33)\n",
" # x = single_sweep(params)\n",
" # print(params)\n",
" x.describe_sweep()\n",
" x.plot_sweep()\n",
" #df = pd.DataFrame.from_records(results)\n",
" #df.to_csv(\"results.csv\")\n",
"\n",
"\n",
"if __name__ == \"__main__\":\n",
" print(random.normalvariate(0.2,4 ))\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10.5 64-bit ('3.10.5')",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.5"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "fac5810790acb4d9d2c04307a4be1a90e3ee9379ddbaed44a05c8df0cba075cf"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@@ -9,10 +9,11 @@ from random import random
def generateStemLength(stemMin: int, stemMax: int) -> int:
# StemMin should be at minimum 1, but recommend setting it to 2
if (stemMin is None or stemMin < 1):
stemMin = 2
if (stemMax is None or stemMax < stemMin):
stemMax = min(stemMin + 3, 10)
# if (stemMin is None or stemMin < 1):
# stemMin = 2
# if (stemMax is None or stemMax < stemMin):
# # stemMax = min(stemMin + 3, 10)
# stemMax = stemMin + 1
# Returns a random whole number between stemMin and stemMax
return math.floor(random() * (stemMax - stemMin + 1) + stemMin)

View File

@@ -2,25 +2,42 @@
# Reduces the stemLength by a random whole number between stemReductionMin and stemReductionMax. The randomness helps obfuscate the sending source and helps propogate the message via floodsub/classic gossipsub to the network sooner.
import math
from random import random
from random import random, normalvariate
def reviseStemLength(stemLength: int, stemReductionMin: float, stemReductionMax: float) -> int:
if (0 > offset > 1):
raise Exception('Invalid reviseStemLength offset, must be between 0 and 1', offset)
def reviseStemLength_uniform(stemLength: int, stemReductionMin: float, stemReductionMax: float) -> int:
if (stemLength > 0):
# Highly recommend stemReductionMin be 1, but it could be set to 0
if (stemReductionMin is None or stemReductionMin < 0):
stemReductionMin = 1
# # Highly recommend stemReductionMin be 1, but it could be set to 0
# if (stemReductionMin is None or stemReductionMin < 0):
# stemReductionMin = 1
# stemReductionMax could be the same value as stemReductionMin if you want a more deterministic, but recommend it be set to stemReductionMin + 1
if (stemReductionMax is None or stemReductionMax < stemReductionMin):
stemReductionMax = stemReductionMin + 1
delta = math.floor((random()+offset) * (stemReductionMax - stemReductionMin + 1) + stemReductionMin)
# # stemReductionMax could be the same value as stemReductionMin if you want a more deterministic, but recommend it be set to stemReductionMin + 1
# if (stemReductionMax is None or stemReductionMax < stemReductionMin):
# stemReductionMax = stemReductionMin + 1
delta = math.floor((random()) * (stemReductionMax - stemReductionMin + 1) + stemReductionMin)
newStemLength = stemLength - delta
if (newStemLength < 0):
newStemLength = 0
return newStemLength
else:
return 0
def reviseStemLength_normal_dist(stemLength: int, mean: float, std: float) -> int:
if (stemLength > 0):
# # Highly recommend stemReductionMin be 1, but it could be set to 0
# if (stemReductionMin is None or stemReductionMin < 0):
# stemReductionMin = 1
# # stemReductionMax could be the same value as stemReductionMin if you want a more deterministic, but recommend it be set to stemReductionMin + 1
# if (stemReductionMax is None or stemReductionMax < stemReductionMin):
# stemReductionMax = stemReductionMin + 1
delta = math.floor(normalvariate(mean, std))
newStemLength = stemLength - delta
#print(f'{newStemLength} (newStem) = {stemLength} (oldStem) - {delta} (delta)')
if (newStemLength < 0):
newStemLength = 0

8001
results_uniform_random.csv Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,30 +0,0 @@
from dandelion_utils.constants import GossipsubStemMin, GossipsubStemMax, numberOfStemPeers, StemReductionMin, StemReductionMax
from dandelion_utils.generateStemLength import generateStemLength
from dandelion_utils.reviseStemLength import reviseStemLength
import numpy as np
import pandas as pd
def printConstants():
print("CONSTANTS:")
print(" - GossipsubStemMin: {}".format(GossipsubStemMin))
print(" - GossipsubStemMax: {}".format(GossipsubStemMax))
print(" - numberOfStemPeers: {}".format(numberOfStemPeers))
print(" - StemReductionMin: {}".format(StemReductionMin))
print(" - StemReductionMax: {}".format(StemReductionMax))
def main():
# Generate a random initial stem length
stemLength = generateStemLength(GossipsubStemMin, GossipsubStemMax)
print("stemLength: {}".format(stemLength))
newStemLength = reviseStemLength(stemLength, StemReductionMin, StemReductionMax)
print("newStemLength: {}".format(newStemLength))
if __name__ == "__main__":
printConstants()
main()

File diff suppressed because one or more lines are too long