Skip to content

Instantly share code, notes, and snippets.

@resilience-me
Last active April 22, 2019 01:51
Show Gist options
  • Save resilience-me/f5a09651aed95203049972945f4e0ede to your computer and use it in GitHub Desktop.
Save resilience-me/f5a09651aed95203049972945f4e0ede to your computer and use it in GitHub Desktop.
# This simulation creates a web of trust with a static number of trust lines
# per person. The swarm redistribution pulses then find credit line paths based
# on those trust lines, without any loops (credit lines in Ripple cannot loop,
# because when they do credit is cleared. )
from __future__ import division
import random
# Set parameters for the simulation
population = 2 ** 13
trustLines = 16
people = {}
def initiateWoT():
for node in range(population):
people[node] = random.sample(range(population), trustLines)
initiateWoT()
creditLines = 4
totalHops = 4
frequency = {}
totalPulses = population
def incrementFrequency(node, step):
frequency.get(node, 0)
if frequency.get(node) == None:
frequency[node] = {}
for hop in range(totalHops):
frequency[node][hop] = 0
frequency[node][step] += 1
def swarmRedistribution():
duplicates = {}
branchingTree = {}
def selectCreditLines(step, agent):
# Select credit line paths without any loops (a person cannot be two times in a single pulse. )
class LinkUtility():
index = {}
counter = 0
linkUtility = LinkUtility()
for creditLine in range(creditLines):
node = 0
indexLength = trustLines - linkUtility.counter
while node < indexLength:
randomLink = random.randint(0, indexLength - 1)
if linkUtility.index.get(randomLink) == None:
linkUtility.index[randomLink] = randomLink
fromIndex = linkUtility.index[randomLink]
if linkUtility.index.get(indexLength - 1) == None:
linkUtility.index[indexLength - 1] = indexLength - 1
linkUtility.index[fromIndex] = linkUtility.index[indexLength - 1]
linkUtility.counter += 1
indexLength -= 1
if duplicates.get(people[agent][fromIndex]) == None:
duplicates[people[agent][fromIndex]] = True
branchingTree[step].append(people[agent][fromIndex])
incrementFrequency(people[agent][fromIndex], step-1)
break
node += 1
if node == indexLength:
# If this happens, just increase the population,
# and/or trust lines, or decrease credit lines,
# or decrease the number of hops
print("Failure. The pulse has looped. ")
quit()
taxPayer = random.randint(0, population - 1)
branchingTree[0] = []
branchingTree[0].append(taxPayer)
for step in range(totalHops):
branchingTree[step+1] = []
for node in branchingTree[step]:
selectCreditLines(step+1, node)
for i in range(totalPulses):
swarmRedistribution()
randomAgent = random.randint(0, population - 1)
print(frequency[randomAgent])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment