- fixed negative sample from distribution for time
This commit is contained in:
2025-01-17 19:47:05 +01:00
parent 7b46054b70
commit d7b968d0a2
3 changed files with 22 additions and 6 deletions

View File

@@ -6,7 +6,7 @@ public class Main {
public static void main(String[] args) {
// Parameters for the simulation
var seed = System.nanoTime();
var total = 1000;
var total = 100000;
var lambda = 1.0 / 4.5;
var mu = 3.2;
var sigma = 0.6;
@@ -22,8 +22,8 @@ public class Main {
sim.addNode(node2);
var maxDepartures = new EndSimulationCriteria.MaxDepartures("Queue", total);
var maxTime = new EndSimulationCriteria.MaxTime(1000.0);
var results = sim.run(maxDepartures, maxTime);
//var maxTime = new EndSimulationCriteria.MaxTime(1000.0);
var results = sim.run(maxDepartures);
// Display the results
for (var entry : results.entrySet()) {

View File

@@ -133,7 +133,7 @@ public class NetSimulation {
if (event.node.maxServers > this.numServerBusy) {
this.numServerBusy++;
var time = event.node.distribution.sample(this.rng);
var time = event.node.getPositiveSample(this.rng);
var departure = Event.newDeparture(event.node, timeNow + time);
fel.add(departure);
} else {
@@ -163,7 +163,7 @@ public class NetSimulation {
if (this.queue.size() < this.numServerBusy) {
this.numServerBusy--;
} else {
var time = event.node.distribution.sample(this.rng);
var time = event.node.getPositiveSample(this.rng);
var departure = Event.newDeparture(event.node, timeNow + time);
fel.add(departure);
}
@@ -188,7 +188,7 @@ public class NetSimulation {
*/
private void addArrivalIf(boolean condition, ServerNode node, double timeNow, PriorityQueue<Event> fel) {
if (condition && node != null) {
var delay = node.distribution.sample(this.rng);
var delay = node.getPositiveSample(this.rng);
fel.add(Event.newArrival(node, timeNow + delay));
}
}

View File

@@ -102,6 +102,22 @@ public class ServerNode {
return null;
}
/**
* Gets a positive sample from the distribution.
* This is useful if you need to generate a positive value from a distribution
* that can generate negative values. For example, the normal distribution.
*
* @param rng The random number generator to use.
* @return A positive sample from the distribution.
*/
public double getPositiveSample(Rng rng) {
double sample;
do {
sample = this.distribution.sample(rng);
} while (sample < 0);
return sample;
}
/**
* Determines if the node should spawn an arrival based on the number of
* arrivals.