Code: Alles auswählen
package ch.hslu.taegli.simmod.sw10;
/* -------------------------------------------------------------------------
* This program - an extension of program ssq1.c - simulates a single-server
* FIFO service node using Exponentially distributed interarrival times and
* Uniformly distributed service times (i.e. a M/U/1 queue).
*
* Name : Ssq2.java (Single Server Queue, version 2)
* Authors : Steve Park & Dave Geyer
* Translated by : Jun Wang & Richard Dutton
* Language : Java
* Latest Revision : 6-16-06
* -------------------------------------------------------------------------
*/
import java.text.*;
class Ssq2Sum { /* sum of ... */
double delay; /* delay times */
double wait; /* wait times */
double service; /* service times */
double interarrival; /* interarrival times */
void initSumParas() {
delay = 0.0;
wait = 0.0;
service = 0.0;
interarrival = 0.0;
}
}
public class Ssq2 {
static long LAST = 2; /* number of jobs processed */
static double START = 0.0; /* initial time */
static double sarrival = START; /* Why did I do this? */
public static void main(String[] args) {
long index = 0; /* job index */
double arrival = START; /* time of arrival */
double delay; /* delay in queue */
double service; /* service time */
double wait; /* delay + service */
double departure = START; /* time of departure */
double[] arrivals = new double[(int) LAST];
double[] departures = new double[(int) LAST];
Ssq2Sum sum = new Ssq2Sum();
sum.initSumParas();
Ssq2 s = new Ssq2();
Rng r = new Rng();
r.putSeed(123456789);
while (index < LAST) {
index++;
arrival = Ssq2.getArrival(r);
arrivals[(int) index-1] = arrival;
if (arrival < departure)
delay = departure - arrival; /* delay in queue */
else
delay = 0.0; /* no delay */
service = s.getService(r);
wait = delay + service;
departure = arrival + wait; /* time of departure */
departures[(int) index-1] = departure;
sum.delay += delay;
sum.wait += wait;
sum.service += service;
}
sum.interarrival = arrival - START;
double area = calculateArea(arrivals, departures);
double l_average = area / departure;
System.out.println("Area: " + area);
System.out.println("l average: " + l_average);
DecimalFormat f = new DecimalFormat("###0.00");
System.out.println("\nfor " + index + " jobs");
System.out.println(" average interarrival time = " + f.format(sum.interarrival / index));
System.out.println(" average wait ............ = " + f.format(sum.wait / index));
System.out.println(" average delay ........... = " + f.format(sum.delay / index));
System.out.println(" average service time .... = " + f.format(sum.service / index));
System.out.println(" average # in the node ... = " + f.format(sum.wait / departure));
System.out.println(" average # in the queue .. = " + f.format(sum.delay / departure));
System.out.println(" utilization ............. = " + f.format(sum.service / departure));
}
static double exponential(double m, Rng r) {
/* ---------------------------------------------------
* generate an Exponential random variate, use m > 0.0
* ---------------------------------------------------
*/
return (-m * Math.log(1.0 - r.random()));
}
double uniform(double a, double b, Rng r) {
/* ------------------------------------------------
* generate an Uniform random variate, use a < b
* ------------------------------------------------
*/
return (a + (b - a) * r.random());
}
static double getArrival(Rng r) {
/* ------------------------------
* generate the next arrival time
* ------------------------------
*/
// static double sarrival = START;
sarrival += exponential(2.0, r);
return (sarrival);
}
double getService(Rng r) {
/* ------------------------------
* generate the next service time
* ------------------------------
*/
return (uniform(1.0, 2.0, r));
}
/**
* Calculates the area
* @param a Arrivals
* @param c Departures
* @return Area
*/
static double calculateArea(double[] a, double[] c) {
double[] t = new double[2*(a.length + c.length)+2]; //x coordinates (time)
double[] lt = new double[2*(a.length + c.length)+2]; //y coordinates (elements in node)
int i=0; //pointer in arrivals
int j=0; //pointer in departures
int l=0; //current number of elements in the node
t[0] = 0.0;
lt[0] = 0.0;
//calculate l(t)
while(i<a.length && j<c.length) {
if(a[i] <= c[j]) { //new job in queue
t[1+2*(i+j)] = a[i];
lt[1+2*(i+j)] = l;
t[1+2*(i+j)+1] = a[i];
lt[1+2*(i+j)+1] = ++l;
i++;
} else { //job finished
t[1+2*(i+j)] = c[j];
lt[1+2*(i+j)] = l;
t[1+2*(i+j)+1] = c[j];
lt[1+2*(i+j)+1] = --l;
j++;
}
}
for(int k=j; k<c.length; k++) { //rest of jobs to finish
t[1+2*(i+k)] = c[k];
lt[1+2*(i+k)] = l;
t[1+2*(i+k)+1] = c[k];
lt[1+2*(i+k)+1] = --l;
}
//Calculate area
t[t.length-1] = t[0]; //add first time again for loop
lt[lt.length-1] = lt[0]; //add first lt agian for loop
double area = 0.0;
for(int k=0; k<t.length-1; k++) {
area += (lt[k] + lt[k+1]) * (t[k] - t[k+1]);
}
area = -0.5*area;
return area; //l average is area / last departure time
}
}