Net creation #2

Merged
Berack96 merged 21 commits from net-creation into main 2025-03-16 18:02:38 +01:00
4 changed files with 40 additions and 14 deletions
Showing only changes of commit 121d4cd44a - Show all commits

View File

@@ -23,11 +23,7 @@ public class Event implements Comparable<Event> {
@Override @Override
public int compareTo(Event other) { public int compareTo(Event other) {
if (this.time < other.time) return Double.compare(this.time, other.time);
return -1;
if (this.time == other.time)
return 0;
return 1;
} }
/** /**

View File

@@ -158,6 +158,23 @@ public class ServerNodeState {
return null; 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 * Create an arrival event to a child node based on the node and the time passed
* as input * as input
@@ -168,12 +185,7 @@ public class ServerNodeState {
* otherwise * otherwise
*/ */
public Event spawnArrivalToChild(double time, Rng rng) { public Event spawnArrivalToChild(double time, Rng rng) {
var random = rng.random(); var child = this.getRandomChild(rng);
for (var child : this.children) { return child > -1 ? Event.newArrival(child, time) : null;
random -= child.weight;
if (random <= 0)
return Event.newArrival(child.index, time);
}
return null;
} }
} }

View File

@@ -97,8 +97,13 @@ public final class Simulation {
case DEPARTURE -> { case DEPARTURE -> {
state.updateDeparture(time); state.updateDeparture(time);
// Spawn unavailability if has unavailable time
this.addToFel(state.spawnUnavailableIfPossible(time, this.rng)); this.addToFel(state.spawnUnavailableIfPossible(time, this.rng));
// Spawn departure if has requests and server is available
this.addToFel(state.spawnDepartureIfPossible(time, this.rng)); this.addToFel(state.spawnDepartureIfPossible(time, this.rng));
// Spawn arrival to self if is source node
this.addToFel(state.spawnArrivalIfPossilbe(time)); this.addToFel(state.spawnArrivalIfPossilbe(time));
// Spawn arrival to child node if queue is not full otherwise drop // Spawn arrival to child node if queue is not full otherwise drop

View File

@@ -5,6 +5,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.HashSet; import java.util.HashSet;
@@ -393,8 +394,18 @@ public class TestSimulation {
@Test @Test
public void simulation() { public void simulation() {
var start = System.nanoTime(); assertThrows(NullPointerException.class, () -> new Simulation(null, rigged));
assertThrows(NullPointerException.class, () -> new Simulation(simpleNet, null));
var sim = new Simulation(simpleNet, rigged); var sim = new Simulation(simpleNet, rigged);
assertTrue(sim.hasEnded());
assertEquals(0, sim.getEventsProcessed());
assertEquals(0.0, sim.getTime(), DELTA);
var fel = sim.getFutureEventList();
assertEquals(0, fel.size());
var start = System.nanoTime();
sim = new Simulation(simpleNet, rigged);
// knowing that it takes time to allocate the object // knowing that it takes time to allocate the object
// we can use the average time // we can use the average time
var endAllocation = System.nanoTime(); var endAllocation = System.nanoTime();
@@ -410,7 +421,7 @@ public class TestSimulation {
assertEquals(0, sim.getNodeState(node0.name).numServerUnavailable); assertEquals(0, sim.getNodeState(node0.name).numServerUnavailable);
assertEquals(0, sim.getNodeState(node1.name).numServerBusy); assertEquals(0, sim.getNodeState(node1.name).numServerBusy);
assertEquals(0, sim.getNodeState(node1.name).numServerUnavailable); assertEquals(0, sim.getNodeState(node1.name).numServerUnavailable);
var fel = sim.getFutureEventList(); fel = sim.getFutureEventList();
assertEquals(0, fel.size()); assertEquals(0, fel.size());
sim.addToFel(Event.newArrival(0, sim.getTime())); sim.addToFel(Event.newArrival(0, sim.getTime()));
@@ -489,6 +500,8 @@ public class TestSimulation {
assertEquals(0, sim.getNodeState(node1.name).numServerUnavailable); assertEquals(0, sim.getNodeState(node1.name).numServerUnavailable);
fel = sim.getFutureEventList(); fel = sim.getFutureEventList();
assertEquals(0, fel.size()); assertEquals(0, fel.size());
final var s = sim;
assertThrows(NullPointerException.class, () -> s.processNextEvent());
var elapsed = (double) (System.nanoTime() - time); var elapsed = (double) (System.nanoTime() - time);
var result = sim.endSimulation(); var result = sim.endSimulation();