Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.mesquite.csim.*;
- import com.mesquite.csim.Process;
- import com.mesquite.csim.file.Files;
- import java.io.*;
- import java.util.ArrayList;
- import static java.lang.Math.*;
- public class Driver extends Model {
- private static final int dim = 6;
- private static final double M = pow( 10, 10) ;
- private static int run = 0;
- private static double time;
- private static double upper;
- private static double lower;
- private static Table sphereVolume;
- private static final double expected[] = {
- PI,
- 4*PI/3,
- pow(PI, 2)/2,
- (8*PI*PI)/15,
- (PI*PI*PI)/6,
- (16*PI*PI*PI)/105,
- pow(PI, 4) / 24,
- 32 * pow(PI, 4) / 945
- };
- private static final int min_runs[] = {
- 3000, // d = 2
- 1200, // d = 3
- 1100, // d = 4
- 1000, // d = 5
- 900, // d = 6
- 800, // d = 7
- 700, // d = 8
- 700, // d = 9
- };
- private static final int points[] = {
- 10000, // d = 2
- 1900000, // d = 3
- 1800000, // d = 4
- 1700000, // d = 5
- 100000, // d = 6
- 1300000, // d = 7
- 1150000, // d = 8
- 1000000, // d = 9
- };
- public static void main( String args[] ) {
- Driver model = new Driver();
- time = System.currentTimeMillis();
- model.run();
- model.report_tables();
- System.out.println("\n-------\nMonte Carlo integration. DIMENSION = " + dim + " SAMPLES = " + points[dim - 2]);
- System.out.println("avg: " + sphereVolume.mean());
- //System.out.println("Confidence interval of 99%: [" + sphereVolume.conf_lower(.99) + ", " + sphereVolume.conf_upper(.99) + "]");
- System.out.println("Confidence interval of 99%: [" + lower + ", " + upper + "]");
- System.out.println("Number of runs: " + sphereVolume.count());
- System.out.println("Expected volume ~~ " + expected[dim-2]);
- System.out.println( "Runs per sec ~~" + sphereVolume.count() / ((System.currentTimeMillis() - time) / 1000));
- System.out.println( "Time ~~ " + (System.currentTimeMillis() - time)/1000);
- }
- public Driver( ) {
- super( "App" );
- }
- public void run( ) {
- start( new Sim() );
- }
- private class Sim extends Process {
- public Sim( ) {
- super( "Sim" );
- }
- public void run() {
- sphereVolume = new Table( "Hits ");
- sphereVolume.setPermanent(true);
- sphereVolume.confidence();
- rand.setSeed(System.currentTimeMillis());
- add( new Monte( ) );
- //add( new Monte( ) );
- eventListEmpty().untimed_wait();
- }
- }
- private class Monte extends Process {
- public Monte() { super( "Monte" ); }
- public ArrayList<Double> volume = new ArrayList<>();
- public void run() {
- while ( true ) {
- double hits = 0;
- for (int i = 0; i < points[dim - 2]; i++) {
- double dist = total();
- if (dist <= 1)
- hits++;
- }
- double vol = (pow(2, dim) * hits) / points[dim - 2];
- sphereVolume.record(vol);
- volume.add(vol);
- System.out.println(sphereVolume.count());
- //run++;
- upper = sphereVolume.mean() + ( 2.576 * sphereVolume.stddev() / sqrt(sphereVolume.count()) );
- lower = sphereVolume.mean() - ( 2.576 * sphereVolume.stddev() / sqrt(sphereVolume.count()) );
- //System.out.println( "Calc lower: " + lower + " Calc upper: " + upper);
- //System.out.println( upper - lower );
- //double var = getVar();
- double temp_std = sqrt(getVar() / sphereVolume.count());
- //System.out.println(" CSIM std: " + temp_std + " Temp std: " + sqrt(getVar() / sphereVolume.count()));
- System.out.println( "csim var: " + sphereVolume.var() + " Calc var " + getVar());
- System.out.println( "Mean: " + sphereVolume.mean() + " STD " + sphereVolume.stddev() + " var " + sphereVolume.var());
- //System.out.println( "Upper: " + sphereVolume.conf_upper(.99) + " Lower: " + sphereVolume.conf_lower(.99));
- //double haynes = 2.576 * sphereVolume.stddev() / sphereVolume.mean();
- double mine = (2.576 * temp_std) / sphereVolume.mean();
- System.out.println(" Mine: " + mine);
- //System.out.println(sphereVolume.conf_upper(.99) - sphereVolume.conf_lower(.99));
- //System.out.println("CAlc " + (upper - lower));
- //if( abs(sphereVolume.conf_upper(.99) - sphereVolume.conf_lower(.99))/abs(sphereVolume.conf_lower(.99)) < pow( 10, -4 ) )
- // break;
- // if( (sphereVolume.conf_upper(.99) - sphereVolume.conf_lower(.99)) == 0 )
- // {
- // run++;
- // continue;
- // }
- // if( (sphereVolume.conf_upper(.99) - sphereVolume.conf_lower(.99)) < pow( 10, -4 ))
- // break;
- if( mine == 0 || sphereVolume.count() < 1000)
- continue;
- if( mine < pow( 10 , -4) )
- break;
- }
- }
- public double getVar() {
- double mean = sphereVolume.mean();
- double temp = 0;
- for(double a :volume)
- temp += (a-mean)*(a-mean);
- return temp/(volume.size()-1);
- }
- public double total( ) {
- double num[] = new double[dim];
- double sum = 0;
- for( int j = 0; j < dim ; j++) {
- num[j] = rand.uniform(0, 1);
- sum += pow( num[j] , 2);
- }
- return sqrt( sum );
- }
- }
- }
Add Comment
Please, Sign In to add comment