Please use JAVA for this problem! Correct answer only has 8-10 lines of code in
ID: 3802489 • Letter: P
Question
Please use JAVA for this problem!
Correct answer only has 8-10 lines of code in java, so dont think too hard on that, it is not a very complex problem
The server queueing simulation model posted on Blackboard that is included with many .java files is as follows:
MainClass.java file is as follows:
public class MainClass{
public static void main(String argv[]){
Simulator ss = new Simulator();
ss.FutureEventList = new EventList();
ss.Customers = new Queue();
ss.stream=new Rand();
ss.Clock=0.0;
ss.MeanInterArrivalTime=7.7;
ss.MeanServiceTime=6.21;
ss.Initialization();
//Loop until clock is greater than 10000
while (ss.Clock<=10000){
Event evt = ss.FutureEventList.getMin(); //get imminent event
ss.FutureEventList.dequeue(); //delete the event
ss.Clock=evt.get_time(); //advance in time
if (evt.get_type()==Simulator.arrival)
ss.ProcessArrival(evt);
else
ss.ProcessDeparture(evt);
}
ss.ReportGeneration();
}
}
Queue.java file is as follows:
import java.util.Vector;
public class Queue{
private Vector all_data;
public Queue(){
all_data = new Vector();
}
public void enqueue(Event e){
all_data.addElement(e);
}
public Event dequeue(){
Event res = all_data.elementAt(0);
all_data.removeElementAt(0);
return res;
}
public Event Get(int i){
return all_data.elementAt(i);
}
}
Rand.java file is as follows:
public class Rand {
final static int a = 16807;
final static int c = 0;
final static int m = 2147483647;
// setting the seed x0.
long x;
public Rand(){
x =123457 ;
}
double next(){
// Calculate next value in sequence.
x = ((a * x) + c) % m;
// Return its 0-to-1 value.
return (double)x/m ;
}
}
Simulator.java file is as follows:
public class Simulator {
public Simulator(){}
public final static int arrival=1;
public final static int departue=2;
public double Clock,MeanInterArrivalTime,MeanServiceTime, LastEventTime, TotalBusy,SumResponseTime,SumWaitTime,wightedQueueLength;
public long NumberOfCustomers,Queuelength, TotalCustomers,NumberInService, NumberOfDepartures, MaxQueueLength;
public int counter,counters;
public EventList FutureEventList;
public Queue Customers;
public Rand stream;
public void Initialization(){
Clock=0.0;
NumberInService=0;
Queuelength=0;
LastEventTime=0.0;
TotalBusy= 0.0 ;
MaxQueueLength = 0 ;
SumResponseTime=0.0;
NumberOfDepartures=0;
SumWaitTime=0.0;
wightedQueueLength=0.0;
//Create First Arrival Event
Event evt = new Event(arrival,exponential(stream,MeanInterArrivalTime));
FutureEventList.enqueue(evt);
}
public void ProcessArrival(Event evt){
Customers.enqueue(evt);
wightedQueueLength+=(Clock-LastEventTime)*Queuelength;
Queuelength++;
//if the server is idle, fetch the event, do statistics and put into service
if(NumberInService==0)
{
ScheduleDeparture();
}
else
TotalBusy+=(Clock-LastEventTime); //server is busy
//adjust max Queue Length statistics
if(MaxQueueLength< Queuelength)
MaxQueueLength=Queuelength;
//Schedule the next arrival
Event next_arrival = new Event(arrival,Clock+exponential(stream,MeanInterArrivalTime));
FutureEventList.enqueue(next_arrival);
LastEventTime=Clock;
}
public void ScheduleDeparture(){
//get the job at the head of the queue
NumberOfCustomers++;
Event depart= new Event(Simulator.departue,Clock+exponential(stream,MeanServiceTime));
//Event depart= new Event(Simulator.departue,Clock+triangular(stream,1,3,8));
double arrive = Customers.Get(0).get_time();
double wait= Clock-arrive;
SumWaitTime+=wait;
FutureEventList.enqueue(depart);
NumberInService=1;
Queuelength--;
}
public void ProcessDeparture(Event e){
//get the customers description
Event finished= (Event) Customers.dequeue();
// if there are customers in the queue then schedule the departure of the next one
wightedQueueLength+=(Clock-LastEventTime)*Queuelength;
if (Queuelength>0)
ScheduleDeparture();
else
NumberInService=0;
//measure the response time and add to the sum
double response= (Clock-finished.get_time());
SumResponseTime+=response;
TotalBusy+=(Clock-LastEventTime);
NumberOfDepartures++;
LastEventTime=Clock;
}
public static double exponential(Rand rng,double mean){
return -mean*Math.log(rng.next());
}
public static double triangular(Rand rng,double a, double b, double c){
double R = rng.next();
double x;
if (R <= (b-a)/(c-a))
x = a + Math.sqrt((b-a)*(c-a)*R);
else
x = c - Math.sqrt((c-b)*(c-a)*(1-R));
return x;
}
public void ReportGeneration(){
double RHO=TotalBusy/Clock;
System.out.println(" Server Utilization " +RHO );
double AverageWaittime=SumWaitTime/NumberOfCustomers;
System.out.println(" Average Wait Time In Queue " + AverageWaittime);
double AverageQueueLength=wightedQueueLength/Clock;
System.out.println(" Average Number Of Customers In Queue " + AverageQueueLength);
System.out.println(" Maximum Number Of Customers In Queue " + MaxQueueLength);
}
}
Event.java is as follows:
public class Event{
private double time ;
private int type;
public Event ( int _type , double _time ){
type = _type ;
time = _time ;
}
public double get_time(){
return time ;
}
public int get_type (){
return type;
}
}
EventList.java file is as follows:
import java.util.PriorityQueue;
import java.util.Comparator;
public class EventList{
public PriorityQueue event_list;
public int size (){
return event_list.size();
}
public EventList(){
event_list = new PriorityQueue(100,
new Comparator(){
public int compare(Event e1, Event e2){
if (e1.get_time() < e2.get_time())
return -1;
if (e1.get_time() > e2.get_time())
return 1;
return 0;
}
});
}
public Event getMin(){
return event_list.peek();
}
public void dequeue(){
event_list.remove();
}
public void enqueue(Event e){
event_list.add(e);
}
}
Explanation / Answer
public class MainClass{
public static void main(String argv[]){
Simulator ss = new Simulator();
ss.FutureEventList = new EventList();
ss.Customers = new Queue();
ss.stream=new Rand();
ss.Clock=0.0;
ss.MeanInterArrivalTime=7.7;
ss.MeanServiceTime=6.21;
ss.Initialization();
//Loop until clock is greater than 10000
while (ss.Clock<=10000){
Event evt = ss.FutureEventList.getMin(); //get imminent event
ss.FutureEventList.dequeue(); //delete the event
ss.Clock=evt.get_time(); //advance in time
if (evt.get_type()==Simulator.arrival)
ss.ProcessArrival(evt);
else
ss.ProcessDeparture(evt);
}
ss.ReportGeneration();
}
}
Queue.java file is as follows:
import java.util.Vector;
public class Queue{
private Vector all_data;
public Queue(){
all_data = new Vector();
}
public void enqueue(Event e){
all_data.addElement(e);
}
public Event dequeue(){
Event res = all_data.elementAt(0);
all_data.removeElementAt(0);
return res;
}
public Event Get(int i){
return all_data.elementAt(i);
}
}
Rand.java file is as follows:
public class Rand {
final static int a = 16807;
final static int c = 0;
final static int m = 2147483647;
// setting the seed x0.
long x;
public Rand(){
x =123457 ;
}
double next(){
// Calculate next value in sequence.
x = ((a * x) + c) % m;
// Return its 0-to-1 value.
return (double)x/m ;
}
}
Simulator.java file is as follows:
public class Simulator {
public Simulator(){}
public final static int arrival=1;
public final static int departue=2;
public double Clock,MeanInterArrivalTime,MeanServiceTime, LastEventTime, TotalBusy,SumResponseTime,SumWaitTime,wightedQueueLength;
public long NumberOfCustomers,Queuelength, TotalCustomers,NumberInService, NumberOfDepartures, MaxQueueLength;
public int counter,counters;
public EventList FutureEventList;
public Queue Customers;
public Rand stream;
public void Initialization(){
Clock=0.0;
NumberInService=0;
Queuelength=0;
LastEventTime=0.0;
TotalBusy= 0.0 ;
MaxQueueLength = 0 ;
SumResponseTime=0.0;
NumberOfDepartures=0;
SumWaitTime=0.0;
wightedQueueLength=0.0;
//Create First Arrival Event
Event evt = new Event(arrival,exponential(stream,MeanInterArrivalTime));
FutureEventList.enqueue(evt);
}
public void ProcessArrival(Event evt){
Customers.enqueue(evt);
wightedQueueLength+=(Clock-LastEventTime)*Queuelength;
Queuelength++;
//if the server is idle, fetch the event, do statistics and put into service
if(NumberInService==0)
{
ScheduleDeparture();
}
else
TotalBusy+=(Clock-LastEventTime); //server is busy
//adjust max Queue Length statistics
if(MaxQueueLength< Queuelength)
MaxQueueLength=Queuelength;
//Schedule the next arrival
Event next_arrival = new Event(arrival,Clock+exponential(stream,MeanInterArrivalTime));
FutureEventList.enqueue(next_arrival);
LastEventTime=Clock;
}
public void ScheduleDeparture(){
//get the job at the head of the queue
NumberOfCustomers++;
Event depart= new Event(Simulator.departue,Clock+exponential(stream,MeanServiceTime));
//Event depart= new Event(Simulator.departue,Clock+triangular(stream,1,3,8));
double arrive = Customers.Get(0).get_time();
double wait= Clock-arrive;
SumWaitTime+=wait;
FutureEventList.enqueue(depart);
NumberInService=1;
Queuelength--;
}
public void ProcessDeparture(Event e){
//get the customers description
Event finished= (Event) Customers.dequeue();
// if there are customers in the queue then schedule the departure of the next one
wightedQueueLength+=(Clock-LastEventTime)*Queuelength;
if (Queuelength>0)
ScheduleDeparture();
else
NumberInService=0;
//measure the response time and add to the sum
double response= (Clock-finished.get_time());
SumResponseTime+=response;
TotalBusy+=(Clock-LastEventTime);
NumberOfDepartures++;
LastEventTime=Clock;
}
public static double exponential(Rand rng,double mean){
return -mean*Math.log(rng.next());
}
public static double triangular(Rand rng,double a, double b, double c){
double R = rng.next();
double x;
if (R <= (b-a)/(c-a))
x = a + Math.sqrt((b-a)*(c-a)*R);
else
x = c - Math.sqrt((c-b)*(c-a)*(1-R));
return x;
}
public void ReportGeneration(){
double RHO=TotalBusy/Clock;
System.out.println(" Server Utilization " +RHO );
double AverageWaittime=SumWaitTime/NumberOfCustomers;
System.out.println(" Average Wait Time In Queue " + AverageWaittime);
double AverageQueueLength=wightedQueueLength/Clock;
System.out.println(" Average Number Of Customers In Queue " + AverageQueueLength);
System.out.println(" Maximum Number Of Customers In Queue " + MaxQueueLength);
}
}