Skip to content

Instantly share code, notes, and snippets.

@resilience-me
Last active April 20, 2019 23:20
Show Gist options
  • Save resilience-me/2a72890ac0d6b5175fd6173c9b7df9b3 to your computer and use it in GitHub Desktop.
Save resilience-me/2a72890ac0d6b5175fd6173c9b7df9b3 to your computer and use it in GitHub Desktop.
from __future__ import division
import random
# Set parameters for the simulation
totalNodes = 4 ** 6
connections = 4
totalHops = 5+1
frequency = {}
totalPulses = totalNodes
def initateMapping():
for node in range(1, totalNodes):
frequency[node] = {}
for step in range(totalHops):
frequency[node][step] = 0
initateMapping()
# Calculate geometric series for connections^totalHops
# to get total nodes in branching tree
treeSize = 0
def calculateTreeSize():
for step in range(totalHops):
global treeSize
treeSize += connections ** step
calculateTreeSize()
# Populate branching trees without any loops (no duplicate random numbers)
def swarmRedistribution():
branchingTree = random.sample(range(1, totalNodes), treeSize)
distance = 0
nextLevel = 0
for node in range(treeSize):
frequency[branchingTree[node]][distance] += 1
if (node == nextLevel):
distance += 1
nextLevel += connections ** distance
# Run the simulation and see if the pulse strength decreases with
# same factor as frequency of getting pulse from certain distance
# increases
def runSimulation():
for pulse in range(totalPulses):
swarmRedistribution()
getBalance()
def getBalance():
balance = {}
randomAgent = random.randint(1, totalNodes)
for step in range(totalHops):
balance[step] = frequency[randomAgent][step] / connections ** step
print("The ratio of the frequency a person received pulses from a given distance, divided by how much the amount had decreased at that distance.")
for step in range(len(balance)):
print("Distance ", step, ". Ratio: ", balance[step])
print("The frequency of a person selected on random, same person that was used to calculate the ratios.")
print(frequency[randomAgent])
runSimulation()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment