Guest User

Untitled

a guest
Dec 13th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. import com.mesquite.csim.*;
  2. import com.mesquite.csim.Process;
  3. import com.mesquite.csim.file.Files;
  4. import java.io.*;
  5. import java.util.ArrayList;
  6.  
  7. import static java.lang.Math.*;
  8.  
  9.  
  10. public class Driver extends Model {
  11.  
  12. private static final int dim = 6;
  13. private static final double M = pow( 10, 10) ;
  14. private static int run = 0;
  15. private static double time;
  16. private static double upper;
  17. private static double lower;
  18. private static Table sphereVolume;
  19. private static final double expected[] = {
  20. PI,
  21. 4*PI/3,
  22. pow(PI, 2)/2,
  23. (8*PI*PI)/15,
  24. (PI*PI*PI)/6,
  25. (16*PI*PI*PI)/105,
  26. pow(PI, 4) / 24,
  27. 32 * pow(PI, 4) / 945
  28. };
  29. private static final int min_runs[] = {
  30. 3000, // d = 2
  31. 1200, // d = 3
  32. 1100, // d = 4
  33. 1000, // d = 5
  34. 900, // d = 6
  35. 800, // d = 7
  36. 700, // d = 8
  37. 700, // d = 9
  38. };
  39.  
  40. private static final int points[] = {
  41. 10000, // d = 2
  42. 1900000, // d = 3
  43. 1800000, // d = 4
  44. 1700000, // d = 5
  45. 100000, // d = 6
  46. 1300000, // d = 7
  47. 1150000, // d = 8
  48. 1000000, // d = 9
  49. };
  50.  
  51.  
  52. public static void main( String args[] ) {
  53. Driver model = new Driver();
  54.  
  55. time = System.currentTimeMillis();
  56. model.run();
  57.  
  58.  
  59. model.report_tables();
  60. System.out.println("\n-------\nMonte Carlo integration. DIMENSION = " + dim + " SAMPLES = " + points[dim - 2]);
  61. System.out.println("avg: " + sphereVolume.mean());
  62. //System.out.println("Confidence interval of 99%: [" + sphereVolume.conf_lower(.99) + ", " + sphereVolume.conf_upper(.99) + "]");
  63. System.out.println("Confidence interval of 99%: [" + lower + ", " + upper + "]");
  64. System.out.println("Number of runs: " + sphereVolume.count());
  65. System.out.println("Expected volume ~~ " + expected[dim-2]);
  66. System.out.println( "Runs per sec ~~" + sphereVolume.count() / ((System.currentTimeMillis() - time) / 1000));
  67. System.out.println( "Time ~~ " + (System.currentTimeMillis() - time)/1000);
  68.  
  69. }
  70.  
  71. public Driver( ) {
  72. super( "App" );
  73. }
  74.  
  75. public void run( ) {
  76. start( new Sim() );
  77. }
  78.  
  79. private class Sim extends Process {
  80. public Sim( ) {
  81. super( "Sim" );
  82. }
  83.  
  84. public void run() {
  85. sphereVolume = new Table( "Hits ");
  86. sphereVolume.setPermanent(true);
  87. sphereVolume.confidence();
  88.  
  89.  
  90. rand.setSeed(System.currentTimeMillis());
  91.  
  92. add( new Monte( ) );
  93. //add( new Monte( ) );
  94.  
  95. eventListEmpty().untimed_wait();
  96.  
  97.  
  98.  
  99. }
  100.  
  101. }
  102.  
  103. private class Monte extends Process {
  104.  
  105. public Monte() { super( "Monte" ); }
  106.  
  107. public ArrayList<Double> volume = new ArrayList<>();
  108.  
  109.  
  110. public void run() {
  111. while ( true ) {
  112. double hits = 0;
  113. for (int i = 0; i < points[dim - 2]; i++) {
  114. double dist = total();
  115. if (dist <= 1)
  116. hits++;
  117. }
  118.  
  119. double vol = (pow(2, dim) * hits) / points[dim - 2];
  120. sphereVolume.record(vol);
  121. volume.add(vol);
  122. System.out.println(sphereVolume.count());
  123. //run++;
  124.  
  125. upper = sphereVolume.mean() + ( 2.576 * sphereVolume.stddev() / sqrt(sphereVolume.count()) );
  126. lower = sphereVolume.mean() - ( 2.576 * sphereVolume.stddev() / sqrt(sphereVolume.count()) );
  127. //System.out.println( "Calc lower: " + lower + " Calc upper: " + upper);
  128. //System.out.println( upper - lower );
  129. //double var = getVar();
  130. double temp_std = sqrt(getVar() / sphereVolume.count());
  131. //System.out.println(" CSIM std: " + temp_std + " Temp std: " + sqrt(getVar() / sphereVolume.count()));
  132. System.out.println( "csim var: " + sphereVolume.var() + " Calc var " + getVar());
  133. System.out.println( "Mean: " + sphereVolume.mean() + " STD " + sphereVolume.stddev() + " var " + sphereVolume.var());
  134. //System.out.println( "Upper: " + sphereVolume.conf_upper(.99) + " Lower: " + sphereVolume.conf_lower(.99));
  135. //double haynes = 2.576 * sphereVolume.stddev() / sphereVolume.mean();
  136.  
  137. double mine = (2.576 * temp_std) / sphereVolume.mean();
  138.  
  139. System.out.println(" Mine: " + mine);
  140. //System.out.println(sphereVolume.conf_upper(.99) - sphereVolume.conf_lower(.99));
  141. //System.out.println("CAlc " + (upper - lower));
  142. //if( abs(sphereVolume.conf_upper(.99) - sphereVolume.conf_lower(.99))/abs(sphereVolume.conf_lower(.99)) < pow( 10, -4 ) )
  143. // break;
  144. // if( (sphereVolume.conf_upper(.99) - sphereVolume.conf_lower(.99)) == 0 )
  145. // {
  146. // run++;
  147. // continue;
  148. // }
  149. // if( (sphereVolume.conf_upper(.99) - sphereVolume.conf_lower(.99)) < pow( 10, -4 ))
  150. // break;
  151.  
  152.  
  153. if( mine == 0 || sphereVolume.count() < 1000)
  154. continue;
  155. if( mine < pow( 10 , -4) )
  156. break;
  157.  
  158.  
  159.  
  160. }
  161. }
  162.  
  163. public double getVar() {
  164. double mean = sphereVolume.mean();
  165. double temp = 0;
  166. for(double a :volume)
  167. temp += (a-mean)*(a-mean);
  168. return temp/(volume.size()-1);
  169.  
  170. }
  171.  
  172.  
  173.  
  174.  
  175. public double total( ) {
  176. double num[] = new double[dim];
  177. double sum = 0;
  178.  
  179. for( int j = 0; j < dim ; j++) {
  180. num[j] = rand.uniform(0, 1);
  181. sum += pow( num[j] , 2);
  182. }
  183. return sqrt( sum );
  184. }
  185.  
  186. }
  187.  
  188.  
  189. }
Add Comment
Please, Sign In to add comment