|
| 1 | +package org.hjug.dsm; |
| 2 | + |
| 3 | +import org.jgrapht.Graph; |
| 4 | +import org.jgrapht.Graphs; |
| 5 | +import org.jgrapht.alg.connectivity.KosarajuStrongConnectivityInspector; |
| 6 | +import org.jgrapht.alg.util.Triple; |
| 7 | +import org.jgrapht.graph.DefaultWeightedEdge; |
| 8 | +import org.jgrapht.opt.graph.sparse.SparseIntDirectedWeightedGraph; |
| 9 | + |
| 10 | +import java.util.*; |
| 11 | +import java.util.concurrent.ConcurrentHashMap; |
| 12 | +import java.util.concurrent.ConcurrentLinkedQueue; |
| 13 | +import java.util.concurrent.ConcurrentSkipListSet; |
| 14 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 15 | +import java.util.stream.Collectors; |
| 16 | +import java.util.stream.IntStream; |
| 17 | + |
| 18 | +// TODO: Delete |
| 19 | +class SparseIntDWGEdgeRemovalCalculator { |
| 20 | + private final Graph<String, DefaultWeightedEdge> graph; |
| 21 | + SparseIntDirectedWeightedGraph sparseGraph; |
| 22 | + List<Triple<Integer, Integer, Double>> sparseEdges; |
| 23 | + List<Integer> sparseEdgesAboveDiagonal; |
| 24 | + private final double sumOfEdgeWeightsAboveDiagonal; |
| 25 | + int vertexCount; |
| 26 | + Map<String, Integer> vertexToInt; |
| 27 | + Map<Integer, String> intToVertex; |
| 28 | + |
| 29 | + |
| 30 | + SparseIntDWGEdgeRemovalCalculator( |
| 31 | + Graph<String, DefaultWeightedEdge> graph, |
| 32 | + SparseIntDirectedWeightedGraph sparseGraph, |
| 33 | + List<Triple<Integer, Integer, Double>> sparseEdges, |
| 34 | + List<Integer> sparseEdgesAboveDiagonal, |
| 35 | + double sumOfEdgeWeightsAboveDiagonal, |
| 36 | + int vertexCount, |
| 37 | + Map<String, Integer> vertexToInt, |
| 38 | + Map<Integer, String> intToVertex) { |
| 39 | + this.graph = graph; |
| 40 | + this.sparseGraph = sparseGraph; |
| 41 | + this.sparseEdges = new CopyOnWriteArrayList<>(sparseEdges); |
| 42 | + this.sparseEdgesAboveDiagonal = new CopyOnWriteArrayList<>(sparseEdgesAboveDiagonal); |
| 43 | + this.sumOfEdgeWeightsAboveDiagonal = sumOfEdgeWeightsAboveDiagonal; |
| 44 | + this.vertexCount = vertexCount; |
| 45 | + this.vertexToInt = new ConcurrentHashMap<>(vertexToInt); |
| 46 | + this.intToVertex = new ConcurrentHashMap<>(intToVertex); |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + public List<EdgeToRemoveInfo> getImpactOfSparseEdgesAboveDiagonalIfRemoved() { |
| 51 | + return sparseEdgesAboveDiagonal.parallelStream() |
| 52 | + .map(this::calculateSparseEdgeToRemoveInfo) |
| 53 | + .sorted(Comparator.comparing(EdgeToRemoveInfo::getPayoff).thenComparing(EdgeToRemoveInfo::getRemovedEdgeWeight)) |
| 54 | + .collect(Collectors.toList()); |
| 55 | + } |
| 56 | + |
| 57 | + private EdgeToRemoveInfo calculateSparseEdgeToRemoveInfo(Integer edgeToRemove) { |
| 58 | + //clone graph and remove edge |
| 59 | + int source = sparseGraph.getEdgeSource(edgeToRemove); |
| 60 | + int target = sparseGraph.getEdgeTarget(edgeToRemove); |
| 61 | + double weight = sparseGraph.getEdgeWeight(edgeToRemove); |
| 62 | + Triple<Integer, Integer, Double> removedEdge = Triple.of(source, target, weight); |
| 63 | + |
| 64 | + List<Triple<Integer, Integer, Double>> tempUpdatedEdgeList = new ArrayList<>(sparseEdges); |
| 65 | + tempUpdatedEdgeList.remove(removedEdge); |
| 66 | + List<Triple<Integer, Integer, Double>> updatedEdgeList = new CopyOnWriteArrayList<>(tempUpdatedEdgeList); |
| 67 | + |
| 68 | + SparseIntDirectedWeightedGraph improvedGraph = new SparseIntDirectedWeightedGraph(vertexCount, updatedEdgeList); |
| 69 | + |
| 70 | + // find edges above diagonal |
| 71 | + List<Integer> sortedSparseVertices = orderVertices(improvedGraph); |
| 72 | + List<Integer> updatedEdges = getSparseEdgesAboveDiagonal(improvedGraph, sortedSparseVertices); |
| 73 | + |
| 74 | + // calculate new graph statistics |
| 75 | + int newEdgeCount = updatedEdges.size(); |
| 76 | + double newEdgeWeightSum = updatedEdges.stream() |
| 77 | + .mapToDouble(improvedGraph::getEdgeWeight).sum(); |
| 78 | + DefaultWeightedEdge defaultWeightedEdge = |
| 79 | + graph.getEdge(intToVertex.get(source), intToVertex.get(target)); |
| 80 | + double payoff = (sumOfEdgeWeightsAboveDiagonal - newEdgeWeightSum) / weight; |
| 81 | + return new EdgeToRemoveInfo(defaultWeightedEdge, (int) weight, newEdgeCount, payoff); |
| 82 | + } |
| 83 | + |
| 84 | + private List<Integer> orderVertices(SparseIntDirectedWeightedGraph sparseGraph) { |
| 85 | + List<Set<Integer>> sccs = new CopyOnWriteArrayList<>(findStronglyConnectedSparseGraphComponents(sparseGraph)); |
| 86 | +// List<Integer> sparseIntSortedActivities = topologicalSortSparseGraph(sccs, sparseGraph); |
| 87 | + List<Integer> sparseIntSortedActivities = topologicalParallelSortSparseGraph(sccs, sparseGraph); |
| 88 | + // reversing corrects rendering of the DSM |
| 89 | + // with sources as rows and targets as columns |
| 90 | + // was needed after AI solution was generated and iterated |
| 91 | + Collections.reverse(sparseIntSortedActivities); |
| 92 | + |
| 93 | + return new CopyOnWriteArrayList<>(sparseIntSortedActivities); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Kosaraju SCC detector avoids stack overflow. |
| 98 | + * It is used by JGraphT's CycleDetector, and makes sense to use it here as well for consistency |
| 99 | + * |
| 100 | + * @param graph |
| 101 | + * @return |
| 102 | + */ |
| 103 | + private List<Set<Integer>> findStronglyConnectedSparseGraphComponents(Graph<Integer, Integer> graph) { |
| 104 | + KosarajuStrongConnectivityInspector<Integer, Integer> kosaraju = |
| 105 | + new KosarajuStrongConnectivityInspector<>(graph); |
| 106 | + return kosaraju.stronglyConnectedSets(); |
| 107 | + } |
| 108 | + |
| 109 | + private List<Integer> topologicalSortSparseGraph(List<Set<Integer>> sccs, Graph<Integer, Integer> graph) { |
| 110 | + List<Integer> sortedActivities = new ArrayList<>(); |
| 111 | + Set<Integer> visited = new HashSet<>(); |
| 112 | + |
| 113 | + sccs.parallelStream() |
| 114 | + .flatMap(Set::parallelStream) |
| 115 | + .filter(activity -> !visited.contains(activity)) |
| 116 | + .forEach(activity -> topologicalSortUtilSparseGraph(activity, visited, sortedActivities, graph)); |
| 117 | + |
| 118 | + |
| 119 | + Collections.reverse(sortedActivities); |
| 120 | + return sortedActivities; |
| 121 | + } |
| 122 | + |
| 123 | + private void topologicalSortUtilSparseGraph( |
| 124 | + Integer activity, Set<Integer> visited, List<Integer> sortedActivities, Graph<Integer, Integer> graph) { |
| 125 | + visited.add(activity); |
| 126 | + |
| 127 | + for (Integer neighbor : Graphs.successorListOf(graph, activity)) { |
| 128 | + if (!visited.contains(neighbor)) { |
| 129 | + topologicalSortUtilSparseGraph(neighbor, visited, sortedActivities, graph); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + sortedActivities.add(activity); |
| 134 | + } |
| 135 | + |
| 136 | + private List<Integer> getSparseEdgesAboveDiagonal(SparseIntDirectedWeightedGraph sparseGraph, List<Integer> sortedActivities) { |
| 137 | + ConcurrentLinkedQueue<Integer> sparseEdgesAboveDiagonal = new ConcurrentLinkedQueue<>(); |
| 138 | + |
| 139 | + int size = sortedActivities.size(); |
| 140 | + IntStream.range(0, size).parallel().forEach(i -> { |
| 141 | + for (int j = i + 1; j < size; j++) { |
| 142 | + Integer edge = sparseGraph.getEdge( |
| 143 | + sortedActivities.get(i), |
| 144 | + sortedActivities.get(j) |
| 145 | + ); |
| 146 | + if (edge != null) { |
| 147 | + sparseEdgesAboveDiagonal.add(edge); |
| 148 | + } |
| 149 | + } |
| 150 | + }); |
| 151 | + |
| 152 | + return new ArrayList<>(sparseEdgesAboveDiagonal); |
| 153 | + } |
| 154 | + |
| 155 | + private List<Integer> topologicalParallelSortSparseGraph(List<Set<Integer>> sccs, Graph<Integer, Integer> graph) { |
| 156 | + ConcurrentLinkedQueue<Integer> sortedActivities = new ConcurrentLinkedQueue<>(); |
| 157 | + Set<Integer> visited = new ConcurrentSkipListSet<>(); |
| 158 | + |
| 159 | + sccs.parallelStream() |
| 160 | + .flatMap(Set::parallelStream) |
| 161 | + .filter(activity -> !visited.contains(activity)) |
| 162 | + .forEach(activity -> topologicalSortUtilSparseGraph(activity, visited, sortedActivities, graph)); |
| 163 | + |
| 164 | + ArrayList<Integer> sortedActivitiesList = new ArrayList<>(sortedActivities); |
| 165 | + Collections.reverse(sortedActivitiesList); |
| 166 | + return sortedActivitiesList; |
| 167 | + } |
| 168 | + |
| 169 | + private void topologicalSortUtilSparseGraph( |
| 170 | + Integer activity, Set<Integer> visited, ConcurrentLinkedQueue<Integer> sortedActivities, Graph<Integer, Integer> graph) { |
| 171 | + visited.add(activity); |
| 172 | + |
| 173 | + Graphs.successorListOf(graph, activity).parallelStream() |
| 174 | + .filter(neighbor -> !visited.contains(neighbor)) |
| 175 | + .forEach(neighbor -> topologicalSortUtilSparseGraph(neighbor, visited, sortedActivities, graph)); |
| 176 | + |
| 177 | + sortedActivities.add(activity); |
| 178 | + } |
| 179 | + |
| 180 | +} |
0 commit comments