Refactor Event comparison logic for clarity and add getRandomChild method to ServerNodeState for improved child selection; update simulation tests for null checks and event processing
This commit is contained in:
@@ -23,11 +23,7 @@ public class Event implements Comparable<Event> {
|
||||
|
||||
@Override
|
||||
public int compareTo(Event other) {
|
||||
if (this.time < other.time)
|
||||
return -1;
|
||||
if (this.time == other.time)
|
||||
return 0;
|
||||
return 1;
|
||||
return Double.compare(this.time, other.time);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -158,6 +158,23 @@ public class ServerNodeState {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a random child based on the weights of the children and the random number
|
||||
* generator passed as input
|
||||
*
|
||||
* @param rng the random number generator
|
||||
* @return the index of the child or -1 if no child is selected
|
||||
*/
|
||||
public int getRandomChild(Rng rng) {
|
||||
var random = rng.random();
|
||||
for (var child : this.children) {
|
||||
random -= child.weight;
|
||||
if (random <= 0)
|
||||
return child.index;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an arrival event to a child node based on the node and the time passed
|
||||
* as input
|
||||
@@ -168,12 +185,7 @@ public class ServerNodeState {
|
||||
* otherwise
|
||||
*/
|
||||
public Event spawnArrivalToChild(double time, Rng rng) {
|
||||
var random = rng.random();
|
||||
for (var child : this.children) {
|
||||
random -= child.weight;
|
||||
if (random <= 0)
|
||||
return Event.newArrival(child.index, time);
|
||||
}
|
||||
return null;
|
||||
var child = this.getRandomChild(rng);
|
||||
return child > -1 ? Event.newArrival(child, time) : null;
|
||||
}
|
||||
}
|
||||
@@ -97,8 +97,13 @@ public final class Simulation {
|
||||
case DEPARTURE -> {
|
||||
state.updateDeparture(time);
|
||||
|
||||
// Spawn unavailability if has unavailable time
|
||||
this.addToFel(state.spawnUnavailableIfPossible(time, this.rng));
|
||||
|
||||
// Spawn departure if has requests and server is available
|
||||
this.addToFel(state.spawnDepartureIfPossible(time, this.rng));
|
||||
|
||||
// Spawn arrival to self if is source node
|
||||
this.addToFel(state.spawnArrivalIfPossilbe(time));
|
||||
|
||||
// Spawn arrival to child node if queue is not full otherwise drop
|
||||
|
||||
Reference in New Issue
Block a user