Refactor SimulationBuilder and SimulationMultiple to accept PrintStream for output; update tests to use null output stream

This commit is contained in:
2025-02-19 11:22:52 +01:00
parent 121d4cd44a
commit 611e14e6fa
3 changed files with 62 additions and 31 deletions

View File

@@ -2,6 +2,7 @@ package net.berack.upo.valpre;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.concurrent.ExecutionException;
import com.esotericsoftware.kryo.KryoException;
@@ -13,6 +14,7 @@ import net.berack.upo.valpre.sim.EndCriteria.MaxTime;
import net.berack.upo.valpre.sim.Net;
import net.berack.upo.valpre.sim.SimulationMultiple;
import net.berack.upo.valpre.sim.stats.CsvResult;
import net.berack.upo.valpre.sim.stats.Result;
/**
* This class is responsible for running the simulation. It parses the arguments
@@ -230,23 +232,38 @@ public class SimulationBuilder {
* @throws ExecutionException If the simulation has an error.
* @throws IOException If the CSV file has a problem.
*/
public void run() throws InterruptedException, ExecutionException, IOException {
public Result.Summary run() throws InterruptedException, ExecutionException, IOException {
return this.run(System.out);
}
/**
* Run the simulation with the given parameters.
* At the end it prints the results and saves them to a CSV file if requested.
*
* @param out the output stream to print the results
* @throws InterruptedException If the simulation is interrupted.
* @throws ExecutionException If the simulation has an error.
* @throws IOException If the CSV file has a problem.
*/
public Result.Summary run(PrintStream out) throws InterruptedException, ExecutionException, IOException {
var nano = System.nanoTime();
var sim = new SimulationMultiple(this.net);
var summary = switch (this.type) {
case Incremental -> sim.runIncremental(this.seed, this.runs, this.confidences, this.endCriteria);
case Incremental -> sim.runIncremental(this.seed, this.runs, out, this.confidences, this.endCriteria);
case Parallel -> sim.runParallel(this.seed, this.runs, this.endCriteria);
case Normal -> sim.run(this.seed, this.runs, this.endCriteria);
};
nano = System.nanoTime() - nano;
System.out.print(summary);
System.out.println("Final time " + nano / 1e6 + "ms");
out.print(summary);
out.println("Final time " + nano / 1e6 + "ms");
if (csv != null) {
new CsvResult(this.csv).saveResults(summary.getRuns());
System.out.println("Data saved to " + this.csv);
out.println("Data saved to " + this.csv);
}
return summary;
}
/**

View File

@@ -1,5 +1,6 @@
package net.berack.upo.valpre.sim;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
@@ -99,16 +100,21 @@ public class SimulationMultiple {
* Run the simulation multiple times with the given seed and end criteria. The
* simulation runs will stop when the relative error of the confidence index is
* less than the given value.
* The results are printed on the console.
* The results are printed on the PrintStream.
*
* @param seed The seed to use for the random number generator.
* @param criterias The criteria to determine when to end the simulation. If
* null then the simulation will run until there are no more
* events.
* @param seed The seed to use for the random number generator.
* @param runs The maximum number of runs to perform.
* @param stream The PrintStream to print the results.
* @param confidences The confidence indices to use to determine when to stop
* the simulation.
* @param criterias The criteria to determine when to end the simulation. If
* null then the simulation will run until there are no more
* events.
* @return The statistics the network.
* @throws IllegalArgumentException If the confidence is not set.
*/
public Result.Summary runIncremental(long seed, int runs, ConfidenceIndices confidences, EndCriteria... criterias) {
public Result.Summary runIncremental(long seed, int runs, PrintStream stream, ConfidenceIndices confidences,
EndCriteria... criterias) {
if (confidences == null)
throw new IllegalArgumentException("Confidence must be not null");
@@ -133,11 +139,11 @@ public class SimulationMultiple {
var oneSting = String.join("], [", errString);
output.append('[').append(oneSting).append("]");
System.out.print(output);
stream.print(output);
}
}
System.out.println(); // remove last printed line
stream.println(); // remove last printed line
return results;
}

View File

@@ -4,6 +4,8 @@ import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.HashSet;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
@@ -153,33 +155,39 @@ public class TestSaveExamplesNet {
@Test
@AfterAll
public void multiSimulation1() throws Exception {
new SimulationBuilder(net1)
.setCsv(csv1)
.setMaxRuns(1000)
.setSeed(2007539552L)
.setParallel(true)
.run();
try (var newOut = new PrintStream(OutputStream.nullOutputStream())) {
new SimulationBuilder(net1)
.setCsv(csv1)
.setMaxRuns(1000)
.setSeed(2007539552L)
.setParallel(true)
.run(newOut);
}
}
@Test
@AfterAll
public void multiSimulation2() throws Exception {
new SimulationBuilder(net2)
.setCsv(csv2)
.setMaxRuns(1000)
.setSeed(2007539552L)
.setParallel(true)
.run();
try (var newOut = new PrintStream(OutputStream.nullOutputStream())) {
new SimulationBuilder(net2)
.setCsv(csv2)
.setMaxRuns(1000)
.setSeed(2007539552L)
.setParallel(true)
.run(newOut);
}
}
@Test
@AfterAll
public void multiSimulation3() throws Exception {
new SimulationBuilder(net3)
.setCsv(csv3)
.setMaxRuns(1000)
.setSeed(2007539552L)
.setParallel(true)
.run();
try (var newOut = new PrintStream(OutputStream.nullOutputStream())) {
new SimulationBuilder(net3)
.setCsv(csv3)
.setMaxRuns(1000)
.setSeed(2007539552L)
.setParallel(true)
.run(newOut);
}
}
}