Advertisement
yojimbos_law

perturbed queue simulator

Dec 4th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. //script simulates encounter selection with a range of user-specified values for # of encounters, length of queue, and rejection rate and records the distribution of queue states for 1m trials with each set of parameters.
  2.  
  3. for i from 1 to 10 print("fix line 24");
  4.  
  5. int[int] value_count;
  6.  
  7. int[int] buffer_queue;
  8.  
  9. void select(int n, int k, int p, int q, int epsilon){
  10. int selection = -1;
  11. boolean in_queue;
  12. int rejection_roll = q;
  13. while(selection == -1){
  14. selection = random(n+epsilon);
  15. if(selection > (n-1) ){
  16. selection = n-1;
  17. }
  18. in_queue = false;
  19. for i from 1 to k{
  20. if(selection == buffer_queue[i]){
  21. in_queue = true;
  22. }
  23. }
  24. if(in_queue && selection != (n-1) ){
  25. rejection_roll = random(q);
  26. if(rejection_roll < p){
  27. selection = -1;
  28. }
  29. }
  30. }
  31. for i from 2 to k {
  32. buffer_queue[i-1] = buffer_queue[i];
  33. }
  34. value_count[selection]++;
  35. buffer_queue[k] = selection;
  36. }
  37.  
  38.  
  39. void generate_and_save_some_queue_data(int n, int k, int p, int q, int epsilon){
  40. //rolls will take values 0,...,n-1
  41. //the values 0,...,n-2 have weight 1
  42. //the value n-1 has weight 1+epsilon.
  43. //queue will remember last k rolls.
  44. //rejection rolls will take values 0,...,q-1 and will reject selections if less than p.
  45. //rejection rate is effectively p/q
  46. //seed selection process with uniform distibution queue
  47. //let's see how long this takes to run
  48. print("starting simulation with n="+n+" k="+k+" p="+p+" q="+q+" epsilon="+epsilon);
  49. int starting_gun = gametime_to_int();
  50. for i from 1 to k{
  51. buffer_queue[i] = random(n);
  52. }
  53.  
  54. //seed queue with more queueily selected things
  55. for i from 1 to k*k {
  56. select(n,k,p,q,epsilon);
  57. }
  58. //let's count some queues for some numerical analysis of steady state distributions
  59. float[string] queue_frequency;
  60. int progress = 0;
  61. buffer queuey_string ;
  62. for i from 1 to 1000000{
  63. select(n,k,p,q,epsilon);
  64. queuey_string.set_length(0);
  65. for j from 1 to k{
  66. if(j == 1){
  67. queuey_string.append(buffer_queue[j].to_string());
  68. }
  69. else{
  70. queuey_string.append(","+buffer_queue[j].to_string());
  71. }
  72. }
  73. queue_frequency[queuey_string.to_string()] += 1;
  74. if(i%100000 == 0){
  75. progress++;
  76. int butts = (gametime_to_int()-starting_gun)/1000;
  77. print("we're "+i/100000+"/10 done after "+butts+" seconds.");
  78. }
  79. }
  80. foreach i in queue_frequency{
  81. queue_frequency[i] /= 1000000;
  82. }
  83.  
  84. //let's label the file in a way that is good.
  85. map_to_file(queue_frequency,"queue frequency data with n="+n+" k="+k+" p="+p+" q="+q+" epsilon="+epsilon+".txt");
  86.  
  87. print_html("and we're done");
  88. }
  89.  
  90. void condense_some_queue_data(int n, int k, int p, int q, int epsilon){
  91. float[string] foreign_import_data;
  92. //string[string] queue_correspondences;
  93. float[boolean][int][int] condensed_data;
  94. file_to_map("queue frequency data with n="+n+" k="+k+" p="+p+" q="+q+" epsilon="+epsilon+".txt",foreign_import_data);
  95. float[int][int] counting_queues;
  96. boolean[int] queue_contents;
  97. int queue_content_count;
  98. int epsilon_count;
  99. foreach i in foreign_import_data{
  100. //reset counting.
  101. queue_content_count = 0;
  102. epsilon_count = 0;
  103. for j from 0 to n-1{
  104. queue_contents[j] = false;
  105. }
  106.  
  107. //split queue at commas.
  108. string[int] queue = split_string(i,",");
  109. for j from 0 to k-1{
  110. //count number of unique things in queue.
  111. if(!queue_contents[queue[j].to_int()]){
  112. queue_contents[queue[j].to_int()] = true;
  113. queue_content_count++;
  114. }
  115.  
  116. //count number of occurrences of the higher probability outcome.
  117. if(queue[j].to_int() == (n-1)){
  118. epsilon_count++;
  119. }
  120.  
  121. }
  122.  
  123. //reindex simulation results by those counts.
  124. condensed_data[false][queue_content_count][epsilon_count] += foreign_import_data[i];
  125. //keep track of how many queue states are in this category.
  126. counting_queues[queue_content_count][epsilon_count] += 1.0;
  127.  
  128. }
  129. foreach i,j in condensed_data[false]{
  130. //normalize by number of queue states in each category.
  131. condensed_data[true][i][j] = condensed_data[false][i][j]/counting_queues[i][j];
  132. }
  133.  
  134. //save reindexed data to file.
  135. map_to_file(condensed_data,"condensed queue frequency data with n="+n+" k="+k+" p="+p+" q="+q+" epsilon="+epsilon+".txt");
  136. }
  137.  
  138.  
  139. /*
  140. generate_and_save_some_queue_data(3,4,1,10,1);
  141. generate_and_save_some_queue_data(3,4,5,10,1);
  142. generate_and_save_some_queue_data(3,4,9,10,1);
  143.  
  144. condense_some_queue_data(3,4,1,10,1);
  145. condense_some_queue_data(3,4,5,10,1);
  146. condense_some_queue_data(3,4,9,10,1);
  147. */
  148.  
  149.  
  150. for a from 11 to 13{
  151. generate_and_save_some_queue_data(2,5,3,4,a);
  152. condense_some_queue_data(2,5,3,4,a);
  153.  
  154. foreach i in value_count{
  155. print(i+": "+value_count[i]);
  156. }
  157. clear(value_count);
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement