Advertisement
Guest User

jemoeder

a guest
Feb 27th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.57 KB | None | 0 0
  1. package essayOR;
  2.  
  3. import java.util.*;
  4. import java.lang.System;
  5. import java.io.*;
  6.  
  7. public class essayMain {
  8. static final int RUNTIME = 5000000;
  9. static final int m_MAX = 5;
  10. static final int M_MAX = 15;
  11. static final double STARTUP = 0.33;
  12. static final int NRBATCHES = 50;
  13. static final double BATCHLENGTH = RUNTIME*(1-STARTUP)/NRBATCHES;
  14.  
  15. int lowSpeedThreshold;
  16. int highSpeedThreshold;
  17.  
  18. int server;
  19.  
  20. EventList eventlist;
  21. Queue queue;
  22. PrintStream out;
  23. PrintStream outfile;
  24. Scanner in;
  25. Observations obs;
  26. Observations cumqobs;
  27. Distributions dist;
  28. double dummy;
  29.  
  30.  
  31. essayMain() {
  32. out = new PrintStream(System.out);
  33. try {
  34. outfile = new PrintStream(new FileOutputStream("output.txt"));
  35. } catch (FileNotFoundException e) {
  36. // TODO Auto-generated catch block
  37. e.printStackTrace();
  38. }
  39. }
  40.  
  41. void start() {
  42. Scanner in = new Scanner(System.in);
  43. out.println("Do you want to run the default system (Y/N):");
  44. char type = in.next().charAt(0);
  45. while (type != 'Y' && type != 'N') {
  46. out.println("Please enter Y or N:");
  47. type = in.next().charAt(0);
  48. }
  49. if (type == 'N') {
  50. out.println("What mean/variance do you want for arrivals:");
  51. double lambda_arrival = in.nextDouble();
  52. out.println("What distribution do you want as sojourn time (regular mode):");
  53. String soj_reg = in.next();
  54. out.println("With mean:");
  55. double soj_reg_mean = in.nextDouble();
  56. out.println("With squared coefficent of variance:");
  57. double soj_reg_sc2 = in.nextDouble();
  58. out.println("What distribution do you want as sojourn time (high speed):");
  59. String soj_fast = in.next();
  60. out.println("With mean:");
  61. double soj_fast_mean = in.nextDouble();
  62. out.println("With squared coefficent of variance:");
  63. double soj_fast_sc2 = in.nextDouble();
  64. out.println("What distribution do you want as vacation time (regular mode):");
  65. String vac = in.next();
  66. out.println("With mean:");
  67. double vac_mean = in.nextDouble();
  68. out.println("With squared coefficent of variance:");
  69. double vac_sc2 = in.nextDouble();
  70. out.println("What distribution do you want as vacation time (high speed):");
  71. String vac_fast = in.next();
  72. out.println("With mean:");
  73. double vac_fast_mean = in.nextDouble();
  74. out.println("With squared coefficent of variance:");
  75. double vac_fast_sc2 = in.nextDouble();
  76. out.println("What distribution do you want for the batchsize:");
  77. String batch = in.next();
  78. out.println("With mean:");
  79. double batch_mean = in.nextDouble();
  80. out.println("With squared coefficent of variance:");
  81. double batch_sc2 = in.nextDouble();
  82. 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);
  83. } else {
  84. dist = new Distributions(0.2, "constant", 2, 0, "exponential", 1, 20, "exponential", 1, 0, "exponential", 1, 0, "geometric", 4, 0);
  85. }
  86. in.close();
  87. simulate();
  88. }
  89.  
  90. void simulate() {
  91. //for(lowSpeedThreshold = 0; lowSpeedThreshold <= m_MAX; lowSpeedThreshold++) {
  92. //for(highSpeedThreshold = lowSpeedThreshold+1; highSpeedThreshold <= lowSpeedThreshold+M_MAX; highSpeedThreshold++) {
  93. highSpeedThreshold = 5;
  94. lowSpeedThreshold = 1;
  95. dist.resetRandom(); //To keep the same random numbers every time it starts simulating again.
  96. simrun();
  97. double percentageHighSpeed = cumqobs.timeHighSpeed/((1-STARTUP)*RUNTIME);
  98. //double switchesPerUnitOfTime = cumqobs.amountOfSwitches/((1-STARTUP)*RUNTIME);
  99. outfile.println(cumqobs.numServed);
  100. //double effectiveLambda = dist.getArrivalRate()*dist.getBatchMean(); //Every arrival consists of more people, so the effective arrival rate is greater than lambda
  101. //double rho = effectiveLambda*dist.getSojourn("mean");
  102. // outfile.print(lowSpeedThreshold);
  103. outfile.print(" ");
  104. outfile.print(cumqobs.timeRegularSpeed/((1-STARTUP)*RUNTIME));
  105. outfile.print(" ");
  106. outfile.print(cumqobs.sojournTime);
  107. outfile.print(" ");
  108. outfile.print(percentageHighSpeed);
  109. outfile.print(" ");
  110. outfile.print((double)(cumqobs.numberOfJobs/((1-STARTUP)*RUNTIME))); //avg of jobs in the system
  111. //outfile.print(" ");
  112. //outfile.print(rho + (Math.pow(rho, 2) + Math.pow(effectiveLambda, 2)* dist.getSojourn("variance"))/(2*(1-rho)));
  113. //outfile.print(" ");
  114. //outfile.print(effectiveLambda*(cumqobs.waitingTime + cumqobs.sojournTime)/cumqobs.numServed); //avg number of customers in the store (LITTLE)
  115. outfile.print(" ");
  116. outfile.print(cumqobs.sojournTime/cumqobs.numServed);
  117. // outfile.print(" ");
  118. // outfile.print(switchesPerUnitOfTime); //avg number of switches per unit of time
  119. //outfile.print(" ");
  120. //outfile.print(cumqobs.numberOfJobs/((1-STARTUP)*RUNTIME) - 2.01 * cumqobs.estimatedSampleVariance + "," + (cumqobs.numberOfJobs/((1-STARTUP)*RUNTIME)+2.01*cumqobs.estimatedSampleVariance));
  121. outfile.println("");
  122. //}
  123. //}
  124.  
  125. }
  126.  
  127. void simrun() {
  128. obs = new Observations();
  129. eventlist = new EventList();
  130. queue = new Queue();
  131. eventlist.insert(new Event(dist.arrival(), "arrival")); //First arrival.
  132. double clock = 0.0;
  133. double te = 0.0;
  134. boolean nonRelevant = true;
  135. int batch = 1;
  136. server = 0;
  137. double [] batchMean = new double [NRBATCHES];
  138. while (clock < RUNTIME) {
  139. if (clock > STARTUP*RUNTIME && nonRelevant) { //Is only true once, and creates new Observations
  140. obs.init();
  141. cumqobs = new Observations();
  142. nonRelevant = false;
  143. } else if (clock > STARTUP*RUNTIME + batch*BATCHLENGTH) { //Batches
  144. cumqobs.add(obs);
  145. batchMean[batch-1] = obs.numberOfJobs/BATCHLENGTH;
  146. obs.init();
  147. batch++;
  148. }
  149. eventlist.setFirst();
  150. Event ev = eventlist.retrieve();
  151. te = ev.getTime();
  152. eventlist = eventlist.remove();
  153. if (ev.getType() == "arrival") {
  154. dummy = dist.batchSize();
  155. for(int i=0; i< dummy; i++) {
  156. handleArrival(clock,te);
  157. }
  158. eventlist.insert(new Event(te + dist.arrival(), "arrival"));
  159. }
  160. else if(ev.getType() == "departure"){
  161. handleDeparture(clock,te);
  162. } else if(ev.getType() == "vacation") {
  163. handleVacation(clock,te);
  164. }
  165. clock = te;
  166. }
  167. cumqobs.add(obs); //Add the last batch to the cumulative Observations
  168. cumqobs.estimatedSampleVariance = estimateSampleVariance(batchMean);
  169. }
  170.  
  171. private double estimateSampleVariance(double[] array) {
  172. double sum = 0;
  173. for (int j = 0; j <= NRBATCHES-1; j++) {
  174. sum += Math.pow(array[j] - cumqobs.numberOfJobs/((1-STARTUP)*RUNTIME), 2);
  175. }
  176.  
  177. return Math.sqrt(sum/(NRBATCHES-1))/Math.sqrt(NRBATCHES);
  178. }
  179.  
  180. void handleArrival(double tc, double tn) {
  181. obs.numberOfJobs += (queue.size() + server%2)*(tn-tc); //Server modulo 2 is 1 if server is serving a customer
  182. if (server == 0) {
  183. double sojournTime = dist.sojournTime();
  184. obs.numServed++; //Add to Observations
  185. obs.sojournTime += sojournTime;
  186. eventlist = eventlist.insert(new Event(tn+sojournTime, "departure"));
  187. server = 1; //The server is now busy
  188. } else {
  189. Customer c = new Customer(tn);
  190. queue = queue.insert(c);
  191. }
  192. }
  193.  
  194. void handleDeparture(double tc, double tn) {
  195. obs.numberOfJobs += (queue.size() + server%2)*(tn-tc); //Server modulo 2 is 1 if server is serving a customer.
  196. if (queue.size() == 0) {
  197. server = 0;
  198. if(dist.isHighSpeed()) { //Check if the server needs to go back to regular speed.
  199. server = 2;
  200. double vacationTime = tn + dist.vacationTime();
  201. obs.beginVacationToRegular(vacationTime);
  202. obs.timeHighSpeed(tn);
  203. eventlist = eventlist.insert(new Event(vacationTime, "vacation"));
  204. dist.changeSpeed();
  205. obs.amountOfSwitches++;
  206. }
  207. } else if(queue.size() >= highSpeedThreshold && !dist.isHighSpeed()){ //Check if the server needs to go to the higher speed.
  208. server = 2; //The server is now on vacation
  209. double vacationTime = tn + dist.vacationTime();
  210. eventlist = eventlist.insert(new Event(vacationTime, "vacation"));
  211. obs.beginVacationToHigh(vacationTime);
  212. obs.timeRegularSpeed(tn);
  213. dist.changeSpeed();
  214. obs.amountOfSwitches++;
  215. } else if(queue.size() <= lowSpeedThreshold && dist.isHighSpeed()) { //Check if the server needs to go back to regular speed.
  216. server = 2; //The server is now on vacation
  217. double vacationTime = tn + dist.vacationTime();
  218. eventlist = eventlist.insert(new Event(vacationTime, "vacation"));
  219. obs.beginVacationToRegular(vacationTime);
  220. obs.timeHighSpeed(tn);
  221. dist.changeSpeed();
  222. obs.amountOfSwitches++;
  223. } else {
  224. queue.setFirst();
  225. Customer c = queue.retrieve();
  226. queue = queue.remove();
  227. double sojournTime = dist.sojournTime();
  228. obs.numServed++;
  229. obs.waitingTime += (tn-c.arrivalTime); //Add to Observations
  230. obs.sojournTime += sojournTime; //
  231. eventlist = eventlist.insert(new Event(tn + sojournTime, "departure"));
  232. }
  233. }
  234.  
  235. void handleVacation(double tc, double tn) {
  236. obs.numberOfJobs += (queue.size() + server%2)*(tn-tc); //Server modulo 2 is 1 if server is serving a customer
  237. if (queue.size() == 0) { //Check if the server needs to server somebody instantly.
  238. server = 0;
  239. } else {
  240. queue.setFirst();
  241. Customer c = (Customer) queue.retrieve(); //Get the next customer.
  242. queue = queue.remove();
  243. double sojournTime = dist.sojournTime();
  244. obs.numServed++;
  245. obs.waitingTime += (tn-c.arrivalTime); //Add to Observations
  246. obs.sojournTime += sojournTime; //
  247. eventlist = eventlist.insert(new Event(tn + sojournTime, "departure"));
  248. server = 1; //The server is now busy
  249. }
  250. }
  251.  
  252. public static void main(String[] argv) {
  253. new essayMain().start();
  254. }
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement