Advertisement
yojimbos_law

deterministic queue generator.ash

Jul 22nd, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. int[int] symbol_sequence;
  2. string[int] word;
  3. string[int] buffer_word;
  4. file_to_map("initial conditions.txt", symbol_sequence);
  5. int[int] buffer_sequence;
  6. int[int] buffer_buffer_sequence;
  7. int[int][int] orbit;
  8. int n;
  9. int k;
  10. int p;
  11. int q;
  12.  
  13. int nonzero;
  14. int hash;
  15. int buffer_hash;
  16. boolean[int] in_queue;
  17. int m;
  18. int out_of_queue_count;
  19. int position_offset;
  20. boolean need_to_carry;
  21. boolean needs_tail;
  22.  
  23. //returns int to be modded in the successor generating function.
  24. int tail_hash(int[int] input, int n, int k){
  25. //print_html("generating a hash");
  26. hash = 0;
  27. nonzero = 0;
  28. foreach i in input{
  29. if(i > k && nonzero <= n**k && input[i] != 0){
  30. hash += input[i]*i;
  31. nonzero++;
  32. }
  33. }
  34. if(hash == 0){
  35. hash += k+1;
  36. }
  37. return hash;
  38. }
  39.  
  40. int[int] successor( int[int] input, int n, int k, int p, int q){
  41. //print_html("generating a successor");
  42.  
  43. //throw a digit on the end of the sequence for later use.
  44. input[input.count() + 1] = 0;
  45.  
  46. //count the number of distinct things in corresponding queue
  47. m=0;
  48. for i from 0 to (n-1){
  49. in_queue[i] = false;
  50. for j from 1 to k{
  51. if(input[j] == i){
  52. in_queue[i] = true;
  53. m++;
  54. break;
  55. }
  56. }
  57. }
  58.  
  59. //fill the buffer sequence with the base-n digits of the output of the successor function.
  60. foreach i in input{
  61. if(i==input.count()){
  62. break;
  63. }
  64. //translate queue. corresponds to the n-fold baker's map.
  65. if(i < k){
  66. buffer_sequence[i] = input[i+1];
  67. }
  68. //use a tail-based hash to spit out a deterministic value.
  69. //the distribution of the values 0 through n-1 should
  70. //be identical to selection with rejection from n symbols
  71. //with rejection rate p/q and queue length k.
  72. if(i == k){
  73. buffer_hash = tail_hash(input,n,k);
  74. if(((q*buffer_hash)%(q*n-m*p)) < n*(q-p)){
  75. //int division returns the floor of the result, which is desired.
  76. buffer_sequence[k] = ((q*buffer_hash)%(q*n-m*p)) / (q-p);
  77. }
  78. else{
  79. //less floaty version of (buffer_hash % (n-m*p/q) -n*(1-p/q))/ (p/q)
  80. buffer_sequence[k] = (((q*buffer_hash) % (q*n-m*p)) - n*(q-p))/ p;
  81. //what we actually want is the that value-th smallest
  82. //number outside of the queue.
  83. out_of_queue_count = 0;
  84. for j from 0 to (n-1){
  85. if(!(in_queue[j])){
  86. if(out_of_queue_count == buffer_sequence[k]){
  87. buffer_sequence[k] = j;
  88. break;
  89. }
  90. out_of_queue_count++;
  91. }
  92. }
  93. }
  94. }
  95.  
  96. //handle case i==k+1 outside of loop (below) for computation time purposes.
  97.  
  98. //multiplying the tail by (n-1)/n.
  99. //equivalent to subtracting tail shifted
  100. //right by 1 from the orignal tail
  101. //before shifting the result to the right by 1.
  102. if(i > k+1){
  103. //above process without accounting for "borrowing".
  104. buffer_sequence[i] = input[i] - input[i-1];
  105. }
  106. }
  107. //handle case i==k+1
  108. buffer_sequence[k+1] = input[k+1];
  109. //handle negative digits
  110. buffer_buffer_sequence = buffer_sequence;
  111. foreach i in buffer_sequence{
  112.  
  113. //debugging
  114. /*
  115. buffer_word[i] = "";
  116. if(i >= k+1){
  117. foreach j in buffer_buffer_sequence{
  118. if(j==k+1){
  119. buffer_word[i] += "|";
  120. }
  121. if(buffer_word[i].length() != 0 && j != k+1){
  122. buffer_word[i]+= ","+buffer_buffer_sequence[j];
  123. }
  124. else{
  125. buffer_word[i]+= buffer_buffer_sequence[j];
  126. }
  127. }
  128. print_html(i+": "+buffer_word[i]);
  129. }
  130. */
  131.  
  132. while(i > k+1 && buffer_buffer_sequence[i] <= -1){
  133. buffer_buffer_sequence[i] += n;
  134. need_to_carry = true;
  135. position_offset = 1;
  136. while(need_to_carry){
  137. if(buffer_buffer_sequence[i-position_offset] > 0){
  138. buffer_buffer_sequence[i-position_offset] -= 1;
  139. need_to_carry = false;
  140. }
  141. else{
  142. buffer_buffer_sequence[i-position_offset] = n-1;
  143. position_offset++;
  144. }
  145. }
  146. }
  147.  
  148. //debugging
  149. /*
  150. buffer_word[i] = "";
  151. if(i >= k+1){
  152. foreach j in buffer_buffer_sequence{
  153. if(j==k+1){
  154. buffer_word[i] += "|";
  155. }
  156. if(buffer_word[i].length() != 0 && j != k+1){
  157. buffer_word[i]+= ","+buffer_buffer_sequence[j];
  158. }
  159. else{
  160. buffer_word[i]+= buffer_buffer_sequence[j];
  161. }
  162. }
  163. print_html(i+": "+buffer_word[i]);
  164. }
  165. */
  166.  
  167. }
  168.  
  169. foreach i in buffer_buffer_sequence{
  170. if(buffer_buffer_sequence[i] >= n){
  171. need_to_carry = true;
  172. }
  173. }
  174.  
  175. while(need_to_carry){
  176. foreach i in buffer_sequence{
  177. while(i > k+1 && buffer_buffer_sequence[i] >=n){
  178. buffer_buffer_sequence[i-1]++;
  179. buffer_buffer_sequence[i] -= n;
  180. }
  181.  
  182. }
  183. need_to_carry = false;
  184. foreach i in buffer_buffer_sequence{
  185. if(buffer_buffer_sequence[i] >= n){
  186. need_to_carry = true;
  187. }
  188. }
  189. }
  190. return buffer_buffer_sequence;
  191. }
  192.  
  193. string make_word(int[int] state, int time, int k){
  194. //print_html("generating a word");
  195. foreach i in state{
  196. if(i==k+1){
  197. word[time] += "|";
  198. }
  199. if(word[time].length() != 0 && i != k+1){
  200. word[time]+= ","+state[i];
  201. }
  202. else{
  203. word[time]+= state[i];
  204. }
  205. }
  206. return word[time];
  207. }
  208.  
  209. int[int][int] generate_orbit( int[int] initial, int iterations, int n, int k, int p, int q){
  210. needs_tail = true;
  211. foreach i in initial{
  212. if(i > k && initial[i] != 0){
  213. needs_tail = false;
  214. }
  215. }
  216. if(needs_tail){
  217. initial[k+1] = 1;
  218. }
  219. buffer_sequence = initial;
  220. orbit[0] = initial;
  221. for i from 1 to iterations{
  222. orbit[i] = successor(orbit[i-1],n,k,p,q);
  223. word[i] = make_word(orbit[i],i,k);
  224. }
  225. return orbit;
  226. }
  227.  
  228. /*
  229. for n from 3 to 5{
  230. for k from 3 to 6{
  231. for p from 1 to 9{
  232. clear(orbit);
  233. clear(word);
  234. clear(buffer_sequence);
  235. clear(buffer_buffer_sequence);
  236. file_to_map("initial conditions.txt", symbol_sequence);
  237. orbit = generate_orbit(symbol_sequence, 30, n, k, p, 10);
  238. print_html("generating a text file for n="+n+", k="+k+", p="+p+", q="+10);
  239. map_to_file(word, "orbit for n="+n+", k="+k+", p="+p+", q="+10+".txt");
  240. foreach i in orbit{
  241. print(word[i]);
  242. }
  243. }
  244. }
  245. }
  246. */
  247.  
  248. n=5;
  249. k=4;
  250. q=20;
  251.  
  252.  
  253. for i from 18 to 18{
  254. clear(orbit);
  255. clear(word);
  256. clear(buffer_sequence);
  257. clear(buffer_buffer_sequence);
  258. file_to_map("initial conditions.txt", symbol_sequence);
  259. orbit = generate_orbit(symbol_sequence, 1000, n, k, i, q);
  260. foreach i in orbit{
  261. print(i+": "+word[i]);
  262. }
  263. print_html("generating a text file for n="+n+", k="+k+", p="+i+", q="+q);
  264. map_to_file(word, "orbit for n="+n+", k="+k+", p="+i+", q="+q+".txt");
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement