Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. package test;
  2.  
  3. import java.util.Random;
  4.  
  5. class Generator implements Runnable {
  6. private final String id;
  7. private static final Random RND = new Random();
  8. private static final int MAX_VALUE = 1000;
  9. public static final int COUNT = 30;
  10. private double[] result = new double[COUNT + 1];
  11. public Generator(String id) {
  12. this.id = id;
  13. }
  14. public long task() {
  15. return RND.nextInt(MAX_VALUE + 1);
  16. }
  17. @Override
  18. public void run() {
  19. result[0] = 0;
  20. for (int i = 0; i < COUNT; i++) {
  21. result[i + 1] = task();
  22. result[0] += result[i + 1];
  23. }
  24. result[0] /= COUNT;
  25. /* disp */
  26. double disp = 0;
  27. for (int i = 0; i < COUNT; i++) {
  28. double pt = result[i+1] - result[0];
  29. disp += pt * pt;
  30. }
  31. disp /= (COUNT - 1);
  32. System.out.println(id + "(AVG: " + result[0] + "; DISP: " + disp + ")");
  33. }
  34. public double getAvg() {
  35. return result[0];
  36. }
  37. public double[] getResult() {
  38. return result;
  39. }
  40. }
  41.  
  42. class Matrix {
  43. public static final int[][] MTX = {
  44. {0, 0, 0, 0, 0, 0, 0},
  45. {6, 0, 0, 0, 0, 0, 0},
  46. {4, 0, 0, 0, 0, 0, 0},
  47. {0, 0, 7, 0, 0, 0, 0},
  48. {0, 3, 0, 0, 0, 0, 0},
  49. {0, 0, 0, 4, 5, 0, 0},
  50. {0, 0, 0, 0, 0, 3, 0}
  51. };
  52. public static final int N = MTX.length;
  53. public static final double MIN = -10000;
  54. public static final double MAX = 10000;
  55. private Double[][] dist;
  56. private Integer[][] next;
  57. public void fill(double inf) {
  58. dist = new Double[N][N];
  59. next = new Integer[N][N];
  60. for (int i = 0; i < N; i++)
  61. for (int j = 0; j < N; j++)
  62. dist[i][j] = inf;
  63. }
  64. public void printArray(Number[][] arr) {
  65. for (Number[] line : arr) {
  66. for (Number el : line) {
  67. System.out.print(el + " ");
  68. }
  69. System.out.println();
  70. }
  71. }
  72. public void print() {
  73. System.out.println("DIST:");
  74. printArray(dist);
  75.  
  76. System.out.println("NEXT: ");
  77. printArray(next);
  78. }
  79. public String criticalPath(double[] data) {
  80. fill(MIN);
  81. int pos = 0;
  82. for (int u = 0; u < N; u++) {
  83. for (int v = 0; v < N; v++) {
  84. if (MTX[u][v] > 0) {
  85. dist[u][v] = data[pos];
  86. pos++;
  87. next[u][v] = v;
  88. }
  89. }
  90. }
  91.  
  92. for (int k = 0; k < N; k++)
  93. for (int i = 0; i < N; i++)
  94. for (int j = 0; j < N; j++)
  95. if (dist[i][k] + dist[k][j] > dist[i][j]) {
  96. dist[i][j] = dist[i][k] + dist[k][j];
  97. next[i][j] = next[i][k];
  98. }
  99. return path(N - 1, 0);
  100. }
  101. public String fastestPath(double[] data) {
  102. fill(MAX);
  103. int pos = 0;
  104. for (int u = 0; u < N; u++) {
  105. for (int v = 0; v < N; v++) {
  106. if (MTX[u][v] > 0) {
  107. dist[u][v] = data[pos];
  108. pos++;
  109. next[u][v] = v;
  110. }
  111. }
  112. }
  113.  
  114. for (int k = 0; k < N; k++)
  115. for (int i = 0; i < N; i++)
  116. for (int j = 0; j < N; j++)
  117. if (dist[i][k] + dist[k][j] < dist[i][j]) {
  118. dist[i][j] = dist[i][k] + dist[k][j];
  119. next[i][j] = next[i][k];
  120. }
  121. return path(N - 1, 0);
  122. }
  123. public String path(int u, int v) {
  124. String result = "";
  125. if (next[u][v] == null) {
  126. return result;
  127. }
  128. result += u + " ";
  129. while (u != v) {
  130. u = next[u][v];
  131. result += u + " ";
  132. }
  133. return result;
  134.  
  135. }
  136. }
  137.  
  138. class Task {
  139. private double[][] results;
  140. private static final Matrix MTX = new Matrix();
  141.  
  142. public Task(Generator[] generators) {
  143. results = new double[generators.length][];
  144. for (int i = 0; i < generators.length; i++) {
  145. results[i] = generators[i].getResult();
  146. }
  147. }
  148.  
  149. public void perform() {
  150. String[] paths = new String[Generator.COUNT + 1];
  151. for (int i = 0; i < Generator.COUNT + 1; i++) {
  152. double[] data = new double[Matrix.N];
  153. for (int j = 0; j < Matrix.N; j++) {
  154. data[j] = results[j][i];
  155. if (i == 0) {
  156. paths[i] = MTX.criticalPath(data);
  157. } else {
  158. paths[i] = MTX.fastestPath(data);
  159. }
  160. }
  161. }
  162. int counter = 0;
  163. String criticalPath = paths[0];
  164. for (String path : paths) {
  165. if (criticalPath.equals(path)) {
  166. counter++;
  167. }
  168. }
  169. System.out.println("CRITICAL PATH CHANCE: " + (double)counter / Generator.COUNT * 100);
  170. }
  171.  
  172.  
  173. }
  174.  
  175.  
  176. class Runner {
  177.  
  178. public static void main(String[] args) throws InterruptedException {
  179. /*Matrix m = new Matrix();
  180. m.floyd();
  181. m.path(6, 0);*/
  182. Generator[] generators = new Generator[Matrix.N];
  183. Thread[] threads = new Thread[Matrix.N];
  184. for (int i = 0; i < Matrix.N; i++ ) {
  185. generators[i] = new Generator ("THREAD " + i);
  186. threads[i] = new Thread(generators[i]);
  187. threads[i].start();
  188. }
  189.  
  190. for (Thread thread : threads) {
  191. thread.join();
  192. }
  193.  
  194. new Task(generators).perform();
  195. }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement