Browse Source

Added All Pairs (edge) s-t disjointPath Problem

Troppmann, Tom 3 years ago
parent
commit
b81622f9e4

+ 141 - 58
src/algorithm/objectiveFunction/GraphMetrics.java

@@ -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;
-		//For disjoint Path
-		Vertex[] Sources;
+		/**Only the Sources/Trains for disjoint Path no Switch or Node**/
+		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 {
 	    }
 		//Generate VertexArray
 		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++;
 		}
+		//Generate Sources Array
+		int sourceCount = 0;
+		G.S = new Vertex[sourcesList.size()];
+		for(int source : sourcesList) {
+			G.S[sourceCount] = new Vertex(source);
+			sourceCount++;
+		}
+		return G;
 	}
 	
 	
 // Example Test
-//	 public static void main(String[] args){
-//		 Graph G = new Graph();
-//		 G.V = new Vertex[9];
-//		 for(int i=0;i<G.V.length;i++)
-//	     {
-//				G.V[i] =  new Vertex(i);
-//	     }
-//		 G.E = new Edge[18];
-//		 
-//		 G.E[0] = new Edge(0, 1, 4);
-//		 G.E[1] = new Edge(1, 2, 4);
-//		 G.E[2] = new Edge(2, 3, 4);
-//		 G.E[3] = new Edge(0, 4, 4);
-//		 G.E[4] = new Edge(0, 5, 4);
-//		 G.E[5] = new Edge(1, 4, 4);
-//		 G.E[6] = new Edge(1, 5, 4);
-//		 G.E[7] = new Edge(2, 6, 4);
-//		 G.E[8] = new Edge(2, 7, 4);
-//		 G.E[9] = new Edge(3, 6, 4);
-//		 G.E[10] = new Edge(3, 7, 4);
-//		 G.E[11] = new Edge(4, 5, 4);
-//		 G.E[12] = new Edge(5, 6, 4);
-//		 G.E[13] = new Edge(6, 7, 4);
-//		 G.E[14] = new Edge(3, 8, 4);
-//		 G.E[15] = new Edge(6, 8, 4);
-//		 G.E[16] = new Edge(7, 8, 4);
-//		 G.E[17] = new Edge(2, 5, 4);
-//		 
-//
-//		 System.out.println("k: " + minimumCut(G.V, G.E));
-//		 System.out.println("AvgShortestDistance " + averageShortestDistance(G.V, G.E));
-//		 
-//	 }
+	 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;
 		}
 	 
+	 /** find the average s_t disjoint paths between all pairs of s and t **/
+	 static double averageEdgeDisjointPathProblem(Graph G) {
+		 //This matrix indicates if a Edge is existent between two vertices
+		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);					 
+			 }
+		 }
+		 //Returns the Average
+		 return disjointPathSum / ((G.S.length * (G.S.length - 1)) / 2);
+	 }
+	 
 	 
-	 /**
+	 
+	 /** find the amount of s_t disjoint paths between s and t **/
+	 private static double s_t_EdgeDisjointPath(int s, int t, int[][] L_input) {
+		 if(s == t) return 0;
+		 //Make Copy of L
+		int [][] L = makeCopyOfMatrix(L_input);
+		double foundPaths = 0;
+		//RepeatBreathFirst search till no path is found
+		boolean b_foundpath = true;
+		while(b_foundpath) {
+			b_foundpath = breathFirstSearch(s,t,L);
+			if(b_foundpath) {
+				foundPaths++;
+			}
+		}
+		return foundPaths;
+	}
+
+	 /** execute one breathFirstSearch to find a path between s and t **/
+	 private static boolean breathFirstSearch(int s, int t, int[][] L) {
+		 //Visited Notes 
+		 Set<Integer> visitedNotes = new HashSet<Integer>();
+		 //Queue to check which node to search next
+		 LinkedList<Integer> queue = new LinkedList<Integer>();
+		 //Map to traverse the path back when found a path 
+		 HashMap<Integer,Integer> noteBeforeMap = new HashMap<Integer,Integer>();
+		 //Add starting Point
+		 queue.add(s);
+		 //Visit all neighbours of the next point in queue
+		 while(!queue.isEmpty()) {
+			 int id = queue.pollFirst();
+			 visitedNotes.add(id);
+			 //For all neighbors add to queue
+			 for(int i = 0; i < L[id].length; i++) {
+				 //Check if edge exist and not visited before
+				 if(L[id][i] != 0 && !visitedNotes.contains(i)) {
+					 
+					 queue.add(i);
+					 noteBeforeMap.putIfAbsent(i, id);
+					 //check if train is found
+					 if(i == t) {
+						 //update connectionMatrix l and remove the found path
+						 int actualID = t;
+						 while(actualID != s) {
+							 int nextID = noteBeforeMap.get(actualID);
+							 //remove edge
+							 L[nextID][actualID] = 0;
+							 L[actualID][nextID] = 0;
+							 actualID = nextID; 
+						 }
+						 return true;
+					 }
+				 }
+				 
+			 }
+		 }
+		 //if last queue element is searched but t is not reachable 
+		 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">

+ 1 - 3
src/algorithm/objectiveFunction/TopologieObjectiveFunction.java

@@ -184,9 +184,7 @@ public class TopologieObjectiveFunction {
 			}
 		
 			double avgShortestPath = GraphMetrics.averageShortestDistance(G.V,  G.E);
-			//TODO replace minimumCut with avg (edge-) disjoint s-t path  
-			//k-edge-conneted
-			int disjpointPaths = GraphMetrics.minimumCut(G.V, G.E);
+			double disjpointPaths = GraphMetrics.averageEdgeDisjointPathProblem(G);
 			f_grid += avgShortestPathPenalty(avgShortestPath) + disjoinPathPenalty(disjpointPaths);
 		}
 		//take average to encourage splitting