Last active
April 20, 2019 23:20
-
-
Save resilience-me/2a72890ac0d6b5175fd6173c9b7df9b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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