From b7b95fad7b203c70d1af62f1cd55c1d00d2b499a Mon Sep 17 00:00:00 2001 From: Berack96 Date: Wed, 29 Jan 2025 16:53:40 +0100 Subject: [PATCH] Added Save and Load Net - added kryo - added save/load Net - added examples in resources - Main require a file.net --- .gitignore | 1 + .vscode/launch.json | 17 ++---- pom.xml | 5 ++ src/main/java/net/berack/upo/valpre/Main.java | 56 +++++------------- .../java/net/berack/upo/valpre/sim/Net.java | 45 ++++++++++++++ src/main/resources/example1.net | Bin 0 -> 388 bytes src/main/resources/example2.net | Bin 0 -> 576 bytes 7 files changed, 71 insertions(+), 53 deletions(-) create mode 100644 src/main/resources/example1.net create mode 100644 src/main/resources/example2.net diff --git a/.gitignore b/.gitignore index 9122c96..0fb3b56 100644 --- a/.gitignore +++ b/.gitignore @@ -25,5 +25,6 @@ replay_pid* # mine +target/* pdf/* *.csv \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index d2fb8bc..30215a4 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,31 +6,24 @@ "configurations": [ { "type": "java", - "name": "Run1k Rand", + "name": "Run1k Simple", "request": "launch", "mainClass": "net.berack.upo.valpre.Main", - "args": "-runs 1000 -p -seed 0" + "args": "-net example1.net -runs 1000 -p -seed 0" }, { "type": "java", - "name": "Run1k save", + "name": "Run1k Complex", "request": "launch", "mainClass": "net.berack.upo.valpre.Main", - "args": "-runs 1000 -p -csv result.csv" - }, - { - "type": "java", - "name": "Run1k", - "request": "launch", - "mainClass": "net.berack.upo.valpre.Main", - "args": "-runs 1000 -p" + "args": "-net example2.net -runs 1000 -p -seed 0" }, { "type": "java", "name": "Run10", "request": "launch", "mainClass": "net.berack.upo.valpre.Main", - "args": "-runs 10" + "args": "-net example1.net -runs 10" }, ] } \ No newline at end of file diff --git a/pom.xml b/pom.xml index c9b6055..1ea74b4 100644 --- a/pom.xml +++ b/pom.xml @@ -31,5 +31,10 @@ commons-math3 3.6.1 + + com.esotericsoftware + kryo + 5.6.2 + \ No newline at end of file diff --git a/src/main/java/net/berack/upo/valpre/Main.java b/src/main/java/net/berack/upo/valpre/Main.java index a72ae64..67e923c 100644 --- a/src/main/java/net/berack/upo/valpre/Main.java +++ b/src/main/java/net/berack/upo/valpre/Main.java @@ -3,10 +3,8 @@ package net.berack.upo.valpre; import java.util.HashMap; import java.util.Map; -import net.berack.upo.valpre.rand.Distribution; -import net.berack.upo.valpre.sim.SimulationMultiple; import net.berack.upo.valpre.sim.Net; -import net.berack.upo.valpre.sim.ServerNode; +import net.berack.upo.valpre.sim.SimulationMultiple; public class Main { public static void main(String[] args) throws Exception { @@ -25,11 +23,17 @@ public class Main { csv = arguments.get("csv"); var parallel = arguments.containsKey("p"); - var net = moreComplexNet(); + var file = arguments.get("net"); + if (file == null) + throw new IllegalArgumentException("Net file needed! Use -net "); + if (file.startsWith("example")) + file = Main.class.getClassLoader().getResource(file).getPath(); + // var maxDepartures = new EndSimulationCriteria.MaxDepartures("Queue", total); // var maxTime = new EndSimulationCriteria.MaxTime(1000.0); /// Run multiple simulations + var net = Net.load(file); var nano = System.nanoTime(); var sim = new SimulationMultiple(net); var results = parallel ? sim.runParallel(seed, runs) : sim.run(seed, runs); @@ -45,44 +49,12 @@ public class Main { } } - public static Net simpleNet() { - var lambda = 1.0 / 4.5; - var mu = 3.2; - var sigma = 0.6; - var total = 10000; - - var distrExp = new Distribution.Exponential(lambda); - var distrNorm = new Distribution.NormalBoxMuller(mu, sigma); - - // Build the network - var net = new Net(); - net.addNode(ServerNode.createLimitedSource("Source", distrExp, total)); - net.addNode(ServerNode.createQueue("Queue", 1, distrNorm)); - net.addConnection(0, 1, 1.0); - net.normalizeWeights(); - - return net; - } - - public static Net moreComplexNet() { - var net = simpleNet(); - var distrNorm = new Distribution.NormalBoxMuller(3.2, 0.6); - var distrNorm2 = new Distribution.NormalBoxMuller(4.2, 0.6); - var distrUnav = new Distribution.UnavailableTime(0.2, distrNorm2); - - // Build the network - net.addNode(ServerNode.createQueue("Queue Wait", 1, distrNorm, distrUnav)); - net.addConnection(1, 2, 1.0); - net.normalizeWeights(); - - return net; - } - public static Map parseParameters(String[] args) { var arguments = new HashMap(); arguments.put("p", false); arguments.put("seed", true); arguments.put("runs", true); + arguments.put("net", true); arguments.put("csv", true); var param = new Parameters("-", arguments); @@ -90,10 +62,12 @@ public class Main { return param.parse(args); } catch (IllegalArgumentException e) { var descriptions = new HashMap(); - descriptions.put("p", "Add this if you want the simulation to use threads (one each run)"); - descriptions.put("seed", "The seed of the simulation"); - descriptions.put("runs", "How many runs the simulator should run"); - descriptions.put("csv", "The filename for saving every run statistics"); + descriptions.put("p", "Add this if you want the simulation to use threads (one each run)."); + descriptions.put("seed", "The seed of the simulation."); + descriptions.put("runs", "How many runs the simulator should run."); + descriptions.put("net", + "The net to use. It should be a file. Use example1.net or example2.net for the provided ones."); + descriptions.put("csv", "The filename for saving every run statistics."); System.out.println(e.getMessage()); System.out.println(param.helper(descriptions)); diff --git a/src/main/java/net/berack/upo/valpre/sim/Net.java b/src/main/java/net/berack/upo/valpre/sim/Net.java index 0802f0a..8bf8842 100644 --- a/src/main/java/net/berack/upo/valpre/sim/Net.java +++ b/src/main/java/net/berack/upo/valpre/sim/Net.java @@ -1,10 +1,20 @@ package net.berack.upo.valpre.sim; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.function.Consumer; +import org.objenesis.strategy.StdInstantiatorStrategy; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.KryoException; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + import net.berack.upo.valpre.rand.Rng; /** @@ -218,6 +228,41 @@ public final class Net { this.servers.stream().forEach(consumer); } + /** + * Save the current net to a file. + * The resulting file is saved with Kryo. + * + * @param file the name of the file + * @throws FileNotFoundException if the path doesn't exist + */ + public void save(String file) throws FileNotFoundException { + var kryo = new Kryo(); + kryo.setRegistrationRequired(false); + + try (var out = new Output(new FileOutputStream(file))) { + kryo.writeClassAndObject(out, this); + } + } + + /** + * Load the net from the file passed as input. + * The net will be the same as the one saved. + * + * @param file the file to load + * @return a new Net object + * @throws KryoException if the file saved is not a net + * @throws FileNotFoundException if the file is not found + */ + public static Net load(String file) throws KryoException, FileNotFoundException { + var kryo = new Kryo(); + kryo.setRegistrationRequired(false); + kryo.setInstantiatorStrategy(new StdInstantiatorStrategy()); + + try (var in = new Input(new FileInputStream(file))) { + return (Net) kryo.readClassAndObject(in); + } + } + /** * A static inner class used to represent the connection between two nodes */ diff --git a/src/main/resources/example1.net b/src/main/resources/example1.net new file mode 100644 index 0000000000000000000000000000000000000000..f8e0fd8dec078034eabde9eee2dc1c63713ffc40 GIT binary patch literal 388 zcmZQ%$V)BJOG+(DOwQITEy&j^OUx-KO4TdQ%+>Qt{ldtYl~|UjS6Y&pqvu#ulvwGL zS^TA$k&&T^kqMv15*6qCyu8%plFa;f3``7Q@WGyukr`o}M`Ce?Z{i1LMi$%#1*aC3 zr55?+r#xi}EKMza%E*dcZBb%gik?emaY<2T640&rc`APSMY)MNPWctSr8zmNpJvSj z0Zs>FFnDawz`)4F$nx+1|Ns2K`K3k4PZ`;WHQBYIAU`iPuOu_^jm+^vAUb2eU=AY# U5HdD1vM`{SiL3zT15Bp?0Dd2iF8}}l literal 0 HcmV?d00001 diff --git a/src/main/resources/example2.net b/src/main/resources/example2.net new file mode 100644 index 0000000000000000000000000000000000000000..0428cd6d51fd7fb1b87eda9ec974b0aee8bc6d9f GIT binary patch literal 576 zcma)&&q~8U5XN_6TJfY0;K4)Bd+`C{U+`c9QpNK$9dLEC8#kL!uhN%LJoq*u)TfZ# zM2h5W+(!IUYNX%pWnjMFd^5llS>7ju(FM;E&2yBAA)bnuhg1TbV~#wtLh@~6@aj;c zWWIqPr_NlYFp1FApJn-MEs#@*o%tQq&FFJpJm}o`H3Vd0AxO9IU+}{Cn X4@Tg#8RqYX<|s1