Skip to content

Instantly share code, notes, and snippets.

@fyookball
Last active August 24, 2019 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fyookball/0b79a56b484063ac25d1ccaf22894dbb to your computer and use it in GitHub Desktop.
Save fyookball/0b79a56b484063ac25d1ccaf22894dbb to your computer and use it in GitHub Desktop.
from scipy.special import comb, factorial
import numpy as np
from itertools import combinations
Nplayers = 3 # don't change this.
Ninputsperplayer = 6 # don't go above 6 because it will take FOREVER to enumerate
typicalamount = 100 # relative to the 'resolution' of 1, how big are typical input amounts?
playerinputs = np.random.exponential(typicalamount, size=(Nplayers, Ninputsperplayer)).astype(int)
playerinputs = np.sort(playerinputs, axis=1)
playersums = np.sum(playerinputs, axis=1)
print(playerinputs)
print("player sums:", playersums)
# What does the final transaction look like:
inputs = np.sort(playerinputs.flatten())
outputs = np.sort(np.sum(playerinputs, axis=1))
print("Transaction inputs: ", inputs)
print("Transaction outputs:", outputs)
candidates = []
def add_candidate(sub, subsub):
fulllist = list(inputs)
sub = list(sub)
subsub = list(subsub)
for x in sub:
fulllist.remove(x)
for x in subsub:
sub.remove(x)
c = np.array([fulllist,sub, subsub])
candidates.append(c)
# Now try to decompose from inputs & outputs
amtotal = np.sum(outputs)
sumhist = np.zeros((amtotal,amtotal))
for sub in combinations(inputs, len(inputs) - Ninputsperplayer):
subamount = np.sum(sub)
for subsub in combinations(sub, len(sub) - Ninputsperplayer):
subsubamount = np.sum(subsub)
sum0 = amtotal-subamount
sum1 = subamount-subsubamount
# sum2 = subsubamount
if sum0 == playersums[0] and sum1 == playersums[1]:
add_candidate(sub,subsub)
sumhist[sum1, sum0] += 1
print(sumhist[playersums[0], playersums[1]], "ways exist at actual configuration")
print("list of all candidate decompositions with sum %s"%(playersums))
for c in candidates:
match = np.all(c == playerinputs)
print(c, "<--- Correct!" if match else "")
@fyookball
Copy link
Author

Sample output:

ubuntu@ubuntu-VirtualBox:~/gitwork3$ python3 sim2.py

[[ 11 20 35 37 166 207]
[ 12 82 84 102 120 190]
[ 4 5 40 64 94 307]]
player sums: [476 590 514]
Transaction inputs: [ 4 5 11 12 20 35 37 40 64 82 84 94 102 120 166 190 207 307]
Transaction outputs: [476 514 590]

67.0 ways exist at actual configuration
list of all candidate decompositions with sum [476 590 514]
[[ 20 40 64 84 102 166]
[ 5 12 82 94 190 207]
[ 4 11 35 37 120 307]]
[[ 12 64 84 94 102 120]
[ 5 35 37 40 166 307]
[ 4 11 20 82 190 207]]
[[ 12 64 84 94 102 120]
[ 5 11 37 40 190 307]
[ 4 20 35 82 166 207]]
[[ 12 64 84 94 102 120]
[ 5 11 20 40 207 307]
[ 4 35 37 82 166 190]]
[[ 12 64 84 94 102 120]
[ 4 11 20 82 166 307]
[ 5 35 37 40 190 207]]
[[ 12 35 37 82 120 190]
[ 11 40 64 102 166 207]
[ 4 5 20 84 94 307]]
[[ 12 35 37 82 120 190]
[ 11 20 84 102 166 207]
[ 4 5 40 64 94 307]]
[[ 12 35 37 82 120 190]
[ 5 11 20 40 207 307]
[ 4 64 84 94 102 166]]
[[ 12 20 82 94 102 166]
[ 5 40 64 84 190 207]
[ 4 11 35 37 120 307]]
[[ 12 20 82 94 102 166]
[ 5 11 37 40 190 307]
[ 4 35 64 84 120 207]]
[[ 12 20 82 94 102 166]
[ 4 35 40 84 120 307]
[ 5 11 37 64 190 207]]
[[ 12 20 82 94 102 166]
[ 4 11 64 84 120 307]
[ 5 35 37 40 190 207]]
[[ 12 20 82 94 102 166]
[ 4 5 64 120 190 207]
[ 11 35 37 40 84 307]]
[[ 12 20 64 94 120 166]
[ 5 11 37 40 190 307]
[ 4 35 82 84 102 207]]
[[ 12 20 64 94 120 166]
[ 4 11 82 84 102 307]
[ 5 35 37 40 190 207]]
[[ 12 20 64 94 120 166]
[ 4 5 82 102 190 207]
[ 11 35 37 40 84 307]]
[[ 12 20 40 94 120 190]
[ 5 11 37 64 166 307]
[ 4 35 82 84 102 207]]
[[ 12 20 40 94 120 190]
[ 4 11 82 84 102 307]
[ 5 35 37 64 166 207]]
[[ 12 20 35 82 120 207]
[ 11 37 84 102 166 190]
[ 4 5 40 64 94 307]]
[[ 12 20 35 82 120 207]
[ 5 11 37 64 166 307]
[ 4 40 84 94 102 190]]
[[ 12 20 35 82 120 207]
[ 5 11 37 40 190 307]
[ 4 64 84 94 102 166]]
[[ 12 20 35 82 120 207]
[ 4 37 64 84 94 307]
[ 5 11 40 102 166 190]]
[[ 11 37 40 102 120 166]
[ 12 35 64 82 190 207]
[ 4 5 20 84 94 307]]
[[ 11 37 40 102 120 166]
[ 5 12 82 94 190 207]
[ 4 20 35 64 84 307]]
[[ 11 37 40 102 120 166]
[ 4 5 20 64 190 307]
[ 12 35 82 84 94 207]]
[[ 11 35 64 82 94 190]
[ 37 40 84 102 120 207]
[ 4 5 12 20 166 307]]
[[ 11 35 64 82 94 190]
[ 20 37 40 120 166 207]
[ 4 5 12 84 102 307]]
[[ 11 35 64 82 94 190]
[ 20 37 40 84 102 307]
[ 4 5 12 120 166 207]]
[[ 11 35 64 82 94 190]
[ 4 20 37 102 120 307]
[ 5 12 40 84 166 207]]
[[ 11 35 64 82 94 190]
[ 4 12 20 40 207 307]
[ 5 37 84 102 120 166]]
[[ 11 35 37 84 102 207]
[ 40 64 82 94 120 190]
[ 4 5 12 20 166 307]]
[[ 11 35 37 84 102 207]
[ 12 20 82 120 166 190]
[ 4 5 40 64 94 307]]
[[ 11 35 37 84 102 207]
[ 5 12 64 82 120 307]
[ 4 20 40 94 166 190]]
[[ 11 35 37 84 102 207]
[ 4 5 20 64 190 307]
[ 12 40 82 94 120 166]]
[[ 11 20 35 37 166 207]
[ 40 64 82 94 120 190]
[ 4 5 12 84 102 307]]
[[ 11 20 35 37 166 207]
[ 12 82 84 102 120 190]
[ 4 5 40 64 94 307]] <--- Correct!
[[ 11 20 35 37 166 207]
[ 5 12 64 82 120 307]
[ 4 40 84 94 102 190]]
[[ 5 37 82 84 102 166]
[ 4 12 20 40 207 307]
[ 11 35 64 94 120 190]]
[[ 5 37 64 84 120 166]
[ 12 35 40 94 102 307]
[ 4 11 20 82 190 207]]
[[ 5 37 64 84 120 166]
[ 4 12 20 40 207 307]
[ 11 35 82 94 102 190]]
[[ 5 37 40 84 120 190]
[ 11 12 64 94 102 307]
[ 4 20 35 82 166 207]]
[[ 5 37 40 84 120 190]
[ 4 11 20 82 166 307]
[ 12 35 64 94 102 207]]
[[ 5 20 40 84 120 207]
[ 11 12 64 94 102 307]
[ 4 35 37 82 166 190]]
[[ 5 20 40 84 120 207]
[ 4 12 37 64 166 307]
[ 11 35 82 94 102 190]]
[[ 5 11 40 64 166 190]
[ 12 20 37 94 120 307]
[ 4 35 82 84 102 207]]
[[ 5 11 40 64 166 190]
[ 4 20 37 102 120 307]
[ 12 35 82 84 94 207]]
[[ 5 11 20 84 166 190]
[ 12 35 40 94 102 307]
[ 4 37 64 82 120 207]]
[[ 5 11 20 84 166 190]
[ 4 37 40 82 120 307]
[ 12 35 64 94 102 207]]
[[ 4 35 64 82 84 207]
[ 12 20 37 94 120 307]
[ 5 11 40 102 166 190]]
[[ 4 35 64 82 84 207]
[ 5 11 37 40 190 307]
[ 12 20 94 102 120 166]]
[[ 4 20 82 84 120 166]
[ 12 35 40 94 102 307]
[ 5 11 37 64 190 207]]
[[ 4 20 82 84 120 166]
[ 11 12 64 94 102 307]
[ 5 35 37 40 190 207]]
[[ 4 20 82 84 120 166]
[ 5 11 37 40 190 307]
[ 12 35 64 94 102 207]]
[[ 4 20 64 102 120 166]
[ 11 12 82 84 94 307]
[ 5 35 37 40 190 207]]
[[ 4 20 64 102 120 166]
[ 5 12 82 94 190 207]
[ 11 35 37 40 84 307]]
[[ 4 20 64 102 120 166]
[ 5 11 37 40 190 307]
[ 12 35 82 84 94 207]]
[[ 4 20 40 102 120 190]
[ 11 12 82 84 94 307]
[ 5 35 37 64 166 207]]
[[ 4 20 40 102 120 190]
[ 5 11 37 64 166 307]
[ 12 35 82 84 94 207]]
[[ 4 11 35 37 82 307]
[ 20 64 94 102 120 190]
[ 5 12 40 84 166 207]]
[[ 4 11 35 37 82 307]
[ 5 40 64 84 190 207]
[ 12 20 94 102 120 166]]
[[ 4 11 20 40 94 307]
[ 12 82 84 102 120 190]
[ 5 35 37 64 166 207]]
[[ 4 11 20 40 94 307]
[ 12 35 64 82 190 207]
[ 5 37 84 102 120 166]]
[[ 4 11 12 40 102 307]
[ 20 82 84 94 120 190]
[ 5 35 37 64 166 207]]
[[ 4 5 64 94 102 207]
[ 12 20 82 120 166 190]
[ 11 35 37 40 84 307]]
[[ 4 5 12 82 166 207]
[ 20 64 94 102 120 190]
[ 11 35 37 40 84 307]]
[[ 4 5 12 82 166 207]
[ 20 37 40 84 102 307]
[ 11 35 64 94 120 190]]
[[ 4 5 12 64 84 307]
[ 20 37 40 120 166 207]
[ 11 35 82 94 102 190]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment