Skip to content

Commit

Permalink
Removed detached nodes, which are caused by cliques self-referencing. (
Browse files Browse the repository at this point in the history
  • Loading branch information
marchermans authored Sep 9, 2024
1 parent 392cac0 commit b3cf254
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,22 +241,7 @@ private void handleRecipeInput(IRecipeIngredient input, Map<IRecipeIngredient, I

private IGraph reduceGraph(final IGraph recipeGraph, final SourceNode sourceNode, Map<ICompoundContainer<?>, IContainerNode> compoundNodes) {

LOGGER.warn("Starting component detection");

final IConnectionFinder<IGraph, INode, IEdge> connectivityInspector = new QueueBasedConnectionFinder<>();
final Set<INode> connectedNodes = connectivityInspector.reachableNodes(recipeGraph, sourceNode);

LOGGER.warn("Connected nodes: {} out of {}", connectedNodes.size(), recipeGraph.vertexSet().size());

final Set<INode> 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.");

Expand Down Expand Up @@ -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();
Expand All @@ -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<IGraph, INode, IEdge> connectivityInspector = new QueueBasedConnectionFinder<>();
final Set<INode> connectedNodes = connectivityInspector.reachableNodes(recipeGraph, sourceNode);

LOGGER.warn("Connected nodes: {} out of {}", connectedNodes.size(), recipeGraph.vertexSet().size());

final Set<INode> 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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,7 +17,6 @@

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class JGraphTCliqueReducer
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public void reduce(G graph, V startNode) {
final SzwarcfiterLauerSimpleCycles<V, E> cycleDetector = new SzwarcfiterLauerSimpleCycles<>(graph);
final List<List<V>> cycles = cycleDetector.findSimpleCycles();
for (List<V> 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");
}
Expand Down

0 comments on commit b3cf254

Please sign in to comment.