Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package essayOR;
- import java.util.*;
- import java.lang.System;
- import java.io.*;
- public class essayMain {
- static final int RUNTIME = 5000000;
- static final int m_MAX = 5;
- static final int M_MAX = 15;
- static final double STARTUP = 0.33;
- static final int NRBATCHES = 50;
- static final double BATCHLENGTH = RUNTIME*(1-STARTUP)/NRBATCHES;
- int lowSpeedThreshold;
- int highSpeedThreshold;
- int server;
- EventList eventlist;
- Queue queue;
- PrintStream out;
- PrintStream outfile;
- Scanner in;
- Observations obs;
- Observations cumqobs;
- Distributions dist;
- double dummy;
- essayMain() {
- out = new PrintStream(System.out);
- try {
- outfile = new PrintStream(new FileOutputStream("output.txt"));
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- void start() {
- Scanner in = new Scanner(System.in);
- out.println("Do you want to run the default system (Y/N):");
- char type = in.next().charAt(0);
- while (type != 'Y' && type != 'N') {
- out.println("Please enter Y or N:");
- type = in.next().charAt(0);
- }
- if (type == 'N') {
- out.println("What mean/variance do you want for arrivals:");
- double lambda_arrival = in.nextDouble();
- out.println("What distribution do you want as sojourn time (regular mode):");
- String soj_reg = in.next();
- out.println("With mean:");
- double soj_reg_mean = in.nextDouble();
- out.println("With squared coefficent of variance:");
- double soj_reg_sc2 = in.nextDouble();
- out.println("What distribution do you want as sojourn time (high speed):");
- String soj_fast = in.next();
- out.println("With mean:");
- double soj_fast_mean = in.nextDouble();
- out.println("With squared coefficent of variance:");
- double soj_fast_sc2 = in.nextDouble();
- out.println("What distribution do you want as vacation time (regular mode):");
- String vac = in.next();
- out.println("With mean:");
- double vac_mean = in.nextDouble();
- out.println("With squared coefficent of variance:");
- double vac_sc2 = in.nextDouble();
- out.println("What distribution do you want as vacation time (high speed):");
- String vac_fast = in.next();
- out.println("With mean:");
- double vac_fast_mean = in.nextDouble();
- out.println("With squared coefficent of variance:");
- double vac_fast_sc2 = in.nextDouble();
- out.println("What distribution do you want for the batchsize:");
- String batch = in.next();
- out.println("With mean:");
- double batch_mean = in.nextDouble();
- out.println("With squared coefficent of variance:");
- double batch_sc2 = in.nextDouble();
- dist = new Distributions(lambda_arrival, soj_reg, soj_reg_mean, soj_reg_sc2, soj_fast, soj_fast_mean, soj_fast_sc2, vac, vac_mean, vac_sc2, vac_fast, vac_fast_mean, vac_fast_sc2, batch, batch_mean, batch_sc2);
- } else {
- dist = new Distributions(0.2, "constant", 2, 0, "exponential", 1, 20, "exponential", 1, 0, "exponential", 1, 0, "geometric", 4, 0);
- }
- in.close();
- simulate();
- }
- void simulate() {
- //for(lowSpeedThreshold = 0; lowSpeedThreshold <= m_MAX; lowSpeedThreshold++) {
- //for(highSpeedThreshold = lowSpeedThreshold+1; highSpeedThreshold <= lowSpeedThreshold+M_MAX; highSpeedThreshold++) {
- highSpeedThreshold = 5;
- lowSpeedThreshold = 1;
- dist.resetRandom(); //To keep the same random numbers every time it starts simulating again.
- simrun();
- double percentageHighSpeed = cumqobs.timeHighSpeed/((1-STARTUP)*RUNTIME);
- //double switchesPerUnitOfTime = cumqobs.amountOfSwitches/((1-STARTUP)*RUNTIME);
- outfile.println(cumqobs.numServed);
- //double effectiveLambda = dist.getArrivalRate()*dist.getBatchMean(); //Every arrival consists of more people, so the effective arrival rate is greater than lambda
- //double rho = effectiveLambda*dist.getSojourn("mean");
- // outfile.print(lowSpeedThreshold);
- outfile.print(" ");
- outfile.print(cumqobs.timeRegularSpeed/((1-STARTUP)*RUNTIME));
- outfile.print(" ");
- outfile.print(cumqobs.sojournTime);
- outfile.print(" ");
- outfile.print(percentageHighSpeed);
- outfile.print(" ");
- outfile.print((double)(cumqobs.numberOfJobs/((1-STARTUP)*RUNTIME))); //avg of jobs in the system
- //outfile.print(" ");
- //outfile.print(rho + (Math.pow(rho, 2) + Math.pow(effectiveLambda, 2)* dist.getSojourn("variance"))/(2*(1-rho)));
- //outfile.print(" ");
- //outfile.print(effectiveLambda*(cumqobs.waitingTime + cumqobs.sojournTime)/cumqobs.numServed); //avg number of customers in the store (LITTLE)
- outfile.print(" ");
- outfile.print(cumqobs.sojournTime/cumqobs.numServed);
- // outfile.print(" ");
- // outfile.print(switchesPerUnitOfTime); //avg number of switches per unit of time
- //outfile.print(" ");
- //outfile.print(cumqobs.numberOfJobs/((1-STARTUP)*RUNTIME) - 2.01 * cumqobs.estimatedSampleVariance + "," + (cumqobs.numberOfJobs/((1-STARTUP)*RUNTIME)+2.01*cumqobs.estimatedSampleVariance));
- outfile.println("");
- //}
- //}
- }
- void simrun() {
- obs = new Observations();
- eventlist = new EventList();
- queue = new Queue();
- eventlist.insert(new Event(dist.arrival(), "arrival")); //First arrival.
- double clock = 0.0;
- double te = 0.0;
- boolean nonRelevant = true;
- int batch = 1;
- server = 0;
- double [] batchMean = new double [NRBATCHES];
- while (clock < RUNTIME) {
- if (clock > STARTUP*RUNTIME && nonRelevant) { //Is only true once, and creates new Observations
- obs.init();
- cumqobs = new Observations();
- nonRelevant = false;
- } else if (clock > STARTUP*RUNTIME + batch*BATCHLENGTH) { //Batches
- cumqobs.add(obs);
- batchMean[batch-1] = obs.numberOfJobs/BATCHLENGTH;
- obs.init();
- batch++;
- }
- eventlist.setFirst();
- Event ev = eventlist.retrieve();
- te = ev.getTime();
- eventlist = eventlist.remove();
- if (ev.getType() == "arrival") {
- dummy = dist.batchSize();
- for(int i=0; i< dummy; i++) {
- handleArrival(clock,te);
- }
- eventlist.insert(new Event(te + dist.arrival(), "arrival"));
- }
- else if(ev.getType() == "departure"){
- handleDeparture(clock,te);
- } else if(ev.getType() == "vacation") {
- handleVacation(clock,te);
- }
- clock = te;
- }
- cumqobs.add(obs); //Add the last batch to the cumulative Observations
- cumqobs.estimatedSampleVariance = estimateSampleVariance(batchMean);
- }
- private double estimateSampleVariance(double[] array) {
- double sum = 0;
- for (int j = 0; j <= NRBATCHES-1; j++) {
- sum += Math.pow(array[j] - cumqobs.numberOfJobs/((1-STARTUP)*RUNTIME), 2);
- }
- return Math.sqrt(sum/(NRBATCHES-1))/Math.sqrt(NRBATCHES);
- }
- void handleArrival(double tc, double tn) {
- obs.numberOfJobs += (queue.size() + server%2)*(tn-tc); //Server modulo 2 is 1 if server is serving a customer
- if (server == 0) {
- double sojournTime = dist.sojournTime();
- obs.numServed++; //Add to Observations
- obs.sojournTime += sojournTime;
- eventlist = eventlist.insert(new Event(tn+sojournTime, "departure"));
- server = 1; //The server is now busy
- } else {
- Customer c = new Customer(tn);
- queue = queue.insert(c);
- }
- }
- void handleDeparture(double tc, double tn) {
- obs.numberOfJobs += (queue.size() + server%2)*(tn-tc); //Server modulo 2 is 1 if server is serving a customer.
- if (queue.size() == 0) {
- server = 0;
- if(dist.isHighSpeed()) { //Check if the server needs to go back to regular speed.
- server = 2;
- double vacationTime = tn + dist.vacationTime();
- obs.beginVacationToRegular(vacationTime);
- obs.timeHighSpeed(tn);
- eventlist = eventlist.insert(new Event(vacationTime, "vacation"));
- dist.changeSpeed();
- obs.amountOfSwitches++;
- }
- } else if(queue.size() >= highSpeedThreshold && !dist.isHighSpeed()){ //Check if the server needs to go to the higher speed.
- server = 2; //The server is now on vacation
- double vacationTime = tn + dist.vacationTime();
- eventlist = eventlist.insert(new Event(vacationTime, "vacation"));
- obs.beginVacationToHigh(vacationTime);
- obs.timeRegularSpeed(tn);
- dist.changeSpeed();
- obs.amountOfSwitches++;
- } else if(queue.size() <= lowSpeedThreshold && dist.isHighSpeed()) { //Check if the server needs to go back to regular speed.
- server = 2; //The server is now on vacation
- double vacationTime = tn + dist.vacationTime();
- eventlist = eventlist.insert(new Event(vacationTime, "vacation"));
- obs.beginVacationToRegular(vacationTime);
- obs.timeHighSpeed(tn);
- dist.changeSpeed();
- obs.amountOfSwitches++;
- } else {
- queue.setFirst();
- Customer c = queue.retrieve();
- queue = queue.remove();
- double sojournTime = dist.sojournTime();
- obs.numServed++;
- obs.waitingTime += (tn-c.arrivalTime); //Add to Observations
- obs.sojournTime += sojournTime; //
- eventlist = eventlist.insert(new Event(tn + sojournTime, "departure"));
- }
- }
- void handleVacation(double tc, double tn) {
- obs.numberOfJobs += (queue.size() + server%2)*(tn-tc); //Server modulo 2 is 1 if server is serving a customer
- if (queue.size() == 0) { //Check if the server needs to server somebody instantly.
- server = 0;
- } else {
- queue.setFirst();
- Customer c = (Customer) queue.retrieve(); //Get the next customer.
- queue = queue.remove();
- double sojournTime = dist.sojournTime();
- obs.numServed++;
- obs.waitingTime += (tn-c.arrivalTime); //Add to Observations
- obs.sojournTime += sojournTime; //
- eventlist = eventlist.insert(new Event(tn + sojournTime, "departure"));
- server = 1; //The server is now busy
- }
- }
- public static void main(String[] argv) {
- new essayMain().start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement