⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simulate.java

📁 用java编的模拟排队论的一些问题!求不同时间段里需要多少个服务人员
💻 JAVA
字号:
public class Simulate{

    public static void main(String[] args){
	double lambda_a = 19.0;
	double lambda_s = 20.0;
	double t_end = 7.0;
	
	Sim1(lambda_a, lambda_s, t_end);
	}
    
    
     
    public static void Sim1(double lambda_a, double lambda_s, double t_end){
    	System.out.println(" #  " + "   event  " + "  ql  " + "   clock   ");
        System.out.println("--------------------------------------------");
        MyRandom r = new MyRandom(12342);// sets up random number generator 
		Queue q = new Queue();
		
    	double clock = 0;
   		double tna = 0;            //the next arrival
   		double arrived = 0;        
   		double waiting_time = 0;   
   		double tsf = 0;        
   		double ttsf = 0;       
   		double tnc = 5000;    
   	    double Awt = 0;            //Average waiting time  
   	    double ttwt = 0;           //total waiting time
   	    double Sff = 0;            //Server free fraction
   	    double f = 0;
   	    int n = 0;                 //queue length
   	    int event = 0;             //the number of event
   	    int Mql = 0;               //Maximum queue length      
   	    int tta = 0;               //total arrival 
   	    int tts = 0 ;              //total service 
   	    int ttw = 0;               //total waiting time longer than 6 minutes
   	    boolean server_free = true;
		

		
               
        // when the end time is reached and queue is empty 
        // the bank was closed and no one wait to served   
        while (clock >= t_end && q.isEmpty() == true) {
        	break;
       	}        
		 
		 
		 
		
		
		//the bank is opening
		while(clock < t_end){	
			event++;
		    n = q.length();  //get the queue length 
	    //get the random arrival time
			if(tna <= t_end  &&  tna < tnc){
				clock = tna;
				tna = clock + r.nextExponential(lambda_a);
		//the service is busy, get the random waiting time	
					if(server_free == true){
						server_free = false;
						tnc = clock + r.nextErlang(3,lambda_s);
				    }
				    	else{
						     q.put(clock); 
				        }
                    n = q.length();
					System.out.println(event + "   Arivl   " + n + "    " + clock);
					
			}
			else{
				clock = tnc;          //set the clock by the next complete
			    n = q.length();       //get the queue length again
					System.out.println(event + "   Compl   " + n + "    " + clock);
				
		 //no one in the queue, get the server free time
		 //the arrival will be served without waiting  		
					if(q.isEmpty() == true){ 
						server_free = true;
						tnc = 5000;	
	       				tsf = tna - clock;
						ttsf +=tsf;
						System.out.println("Server is free for: " +tsf+ "	tt server free yet: "+ttsf );
						}
		 //service is busy, get the arrival into queue				
				    	else{
							arrived = q.get();				                 
							tnc = clock + r.nextErlang(3,lambda_s);
							waiting_time = clock - arrived;
							ttwt +=waiting_time; //total waitting time
		 //calculate the the total waiting time which more than 6 min					
							 if(waiting_time> 6.0/60){
       	                       ttw++;
       	                     } 
       	                     
					System.out.println("Guy is served, has wait: "+ waiting_time );
			
						}			
		    
			}
                 Mql = Math.max(n,Mql); //get the maximum queue length 
                
     
         }       
  
                    
                    
                    		
		
        //the bank is closing(no one arrival),and there is someone
		//in the queue waitting to served		
		while(clock >= t_end ){
		    event++;
		    n = q.length();
		    clock = tnc;
		    System.out.println(event + "   Compl   " + n + "    " + clock);
		//the queue is not empty,get the waiting time of each arrival
		       if ( q.isEmpty() == false ){
		          arrived = q.get();
		          waiting_time = clock - arrived;
		          ttwt +=waiting_time;
		       }
		//if the queue is empty, break out the loop       
		            else
		            	break;
        //calculate the the total waiting time which more than 6 min
		       if(waiting_time> 6.0/60){
       	          ttw++;
       	       }
       	          
		    System.out.println("Guy is served, has wait: "+ waiting_time );
		    tnc = clock + r.nextErlang(3,lambda_s);
		    
		    Mql = Math.max(n,Mql);  //get the maximum queue length
  
        }
      
       Awt = 2 * ttwt / event; 
       Sff = ttsf / clock;     
       tta = event / 2;        
       tts = event / 2;        
       f = ((double)ttw / (double)tts) * 100;   

     System.out.println("Average waiting time :  "+ Awt);
     System.out.println("Server free fraction :  "+ Sff);
     System.out.println("Maximum queue length :  "+ Mql);
     System.out.println("tt_arrivals          :  "+ tta);
     System.out.println("tt_wait_time         :  "+ ttwt);
     System.out.println("tt_served            :  "+ tts );
     System.out.println("tt wait > 6min       :  "+ ttw);
     System.out.println("fraction > 6min      :  "+f+"%");
     
     }
     
    
     
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -