|
@@ -2,10 +2,14 @@ package algorithm.objectiveFunction;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.HashSet;
|
|
|
import java.util.LinkedList;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map.Entry;
|
|
|
+import java.util.Set;
|
|
|
|
|
|
import classes.AbstractCanvasObject;
|
|
|
+import classes.HolonObject;
|
|
|
import classes.HolonSwitch;
|
|
|
import ui.model.Consumer;
|
|
|
import ui.model.DecoratedCable;
|
|
@@ -36,8 +40,8 @@ public class GraphMetrics {
|
|
|
public static class Graph{
|
|
|
Vertex[] V;
|
|
|
Edge[] E;
|
|
|
-
|
|
|
- Vertex[] Sources;
|
|
|
+
|
|
|
+ Vertex[] S;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -50,18 +54,23 @@ public class GraphMetrics {
|
|
|
public static Graph convertDecoratedNetworkToGraph(DecoratedNetwork net) {
|
|
|
Graph G = new Graph();
|
|
|
HashMap<AbstractCanvasObject, Integer> objectToId = new HashMap<>();
|
|
|
+ List<Integer> sourcesList = new ArrayList<Integer>();
|
|
|
int count = 0;
|
|
|
for(Consumer con: net.getConsumerList()) {
|
|
|
- objectToId.putIfAbsent(con.getModel(), count++);
|
|
|
+ objectToId.put(con.getModel(), count++);
|
|
|
+ sourcesList.add(count);
|
|
|
}
|
|
|
for(Consumer con: net.getConsumerSelfSuppliedList()) {
|
|
|
- objectToId.putIfAbsent(con.getModel(), count++);
|
|
|
+ objectToId.put(con.getModel(), count++);
|
|
|
+ sourcesList.add(count);
|
|
|
}
|
|
|
for(Passiv pas: net.getPassivNoEnergyList()) {
|
|
|
- objectToId.putIfAbsent(pas.getModel(), count++);
|
|
|
+ objectToId.put(pas.getModel(), count++);
|
|
|
+ sourcesList.add(count);
|
|
|
}
|
|
|
for(Supplier sup: net.getSupplierList()) {
|
|
|
- objectToId.putIfAbsent(sup.getModel(), count++);
|
|
|
+ objectToId.put(sup.getModel(), count++);
|
|
|
+ sourcesList.add(count);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -114,62 +123,47 @@ public class GraphMetrics {
|
|
|
}
|
|
|
|
|
|
G.V = new Vertex[objectToId.size()];
|
|
|
- for(int i=0;i<G.V.length;i++)
|
|
|
- {
|
|
|
- G.V[i] = new Vertex(i);
|
|
|
- }
|
|
|
- return G;
|
|
|
- }
|
|
|
-
|
|
|
- public static void main(String[] args) {
|
|
|
- System.out.println("Test");
|
|
|
- int amountSwitches = 30;
|
|
|
- double times = Math.pow(2, amountSwitches);
|
|
|
- for(int i = 0; i < times; i++) {
|
|
|
- System.out.print("i(" + i +"):");
|
|
|
- for(int k = 0; k < amountSwitches; k++) {
|
|
|
- boolean result = ((i >> k) & 1) != 0;
|
|
|
- System.out.print(result + ", ");
|
|
|
- }
|
|
|
- System.out.println();
|
|
|
+ int entryCount = 0;
|
|
|
+ for(Entry<AbstractCanvasObject, Integer> entry : objectToId.entrySet()) {
|
|
|
+ G.V[entryCount] = new Vertex(entry.getValue());
|
|
|
+ entryCount++;
|
|
|
}
|
|
|
+
|
|
|
+ int sourceCount = 0;
|
|
|
+ G.S = new Vertex[sourcesList.size()];
|
|
|
+ for(int source : sourcesList) {
|
|
|
+ G.S[sourceCount] = new Vertex(source);
|
|
|
+ sourceCount++;
|
|
|
+ }
|
|
|
+ return G;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+ public static void main(String[] args){
|
|
|
+ Graph G = new Graph();
|
|
|
+ G.V = new Vertex[5];
|
|
|
+ for(int i=0;i<G.V.length;i++)
|
|
|
+ {
|
|
|
+ G.V[i] = new Vertex(i);
|
|
|
+ }
|
|
|
+ G.E = new Edge[5];
|
|
|
+
|
|
|
+ G.E[0] = new Edge(0, 1, 1);
|
|
|
+ G.E[1] = new Edge(1, 2, 1);
|
|
|
+ G.E[2] = new Edge(2, 3, 1);
|
|
|
+ G.E[3] = new Edge(3, 0, 1);
|
|
|
+ G.E[4] = new Edge(0, 4, 1);
|
|
|
+
|
|
|
+ G.S = new Vertex[3];
|
|
|
+ G.S[0] = new Vertex(0);
|
|
|
+ G.S[1] = new Vertex(1);
|
|
|
+ G.S[2] = new Vertex(4);
|
|
|
+
|
|
|
+ System.out.println("minimumCut: " + minimumCut(G.V, G.E));
|
|
|
+ System.out.println("AvgShortestDistance " + averageShortestDistance(G.V, G.E));
|
|
|
+ System.out.println("DisjointPath " + averageEdgeDisjointPathProblem(G));
|
|
|
+ }
|
|
|
|
|
|
static int[][] generateDisjointWeightMatrix(Vertex[] V, Edge[] E){
|
|
|
int[][] L = new int[V.length][V.length];
|
|
@@ -180,8 +174,97 @@ public class GraphMetrics {
|
|
|
return L;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ static double averageEdgeDisjointPathProblem(Graph G) {
|
|
|
+
|
|
|
+ int[][] L = generateDisjointWeightMatrix(G.V, G.E);
|
|
|
+ double disjointPathSum = 0.0;
|
|
|
+ for(int s = 0; s < G.S.length; s++) {
|
|
|
+ for(int t = s; t < G.S.length; t++) {
|
|
|
+ disjointPathSum += s_t_EdgeDisjointPath(G.S[s].id, G.S[t].id, L);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return disjointPathSum / ((G.S.length * (G.S.length - 1)) / 2);
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+ private static double s_t_EdgeDisjointPath(int s, int t, int[][] L_input) {
|
|
|
+ if(s == t) return 0;
|
|
|
+
|
|
|
+ int [][] L = makeCopyOfMatrix(L_input);
|
|
|
+ double foundPaths = 0;
|
|
|
+
|
|
|
+ boolean b_foundpath = true;
|
|
|
+ while(b_foundpath) {
|
|
|
+ b_foundpath = breathFirstSearch(s,t,L);
|
|
|
+ if(b_foundpath) {
|
|
|
+ foundPaths++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return foundPaths;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private static boolean breathFirstSearch(int s, int t, int[][] L) {
|
|
|
+
|
|
|
+ Set<Integer> visitedNotes = new HashSet<Integer>();
|
|
|
+
|
|
|
+ LinkedList<Integer> queue = new LinkedList<Integer>();
|
|
|
+
|
|
|
+ HashMap<Integer,Integer> noteBeforeMap = new HashMap<Integer,Integer>();
|
|
|
+
|
|
|
+ queue.add(s);
|
|
|
+
|
|
|
+ while(!queue.isEmpty()) {
|
|
|
+ int id = queue.pollFirst();
|
|
|
+ visitedNotes.add(id);
|
|
|
+
|
|
|
+ for(int i = 0; i < L[id].length; i++) {
|
|
|
+
|
|
|
+ if(L[id][i] != 0 && !visitedNotes.contains(i)) {
|
|
|
+
|
|
|
+ queue.add(i);
|
|
|
+ noteBeforeMap.putIfAbsent(i, id);
|
|
|
+
|
|
|
+ if(i == t) {
|
|
|
+
|
|
|
+ int actualID = t;
|
|
|
+ while(actualID != s) {
|
|
|
+ int nextID = noteBeforeMap.get(actualID);
|
|
|
+
|
|
|
+ L[nextID][actualID] = 0;
|
|
|
+ L[actualID][nextID] = 0;
|
|
|
+ actualID = nextID;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * Makes a deep Copy of a Integer Matrix
|
|
|
+ * @param l_input
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static int[][] makeCopyOfMatrix(int[][] matrix) {
|
|
|
+ int[][] copyMatrix = new int[matrix.length][matrix[0].length];
|
|
|
+ for(int i = 0; i < matrix.length; i++) {
|
|
|
+ for(int j = 0; j < matrix[0].length; j++) {
|
|
|
+ copyMatrix[i][j] = matrix[i][j];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return copyMatrix;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
* Stoer Wagner Minimal Cut Algo
|
|
|
* Source from
|
|
|
* <a href="https://github.com/hunglvosu/algorithm/blob/master/algorithm/src/Stoer-Wagner.c">
|