Enhance simulation options in InteractiveConsole; add confidence index handling and new seed generation; update Plot behavior to dispose on close

This commit is contained in:
2025-03-26 14:54:55 +01:00
parent 57c9dd9733
commit 78dcac0a56
4 changed files with 22 additions and 13 deletions

View File

@@ -1,16 +1,15 @@
package net.berack.upo.valpre;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;
import com.esotericsoftware.kryo.KryoException;
import net.berack.upo.valpre.rand.Distribution;
import net.berack.upo.valpre.rand.Rng;
import net.berack.upo.valpre.sim.Net;
import net.berack.upo.valpre.sim.ServerNode;
@@ -137,16 +136,18 @@ public class InteractiveConsole {
/**
* Run the simulation with the net.
*/
private void simpleRuns() throws InterruptedException, ExecutionException, IOException {
var choice = choose("Choose what to do:", "100 Run", "1K Runs", "1K Runs + Plot");
private void simpleRuns() throws Exception {
var choice = choose("Choose what to do:", "100 Run", "1K Runs", "Run max 10K with confidence", "Back");
var seed = Rng.newSeed();
var csv = "run" + seed + ".csv";
switch (choice) {
case 1 -> new SimulationBuilder(net).setMaxRuns(100).setParallel(true).run();
case 2 -> new SimulationBuilder(net).setMaxRuns(1000).setParallel(true).run();
case 3 -> {
var randName = "rand" + System.currentTimeMillis() + ".csv";
new SimulationBuilder(net).setMaxRuns(1000).setParallel(true).setCsv(randName).run();
new Plot(randName).show();
new File(randName).delete();
case 1 -> new SimulationBuilder(net).setSeed(seed).setMaxRuns(100).setParallel(true).setCsv(csv).run();
case 2 -> new SimulationBuilder(net).setSeed(seed).setMaxRuns(1000).setParallel(true).setCsv(csv).run();
case 3 -> new SimulationBuilder(net).setSeed(seed).setMaxRuns(1000).setParallel(true).setCsv(csv).run();
case 4 -> {
var indices = ask("Confidence indices with format [node:stat=confidence:relativeError];[..]\n");
new SimulationBuilder(net).setSeed(seed).setMaxRuns(10000).parseConfidenceIndices(indices).setCsv(csv)
.run();
}
default -> {
}

View File

@@ -113,7 +113,7 @@ public class Plot {
var frame = new JFrame("Graph of the Simulation");
frame.add(rootPane);
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
});
}

View File

@@ -131,4 +131,13 @@ public class Rng {
var t = multiplier * (seed % Q) - R * (seed / Q);
return t > 0 ? t : (t + modulus);
}
/**
* This creates a new seed based on the current time.
*
* @return a new seed
*/
public static long newSeed() {
return System.currentTimeMillis() % MODULUS;
}
}

View File

@@ -146,5 +146,4 @@ public class SimulationMultiple {
stream.println(); // remove last printed line
return results;
}
}