All pastes #393643 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-03-13 18:56 UTC
rendered paste body
import java.util.Scanner;
import java.util.Random;
import java.lang.Math;

public class sim2 
{
	static double TSs=0,TQs=0,Ws=0,T=0,constant,lambda,TS,runTime,a,c;
	static int TSc=0,TQc=0,Wc=0,Birthcount=0; //counters
	static double Lprob = 0;
	static LList planner = new LList();
	static LList queue = new LList();
	
	public static void main(String[] args){
		lambda = 30;
		TS = .03;
		runTime = 100;
		c = .05;
		//c = .001/.01/.02/.05<==============================================
		
		constant = runTime / 200;//monitering
		planner.insert(T + getRandTime(lambda), 0 , 0);
		planner.insert(T + constant, 2, 0);
		
		while(T < runTime){
			double b;
			T = planner.head.time;
			b = planner.head.func;
			
			planner.remove();
			
			if(b == 0){
				birth();}
			else if(b == 1){
				death();}
			else if(b == 2){
				monitor();
				}
		}
		results();
	}
	
	public static double getRandTime(double rate){
		double U,V;
		Random r = new Random();
		U = r.nextDouble();
		V = - Math.log(1-U)/rate;
		return V;
	}
	public static void birth(){
		Birthcount++;
		queue.insertEnd(T, 0, getRandTime(1.0/TS));//generates time wanted serving
		
		planner.insert(T + getRandTime(lambda), 0 , 0);//inserts next birth
		
		if(queue.head.wants > c){//wants more than time slice
			planner.insert(T + c, 1 , 0);//birth for continous
			queue.head.wants = c - queue.head.wants;//subtract time already served
		}else{//time slice is enought
			planner.insert(T + queue.head.wants, 1 , 0);//death upon wanting 
			queue.head.wants =0;//needs no more time serving
		}
	}
	public static void death(){

		double initT = queue.head.time;//get timestamp
		double headw = queue.head.wants;//get wanting serve
		
		if(queue.head.wants == 0){//success, update counters
			TQs += (T-initT)/100;//updating time and changing units.
			TQc++;
			queue.remove();
		}else{
			queue.insertEnd(initT, 0, headw);//wants more, rebirth
			queue.remove();
		}
	}
	public static void monitor(){
		Ws += queue.total;
		Wc++;
		planner.insert(T + constant, 2, 0);
	}
	public static void results(){
		double Lamb,Ts,Ro,Q,W=0,Tq,Tw=0;
		//calculate real values
		Lamb = lambda;
		Ts = TS;
		Ro = Lamb*Ts;
		Q = Ro / (1 - Ro);
		W = Q - Ro;
		Tq =  Q / Lamb;
		Tw = W / Lamb;
		//output real values
		System.out.println("Real Values:\n");
		System.out.println("Tq = " + Tq);
		System.out.println("---------------------------");
		//calculate observed values
		Lamb = Birthcount / runTime;
		Ts = TSs / TSc;
		Ro = Lamb*Ts;
		Q = Ro / (1 - Ro);
		W = Ws/Wc;
		Tq =  TQs/ TQc;
		Tw = W/Lamb;
		//output observed values
		System.out.println("Observed Values: for c: " + c + "\n");
		System.out.println("Tq = " + Tq);
		System.out.println("---------------------------");
	}
}