summaryrefslogtreecommitdiff
path: root/cyganie.py
diff options
context:
space:
mode:
Diffstat (limited to 'cyganie.py')
-rw-r--r--cyganie.py91
1 files changed, 91 insertions, 0 deletions
diff --git a/cyganie.py b/cyganie.py
new file mode 100644
index 0000000..7d4a4f5
--- /dev/null
+++ b/cyganie.py
@@ -0,0 +1,91 @@
+#!/usr/bin/env python
+# coding=utf-8
+
+import json, random, sys
+from operator import add
+from collections import Counter
+
+movements = json.load(open('movements.json'))
+
+movement = movements[sys.argv[1]]
+
+gypsy_limit = int(sys.argv[2]) if len(sys.argv) > 2 else 7
+gypsy_dist = [1]*gypsy_limit + [0]*(movement['tables']*4-gypsy_limit)
+
+samples = int(sys.argv[3]) if len(sys.argv) > 3 else 50
+
+gypsy_transformation = {'00': '00',
+ '01': '10',
+ '10': '01',
+ '11': '11',
+ '02': '20',
+ '20': '02',
+ '12': '21',
+ '21': '12',
+ '22': '22'}
+poverty_probability = {'00': 0.0,
+ '01': 0.0,
+ '10': 0.0,
+ '11': 0.5,
+ '02': 0.0,
+ '20': 0.0,
+ '12': 1.0,
+ '21': 1.0,
+ '22': 1.0}
+
+def position_to_index(position):
+ letter = position[-1]
+ position = filter(lambda x: x.isdigit(), position)
+ return (int(position) - 1) * 2 + (0 if letter == 'N' else 1)
+
+cumulative_poverties = [0] * movement['tables']
+cumulative_pairs_histogram = []
+
+for s in range(samples):
+ print >> sys.stderr, 'Iteration #' + str(s)
+ cumulative_pairs = [0] * (movement['tables'] * 2)
+ positions = map(lambda no: no-1, movement['positions'])
+ rotation = map(position_to_index, movement['movement'])
+ poverties = [0] * movement['tables']
+
+ random.shuffle(gypsy_dist)
+ pairs = map(sum, zip(gypsy_dist[0::2], gypsy_dist[1::2]))
+
+ for round in range(movement['rounds']+1):
+ gypsy_pairs = map(lambda pair: pairs[pair], positions)
+ tables = map(lambda pair: ''.join(map(lambda p: str(p), pair)), zip(gypsy_pairs[0::2], gypsy_pairs[1::2]))
+ poverties = map(lambda t: int(random.random() < poverty_probability[t]), tables)
+ if round > 0:
+ cumulative_poverties = map(add, cumulative_poverties, poverties)
+ print >> sys.stderr, 'round ' + str(round)
+ print >> sys.stderr, ' '.join(tables)
+ if round > 0:
+ print >> sys.stderr, ' '.join([' *' if p else ' ' for p in poverties])
+ tables = map(lambda t: gypsy_transformation[t], tables)
+ print >> sys.stderr, ' '.join(tables)
+ table_gypsies = map(int, ''.join(tables))
+ for index, gypsies in enumerate(table_gypsies):
+ pairs[positions[index]] = gypsies
+ if round > 0:
+ cumulative_pairs = map(add, cumulative_pairs, pairs)
+ if round > 0:
+ new_positions = [-1] * len(positions)
+ for index, position in enumerate(rotation):
+ new_positions[position] = positions[rotation[index-1 % len(rotation)]]
+ for index, position in enumerate(new_positions):
+ if position == -1:
+ new_positions[index] = positions[index]
+ positions = new_positions
+ cumulative_pairs_histogram += cumulative_pairs
+ print >> sys.stderr, '---'
+
+print 'Bieda na stole:'
+poverties_sum = sum(cumulative_poverties)
+for table, pov in enumerate(cumulative_poverties):
+ print '{:>3d} ({:>5d})'.format(table+1, pov), '#' * (100 * pov / poverties_sum)
+
+print 'Liczba par wg cyganorund w turnieju:'
+pairs_sum = sum(cumulative_pairs_histogram)
+cumulative_pairs_histogram = Counter(cumulative_pairs_histogram)
+for key in sorted(cumulative_pairs_histogram.keys()):
+ print '{:>3d} ({:>5d})'.format(key, cumulative_pairs_histogram[key]), '#' * (100 * cumulative_pairs_histogram[key] / pairs_sum)