From 78dcac0a56aa213b1c64ee6a01552c7929c044c4 Mon Sep 17 00:00:00 2001 From: Berack96 Date: Wed, 26 Mar 2025 14:54:55 +0100 Subject: [PATCH] Enhance simulation options in InteractiveConsole; add confidence index handling and new seed generation; update Plot behavior to dispose on close --- .../berack/upo/valpre/InteractiveConsole.java | 23 ++++++++++--------- src/main/java/net/berack/upo/valpre/Plot.java | 2 +- .../java/net/berack/upo/valpre/rand/Rng.java | 9 ++++++++ .../upo/valpre/sim/SimulationMultiple.java | 1 - 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/berack/upo/valpre/InteractiveConsole.java b/src/main/java/net/berack/upo/valpre/InteractiveConsole.java index 6d1b2b1..adb323d 100644 --- a/src/main/java/net/berack/upo/valpre/InteractiveConsole.java +++ b/src/main/java/net/berack/upo/valpre/InteractiveConsole.java @@ -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 -> { } diff --git a/src/main/java/net/berack/upo/valpre/Plot.java b/src/main/java/net/berack/upo/valpre/Plot.java index 15c815c..471cd12 100644 --- a/src/main/java/net/berack/upo/valpre/Plot.java +++ b/src/main/java/net/berack/upo/valpre/Plot.java @@ -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); }); } diff --git a/src/main/java/net/berack/upo/valpre/rand/Rng.java b/src/main/java/net/berack/upo/valpre/rand/Rng.java index 2a2cbd3..7b222f9 100644 --- a/src/main/java/net/berack/upo/valpre/rand/Rng.java +++ b/src/main/java/net/berack/upo/valpre/rand/Rng.java @@ -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; + } } diff --git a/src/main/java/net/berack/upo/valpre/sim/SimulationMultiple.java b/src/main/java/net/berack/upo/valpre/sim/SimulationMultiple.java index 755b79d..dfc280d 100644 --- a/src/main/java/net/berack/upo/valpre/sim/SimulationMultiple.java +++ b/src/main/java/net/berack/upo/valpre/sim/SimulationMultiple.java @@ -146,5 +146,4 @@ public class SimulationMultiple { stream.println(); // remove last printed line return results; } - }