GraphMetrics.java 10 KB

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