GraphMetrics.java 10.0 KB

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