Readme
- Refactor simulation handling and improve net iteration methods
This commit is contained in:
@@ -18,7 +18,7 @@ public class Main {
|
||||
var param = Main.getParameters(program, subArgs);
|
||||
switch (program) {
|
||||
case "simulation" -> {
|
||||
new Simulation(param.get("net"))
|
||||
new SimulationBuilder(param.get("net"))
|
||||
.setCsv(param.get("csv"))
|
||||
.setRuns(param.getOrDefault("runs", Integer::parseInt, 100))
|
||||
.setSeed(param.getOrDefault("seed", Long::parseLong, 2007539552L))
|
||||
|
||||
@@ -142,7 +142,9 @@ public class Plot {
|
||||
var columnKey = String.format("%.3f", columnVal);
|
||||
dataset.addValue(frequency[i], "Frequency", columnKey);
|
||||
}
|
||||
this.panelBarChart.getChart().getCategoryPlot().setDataset(dataset);
|
||||
var chart = this.panelBarChart.getChart();
|
||||
chart.getCategoryPlot().setDataset(dataset);
|
||||
chart.setTitle(stat + " distribution");
|
||||
|
||||
var model = this.statList.getModel();
|
||||
for (int i = 0; i < model.getSize(); i++) {
|
||||
|
||||
@@ -14,7 +14,7 @@ import net.berack.upo.valpre.sim.stats.CsvResult;
|
||||
* This class is responsible for running the simulation. It parses the arguments
|
||||
* and runs the simulation with the given parameters.
|
||||
*/
|
||||
public class Simulation {
|
||||
public class SimulationBuilder {
|
||||
private String csv;
|
||||
private int runs;
|
||||
private long seed;
|
||||
@@ -28,7 +28,7 @@ public class Simulation {
|
||||
* @param netFile the net file to load
|
||||
* @throws IOException if the file has a problem
|
||||
*/
|
||||
public Simulation(String netFile) throws IOException {
|
||||
public SimulationBuilder(String netFile) throws IOException {
|
||||
try {
|
||||
var file = Parameters.getFileOrExample(netFile);
|
||||
this.net = Net.load(file);
|
||||
@@ -46,7 +46,7 @@ public class Simulation {
|
||||
* @param net the net
|
||||
* @throws IllegalArgumentException if the net is null
|
||||
*/
|
||||
public Simulation(Net net) {
|
||||
public SimulationBuilder(Net net) {
|
||||
if (net == null)
|
||||
throw new IllegalArgumentException("Net needed!");
|
||||
this.net = net;
|
||||
@@ -59,7 +59,7 @@ public class Simulation {
|
||||
* @throws IllegalArgumentException if the runs are less than 1
|
||||
* @return this simulation
|
||||
*/
|
||||
public Simulation setRuns(int runs) {
|
||||
public SimulationBuilder setRuns(int runs) {
|
||||
if (runs <= 0)
|
||||
throw new IllegalArgumentException("Runs must be greater than 0!");
|
||||
|
||||
@@ -73,7 +73,7 @@ public class Simulation {
|
||||
* @param seed the seed
|
||||
* @return this simulation
|
||||
*/
|
||||
public Simulation setSeed(long seed) {
|
||||
public SimulationBuilder setSeed(long seed) {
|
||||
this.seed = seed;
|
||||
return this;
|
||||
}
|
||||
@@ -85,7 +85,7 @@ public class Simulation {
|
||||
* @param parallel if the simulation should run in parallel
|
||||
* @return this simulation
|
||||
*/
|
||||
public Simulation setParallel(boolean parallel) {
|
||||
public SimulationBuilder setParallel(boolean parallel) {
|
||||
this.parallel = parallel;
|
||||
return this;
|
||||
}
|
||||
@@ -96,7 +96,7 @@ public class Simulation {
|
||||
* @param csv the CSV file
|
||||
* @return this simulation
|
||||
*/
|
||||
public Simulation setCsv(String csv) {
|
||||
public SimulationBuilder setCsv(String csv) {
|
||||
this.csv = csv;
|
||||
return this;
|
||||
}
|
||||
@@ -110,7 +110,7 @@ public class Simulation {
|
||||
* @return this simulation
|
||||
* @throws IllegalArgumentException if the criteria are null
|
||||
*/
|
||||
public Simulation setEndCriteria(EndCriteria... criterias) {
|
||||
public SimulationBuilder setEndCriteria(EndCriteria... criterias) {
|
||||
if (criterias == null)
|
||||
throw new IllegalArgumentException("End criteria cannot be null!");
|
||||
this.endCriteria = criterias;
|
||||
@@ -7,8 +7,8 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.objenesis.strategy.StdInstantiatorStrategy;
|
||||
|
||||
@@ -25,7 +25,7 @@ import net.berack.upo.valpre.rand.Rng;
|
||||
* connections between nodes. In order to start a simulation, at least one node
|
||||
* must be a Source or must generate at least one event to be processed.
|
||||
*/
|
||||
public final class Net {
|
||||
public final class Net implements Iterable<ServerNode> {
|
||||
private final List<ServerNode> servers = new ArrayList<>();
|
||||
private final HashMap<ServerNode, Integer> indices = new HashMap<>();
|
||||
private final List<List<Connection>> connections = new ArrayList<>();
|
||||
@@ -220,16 +220,6 @@ public final class Net {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply a consumer to all the nodes. The implementation uses a stream and for
|
||||
* this reason you should consider to make thread safe the consumer.
|
||||
*
|
||||
* @param consumer a function that takes in input a ServerNode
|
||||
*/
|
||||
public void forEachNode(Consumer<ServerNode> consumer) {
|
||||
this.servers.stream().forEach(consumer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the current net to a file.
|
||||
* The resulting file is saved with Kryo.
|
||||
@@ -304,4 +294,9 @@ public final class Net {
|
||||
this.weight = weight;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<ServerNode> iterator() {
|
||||
return this.servers.iterator();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,8 +40,17 @@ public final class Simulation {
|
||||
this.rng = rng;
|
||||
this.time = 0.0d;
|
||||
|
||||
// check for ending criteria in simulation
|
||||
boolean hasLimit = false;
|
||||
for (var node : net)
|
||||
if (node.spawnArrivals != Integer.MAX_VALUE)
|
||||
hasLimit = true;
|
||||
|
||||
if (!hasLimit && (criterias == null || criterias.length == 0))
|
||||
throw new IllegalArgumentException("At least one end criteria is needed!");
|
||||
|
||||
// Initial arrivals (if spawned)
|
||||
net.forEachNode(node -> {
|
||||
net.forEach(node -> {
|
||||
this.states.put(node.name, new NodeState());
|
||||
if (node.shouldSpawnArrival(0))
|
||||
this.addArrival(node);
|
||||
@@ -152,7 +161,7 @@ public final class Simulation {
|
||||
* on the given node, and the delay is determined by the node's service
|
||||
* distribution.
|
||||
*
|
||||
* @param node The node to create the event for.
|
||||
* @param node The node to create the event for.
|
||||
* @param state The current state of the node
|
||||
*/
|
||||
public void addDepartureIfPossible(ServerNode node, NodeState state) {
|
||||
|
||||
@@ -134,7 +134,7 @@ public class TestSimulation {
|
||||
var nodes = new HashSet<ServerNode>();
|
||||
nodes.add(node);
|
||||
nodes.add(node1);
|
||||
net.forEachNode(n -> assertTrue(nodes.contains(n)));
|
||||
net.forEach(n -> assertTrue(nodes.contains(n)));
|
||||
|
||||
net.addConnection(0, 1, 1.0);
|
||||
var conn = net.getChildren(0);
|
||||
|
||||
Reference in New Issue
Block a user