Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.concurrent.*;
- import java.util.concurrent.atomic.*;
- public class Main {
- private static int no_of_simulations;
- private static double p0, p;
- private static volatile AtomicInteger count = new AtomicInteger(0); // counts how many simulations have finished
- private static volatile ConcurrentLinkedQueue<Long> data = new ConcurrentLinkedQueue<Long>(); // contains all simulated data
- public static void main(String[] args) throws InterruptedException, IOException {
- // parse the command line arguments
- no_of_simulations = Integer.parseInt(args[0]);
- p0 = Double.parseDouble(args[1]);
- p = Double.parseDouble(args[2]);
- if (p0 < 0.0 || p0 > 1.0 || p < 0.0 || p > 1.0)
- throw new IllegalArgumentException("p0 and p must be in the interval (0, 1).");
- double mean = Math.round(1000.0 * (1.0 - p0) / p) / 1000.0; // mean rounded to three decimal places
- int no_of_threads = Integer.parseInt(args[3]);
- // declare and start all threads
- Thread[] workers = new Thread[no_of_threads];
- int simulations_per_thread = no_of_simulations / no_of_threads;
- int remainder = no_of_simulations % no_of_threads;
- for (int i = 0; i < no_of_threads; i++) {
- if (i == 0)
- workers[i] = new GW(simulations_per_thread + remainder);
- else
- workers[i] = new GW(simulations_per_thread);
- workers[i].start();
- }
- // wait for all threads to finish
- for (Thread t : workers)
- t.join();
- StringBuilder sb = new StringBuilder();
- for (Long element : data)
- sb.append(element.toString() + "\n");
- String path = "data " + no_of_simulations + " " + mean + ".csv";
- BufferedWriter br = new BufferedWriter(new FileWriter(path));
- try {
- br.write(sb.toString());
- } catch (IOException e) {
- System.out.println(e);
- } finally {
- br.close();
- }
- System.out.println("Done! Data written to " + path);
- }
- private static class GW extends Thread {
- private int simulations;
- private static final int progress_step = 100;
- public GW(int simulations) { this.simulations = simulations; }
- // simulate <<this.simulations>> number of GW processes
- public void run() {
- int total_progress;
- for (int i = 1; i <= simulations; i++) {
- data.add(kontur2());
- if ((i % progress_step == 0 && i > 1) || i == simulations) {
- total_progress = count.addAndGet(progress_step);
- System.out.println("Progress: " + total_progress + " of " + no_of_simulations);
- }
- }
- }
- // simulates a GW process according to the branching method
- private long branching() {
- long w = 1, z = 1, sum;
- while (z != 0) {
- sum = 0;
- for (int i = 1; i <= z; i++)
- sum += random_LF(p0, p);
- z = sum;
- w += z;
- }
- return w;
- }
- // simulates a GW process according to the contour process
- private long kontur() {
- int v = 0;
- long s = 0;
- double u;
- while (v > -1) {
- int rg = random_geom(p0) - 1; // argumentet för rgeom är olika beroende på om Math.random() <= 1-p0 eller inte
- if (Math.random() <= 1-p0) {
- v += rg; s += rg;
- }
- else if (v - rg >= -1) {
- v -= rg; s += rg;
- }
- else {
- v = -1; s += v + 1;
- }
- }
- return (s / 2 + 1);
- }
- private long kontur2() {
- long v = 0, s = 1;
- int rg;
- while (v > -1) {
- if (Math.random() <= 1-p0) {
- rg = random_geom(p0);
- v += rg;
- s += rg;
- }
- else {
- rg = random_geom(1-p);
- v -= rg;
- }
- }
- return s;
- }
- // generates a random variable from the shifted geometric distribution
- private int random_geom(double p) {
- double u;
- do {
- u = Math.random();
- } while (u == 0.0);
- return (int)Math.ceil(Math.log(1-u)/Math.log(1-p));
- }
- // generates a random variable from the linear fractional distribution
- private int random_LF(double p0, double p) {
- if (Math.random() <= 1-p0)
- return random_geom(p);
- else
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment