-TesseracT-

Untitled

Mar 24th, 2020
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.93 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.concurrent.*;
  3. import java.util.concurrent.atomic.*;
  4.  
  5. public class Main {
  6.    
  7.     private static int no_of_simulations;
  8.     private static double p0, p;
  9.     private static volatile AtomicInteger count = new AtomicInteger(0); // counts how many simulations have finished
  10.     private static volatile ConcurrentLinkedQueue<Long> data = new ConcurrentLinkedQueue<Long>(); // contains all simulated data
  11.    
  12.     public static void main(String[] args) throws InterruptedException, IOException {
  13.         // parse the command line arguments
  14.         no_of_simulations = Integer.parseInt(args[0]);
  15.         p0 = Double.parseDouble(args[1]);
  16.         p = Double.parseDouble(args[2]);
  17.         if (p0 < 0.0 || p0 > 1.0 || p < 0.0 || p > 1.0)
  18.             throw new IllegalArgumentException("p0 and p must be in the interval (0, 1).");
  19.         double mean = Math.round(1000.0 * (1.0 - p0) / p) / 1000.0; // mean rounded to three decimal places
  20.         int no_of_threads = Integer.parseInt(args[3]);
  21.        
  22.         // declare and start all threads
  23.         Thread[] workers = new Thread[no_of_threads];
  24.         int simulations_per_thread = no_of_simulations / no_of_threads;
  25.         int remainder = no_of_simulations % no_of_threads;
  26.         for (int i = 0; i < no_of_threads; i++) {
  27.             if (i == 0)
  28.                 workers[i] = new GW(simulations_per_thread + remainder);
  29.             else
  30.                 workers[i] = new GW(simulations_per_thread);
  31.             workers[i].start();
  32.         }
  33.        
  34.         // wait for all threads to finish
  35.         for (Thread t : workers)
  36.             t.join();
  37.        
  38.         StringBuilder sb = new StringBuilder();
  39.         for (Long element : data)
  40.             sb.append(element.toString() + "\n");
  41.        
  42.         String path = "data " + no_of_simulations + " " + mean + ".csv";
  43.         BufferedWriter br = new BufferedWriter(new FileWriter(path));
  44.         try {
  45.             br.write(sb.toString());
  46.         } catch (IOException e) {
  47.             System.out.println(e);
  48.         } finally {
  49.             br.close();
  50.         }
  51.        
  52.         System.out.println("Done! Data written to " + path);
  53.     }
  54.    
  55.  
  56.     private static class GW extends Thread {
  57.         private int simulations;
  58.         private static final int progress_step = 100;
  59.        
  60.         public GW(int simulations) { this.simulations = simulations; }
  61.        
  62.         // simulate <<this.simulations>> number of GW processes
  63.         public void run() {
  64.             int total_progress;
  65.             for (int i = 1; i <= simulations; i++) {
  66.                 data.add(kontur2());
  67.                 if ((i % progress_step == 0 && i > 1) || i == simulations) {
  68.                     total_progress = count.addAndGet(progress_step);
  69.                     System.out.println("Progress: " + total_progress + " of " + no_of_simulations);
  70.                 }
  71.             }
  72.         }
  73.        
  74.         // simulates a GW process according to the branching method
  75.         private long branching() {
  76.             long w = 1, z = 1, sum;
  77.             while (z != 0) {
  78.                 sum = 0;
  79.                 for (int i = 1; i <= z; i++)
  80.                     sum += random_LF(p0, p);
  81.                 z = sum;
  82.                 w += z;
  83.             }
  84.             return w;
  85.         }
  86.        
  87.         // simulates a GW process according to the contour process
  88.         private long kontur() {
  89.             int v = 0;
  90.             long s = 0;
  91.             double u;
  92.             while (v > -1) {
  93.                 int rg = random_geom(p0) - 1; // argumentet för rgeom är olika beroende på om Math.random() <= 1-p0 eller inte
  94.                 if (Math.random() <= 1-p0) {
  95.                     v += rg; s += rg;
  96.                 }
  97.                 else if (v - rg >= -1) {
  98.                     v -= rg; s += rg;
  99.                 }
  100.                 else {
  101.                     v = -1; s += v + 1;
  102.                 }
  103.             }
  104.             return (s / 2 + 1);
  105.         }
  106.        
  107.         private long kontur2() {
  108.             long v = 0, s = 1;
  109.             int rg;
  110.             while (v > -1) {
  111.                 if (Math.random() <= 1-p0) {
  112.                     rg = random_geom(p0);
  113.                     v += rg;
  114.                     s += rg;
  115.                 }
  116.                 else {
  117.                     rg = random_geom(1-p);
  118.                     v -= rg;
  119.                 }
  120.             }
  121.             return s;
  122.         }
  123.        
  124.         // generates a random variable from the shifted geometric distribution
  125.         private int random_geom(double p) {
  126.             double u;
  127.             do {
  128.                 u = Math.random();
  129.             } while (u == 0.0);
  130.             return (int)Math.ceil(Math.log(1-u)/Math.log(1-p));
  131.         }
  132.        
  133.         // generates a random variable from the linear fractional distribution
  134.         private int random_LF(double p0, double p) {
  135.             if (Math.random() <= 1-p0)
  136.                 return random_geom(p);
  137.             else
  138.                 return 0;
  139.         }
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment