Added Save and Load Net
- added kryo - added save/load Net - added examples in resources - Main require a file.net
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -25,5 +25,6 @@ replay_pid*
|
|||||||
|
|
||||||
|
|
||||||
# mine
|
# mine
|
||||||
|
target/*
|
||||||
pdf/*
|
pdf/*
|
||||||
*.csv
|
*.csv
|
||||||
17
.vscode/launch.json
vendored
17
.vscode/launch.json
vendored
@@ -6,31 +6,24 @@
|
|||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"type": "java",
|
"type": "java",
|
||||||
"name": "Run1k Rand",
|
"name": "Run1k Simple",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"mainClass": "net.berack.upo.valpre.Main",
|
"mainClass": "net.berack.upo.valpre.Main",
|
||||||
"args": "-runs 1000 -p -seed 0"
|
"args": "-net example1.net -runs 1000 -p -seed 0"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "java",
|
"type": "java",
|
||||||
"name": "Run1k save",
|
"name": "Run1k Complex",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"mainClass": "net.berack.upo.valpre.Main",
|
"mainClass": "net.berack.upo.valpre.Main",
|
||||||
"args": "-runs 1000 -p -csv result.csv"
|
"args": "-net example2.net -runs 1000 -p -seed 0"
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "java",
|
|
||||||
"name": "Run1k",
|
|
||||||
"request": "launch",
|
|
||||||
"mainClass": "net.berack.upo.valpre.Main",
|
|
||||||
"args": "-runs 1000 -p"
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "java",
|
"type": "java",
|
||||||
"name": "Run10",
|
"name": "Run10",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"mainClass": "net.berack.upo.valpre.Main",
|
"mainClass": "net.berack.upo.valpre.Main",
|
||||||
"args": "-runs 10"
|
"args": "-net example1.net -runs 10"
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
5
pom.xml
5
pom.xml
@@ -31,5 +31,10 @@
|
|||||||
<artifactId>commons-math3</artifactId>
|
<artifactId>commons-math3</artifactId>
|
||||||
<version>3.6.1</version>
|
<version>3.6.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.esotericsoftware</groupId>
|
||||||
|
<artifactId>kryo</artifactId>
|
||||||
|
<version>5.6.2</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
@@ -3,10 +3,8 @@ package net.berack.upo.valpre;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
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.Net;
|
||||||
import net.berack.upo.valpre.sim.ServerNode;
|
import net.berack.upo.valpre.sim.SimulationMultiple;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
@@ -25,11 +23,17 @@ public class Main {
|
|||||||
csv = arguments.get("csv");
|
csv = arguments.get("csv");
|
||||||
var parallel = arguments.containsKey("p");
|
var parallel = arguments.containsKey("p");
|
||||||
|
|
||||||
var net = moreComplexNet();
|
var file = arguments.get("net");
|
||||||
|
if (file == null)
|
||||||
|
throw new IllegalArgumentException("Net file needed! Use -net <file>");
|
||||||
|
if (file.startsWith("example"))
|
||||||
|
file = Main.class.getClassLoader().getResource(file).getPath();
|
||||||
|
|
||||||
// var maxDepartures = new EndSimulationCriteria.MaxDepartures("Queue", total);
|
// var maxDepartures = new EndSimulationCriteria.MaxDepartures("Queue", total);
|
||||||
// var maxTime = new EndSimulationCriteria.MaxTime(1000.0);
|
// var maxTime = new EndSimulationCriteria.MaxTime(1000.0);
|
||||||
|
|
||||||
/// Run multiple simulations
|
/// Run multiple simulations
|
||||||
|
var net = Net.load(file);
|
||||||
var nano = System.nanoTime();
|
var nano = System.nanoTime();
|
||||||
var sim = new SimulationMultiple(net);
|
var sim = new SimulationMultiple(net);
|
||||||
var results = parallel ? sim.runParallel(seed, runs) : sim.run(seed, runs);
|
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<String, String> parseParameters(String[] args) {
|
public static Map<String, String> parseParameters(String[] args) {
|
||||||
var arguments = new HashMap<String, Boolean>();
|
var arguments = new HashMap<String, Boolean>();
|
||||||
arguments.put("p", false);
|
arguments.put("p", false);
|
||||||
arguments.put("seed", true);
|
arguments.put("seed", true);
|
||||||
arguments.put("runs", true);
|
arguments.put("runs", true);
|
||||||
|
arguments.put("net", true);
|
||||||
arguments.put("csv", true);
|
arguments.put("csv", true);
|
||||||
|
|
||||||
var param = new Parameters("-", arguments);
|
var param = new Parameters("-", arguments);
|
||||||
@@ -90,10 +62,12 @@ public class Main {
|
|||||||
return param.parse(args);
|
return param.parse(args);
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
var descriptions = new HashMap<String, String>();
|
var descriptions = new HashMap<String, String>();
|
||||||
descriptions.put("p", "Add this if you want the simulation to use threads (one each run)");
|
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("seed", "The seed of the simulation.");
|
||||||
descriptions.put("runs", "How many runs the simulator should run");
|
descriptions.put("runs", "How many runs the simulator should run.");
|
||||||
descriptions.put("csv", "The filename for saving every run statistics");
|
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(e.getMessage());
|
||||||
System.out.println(param.helper(descriptions));
|
System.out.println(param.helper(descriptions));
|
||||||
|
|||||||
@@ -1,10 +1,20 @@
|
|||||||
package net.berack.upo.valpre.sim;
|
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.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Consumer;
|
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;
|
import net.berack.upo.valpre.rand.Rng;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -218,6 +228,41 @@ public final class Net {
|
|||||||
this.servers.stream().forEach(consumer);
|
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
|
* A static inner class used to represent the connection between two nodes
|
||||||
*/
|
*/
|
||||||
|
|||||||
BIN
src/main/resources/example1.net
Normal file
BIN
src/main/resources/example1.net
Normal file
Binary file not shown.
BIN
src/main/resources/example2.net
Normal file
BIN
src/main/resources/example2.net
Normal file
Binary file not shown.
Reference in New Issue
Block a user