GraphMetrics.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. package algorithm.objectiveFunction;
  2. import java.util.HashMap;
  3. import java.util.LinkedList;
  4. import classes.AbstractCanvasObject;
  5. import ui.model.Consumer;
  6. import ui.model.DecoratedCable;
  7. import ui.model.DecoratedNetwork;
  8. import ui.model.Passiv;
  9. import ui.model.Supplier;
  10. public class GraphMetrics {
  11. public static class Vertex{
  12. public int id = 0;
  13. public Vertex(int id){
  14. this.id = id;
  15. }
  16. }
  17. private static class Edge{
  18. public int idA, idB;
  19. public double weight;
  20. public Edge(int idA, int idB, double weight){
  21. this.idA = idA;
  22. this.idB = idB;
  23. this.weight = weight;
  24. }
  25. }
  26. public static class Graph{
  27. Vertex[] V;
  28. Edge[] E;
  29. }
  30. /**
  31. * Convert a DecoratedNetwork to a Graph
  32. * @param net
  33. * @return a equivalent Graph to the DecoratedNetwork
  34. */
  35. public static Graph convertDecoratedNetworkToGraph(DecoratedNetwork net) {
  36. Graph G = new Graph();
  37. HashMap<AbstractCanvasObject, Integer> objectToId = new HashMap<>();
  38. int count = 0;
  39. for(Consumer con: net.getConsumerList()) {
  40. objectToId.putIfAbsent(con.getModel(), count++);
  41. }
  42. for(Consumer con: net.getConsumerSelfSuppliedList()) {
  43. objectToId.putIfAbsent(con.getModel(), count++);
  44. }
  45. for(Passiv pas: net.getPassivNoEnergyList()) {
  46. objectToId.putIfAbsent(pas.getModel(), count++);
  47. }
  48. for(Supplier sup: net.getSupplierList()) {
  49. objectToId.putIfAbsent(sup.getModel(), count++);
  50. }
  51. G.E = new Edge[net.getDecoratedCableList().size()];
  52. int edgeCount = 0;
  53. for(DecoratedCable cable : net.getDecoratedCableList()){
  54. AbstractCanvasObject objectA = cable.getModel().getA();
  55. AbstractCanvasObject objectB = cable.getModel().getB();
  56. if(objectA == null) {
  57. System.out.println("Edge: " + cable + "objectA == null");
  58. continue;
  59. }
  60. if(objectB == null) {
  61. System.out.println("Edge: " + cable + "objectB == null");
  62. continue;
  63. }
  64. int idA = -1;
  65. if(objectToId.containsKey(objectA)) {
  66. idA = objectToId.get(objectA);
  67. }else {
  68. idA = count;
  69. objectToId.put(objectA, count++);
  70. }
  71. int idB = -1;
  72. if(objectToId.containsKey(objectB)) {
  73. idB = objectToId.get(objectB);
  74. }else {
  75. idB = count;
  76. objectToId.put(objectB, count++);
  77. }
  78. double length = cable.getModel().getLength();
  79. G.E[edgeCount++] = new Edge(idA, idB, length);
  80. }
  81. G.V = new Vertex[objectToId.size()];
  82. for(int i=0;i<G.V.length;i++)
  83. {
  84. G.V[i] = new Vertex(i);
  85. }
  86. return G;
  87. }
  88. public static void main(String[] args) {
  89. System.out.println("Test");
  90. int amountSwitches = 30;
  91. double times = Math.pow(2, amountSwitches);
  92. for(int i = 0; i < times; i++) {
  93. System.out.print("i(" + i +"):");
  94. for(int k = 0; k < amountSwitches; k++) {
  95. boolean result = ((i >> k) & 1) != 0;
  96. System.out.print(result + ", ");
  97. }
  98. System.out.println();
  99. }
  100. }
  101. // Example Test
  102. // public static void main(String[] args){
  103. // Graph G = new Graph();
  104. // G.V = new Vertex[9];
  105. // for(int i=0;i<G.V.length;i++)
  106. // {
  107. // G.V[i] = new Vertex(i);
  108. // }
  109. // G.E = new Edge[18];
  110. //
  111. // G.E[0] = new Edge(0, 1, 4);
  112. // G.E[1] = new Edge(1, 2, 4);
  113. // G.E[2] = new Edge(2, 3, 4);
  114. // G.E[3] = new Edge(0, 4, 4);
  115. // G.E[4] = new Edge(0, 5, 4);
  116. // G.E[5] = new Edge(1, 4, 4);
  117. // G.E[6] = new Edge(1, 5, 4);
  118. // G.E[7] = new Edge(2, 6, 4);
  119. // G.E[8] = new Edge(2, 7, 4);
  120. // G.E[9] = new Edge(3, 6, 4);
  121. // G.E[10] = new Edge(3, 7, 4);
  122. // G.E[11] = new Edge(4, 5, 4);
  123. // G.E[12] = new Edge(5, 6, 4);
  124. // G.E[13] = new Edge(6, 7, 4);
  125. // G.E[14] = new Edge(3, 8, 4);
  126. // G.E[15] = new Edge(6, 8, 4);
  127. // G.E[16] = new Edge(7, 8, 4);
  128. // G.E[17] = new Edge(2, 5, 4);
  129. //
  130. //
  131. // System.out.println("k: " + minimumCut(G.V, G.E));
  132. // System.out.println("AvgShortestDistance " + averageShortestDistance(G.V, G.E));
  133. //
  134. // }
  135. static int[][] generateDisjointWeightMatrix(Vertex[] V, Edge[] E){
  136. int[][] L = new int[V.length][V.length];
  137. for(int i = 0; i < E.length; i++) {
  138. L[E[i].idA][E[i].idB] = 1;
  139. L[E[i].idB][E[i].idA] = 1;
  140. }
  141. return L;
  142. }
  143. /**
  144. * Stoer Wagner Minimal Cut Algo
  145. * Source from
  146. * <a href="https://github.com/hunglvosu/algorithm/blob/master/algorithm/src/Stoer-Wagner.c">
  147. * https://github.com/hunglvosu/algorithm/blob/master/algorithm/src/Stoer-Wagner.c</a> <br>
  148. * in C formatted in java
  149. * @param V Vertex Array
  150. * @param E Edge Array
  151. * @return
  152. */
  153. static int minimumCut(Vertex[] V, Edge[] E) {
  154. int[][] W = generateDisjointWeightMatrix(V, E);
  155. boolean[] Del = new boolean[V.length];
  156. int n = V.length; // the number of veritces
  157. int m = E.length;
  158. return StoerWagner(n, m, W, Del);
  159. }
  160. static int StoerWagner(int n, int m, int[][] W, boolean[] Del){
  161. int C = Integer.MAX_VALUE;
  162. for(int V = n; V > 1; V--){
  163. int cutValue = minCutPhase(V, W, Del, n, m);
  164. C = (C < cutValue ? C: cutValue);
  165. }
  166. return C;
  167. }
  168. static int minCutPhase(int V, int[][] W, boolean[] Del, int n, int m){
  169. int i = 0, j = 0;
  170. int[] s = new int[2];
  171. if(V == 2) {
  172. for( i = 0; i < n; i++){
  173. if(Del[i] == false){
  174. s[j] = i; j++;
  175. }
  176. }
  177. return W[s[0]][s[1]];
  178. }
  179. int[] L = new int[n];
  180. boolean[] T = new boolean[n];
  181. i = 1; // the number of vertices in the tree T
  182. j = 0;
  183. int v,u;
  184. while( i <= V){
  185. v = maxStickiness(T,L, Del, n);
  186. T[v] = true;
  187. for(u = 0; u < n; u++){
  188. if(W[v][u] != 0 && Del[u] == false && T[u] == false){
  189. L[u] = L[u] + W[u][v];
  190. }
  191. }
  192. if( i >= V-1){
  193. s[j] = v; j++;
  194. }
  195. i++;
  196. }
  197. merge(s[0], s[1], n, W, Del);
  198. return L[s[1]];
  199. }
  200. static int maxStickiness(boolean[] T, int[] L, boolean[] Del, int n){
  201. int i = 0;
  202. int v = 0;
  203. int max = 0;
  204. for(i = 0; i < n; i++){
  205. if(Del[i] == false && T[i] == false && max < L[i]){
  206. v = i;
  207. max = L[i];
  208. }
  209. }
  210. return v;
  211. }
  212. static void merge(int s, int t, int n, int[][] W, boolean[] Del){
  213. int v = 0;
  214. for(v = 0; v < n; v++){
  215. if(Del[v] == false && v != s && v!= t){
  216. W[s][v] = W[s][v] + W[v][t];
  217. W[v][s] = W[s][v];
  218. }
  219. }
  220. Del[t] = true;
  221. }
  222. static double[][] basicAllPairsShortestPath(Vertex[] V, Edge[] E){
  223. double[][] L = generateWeightMatrix(V, E);
  224. double[][] D = generateDistanceMatrix(V);
  225. boolean[] flag = generateFlagList(V);
  226. for(int i=0;i<V.length;i++) {
  227. modifiedDikstra(V[i].id, L, D, flag);
  228. }
  229. return D;
  230. }
  231. static double averageShortestDistance(Vertex[] V, Edge[] E) {
  232. if(V.length <= 1) return 0.0;
  233. double[][] D = basicAllPairsShortestPath(V, E);
  234. double sum = 0;
  235. int maxColumn = 1;
  236. for(int row = 1; row < D.length; row++) {
  237. for(int column = 0; column < maxColumn; column++) {
  238. sum += D[row][column];
  239. }
  240. maxColumn++;
  241. }
  242. int numbers = ((D.length - 1) * (D.length)) / 2;
  243. return sum / (double)numbers;
  244. }
  245. /**
  246. * @return Updated Distance Matrix D and flag List
  247. */
  248. static void modifiedDikstra(int source, double[][] L, double[][] D, boolean[] flag) {
  249. D[source][source] = 0;
  250. LinkedList<Integer> visitedNotes = new LinkedList<Integer>();
  251. LinkedList<Integer> minPriorityQueue = new LinkedList<Integer>();
  252. minPriorityQueue.add(source);
  253. while(!minPriorityQueue.isEmpty()) {
  254. minPriorityQueue.sort((a,b) -> Double.compare(D[source][a], D[source][b]));
  255. int target = minPriorityQueue.pop();
  256. if(flag[source] == true) {
  257. for(int outgoingID = 0; outgoingID < L.length; outgoingID++) {
  258. if(D[source][target] + L[target][outgoingID] < D[source][outgoingID]) {
  259. D[source][outgoingID] = D[source][target] + L[target][outgoingID];
  260. }
  261. }
  262. }else {
  263. for(int outgoingID = 0; outgoingID < L.length; outgoingID++) {
  264. if(L[target][outgoingID] == Double.POSITIVE_INFINITY) continue;
  265. if(D[source][target] + L[target][outgoingID] < D[source][outgoingID]) {
  266. D[source][outgoingID] = D[source][target] + L[target][outgoingID];
  267. if(!visitedNotes.contains(outgoingID)) {
  268. minPriorityQueue.add(outgoingID);
  269. }
  270. }
  271. }
  272. }
  273. visitedNotes.add(target);
  274. }
  275. flag[source] = true;
  276. }
  277. static void printMatrix(int[][] matrix) {
  278. for(int i=0;i<matrix.length;i++)
  279. {
  280. for(int j=0;j<matrix.length;j++)
  281. {
  282. System.out.print(matrix[i][j] + " ");
  283. }
  284. System.out.println();
  285. }
  286. }
  287. static void printMatrix(double[][] matrix) {
  288. for(int i=0;i<matrix.length;i++)
  289. {
  290. for(int j=0;j<matrix.length;j++)
  291. {
  292. System.out.print(matrix[i][j] + " ");
  293. }
  294. System.out.println();
  295. }
  296. }
  297. static double[][] generateWeightMatrix(Vertex[] V, Edge[] E){
  298. double[][] L = new double[V.length][V.length];
  299. for(int i = 0; i < E.length; i++) {
  300. L[E[i].idA][E[i].idB] = E[i].weight;
  301. L[E[i].idB][E[i].idA] = E[i].weight;
  302. }
  303. for(int i=0;i<L.length;i++)
  304. {
  305. for(int j=0;j<L.length;j++)
  306. {
  307. if(L[i][j]==0.0) L[i][j] = Double.POSITIVE_INFINITY;
  308. }
  309. }
  310. for(int i=0;i<L.length;i++)
  311. {
  312. L[i][i] = 0.0;
  313. }
  314. return L;
  315. }
  316. static double[][] generateDistanceMatrix(Vertex[] V) {
  317. double[][] D = new double[V.length][V.length];
  318. for(int i=0;i<D.length;i++)
  319. {
  320. for(int j=0;j<D.length;j++)
  321. {
  322. D[i][j] = Double.POSITIVE_INFINITY;
  323. }
  324. }
  325. return D;
  326. }
  327. private static boolean[] generateFlagList(Vertex[] V) {
  328. boolean[] flag = new boolean[V.length];
  329. return flag;
  330. }
  331. }