Refactor end criteria handling in SimulationBuilder and remove static parse method from EndCriteria
This commit is contained in:
@@ -5,8 +5,6 @@ import java.net.URISyntaxException;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
import net.berack.upo.valpre.sim.EndCriteria;
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
if (args.length == 0)
|
if (args.length == 0)
|
||||||
@@ -23,7 +21,7 @@ public class Main {
|
|||||||
.setMaxRuns(param.getOrDefault("runs", Integer::parseInt, 100))
|
.setMaxRuns(param.getOrDefault("runs", Integer::parseInt, 100))
|
||||||
.setSeed(param.getOrDefault("seed", Long::parseLong, 2007539552L))
|
.setSeed(param.getOrDefault("seed", Long::parseLong, 2007539552L))
|
||||||
.setParallel(param.get("p") != null)
|
.setParallel(param.get("p") != null)
|
||||||
.setEndCriteria(EndCriteria.parse(param.get("end")))
|
.parseEndCriteria(param.get("end"))
|
||||||
.parseConfidenceIndices(param.get("i"))
|
.parseConfidenceIndices(param.get("i"))
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ import com.esotericsoftware.kryo.KryoException;
|
|||||||
|
|
||||||
import net.berack.upo.valpre.sim.ConfidenceIndices;
|
import net.berack.upo.valpre.sim.ConfidenceIndices;
|
||||||
import net.berack.upo.valpre.sim.EndCriteria;
|
import net.berack.upo.valpre.sim.EndCriteria;
|
||||||
|
import net.berack.upo.valpre.sim.EndCriteria.MaxArrivals;
|
||||||
|
import net.berack.upo.valpre.sim.EndCriteria.MaxDepartures;
|
||||||
|
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;
|
||||||
@@ -122,6 +125,48 @@ public class SimulationBuilder {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the end criteria for the simulation.
|
||||||
|
* Parses the given string to create an array of end criteria.
|
||||||
|
* The string passed must be in the following format:
|
||||||
|
* [criteria1];[criteria2];...;[criteriaN]
|
||||||
|
*
|
||||||
|
* and each criteria must be in the following format:
|
||||||
|
* ClassName:param1,param2,...,paramN
|
||||||
|
*
|
||||||
|
* If the string is empty or null, no criteria are set.
|
||||||
|
* If one of the criteria is not valid, an exception is thrown.
|
||||||
|
*
|
||||||
|
* @param criterias The string to parse.
|
||||||
|
* @return this builder
|
||||||
|
* @throws IllegalArgumentException If one of the criteria is not valid.
|
||||||
|
*/
|
||||||
|
public SimulationBuilder parseEndCriteria(String criterias) {
|
||||||
|
if (criterias == null || criterias.isEmpty()) {
|
||||||
|
this.endCriteria = new EndCriteria[0];
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
var criteria = criterias.split(";");
|
||||||
|
this.endCriteria = new EndCriteria[criteria.length];
|
||||||
|
for (int i = 0; i < criteria.length; i++) {
|
||||||
|
var current = criteria[i].substring(1, criteria[i].length() - 1); // Remove the brackets
|
||||||
|
var parts = current.split(":");
|
||||||
|
if (parts.length != 2)
|
||||||
|
throw new IllegalArgumentException("Invalid criteria: " + current);
|
||||||
|
|
||||||
|
var className = parts[0];
|
||||||
|
var params = parts[1].split(",");
|
||||||
|
this.endCriteria[i] = switch (className) {
|
||||||
|
case "MaxArrivals" -> new MaxArrivals(params[0], Integer.parseInt(params[1]));
|
||||||
|
case "MaxDepartures" -> new MaxDepartures(params[0], Integer.parseInt(params[1]));
|
||||||
|
case "MaxTime" -> new MaxTime(Double.parseDouble(params[0]));
|
||||||
|
default -> throw new IllegalArgumentException("Invalid criteria: " + current);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a confidence index for the given node and stat.
|
* Add a confidence index for the given node and stat.
|
||||||
* The confidence index is used to determine when the simulation should stop.
|
* The confidence index is used to determine when the simulation should stop.
|
||||||
|
|||||||
@@ -12,45 +12,6 @@ public interface EndCriteria {
|
|||||||
*/
|
*/
|
||||||
public boolean shouldEnd(Simulation run);
|
public boolean shouldEnd(Simulation run);
|
||||||
|
|
||||||
/**
|
|
||||||
* Parses the given string to create an array of end criteria.
|
|
||||||
* The string passed must be in the following format:
|
|
||||||
* [criteria1];[criteria2];...;[criteriaN]
|
|
||||||
*
|
|
||||||
* and each criteria must be in the following format:
|
|
||||||
* ClassName:param1,param2,...,paramN
|
|
||||||
*
|
|
||||||
* If the string is empty or null, an empty array is returned.
|
|
||||||
* If one of the criteria is not valid, an exception is thrown.
|
|
||||||
*
|
|
||||||
* @param criterias The string to parse.
|
|
||||||
* @return An array of end criteria.
|
|
||||||
* @throws IllegalArgumentException If one of the criteria is not valid.
|
|
||||||
*/
|
|
||||||
public static EndCriteria[] parse(String criterias) {
|
|
||||||
if (criterias == null || criterias.isEmpty())
|
|
||||||
return new EndCriteria[0];
|
|
||||||
|
|
||||||
var criteria = criterias.split(";");
|
|
||||||
var endCriteria = new EndCriteria[criteria.length];
|
|
||||||
for (int i = 0; i < criteria.length; i++) {
|
|
||||||
var current = criteria[i].substring(1, criteria[i].length() - 1); // Remove the brackets
|
|
||||||
var parts = current.split(":");
|
|
||||||
if (parts.length != 2)
|
|
||||||
throw new IllegalArgumentException("Invalid criteria: " + current);
|
|
||||||
|
|
||||||
var className = parts[0];
|
|
||||||
var params = parts[1].split(",");
|
|
||||||
endCriteria[i] = switch (className) {
|
|
||||||
case "MaxArrivals" -> new MaxArrivals(params[0], Integer.parseInt(params[1]));
|
|
||||||
case "MaxDepartures" -> new MaxDepartures(params[0], Integer.parseInt(params[1]));
|
|
||||||
case "MaxTime" -> new MaxTime(Double.parseDouble(params[0]));
|
|
||||||
default -> throw new IllegalArgumentException("Invalid criteria: " + current);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return endCriteria;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ends the simulation when the given node has reached the specified number of
|
* Ends the simulation when the given node has reached the specified number of
|
||||||
* arrivals.
|
* arrivals.
|
||||||
|
|||||||
Reference in New Issue
Block a user