Net creation #2

Merged
Berack96 merged 21 commits from net-creation into main 2025-03-16 18:02:38 +01:00
3 changed files with 62 additions and 31 deletions
Showing only changes of commit 611e14e6fa - Show all commits

View File

@@ -2,6 +2,7 @@ package net.berack.upo.valpre;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import com.esotericsoftware.kryo.KryoException; 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.Net;
import net.berack.upo.valpre.sim.SimulationMultiple; import net.berack.upo.valpre.sim.SimulationMultiple;
import net.berack.upo.valpre.sim.stats.CsvResult; 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 * 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 ExecutionException If the simulation has an error.
* @throws IOException If the CSV file has a problem. * @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 nano = System.nanoTime();
var sim = new SimulationMultiple(this.net); var sim = new SimulationMultiple(this.net);
var summary = switch (this.type) { 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 Parallel -> sim.runParallel(this.seed, this.runs, this.endCriteria);
case Normal -> sim.run(this.seed, this.runs, this.endCriteria); case Normal -> sim.run(this.seed, this.runs, this.endCriteria);
}; };
nano = System.nanoTime() - nano; nano = System.nanoTime() - nano;
System.out.print(summary); out.print(summary);
System.out.println("Final time " + nano / 1e6 + "ms"); out.println("Final time " + nano / 1e6 + "ms");
if (csv != null) { if (csv != null) {
new CsvResult(this.csv).saveResults(summary.getRuns()); 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; package net.berack.upo.valpre.sim;
import java.io.PrintStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors; 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 * 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 * simulation runs will stop when the relative error of the confidence index is
* less than the given value. * 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 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 * @param criterias The criteria to determine when to end the simulation. If
* null then the simulation will run until there are no more * null then the simulation will run until there are no more
* events. * events.
* @return The statistics the network. * @return The statistics the network.
* @throws IllegalArgumentException If the confidence is not set. * @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) if (confidences == null)
throw new IllegalArgumentException("Confidence must be not null"); throw new IllegalArgumentException("Confidence must be not null");
@@ -133,11 +139,11 @@ public class SimulationMultiple {
var oneSting = String.join("], [", errString); var oneSting = String.join("], [", errString);
output.append('[').append(oneSting).append("]"); 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; return results;
} }

View File

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