fork(4) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. struct params_t{
  8. int world_size; // size of the world (side of the square world)
  9. double initial_proportion; // initial proportion of living cells
  10. int max_t; // maximum time for simulation
  11. int delay; // period in terms of the number of time steps before
  12. // each display of the world (to make more efficient
  13. // outputs)
  14. int num_runs; // number of runs to execute
  15. int display; // turn on the display-world feature (yes=1, no=0)
  16. };
  17.  
  18. struct stats_t {
  19. int num_steps; // the number of time steps
  20. vector<int> num_living; // the number of living cells (at a time)
  21. vector<int> num_dead; // the number of dead cells (at a time)
  22. };
  23.  
  24. void initialize_world(vector< vector<int> > &world, double p)
  25. {
  26. int i, j, n;
  27.  
  28. n = world.size();
  29.  
  30. // generate each cell to be alive with probability p
  31.  
  32. for (i=0; i<n; i++)
  33. for (j=0; j<n; j++)
  34. {
  35. if (drand48()<p)
  36. world[i][j] = 1;
  37. else
  38. world[i][j] = 0;
  39. }
  40. }
  41.  
  42. int count_neighbors(vector< vector<int> > &world,
  43. int i,
  44. int j,
  45. int n)
  46. {
  47. int di, dj;
  48. int num_neighbors=0;
  49.  
  50. // note that this assumes wrap-around world, in which
  51. // the last cell in any row is a neighbor of the very
  52. // first cell in the row; similarly for the columns
  53.  
  54. for (di = -1; di <= 1; di++)
  55. for (dj = -1; dj <= 1; dj++)
  56. if ((di!=0)||(dj!=0))
  57. num_neighbors += world[(i+di+n)%n][(j+dj+n)%n];
  58.  
  59. // return the number of neighbors
  60.  
  61. return num_neighbors;
  62. }
  63.  
  64. void process_world(vector< vector<int> > &new_world,
  65. vector< vector<int> > &world)
  66. {
  67. int i, j;
  68. int n = world.size();
  69.  
  70. // count neighbors of every cell, and generate the new
  71. // world using the basic rules of the game of life
  72. // - living cell dies if less than 2 living neighbors
  73. // - living cell dies if more than 3 living neighbors
  74. // - dead cell changes to living if exactly 3 neighbors
  75. // - otherwise, no change
  76.  
  77. for (i=0; i<n; i++)
  78. for (j=0; j<n; j++)
  79. {
  80. int num_neighbors = count_neighbors(world, i, j, n);
  81.  
  82. // these conditions implement the rules
  83.  
  84. if (world[i][j]==1) // living cell
  85. {
  86. if (num_neighbors<2)
  87. new_world[i][j] = 0; // died because of underpopulation
  88. else
  89. if (num_neighbors>3)
  90. new_world[i][j] = 0; // died because of overcrowding
  91. else
  92. new_world[i][j] = 1; // cell goes on living
  93. }
  94. else
  95. if (num_neighbors==3) // dead cell
  96. new_world[i][j] = 1; // reproduction (new cell)
  97. else
  98. new_world[i][j] = 0; // remains empty cell
  99. }
  100. }
  101.  
  102. void display_world(vector< vector<int> > &world)
  103. {
  104. int i, j;
  105. int n = world.size();
  106. int num_living = 0;
  107. int num_dead = 0;
  108.  
  109. // show the content of each cell in a reasonably readable format
  110. // (as a matrix with * denoting a living cell)
  111.  
  112. for (i=0; i<n; i++)
  113. {
  114. for (j=0; j<n; j++)
  115. if (world[i][j]==1)
  116. {
  117. cout << "* ";
  118. num_living++;
  119. }
  120. else
  121. {
  122. cout << " ";
  123. num_dead++;
  124. }
  125. cout << endl;
  126. }
  127.  
  128. cout << endl;
  129. cout << "num_living = " << num_living << endl;
  130. cout << "num_dead = " << num_dead << endl;
  131. cout << endl;
  132. }
  133.  
  134. void compute_statistics(vector< vector<int> > &world,
  135. stats_t &stats,
  136. int t)
  137. {
  138. int i,j;
  139. int n = world.size();
  140.  
  141. // the number of living and dead cells is 0 initially
  142.  
  143. stats.num_living[t] = 0;
  144. stats.num_dead[t] = 0;
  145.  
  146. // do the counts
  147.  
  148. for (i=0; i<n; i++)
  149. for (j=0; j<n; j++)
  150. if (world[i][j]==1)
  151. stats.num_living[t]++;
  152. else
  153. stats.num_dead[t]++;
  154. }
  155.  
  156. void run_simulation(params_t &params, stats_t &stats)
  157. {
  158. int i, t = 0;
  159. vector< vector<int> > world(params.world_size);
  160. vector< vector<int> > new_world(params.world_size);
  161.  
  162. // store the number of steps and make sure that there
  163. // is enough space to store the statistics
  164.  
  165. stats.num_steps=params.max_t;
  166. stats.num_living.resize(params.max_t+1);
  167. stats.num_dead.resize(params.max_t+1);
  168.  
  169. // runs the simulation
  170.  
  171. for (i=0; i<params.world_size; i++)
  172. {
  173. world[i].resize(params.world_size);
  174. new_world[i].resize(params.world_size);
  175. }
  176.  
  177. // initialize the world
  178.  
  179. initialize_world(world, params.initial_proportion);
  180.  
  181. // compute statistics for the current time step (now t=0)
  182.  
  183. compute_statistics(world, stats, t);
  184.  
  185. // print out the initial world
  186.  
  187. if (params.display)
  188. {
  189. cout << "----------------------------------------------------------" << endl;
  190. cout << "INITIAL WORLD" << endl << endl;
  191. display_world(world);
  192. }
  193.  
  194. for (t=1; t<=params.max_t; t++)
  195. {
  196. // create new world using the old world and the rules
  197.  
  198. process_world(new_world, world);
  199.  
  200. // new world becomes current world
  201.  
  202. swap(world, new_world);
  203.  
  204. // compute the statistics
  205.  
  206. compute_statistics(world, stats, t);
  207.  
  208. // print out the world (once in each params.delay steps)
  209.  
  210. if (((t-1)%params.delay==0)&&(params.display))
  211. {
  212. cout << "----------------------------------------------------------" << endl;
  213. cout << "WORLD AT TIME " << t << endl << endl;
  214. display_world(world);
  215. }
  216. }
  217. }
  218.  
  219. void process_statistics(vector<stats_t> stats)
  220. {
  221. int run;
  222. int t;
  223. int num_runs = stats.size();
  224. int max_t = stats[0].num_steps;
  225.  
  226. cout << "Final statistics over " << num_runs << " runs:" << endl;
  227.  
  228. // process statistics for all time steps and runs
  229. // (averaging the numbers for each time step over
  230. // the runs)
  231.  
  232. for (t=0; t<=max_t; t++)
  233. {
  234. double avg_num_living = 0;
  235. double avg_num_dead = 0;
  236.  
  237. for (run=0; run<num_runs; run++)
  238. {
  239. avg_num_living += stats[run].num_living[t];
  240. avg_num_dead += stats[run].num_dead[t];
  241. }
  242.  
  243. avg_num_living /= num_runs;
  244. avg_num_dead /= num_runs;
  245.  
  246. cout << t << " " << avg_num_living << " " << avg_num_dead << " ";
  247. cout << " " << avg_num_living*100/(avg_num_living+avg_num_dead) << " ";
  248. cout << " " << avg_num_dead*100/(avg_num_living+avg_num_dead) << " ";
  249. cout << endl;
  250. }
  251. }
  252.  
  253. int main()
  254. {
  255. int run;
  256. params_t params;
  257. vector<stats_t> stats;
  258.  
  259. // initialize random number generator
  260.  
  261. srand48(123);
  262.  
  263. // read user parameters
  264.  
  265. cin >> params.world_size;
  266. cin >> params.initial_proportion;
  267. cin >> params.max_t;
  268. cin >> params.delay;
  269. cin >> params.num_runs;
  270. cin >> params.display;
  271.  
  272. // adjust parameters if necessary
  273.  
  274. if (params.world_size<1)
  275. params.world_size=1;
  276. if (params.initial_proportion<0)
  277. params.initial_proportion=0;
  278. if (params.initial_proportion>1)
  279. params.initial_proportion=1;
  280. if (params.max_t<1)
  281. params.max_t=1;
  282. if (params.delay<1)
  283. params.delay=1;
  284. if (params.num_runs<1)
  285. params.num_runs=1;
  286.  
  287. // print out parameters
  288.  
  289. cout << "World size: " << params.world_size << endl;
  290. cout << "Initial proportion: " << params.initial_proportion << endl;
  291. cout << "Maximum time steps: " << params.max_t << endl;
  292. cout << "Reporting period: " << params.delay << endl;
  293. cout << "Number of runs: " << params.num_runs << endl;
  294. cout << "Display world: " << params.display << endl;
  295. cout << endl;
  296.  
  297. // run the game of life simulation
  298.  
  299. stats.resize(params.num_runs); // resize the vector for statistics
  300. for (run=0; run<params.num_runs; run++)
  301. {
  302. cout << "Executing run " << run << endl;
  303. run_simulation(params, stats[run]); // execute one run
  304. }
  305. cout << endl;
  306.  
  307. // process statistics
  308.  
  309. process_statistics(stats);
  310.  
  311. // return 0 from main
  312.  
  313. return 0;
  314. }
  315.  
Success #stdin #stdout 1.05s 2876KB
stdin
20
0.2
100
1
100
0
stdout
World size:         20
Initial proportion: 0.2
Maximum time steps: 100
Reporting period:   1
Number of runs:     100
Display world:      0

Executing run 0
Executing run 1
Executing run 2
Executing run 3
Executing run 4
Executing run 5
Executing run 6
Executing run 7
Executing run 8
Executing run 9
Executing run 10
Executing run 11
Executing run 12
Executing run 13
Executing run 14
Executing run 15
Executing run 16
Executing run 17
Executing run 18
Executing run 19
Executing run 20
Executing run 21
Executing run 22
Executing run 23
Executing run 24
Executing run 25
Executing run 26
Executing run 27
Executing run 28
Executing run 29
Executing run 30
Executing run 31
Executing run 32
Executing run 33
Executing run 34
Executing run 35
Executing run 36
Executing run 37
Executing run 38
Executing run 39
Executing run 40
Executing run 41
Executing run 42
Executing run 43
Executing run 44
Executing run 45
Executing run 46
Executing run 47
Executing run 48
Executing run 49
Executing run 50
Executing run 51
Executing run 52
Executing run 53
Executing run 54
Executing run 55
Executing run 56
Executing run 57
Executing run 58
Executing run 59
Executing run 60
Executing run 61
Executing run 62
Executing run 63
Executing run 64
Executing run 65
Executing run 66
Executing run 67
Executing run 68
Executing run 69
Executing run 70
Executing run 71
Executing run 72
Executing run 73
Executing run 74
Executing run 75
Executing run 76
Executing run 77
Executing run 78
Executing run 79
Executing run 80
Executing run 81
Executing run 82
Executing run 83
Executing run 84
Executing run 85
Executing run 86
Executing run 87
Executing run 88
Executing run 89
Executing run 90
Executing run 91
Executing run 92
Executing run 93
Executing run 94
Executing run 95
Executing run 96
Executing run 97
Executing run 98
Executing run 99

Final statistics over 100 runs:
0 80.46 319.54  20.115  79.885 
1 83.54 316.46  20.885  79.115 
2 71.89 328.11  17.9725  82.0275 
3 72.41 327.59  18.1025  81.8975 
4 68.92 331.08  17.23  82.77 
5 67.03 332.97  16.7575  83.2425 
6 67.02 332.98  16.755  83.245 
7 65.92 334.08  16.48  83.52 
8 65.16 334.84  16.29  83.71 
9 63.51 336.49  15.8775  84.1225 
10 63.55 336.45  15.8875  84.1125 
11 62.57 337.43  15.6425  84.3575 
12 62.17 337.83  15.5425  84.4575 
13 61.5 338.5  15.375  84.625 
14 60.67 339.33  15.1675  84.8325 
15 59.41 340.59  14.8525  85.1475 
16 59.32 340.68  14.83  85.17 
17 58.48 341.52  14.62  85.38 
18 56.89 343.11  14.2225  85.7775 
19 56.7 343.3  14.175  85.825 
20 56.29 343.71  14.0725  85.9275 
21 56.18 343.82  14.045  85.955 
22 55.22 344.78  13.805  86.195 
23 54.78 345.22  13.695  86.305 
24 53.61 346.39  13.4025  86.5975 
25 53.82 346.18  13.455  86.545 
26 54 346  13.5  86.5 
27 52.74 347.26  13.185  86.815 
28 52.47 347.53  13.1175  86.8825 
29 51.48 348.52  12.87  87.13 
30 50.79 349.21  12.6975  87.3025 
31 51.01 348.99  12.7525  87.2475 
32 51.11 348.89  12.7775  87.2225 
33 51.2 348.8  12.8  87.2 
34 51.35 348.65  12.8375  87.1625 
35 51.45 348.55  12.8625  87.1375 
36 50.69 349.31  12.6725  87.3275 
37 51.03 348.97  12.7575  87.2425 
38 49.79 350.21  12.4475  87.5525 
39 48.92 351.08  12.23  87.77 
40 48.71 351.29  12.1775  87.8225 
41 48.41 351.59  12.1025  87.8975 
42 47.08 352.92  11.77  88.23 
43 47.21 352.79  11.8025  88.1975 
44 46.87 353.13  11.7175  88.2825 
45 46.46 353.54  11.615  88.385 
46 46.95 353.05  11.7375  88.2625 
47 45.63 354.37  11.4075  88.5925 
48 47.67 352.33  11.9175  88.0825 
49 46.69 353.31  11.6725  88.3275 
50 46.21 353.79  11.5525  88.4475 
51 46.62 353.38  11.655  88.345 
52 45.38 354.62  11.345  88.655 
53 45.21 354.79  11.3025  88.6975 
54 44.83 355.17  11.2075  88.7925 
55 45.5 354.5  11.375  88.625 
56 45.67 354.33  11.4175  88.5825 
57 46.16 353.84  11.54  88.46 
58 45.4 354.6  11.35  88.65 
59 45.2 354.8  11.3  88.7 
60 46.12 353.88  11.53  88.47 
61 45.67 354.33  11.4175  88.5825 
62 44.62 355.38  11.155  88.845 
63 45.1 354.9  11.275  88.725 
64 45.03 354.97  11.2575  88.7425 
65 44.26 355.74  11.065  88.935 
66 43.62 356.38  10.905  89.095 
67 42.25 357.75  10.5625  89.4375 
68 42.1 357.9  10.525  89.475 
69 42.98 357.02  10.745  89.255 
70 42.34 357.66  10.585  89.415 
71 41.59 358.41  10.3975  89.6025 
72 42.77 357.23  10.6925  89.3075 
73 43.22 356.78  10.805  89.195 
74 43.33 356.67  10.8325  89.1675 
75 42.35 357.65  10.5875  89.4125 
76 41.93 358.07  10.4825  89.5175 
77 41.14 358.86  10.285  89.715 
78 40.7 359.3  10.175  89.825 
79 40.44 359.56  10.11  89.89 
80 40.25 359.75  10.0625  89.9375 
81 39.73 360.27  9.9325  90.0675 
82 40.06 359.94  10.015  89.985 
83 39.18 360.82  9.795  90.205 
84 39.29 360.71  9.8225  90.1775 
85 39.53 360.47  9.8825  90.1175 
86 40.77 359.23  10.1925  89.8075 
87 38.92 361.08  9.73  90.27 
88 39.7 360.3  9.925  90.075 
89 39.34 360.66  9.835  90.165 
90 39.44 360.56  9.86  90.14 
91 39.09 360.91  9.7725  90.2275 
92 38.77 361.23  9.6925  90.3075 
93 37.45 362.55  9.3625  90.6375 
94 37.22 362.78  9.305  90.695 
95 37.43 362.57  9.3575  90.6425 
96 37.43 362.57  9.3575  90.6425 
97 36.34 363.66  9.085  90.915 
98 35.49 364.51  8.8725  91.1275 
99 35.52 364.48  8.88  91.12 
100 34.8 365.2  8.7  91.3