Refactor Net and ServerNodeState classes to improve child node handling and simplify simulation initialization
This commit is contained in:
@@ -17,8 +17,6 @@ import com.esotericsoftware.kryo.KryoException;
|
||||
import com.esotericsoftware.kryo.io.Input;
|
||||
import com.esotericsoftware.kryo.io.Output;
|
||||
|
||||
import net.berack.upo.valpre.rand.Rng;
|
||||
|
||||
/**
|
||||
* A class that represents a network of queues, each with its own servers.
|
||||
* The network in question is created by adding a node and then establishing
|
||||
@@ -141,26 +139,6 @@ public final class Net implements Iterable<ServerNode> {
|
||||
return this.servers.get(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get one of the child nodes from the parent specified. If the index is out of
|
||||
* bounds then an exception is thrown. If the node has no child then -1 is
|
||||
* returned.
|
||||
*
|
||||
* @param parent the parent node
|
||||
* @param rng the random number generator used for getting one of the child
|
||||
* @throws IndexOutOfBoundsException If the index is not in the range
|
||||
* @return the resultig node
|
||||
*/
|
||||
public int getChildOf(int parent, Rng rng) {
|
||||
var random = rng.random();
|
||||
for (var conn : this.connections.get(parent)) {
|
||||
random -= conn.weight;
|
||||
if (random <= 0)
|
||||
return conn.index;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of all the children of the parent.
|
||||
* In the list there is the node and the weight associated with.
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package net.berack.upo.valpre.sim;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.List;
|
||||
|
||||
import net.berack.upo.valpre.rand.Rng;
|
||||
import net.berack.upo.valpre.sim.Net.Connection;
|
||||
import net.berack.upo.valpre.sim.stats.NodeStats;
|
||||
|
||||
/**
|
||||
@@ -17,9 +19,9 @@ public class ServerNodeState {
|
||||
public final ArrayDeque<Double> queue = new ArrayDeque<>();
|
||||
|
||||
public final int index;
|
||||
public final Net net;
|
||||
public final ServerNode node;
|
||||
public final NodeStats stats = new NodeStats();
|
||||
public final List<Connection> children;
|
||||
|
||||
/**
|
||||
* Create a new node state based on the index and the net passed as input
|
||||
@@ -29,8 +31,8 @@ public class ServerNodeState {
|
||||
*/
|
||||
ServerNodeState(int index, Net net) {
|
||||
this.index = index;
|
||||
this.net = net;
|
||||
this.node = net.getNode(index);
|
||||
this.children = net.getChildren(index);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -166,9 +168,14 @@ public class ServerNodeState {
|
||||
* otherwise
|
||||
*/
|
||||
public Event spawnArrivalToChild(double time, Rng rng) {
|
||||
var childIndex = this.net.getChildOf(this.index, rng);
|
||||
if (childIndex >= 0)
|
||||
return Event.newArrival(childIndex, time);
|
||||
if (!this.children.isEmpty()) {
|
||||
var random = rng.random();
|
||||
for (var child : this.children) {
|
||||
random -= child.weight;
|
||||
if (random <= 0)
|
||||
return Event.newArrival(child.index, time);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -24,10 +24,15 @@ public final class Simulation {
|
||||
private long eventProcessed = 0;
|
||||
|
||||
/**
|
||||
* Creates a new run of the simulation with the given nodes and random number
|
||||
* generator.
|
||||
* Creates a new simulation for the given network.
|
||||
* The random number generator is used to generate random numbers for the
|
||||
* simulation.
|
||||
* The simulation will end when the given criteria are met.
|
||||
* NOTE: the network passed is only used to create the initial states of the
|
||||
* nodes, so the simulation is not affected by changes to the network after
|
||||
* the creation of this object.
|
||||
*
|
||||
* @param states The nodes in the network.
|
||||
* @param net The network to simulate.
|
||||
* @param rng The random number generator to use.
|
||||
* @param criterias when the simulation has to end.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user