From b3cf2542021d77e89ca2b2bfa68f0434c9a322fc Mon Sep 17 00:00:00 2001 From: Marc Hermans Date: Mon, 9 Sep 2024 20:46:44 +0200 Subject: [PATCH] Removed detached nodes, which are caused by cliques self-referencing. (#30) --- .../JGraphTBasedCompoundAnalyzer.java | 38 +++++++++++-------- .../jgrapht/clique/JGraphTCliqueReducer.java | 9 ++--- .../cycles/direct/DFSDirectCycleReducer.java | 5 ++- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/ldtteam/aequivaleo/analysis/JGraphTBasedCompoundAnalyzer.java b/src/main/java/com/ldtteam/aequivaleo/analysis/JGraphTBasedCompoundAnalyzer.java index 3106748..32e66f7 100755 --- a/src/main/java/com/ldtteam/aequivaleo/analysis/JGraphTBasedCompoundAnalyzer.java +++ b/src/main/java/com/ldtteam/aequivaleo/analysis/JGraphTBasedCompoundAnalyzer.java @@ -241,22 +241,7 @@ private void handleRecipeInput(IRecipeIngredient input, Map, IContainerNode> compoundNodes) { - LOGGER.warn("Starting component detection"); - - final IConnectionFinder connectivityInspector = new QueueBasedConnectionFinder<>(); - final Set connectedNodes = connectivityInspector.reachableNodes(recipeGraph, sourceNode); - - LOGGER.warn("Connected nodes: {} out of {}", connectedNodes.size(), recipeGraph.vertexSet().size()); - - final Set nodesToRemove = new HashSet<>(Sets.difference(recipeGraph.vertexSet(), connectedNodes)); - nodesToRemove.forEach(v -> { - recipeGraph.removeVertex(v); - LOGGER.debug("Removed node: {}", v); - }); - - LOGGER.info("Removed {} nodes from the graph", nodesToRemove.size()); - - LOGGER.warn("Finished component detection"); + removeDetachedComponents(recipeGraph, sourceNode); LOGGER.warn("Starting clique reduction."); @@ -288,6 +273,8 @@ private IGraph reduceGraph(final IGraph recipeGraph, final SourceNode sourceNode LOGGER.warn("Stripped input values from known nodes."); + removeDetachedComponents(recipeGraph, sourceNode); + LOGGER.warn("Starting cycle reduction."); final IJGraphTBasedCompoundCycleTracer tracer = createTracer(); @@ -306,6 +293,25 @@ private IGraph reduceGraph(final IGraph recipeGraph, final SourceNode sourceNode return recipeGraph; } + private static void removeDetachedComponents(IGraph recipeGraph, SourceNode sourceNode) { + LOGGER.warn("Starting component detection"); + + final IConnectionFinder connectivityInspector = new QueueBasedConnectionFinder<>(); + final Set connectedNodes = connectivityInspector.reachableNodes(recipeGraph, sourceNode); + + LOGGER.warn("Connected nodes: {} out of {}", connectedNodes.size(), recipeGraph.vertexSet().size()); + + final Set nodesToRemove = new HashSet<>(Sets.difference(recipeGraph.vertexSet(), connectedNodes)); + nodesToRemove.forEach(v -> { + recipeGraph.removeVertex(v); + LOGGER.debug("Removed node: {}", v); + }); + + LOGGER.info("Removed {} nodes from the graph", nodesToRemove.size()); + + LOGGER.warn("Finished component detection"); + } + public void calculate() { if (this.primaryOwner == null) { throw new IllegalArgumentException("First passed world is null"); diff --git a/src/main/java/com/ldtteam/aequivaleo/analysis/jgrapht/clique/JGraphTCliqueReducer.java b/src/main/java/com/ldtteam/aequivaleo/analysis/jgrapht/clique/JGraphTCliqueReducer.java index ca3016b..5c3099b 100755 --- a/src/main/java/com/ldtteam/aequivaleo/analysis/jgrapht/clique/JGraphTCliqueReducer.java +++ b/src/main/java/com/ldtteam/aequivaleo/analysis/jgrapht/clique/JGraphTCliqueReducer.java @@ -1,12 +1,12 @@ package com.ldtteam.aequivaleo.analysis.jgrapht.clique; -import com.google.common.collect.HashBasedTable; -import com.google.common.collect.Table; -import com.ldtteam.aequivaleo.analysis.jgrapht.aequivaleo.*; +import com.ldtteam.aequivaleo.analysis.jgrapht.aequivaleo.IEdge; +import com.ldtteam.aequivaleo.analysis.jgrapht.aequivaleo.IFreeNode; +import com.ldtteam.aequivaleo.analysis.jgrapht.aequivaleo.IGraph; +import com.ldtteam.aequivaleo.analysis.jgrapht.aequivaleo.INode; import com.ldtteam.aequivaleo.analysis.jgrapht.clique.graph.CliqueDetectionEdge; import com.ldtteam.aequivaleo.analysis.jgrapht.clique.graph.CliqueDetectionGraph; import com.ldtteam.aequivaleo.analysis.jgrapht.clique.reducer.ReductionGraphBuilder; -import com.ldtteam.aequivaleo.utils.AnalysisLogHandler; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.Contract; @@ -17,7 +17,6 @@ import java.util.*; import java.util.function.Function; -import java.util.stream.Collectors; public class JGraphTCliqueReducer { diff --git a/src/main/java/com/ldtteam/aequivaleo/analysis/jgrapht/cycles/direct/DFSDirectCycleReducer.java b/src/main/java/com/ldtteam/aequivaleo/analysis/jgrapht/cycles/direct/DFSDirectCycleReducer.java index 1953050..05afa06 100644 --- a/src/main/java/com/ldtteam/aequivaleo/analysis/jgrapht/cycles/direct/DFSDirectCycleReducer.java +++ b/src/main/java/com/ldtteam/aequivaleo/analysis/jgrapht/cycles/direct/DFSDirectCycleReducer.java @@ -36,7 +36,10 @@ public void reduce(G graph, V startNode) { final SzwarcfiterLauerSimpleCycles cycleDetector = new SzwarcfiterLauerSimpleCycles<>(graph); final List> cycles = cycleDetector.findSimpleCycles(); for (List cycle : cycles) { - AequivaleoLogger.warning("Found cycle: " + cycle); + AequivaleoLogger.warning(" - Found cycle: " + cycle.size()); + for (V vertex : cycle) { + AequivaleoLogger.warning(" - " + vertex); + } } AequivaleoLogger.endBigWarning("Cycle Reduction Inspection"); }