Skip to content

Commit

Permalink
#28 disabled unfinished IsomophismHandler with high heuristics usage
Browse files Browse the repository at this point in the history
it wasn't really working, I disabled it for now by making the class
abstract. later I'll probably implement it the way it was intended to,
potentially being my best IsomorphismHandler when it's done.
  • Loading branch information
hoechp committed Dec 21, 2016
1 parent 1f80d94 commit a7d8418
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 89 deletions.
9 changes: 5 additions & 4 deletions src/main/java/org/fujaba/graphengine/GraphEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import org.fujaba.graphengine.graph.adapter.GraphToSigmaJsAdapter;
import org.fujaba.graphengine.graph.adapter.NodeAdapter;
import org.fujaba.graphengine.isomorphismtools.IsomorphismHandler;
import org.fujaba.graphengine.isomorphismtools.IsomorphismHandlerCSPWithHeuristics;
import org.fujaba.graphengine.isomorphismtools.IsomorphismHandlerCSPHighHeuristics;
import org.fujaba.graphengine.isomorphismtools.IsomorphismHandlerCSPLowHeuristics;
import org.fujaba.graphengine.isomorphismtools.IsomorphismHandlerSorting;
import org.fujaba.graphengine.isomorphismtools.sort.NodeSortTree;
import org.fujaba.graphengine.isomorphismtools.sort.adapter.NodeSortTreeAdapter;
Expand Down Expand Up @@ -51,7 +52,7 @@ public static void setMainIsomorphismHandler(IsomorphismHandler isomorphismHandl
*/
public static IsomorphismHandler getMainIsomorphismHandler() {
if (mainIsomorphismHandler == null) {
mainIsomorphismHandler = new IsomorphismHandlerCSPWithHeuristics();
mainIsomorphismHandler = new IsomorphismHandlerCSPLowHeuristics();
}
return mainIsomorphismHandler;
}
Expand All @@ -61,7 +62,7 @@ public static IsomorphismHandler getMainIsomorphismHandler() {
*/
public static IsomorphismHandler getMappingFallback() {
if (mappingFallback == null) {
mappingFallback = new IsomorphismHandlerCSPWithHeuristics();
mappingFallback = new IsomorphismHandlerCSPLowHeuristics();
}
return mappingFallback;
}
Expand All @@ -81,7 +82,7 @@ public static IsomorphismHandler getNormalizationFallback() {
*/
public static IsomorphismHandler getSplitGraphFallback() {
if (splitGraphFallback == null) {
splitGraphFallback = new IsomorphismHandlerCSPWithHeuristics();
splitGraphFallback = new IsomorphismHandlerCSPLowHeuristics();
}
return splitGraphFallback;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.fujaba.graphengine.graph.Node;
import org.fujaba.graphengine.isomorphismtools.heuristics.NodeWithConflict;

public class IsomorphismHandlerCSPWithMoreHeuristics extends IsomorphismHandler {
public abstract class IsomorphismHandlerCSPHighHeuristics extends IsomorphismHandler {

private static ArrayList<Node> getDepthFirstSortedNodeList(Graph graph) {
// obtain all parts of the graph - where each part's nodes are connected with each other:
Expand Down Expand Up @@ -42,10 +42,10 @@ public HashMap<Node, Node> mappingFrom(Graph subGraphInitial, Graph baseGraph) {
}
// now I'm trying to find 'loosely matched candidates':
Graph subGraph = subGraphInitial.clone();
ArrayList<ArrayList<Node>> couldMatch2 = new ArrayList<ArrayList<Node>>();
ArrayList<ArrayList<Node>> couldMatch = new ArrayList<ArrayList<Node>>();
for (int i = 0; i < subGraph.getNodes().size(); ++i) {
Node subNode = subGraph.getNodes().get(i);
couldMatch2.add(new ArrayList<Node>());
couldMatch.add(new ArrayList<Node>());
nodeMatch: for (int j = 0; j < baseGraph.getNodes().size(); ++j) {
Node node = baseGraph.getNodes().get(j);
// check existence of outgoing edges and their count:
Expand All @@ -62,68 +62,24 @@ public HashMap<Node, Node> mappingFrom(Graph subGraphInitial, Graph baseGraph) {
continue nodeMatch;
}
}
couldMatch2.get(couldMatch2.size() - 1).add(node);
couldMatch.get(couldMatch.size() - 1).add(node);
}
if (couldMatch2.get(couldMatch2.size() - 1).size() == 0) {
if (couldMatch.get(couldMatch.size() - 1).size() == 0) {
return null; // no mapping for this node => fail
}
}
couldMatch2 = removeImpossibleCandidates(couldMatch2);
if (couldMatch2 == null) {
couldMatch = removeImpossibleCandidates(couldMatch);
if (couldMatch == null) {
// after removing 'impossible' candidates, there's no match anymore => fail
return null;
}
if (subGraph.getNodes().size() == 1) {
// a single node with a candidate is a match => success
HashMap<Node, Node> singleNodeMapping = new HashMap<Node, Node>();
singleNodeMapping.put(subGraph.getNodes().get(0), couldMatch2.get(0).get(0));
singleNodeMapping.put(subGraph.getNodes().get(0), couldMatch.get(0).get(0));
return singleNodeMapping;
}
/*
* here I'm starting the application of the heuristics of the maximum restricted variable (H1) and the minimum node order (H2):
*/
// first save the old order of the matches:
HashMap<Node, Integer> oldIndices = new HashMap<Node, Integer>();
for (int i = 0; i < subGraph.getNodes().size(); ++i) {
oldIndices.put(subGraph.getNodes().get(i), i);
}
// now check for the maximum restricted variables (H1):
ArrayList<Integer> minimumIndices = new ArrayList<Integer>();
int minimumValue = Integer.MAX_VALUE;
for (int i = 0; i < subGraph.getNodes().size(); ++i) { // minimum candidates
if (couldMatch2.get(i).size() <= minimumValue) {
if (couldMatch2.get(i).size() < minimumValue) {
minimumIndices = new ArrayList<Integer>();
minimumValue = couldMatch2.get(i).size();
}
minimumIndices.add(i);
}
}
// now check within those for the minimum node order (H2):
int indicesIndex = -1;
minimumValue = Integer.MAX_VALUE;
for (int i = 0; i < minimumIndices.size(); ++i) { // minimum node order (outgoing)
int outgoingCount = 0;
Node currentNode = subGraph.getNodes().get(minimumIndices.get(i));
for (String key: currentNode.getEdges().keySet()) {
outgoingCount += currentNode.getEdges(key).size();
}
if (outgoingCount < minimumValue) {
minimumValue = outgoingCount;
indicesIndex = i;
}
}
// here we have the 'best' node to start with:
Node heuristicallySelectedFirstNode = subGraph.getNodes().get(minimumIndices.get(indicesIndex));
subGraph.getNodes().remove(heuristicallySelectedFirstNode); // remove from old position
subGraph.getNodes().add(0, heuristicallySelectedFirstNode); // put in front
// now order the nodes in a depth-first fashion, with the heuristically selected first node as 'root':
ArrayList<Node> sortedNodes = getDepthFirstSortedNodeList(subGraph);
// restore the matches to the new order:
ArrayList<ArrayList<Node>> couldMatch = new ArrayList<ArrayList<Node>>();
for (int i = 0; i < sortedNodes.size(); ++i) {
couldMatch.add(couldMatch2.get(oldIndices.get(sortedNodes.get(i))));
}
// and build candidates into objects, that contain an additional conflict-value and are sortable
ArrayList<ArrayList<NodeWithConflict>> couldMatchWithConflict = new ArrayList<ArrayList<NodeWithConflict>>();
for (int i = 0; i < couldMatch.size(); ++i) {
Expand Down Expand Up @@ -164,13 +120,13 @@ public HashMap<Node, Node> mappingFrom(Graph subGraphInitial, Graph baseGraph) {
* here I'm starting the application of the heuristics of the maximum restricted variable (H1) and the minimum node order (H2):
*/
// first save the old order of the matches:
oldIndices = new HashMap<Node, Integer>();
HashMap<Node, Integer> oldIndices = new HashMap<Node, Integer>();
for (int i = 0; i < sortedNodes.size(); ++i) {
oldIndices.put(sortedNodes.get(i), i);
}
// now check for the maximum restricted variables (H1):
minimumIndices = new ArrayList<Integer>();
minimumValue = Integer.MAX_VALUE;
ArrayList<Integer> minimumIndices = new ArrayList<Integer>();
int minimumValue = Integer.MAX_VALUE;
for (int i = startThisHeuristicsAtIndex; i < sortedNodes.size(); ++i) { // minimum candidates
if (couldMatchWithConflict.get(i).size() <= minimumValue) {
if (couldMatchWithConflict.get(i).size() < minimumValue) {
Expand All @@ -181,7 +137,7 @@ public HashMap<Node, Node> mappingFrom(Graph subGraphInitial, Graph baseGraph) {
}
}
// now check within those for the minimum node order (H2):
indicesIndex = -1;
int indicesIndex = -1;
minimumValue = Integer.MAX_VALUE;
for (int i = 0; i < minimumIndices.size(); ++i) { // minimum node order (outgoing)
int outgoingCount = 0;
Expand All @@ -195,9 +151,9 @@ public HashMap<Node, Node> mappingFrom(Graph subGraphInitial, Graph baseGraph) {
}
}
// here we have the 'best' node to start with:
heuristicallySelectedFirstNode = subGraph.getNodes().get(minimumIndices.get(indicesIndex));
// sortedNodes.remove(heuristicallySelectedFirstNode); // remove from old position
// sortedNodes.add(startThisHeuristicsAtIndex, heuristicallySelectedFirstNode); // put in front
Node heuristicallySelectedFirstNode = subGraph.getNodes().get(minimumIndices.get(indicesIndex));
sortedNodes.remove(heuristicallySelectedFirstNode); // remove from old position
sortedNodes.add(startThisHeuristicsAtIndex, heuristicallySelectedFirstNode); // put in front
// restore the matches to the new order:
ArrayList<ArrayList<NodeWithConflict>> couldMatchWithConflict2 = new ArrayList<ArrayList<NodeWithConflict>>();
for (int i = 0; i < sortedNodes.size(); ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.fujaba.graphengine.graph.Graph;
import org.fujaba.graphengine.graph.Node;

public class IsomorphismHandlerCSPWithHeuristics extends IsomorphismHandler {
public class IsomorphismHandlerCSPLowHeuristics extends IsomorphismHandler {

private static ArrayList<Node> getDepthFirstSortedNodeList(Graph graph) {
// obtain all parts of the graph - where each part's nodes are connected with each other:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.fujaba.graphengine.graph.Graph;
import org.fujaba.graphengine.graph.Node;

public class IsomorphismHandlerCSP extends IsomorphismHandler {
public class IsomorphismHandlerDepthFirstBacktracking extends IsomorphismHandler {

private static ArrayList<Node> getDepthFirstSortedNodeList(Graph graph) {
// obtain all parts of the graph - where each part's nodes are connected with each other:
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/fujaba/graphengine/unitTests/GraphTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.fujaba.graphengine.graph.Graph;
import org.fujaba.graphengine.graph.Node;
import org.fujaba.graphengine.isomorphismtools.IsomorphismHandler;
import org.fujaba.graphengine.isomorphismtools.IsomorphismHandlerCSPWithHeuristics;
import org.fujaba.graphengine.isomorphismtools.IsomorphismHandlerCSPLowHeuristics;
import org.fujaba.graphengine.isomorphismtools.IsomorphismHandlerCombinatorial;
import org.fujaba.graphengine.isomorphismtools.IsomorphismHandlerSorting;
import org.junit.Assert;
Expand Down Expand Up @@ -367,7 +367,7 @@ public void testIsomorphismTestGraph() {

IsomorphismHandler isomorphismHandler = null;
// isomorphismHandler = GraphEngine.getMainIsomorphismHandler();
// isomorphismHandler = new IsomorphismHandlerCSPWithHeuristics();
// isomorphismHandler = new IsomorphismHandlerCSPLowHeuristics();
// isomorphismHandler = new IsomorphismHandlerSorting();
isomorphismHandler = new IsomorphismHandlerCombinatorial();
long start = System.nanoTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.fujaba.graphengine.PatternEngine;
import org.fujaba.graphengine.graph.Graph;
import org.fujaba.graphengine.graph.Node;
import org.fujaba.graphengine.isomorphismtools.IsomorphismHandlerCSPWithHeuristics;
import org.fujaba.graphengine.isomorphismtools.IsomorphismHandlerCSPLowHeuristics;
import org.fujaba.graphengine.isomorphismtools.IsomorphismHandlerSorting;
import org.fujaba.graphengine.pattern.PatternAttribute;
import org.fujaba.graphengine.pattern.PatternEdge;
Expand Down Expand Up @@ -267,9 +267,9 @@ public void testNegativePatternVariantsWithDifferentIsomorphismCheckApproaches()
System.out.println("GraphEngine: " + duration + "ms");

begin = System.nanoTime();
Assert.assertTrue(new IsomorphismHandlerCSPWithHeuristics().isIsomorphTo(carGraph, carGraphChangedNodeOrder));
Assert.assertTrue(new IsomorphismHandlerCSPLowHeuristics().isIsomorphTo(carGraph, carGraphChangedNodeOrder));
duration = (System.nanoTime() - begin) / 1e6;
System.out.println("IsomorphismHandlerCSPWithHeuristics: " + duration + "ms");
System.out.println("IsomorphismHandlerCSPLowHeuristics: " + duration + "ms");

begin = System.nanoTime();
Assert.assertTrue(new IsomorphismHandlerSorting().isIsomorphTo(carGraph, carGraphChangedNodeOrder));
Expand Down
36 changes: 17 additions & 19 deletions src/test/java/org/fujaba/graphengine/unitTests/RoadworkExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import org.fujaba.graphengine.graph.Graph;
import org.fujaba.graphengine.graph.Node;
import org.fujaba.graphengine.isomorphismtools.IsomorphismHandler;
import org.fujaba.graphengine.isomorphismtools.IsomorphismHandlerCSP;
import org.fujaba.graphengine.isomorphismtools.IsomorphismHandlerCSPWithHeuristics;
import org.fujaba.graphengine.isomorphismtools.IsomorphismHandlerCSPWithMoreHeuristics;
import org.fujaba.graphengine.isomorphismtools.IsomorphismHandlerDepthFirstBacktracking;
import org.fujaba.graphengine.isomorphismtools.IsomorphismHandlerCSPLowHeuristics;
import org.fujaba.graphengine.isomorphismtools.IsomorphismHandlerCSPHighHeuristics;
import org.fujaba.graphengine.isomorphismtools.IsomorphismHandlerCombinatorial;
import org.fujaba.graphengine.pattern.PatternAttribute;
import org.fujaba.graphengine.pattern.PatternEdge;
Expand Down Expand Up @@ -312,8 +312,8 @@ public void testRoadworkExample() {
Graph reachabilityGraph;


GraphEngine.setMainIsomorphismHandler(new IsomorphismHandlerCSPWithMoreHeuristics());
System.out.println("\nnew IsomorphismHandlerCSPWithMoreHeuristics()");
GraphEngine.setMainIsomorphismHandler(new IsomorphismHandlerDepthFirstBacktracking());
System.out.println("\nnew IsomorphismHandlerDepthFirstBacktracking()");



Expand All @@ -325,7 +325,6 @@ public void testRoadworkExample() {
System.out.println("== " + ((System.nanoTime() - begin) / 1e9 / 60) + " m");
System.out.println("== " + ((System.nanoTime() - begin) / 1e9 / 60 / 60) + " h");
System.out.println(reachabilityGraph.getNodes().size() + " node" + (reachabilityGraph.getNodes().size() != 1 ? "s" : "") + " in the 'reachabilityGraph'");


System.out.println("\n(minimal + 1) starting to build reachability graph for RoadworkExample...");
begin = System.nanoTime();
Expand All @@ -335,7 +334,6 @@ public void testRoadworkExample() {
System.out.println("== " + ((System.nanoTime() - begin) / 1e9 / 60) + " m");
System.out.println("== " + ((System.nanoTime() - begin) / 1e9 / 60 / 60) + " h");
System.out.println(reachabilityGraph.getNodes().size() + " node" + (reachabilityGraph.getNodes().size() != 1 ? "s" : "") + " in the 'reachabilityGraph'");


// System.out.println("\n(original) starting to build reachability graph for RoadworkExample...");
// begin = System.nanoTime();
Expand All @@ -345,11 +343,13 @@ public void testRoadworkExample() {
// System.out.println("== " + ((System.nanoTime() - begin) / 1e9 / 60) + " m");
// System.out.println("== " + ((System.nanoTime() - begin) / 1e9 / 60 / 60) + " h");
// System.out.println(reachabilityGraph.getNodes().size() + " node" + (reachabilityGraph.getNodes().size() != 1 ? "s" : "") + " in the 'reachabilityGraph'");

// new GraphDumper(reachabilityGraph).dumpGraph("roadwork.html");





GraphEngine.setMainIsomorphismHandler(new IsomorphismHandlerCSP());
System.out.println("\nnew IsomorphismHandlerCSP()");
GraphEngine.setMainIsomorphismHandler(new IsomorphismHandlerCSPLowHeuristics());
System.out.println("\nnew IsomorphismHandlerCSPLowHeuristics()");



Expand All @@ -361,7 +361,6 @@ public void testRoadworkExample() {
System.out.println("== " + ((System.nanoTime() - begin) / 1e9 / 60) + " m");
System.out.println("== " + ((System.nanoTime() - begin) / 1e9 / 60 / 60) + " h");
System.out.println(reachabilityGraph.getNodes().size() + " node" + (reachabilityGraph.getNodes().size() != 1 ? "s" : "") + " in the 'reachabilityGraph'");


System.out.println("\n(minimal + 1) starting to build reachability graph for RoadworkExample...");
begin = System.nanoTime();
Expand All @@ -371,7 +370,6 @@ public void testRoadworkExample() {
System.out.println("== " + ((System.nanoTime() - begin) / 1e9 / 60) + " m");
System.out.println("== " + ((System.nanoTime() - begin) / 1e9 / 60 / 60) + " h");
System.out.println(reachabilityGraph.getNodes().size() + " node" + (reachabilityGraph.getNodes().size() != 1 ? "s" : "") + " in the 'reachabilityGraph'");


// System.out.println("\n(original) starting to build reachability graph for RoadworkExample...");
// begin = System.nanoTime();
Expand All @@ -382,11 +380,12 @@ public void testRoadworkExample() {
// System.out.println("== " + ((System.nanoTime() - begin) / 1e9 / 60 / 60) + " h");
// System.out.println(reachabilityGraph.getNodes().size() + " node" + (reachabilityGraph.getNodes().size() != 1 ? "s" : "") + " in the 'reachabilityGraph'");


// new GraphDumper(reachabilityGraph).dumpGraph("roadwork.html");



GraphEngine.setMainIsomorphismHandler(new IsomorphismHandlerCSPWithHeuristics());
System.out.println("\nnew IsomorphismHandlerCSPWithHeuristics()");
GraphEngine.setMainIsomorphismHandler(new IsomorphismHandlerCombinatorial());
System.out.println("\nnew IsomorphismHandlerCombinatorial()");



Expand All @@ -398,7 +397,6 @@ public void testRoadworkExample() {
System.out.println("== " + ((System.nanoTime() - begin) / 1e9 / 60) + " m");
System.out.println("== " + ((System.nanoTime() - begin) / 1e9 / 60 / 60) + " h");
System.out.println(reachabilityGraph.getNodes().size() + " node" + (reachabilityGraph.getNodes().size() != 1 ? "s" : "") + " in the 'reachabilityGraph'");


System.out.println("\n(minimal + 1) starting to build reachability graph for RoadworkExample...");
begin = System.nanoTime();
Expand All @@ -408,7 +406,6 @@ public void testRoadworkExample() {
System.out.println("== " + ((System.nanoTime() - begin) / 1e9 / 60) + " m");
System.out.println("== " + ((System.nanoTime() - begin) / 1e9 / 60 / 60) + " h");
System.out.println(reachabilityGraph.getNodes().size() + " node" + (reachabilityGraph.getNodes().size() != 1 ? "s" : "") + " in the 'reachabilityGraph'");


// System.out.println("\n(original) starting to build reachability graph for RoadworkExample...");
// begin = System.nanoTime();
Expand All @@ -419,7 +416,8 @@ public void testRoadworkExample() {
// System.out.println("== " + ((System.nanoTime() - begin) / 1e9 / 60 / 60) + " h");
// System.out.println(reachabilityGraph.getNodes().size() + " node" + (reachabilityGraph.getNodes().size() != 1 ? "s" : "") + " in the 'reachabilityGraph'");

// new GraphDumper(reachabilityGraph).dumpGraph("test.html");
// new GraphDumper(reachabilityGraph).dumpGraph("roadwork.html");


GraphEngine.prepareGraphAsJsonFileForSigmaJs(
GraphEngine.getGson().fromJson(
Expand Down

0 comments on commit a7d8418

Please sign in to comment.