List of usage examples for com.google.common.collect TreeMultiset toString
public String toString()
From source file:org.apromore.similaritysearch.common.algos.GraphEditDistanceGreedy.java
public Set<TwoVertices> compute(Graph sg1, Graph sg2) { init(sg1, sg2);//from w w w. ja va 2 s . c o m //INIT BestMapping mapping = new BestMapping(); Set<TwoVertices> openCouples = times(sg1.getVertices(), sg2.getVertices(), ledcutoff); double shortestEditDistance = Double.MAX_VALUE; Random randomized = new Random(123456789); int stepn = 0; //STEP boolean doStep = true; while (doStep) { doStep = false; stepn++; Vector<TwoVertices> bestCandidates = new Vector<TwoVertices>(); double newShortestEditDistance = shortestEditDistance; for (TwoVertices couple : openCouples) { double newEditDistance = this.editDistance(mapping, couple); if (newEditDistance < newShortestEditDistance) { bestCandidates = new Vector<TwoVertices>(); bestCandidates.add(couple); newShortestEditDistance = newEditDistance; } else if (newEditDistance == newShortestEditDistance) { bestCandidates.add(couple); } } if (bestCandidates.size() > 0) { TwoVertices couple; // Case 1: Only one candidate pair if (bestCandidates.size() == 1) { couple = bestCandidates.firstElement(); } else { // CASE 2: Lexicographical order is enough TreeMultimap<String, TwoVertices> tmap = TreeMultimap.create(); for (TwoVertices pair : bestCandidates) { String label1 = sg1.getVertexLabel(pair.v1); String label2 = sg2.getVertexLabel(pair.v2); if (label1 != null && label2 != null && label1.compareTo(label2) > 0) { String tmp = label1; label1 = label2; label2 = tmp; } tmap.put(label1 + label2, pair); } String firstkey = tmap.keySet().first(); if (tmap.get(firstkey).size() == 1) { couple = tmap.get(firstkey).first(); } else if (tmap.get(firstkey).size() > 1) { Set<TwoVertices> set = tmap.get(firstkey); TreeMultimap<String, TwoVertices> tmapp = TreeMultimap.create(); String label1; String tmpLabel; TreeMultiset<String> mset = TreeMultiset.create(); for (TwoVertices pair : set) { label1 = sg1.getVertexLabel(pair.v1); mset.clear(); for (Vertex n : sg1.getPreset(pair.v1)) { tmpLabel = sg1.getVertexLabel(n.getID()); if (tmpLabel != null) { mset.add(tmpLabel); } } label1 += mset.toString(); mset.clear(); for (Vertex n : sg1.getPostset(pair.v1)) { tmpLabel = sg1.getVertexLabel(n.getID()); if (tmpLabel != null) { mset.add(tmpLabel); } } label1 += mset.toString(); String label2 = sg2.getVertexLabel(pair.v2); mset.clear(); for (Vertex n : sg2.getPreset(pair.v2)) { tmpLabel = sg2.getVertexLabel(n.getID()); if (tmpLabel != null) { mset.add(tmpLabel); } } label2 += mset.toString(); mset.clear(); for (Vertex n : sg2.getPostset(pair.v2)) { tmpLabel = sg2.getVertexLabel(n.getID()); if (tmpLabel != null) { mset.add(tmpLabel); } } label2 += mset.toString(); if (label1.compareTo(label2) > 0) { String tmp = label1; label1 = label2; label2 = tmp; } tmapp.put(label1 + label2, pair); } String contextkey = tmapp.keySet().first(); // CASE 3: Composite labels (concatenation of labels of nodes surrounding the target vertex) if (tmapp.get(contextkey).size() == 1) { couple = tmapp.get(contextkey).first(); } else { // CASE 4: Non deterministic choice (Choose a random candidate) deterministic = false; couple = bestCandidates.get(randomized.nextInt(bestCandidates.size())); } } else { // CASE 5: Non deterministic choice (Choose a random candidate) // System.out.println("oops ..."); deterministic = false; couple = bestCandidates.get(randomized.nextInt(bestCandidates.size())); } } Set<TwoVertices> newOpenCouples = new HashSet<TwoVertices>(); for (TwoVertices p : openCouples) { if (!p.v1.equals(couple.v1) && !p.v2.equals(couple.v2)) { newOpenCouples.add(p); } } openCouples = newOpenCouples; mapping.addPair(couple); shortestEditDistance = newShortestEditDistance; doStep = true; } } //Return the smallest edit distance return mapping.mapping; }
From source file:org.apromore.similaritysearch.common.algos.GraphEditDistanceGreedy.java
public double computeGED(Graph sg1, Graph sg2) { BestMapping mapping = new BestMapping(); double shortestEditDistance = Double.MAX_VALUE; Random randomized = new Random(123456789); try {/*from w w w.j a v a 2s . c o m*/ // INIT init(sg1, sg2); Set<TwoVertices> openCouples = times(sg1.getVertices(), sg2.getVertices(), ledcutoff); // STEP boolean doStep = true; while (doStep) { doStep = false; Vector<TwoVertices> bestCandidates = new Vector<TwoVertices>(); double newShortestEditDistance = shortestEditDistance; for (TwoVertices couple : openCouples) { double newEditDistance = this.editDistance(mapping, couple); if (newEditDistance < newShortestEditDistance) { bestCandidates = new Vector<TwoVertices>(); bestCandidates.add(couple); newShortestEditDistance = newEditDistance; } else if (newEditDistance == newShortestEditDistance) { bestCandidates.add(couple); } } if (bestCandidates.size() > 0) { TwoVertices couple; // Case 1: Only one candidate pair if (bestCandidates.size() == 1) { couple = bestCandidates.firstElement(); } else { // CASE 2: Lexicographical order is enough TreeMultimap<String, TwoVertices> tmap = TreeMultimap.create(); for (TwoVertices pair : bestCandidates) { String label1 = sg1.getVertexLabel(pair.v1); String label2 = sg2.getVertexLabel(pair.v2); if (label1 != null && label2 != null && label1.compareTo(label2) > 0) { String tmp = label1; label1 = label2; label2 = tmp; } tmap.put(label1 + label2, pair); } String firstkey = tmap.keySet().first(); if (tmap.get(firstkey).size() == 1) { couple = tmap.get(firstkey).first(); } else if (tmap.get(firstkey).size() > 1) { Set<TwoVertices> set = tmap.get(firstkey); TreeMultimap<String, TwoVertices> tmapp = TreeMultimap.create(); String label1; String tmpLabel; TreeMultiset<String> mset = TreeMultiset.create(); for (TwoVertices pair : set) { label1 = sg1.getVertexLabel(pair.v1); mset.clear(); for (Vertex n : sg1.getPreset(pair.v1)) { tmpLabel = sg1.getVertexLabel(n.getID()); if (tmpLabel != null) { mset.add(tmpLabel); } } label1 += mset.toString(); mset.clear(); for (Vertex n : sg1.getPostset(pair.v1)) { tmpLabel = sg1.getVertexLabel(n.getID()); if (tmpLabel != null) { mset.add(tmpLabel); } } label1 += mset.toString(); String label2 = sg2.getVertexLabel(pair.v2); mset.clear(); for (Vertex n : sg2.getPreset(pair.v2)) { tmpLabel = sg2.getVertexLabel(n.getID()); if (tmpLabel != null) { mset.add(tmpLabel); } } label2 += mset.toString(); mset.clear(); for (Vertex n : sg2.getPostset(pair.v2)) { tmpLabel = sg2.getVertexLabel(n.getID()); if (tmpLabel != null) { mset.add(tmpLabel); } } label2 += mset.toString(); if (label1.compareTo(label2) > 0) { String tmp = label1; label1 = label2; label2 = tmp; } tmapp.put(label1 + label2, pair); } String contextkey = tmapp.keySet().first(); // CASE 3: Composite labels (concatenation of labels of nodes surrounding the target vertex) if (tmapp.get(contextkey).size() == 1) { couple = tmapp.get(contextkey).first(); } else { // CASE 4: Non deterministic choice (Choose a random candidate) deterministic = false; couple = bestCandidates.get(randomized.nextInt(bestCandidates.size())); } } else { // CASE 5: Non deterministic choice (Choose a random candidate) // System.out.println("oops ..."); deterministic = false; couple = bestCandidates.get(randomized.nextInt(bestCandidates.size())); } } Set<TwoVertices> newOpenCouples = new HashSet<TwoVertices>(); for (TwoVertices p : openCouples) { if (!p.v1.equals(couple.v1) && !p.v2.equals(couple.v2)) { newOpenCouples.add(p); } } openCouples = newOpenCouples; mapping.addPair(couple); shortestEditDistance = newShortestEditDistance; doStep = true; } } nrSubstitudedVertices = mapping.size(); } catch (Exception e) { LOGGER.error("Error occured while processing Distance Greedy Similarity Search ", e); } // Return the smallest edit distance return shortestEditDistance; }
From source file:common.algos.GraphEditDistanceGreedy.java
public double computeGED(Graph sg1, Graph sg2) { BestMapping mapping = new BestMapping(); double shortestEditDistance = Double.MAX_VALUE; Random randomized = new Random(123456789); try {/* w ww .ja v a 2 s. co m*/ // INIT init(sg1, sg2); Set<TwoVertices> openCouples = times(sg1.getVertices(), sg2.getVertices(), ledcutoff); // STEP boolean doStep = true; while (doStep) { doStep = false; Vector<TwoVertices> bestCandidates = new Vector<TwoVertices>(); double newShortestEditDistance = shortestEditDistance; for (TwoVertices couple : openCouples) { double newEditDistance = this.editDistance(mapping, couple); if (newEditDistance < newShortestEditDistance) { bestCandidates = new Vector<TwoVertices>(); bestCandidates.add(couple); newShortestEditDistance = newEditDistance; } else if (newEditDistance == newShortestEditDistance) { bestCandidates.add(couple); } } if (bestCandidates.size() > 0) { TwoVertices couple; // Case 1: Only one candidate pair if (bestCandidates.size() == 1) { couple = bestCandidates.firstElement(); } else { // CASE 2: Lexicographical order is enough TreeMultimap<String, TwoVertices> tmap = TreeMultimap.create(); for (TwoVertices pair : bestCandidates) { String label1 = sg1.getVertexLabel(pair.v1); String label2 = sg2.getVertexLabel(pair.v2); if (label1 != null && label2 != null && label1.compareTo(label2) > 0) { String tmp = label1; label1 = label2; label2 = tmp; } tmap.put(label1 + label2, pair); } String firstkey = tmap.keySet().first(); if (tmap.get(firstkey).size() == 1) { couple = tmap.get(firstkey).first(); } else if (tmap.get(firstkey).size() > 1) { Set<TwoVertices> set = tmap.get(firstkey); TreeMultimap<String, TwoVertices> tmapp = TreeMultimap.create(); String label1; String tmpLabel; TreeMultiset<String> mset = TreeMultiset.create(); for (TwoVertices pair : set) { label1 = sg1.getVertexLabel(pair.v1); mset.clear(); for (Vertex n : sg1.getPreset(pair.v1)) { tmpLabel = sg1.getVertexLabel(n.getID()); if (tmpLabel != null) { mset.add(tmpLabel); } } label1 += mset.toString(); mset.clear(); for (Vertex n : sg1.getPostset(pair.v1)) { tmpLabel = sg1.getVertexLabel(n.getID()); if (tmpLabel != null) { mset.add(tmpLabel); } } label1 += mset.toString(); String label2 = sg2.getVertexLabel(pair.v2); mset.clear(); for (Vertex n : sg2.getPreset(pair.v2)) { tmpLabel = sg2.getVertexLabel(n.getID()); if (tmpLabel != null) { mset.add(tmpLabel); } } label2 += mset.toString(); mset.clear(); for (Vertex n : sg2.getPostset(pair.v2)) { tmpLabel = sg2.getVertexLabel(n.getID()); if (tmpLabel != null) { mset.add(tmpLabel); } } label2 += mset.toString(); if (label1.compareTo(label2) > 0) { String tmp = label1; label1 = label2; label2 = tmp; } tmapp.put(label1 + label2, pair); } String contextkey = tmapp.keySet().first(); // CASE 3: Composite labels (concatenation of labels of nodes surrounding the target vertex) if (tmapp.get(contextkey).size() == 1) { couple = tmapp.get(contextkey).first(); } else { // CASE 4: Non deterministic choice (Choose a random candidate) deterministic = false; couple = bestCandidates.get(randomized.nextInt(bestCandidates.size())); } } else { // CASE 5: Non deterministic choice (Choose a random candidate) // System.out.println("oops ..."); deterministic = false; couple = bestCandidates.get(randomized.nextInt(bestCandidates.size())); } } Set<TwoVertices> newOpenCouples = new HashSet<TwoVertices>(); for (TwoVertices p : openCouples) { if (!p.v1.equals(couple.v1) && !p.v2.equals(couple.v2)) { newOpenCouples.add(p); } } openCouples = newOpenCouples; mapping.addPair(couple); shortestEditDistance = newShortestEditDistance; doStep = true; } } nrSubstitudedVertices = mapping.size(); } catch (Exception e) { } // Return the smallest edit distance return shortestEditDistance; }
From source file:org.apromore.toolbox.clustering.dissimilarity.algorithm.SimpleGEDDeterministicGreedy.java
public double compute(SimpleGraph sg1, SimpleGraph sg2) { init(sg1, sg2);/*from www . java 2 s . c o m*/ TwoVertices couple; Vector<TwoVertices> bestCandidates; Set<TwoVertices> newMapping; Set<TwoVertices> newOpenCouples; Set<TwoVertices> mapping = new HashSet<>(); Set<TwoVertices> openCouples = findCouples(sg1.getVertices(), sg2.getVertices()); String tmp, label1, label2, contextkey, firstkey; double newEditDistance; double newShortestEditDistance; double shortestEditDistance = Double.MAX_VALUE; Random randomized = new Random(123456789); TreeMultiset<String> mset; TreeMultimap<String, TwoVertices> tmap; TreeMultimap<String, TwoVertices> tmapp; boolean doStep = true; while (doStep) { doStep = false; bestCandidates = new Vector<>(); newShortestEditDistance = shortestEditDistance; for (TwoVertices oCouple : openCouples) { newMapping = new HashSet<>(mapping); newMapping.add(oCouple); newEditDistance = this.editDistance(newMapping); LOGGER.debug("Couple Distance: " + newEditDistance + " - " + oCouple.v1 + " * " + oCouple.v2); if (newEditDistance < newShortestEditDistance) { bestCandidates = new Vector<>(); bestCandidates.add(oCouple); newShortestEditDistance = newEditDistance; } else if (newEditDistance == newShortestEditDistance) { bestCandidates.add(oCouple); } } if (bestCandidates.size() > 0) { if (bestCandidates.size() == 1) couple = bestCandidates.firstElement(); else { tmap = TreeMultimap.create(); for (TwoVertices pair : bestCandidates) { label1 = sg1.getLabel(pair.v1); label2 = sg2.getLabel(pair.v2); if (label1.compareTo(label2) > 0) { tmp = label1; label1 = label2; label2 = tmp; } tmap.put(label1 + label2, pair); } firstkey = tmap.keySet().first(); LOGGER.debug("firstkey: " + firstkey); if (tmap.get(firstkey).size() == 1) { couple = tmap.get(firstkey).first(); } else if (tmap.get(firstkey).size() > 1) { tmapp = TreeMultimap.create(); mset = TreeMultiset.create(); for (TwoVertices pair : tmap.get(firstkey)) { label1 = sg1.getLabel(pair.v1); mset.clear(); for (Integer n : sg1.preSet(pair.v1)) { mset.add(sg1.getLabel(n)); } label1 += mset.toString(); mset.clear(); for (Integer n : sg1.postSet(pair.v1)) { mset.add(sg1.getLabel(n)); } label1 += mset.toString(); label2 = sg2.getLabel(pair.v2); mset.clear(); for (Integer n : sg2.preSet(pair.v2)) { mset.add(sg2.getLabel(n)); } label2 += mset.toString(); mset.clear(); for (Integer n : sg2.postSet(pair.v2)) { mset.add(sg2.getLabel(n)); } label2 += mset.toString(); if (label1.compareTo(label2) > 0) { tmp = label1; label1 = label2; label2 = tmp; } tmapp.put(label1 + label2, pair); } contextkey = tmapp.keySet().first(); if (tmapp.get(contextkey).size() == 1) { couple = tmapp.get(contextkey).first(); } else { deterministic = false; couple = bestCandidates.get(randomized.nextInt(bestCandidates.size())); } } else { // System.out.println("oops ..."); deterministic = false; couple = bestCandidates.get(randomized.nextInt(bestCandidates.size())); } } newOpenCouples = new HashSet<>(); for (TwoVertices p : openCouples) { if (!p.v1.equals(couple.v1) && !p.v2.equals(couple.v2)) { newOpenCouples.add(p); } } openCouples = newOpenCouples; LOGGER.debug("openCouples: " + openCouples.size()); mapping.add(couple); shortestEditDistance = newShortestEditDistance; doStep = true; } } LOGGER.debug("Mappings: " + mapping.size()); return shortestEditDistance; }
From source file:org.apromore.toolbox.clustering.dissimilarity.algorithm.CanonicalGEDDeterministicGreedy.java
/** * @see CanonicalDistanceAlgorithm#compute(org.apromore.graph.canonical.Canonical, org.apromore.graph.canonical.Canonical) * {@inheritDoc}//from w w w . j a v a 2s. com */ @Override public double compute(Canonical sg1, Canonical sg2) { init(sg1, sg2); GEDEdge couple; Vector<GEDEdge> bestCandidates; Set<GEDEdge> newMapping; Set<GEDEdge> newOpenCouples; Set<GEDEdge> mapping = new HashSet<>(); Set<GEDEdge> openCouples = findCouples(sg1.getNodes(), sg2.getNodes()); String tmp, label1, label2, contextkey, firstkey; double newEditDistance; double newShortestEditDistance; double shortestEditDistance = Double.MAX_VALUE; Random randomized = new Random(123456789); TreeMultiset<String> mset; TreeMultimap<String, GEDEdge> tmap; TreeMultimap<String, GEDEdge> tmapp; boolean doStep = true; while (doStep) { doStep = false; bestCandidates = new Vector<>(); newShortestEditDistance = shortestEditDistance; for (GEDEdge oCouple : openCouples) { newMapping = new HashSet<>(mapping); newMapping.add(oCouple); newEditDistance = this.editDistance(newMapping); LOGGER.debug("Couple Distance: " + newEditDistance + " - " + oCouple.getSource().getId() + " * " + oCouple.getTarget().getId()); if (newEditDistance < newShortestEditDistance) { bestCandidates = new Vector<>(); bestCandidates.add(oCouple); newShortestEditDistance = newEditDistance; } else if (newEditDistance == newShortestEditDistance) { bestCandidates.add(oCouple); } } if (bestCandidates.size() > 0) { // Case 1: Only one candidate pair if (bestCandidates.size() == 1) couple = bestCandidates.firstElement(); else { // CASE 2: Lexicographical order is enough tmap = TreeMultimap.create(); for (GEDEdge pair : bestCandidates) { label1 = pair.getSource().getName(); label2 = pair.getTarget().getName(); if (label1.compareTo(label2) > 0) { tmp = label1; label1 = label2; label2 = tmp; } tmap.put(label1 + label2, pair); } firstkey = tmap.keySet().first(); LOGGER.debug("firstkey: " + firstkey); if (tmap.get(firstkey).size() == 1) { couple = tmap.get(firstkey).first(); } else if (tmap.get(firstkey).size() > 1) { tmapp = TreeMultimap.create(); mset = TreeMultiset.create(); for (GEDEdge pair : tmap.get(firstkey)) { label1 = pair.getSource().getName(); mset.clear(); for (CPFNode n : sg1.getDirectPredecessors(pair.getSource())) { mset.add(n.getName()); } label1 += mset.toString(); mset.clear(); for (CPFNode n : sg1.getDirectSuccessors(pair.getSource())) { mset.add(n.getName()); } label1 += mset.toString(); label2 = pair.getTarget().getName(); mset.clear(); for (CPFNode n : sg2.getDirectPredecessors(pair.getTarget())) { mset.add(n.getName()); } label2 += mset.toString(); mset.clear(); for (CPFNode n : sg2.getDirectSuccessors(pair.getTarget())) { mset.add(n.getName()); } label2 += mset.toString(); if (label1.compareTo(label2) > 0) { tmp = label1; label1 = label2; label2 = tmp; } tmapp.put(label1 + label2, pair); } contextkey = tmapp.keySet().first(); // CASE 3: Composite labels (concatenation of labels of nodes surrounding the target vertex) if (tmapp.get(contextkey).size() == 1) { couple = tmapp.get(contextkey).first(); } else { // CASE 4: Non deterministic choice (Choose a random candidate) deterministic = false; couple = bestCandidates.get(randomized.nextInt(bestCandidates.size())); } } else { // CASE 5: Non deterministic choice (Choose a random candidate) deterministic = false; couple = bestCandidates.get(randomized.nextInt(bestCandidates.size())); } } newOpenCouples = new HashSet<>(); for (GEDEdge p : openCouples) { if (!p.getSource().getId().equalsIgnoreCase(couple.getSource().getId()) && !p.getTarget().getId().equalsIgnoreCase(couple.getTarget().getId())) { newOpenCouples.add(p); } } openCouples = newOpenCouples; LOGGER.debug("openCouples: " + openCouples.size()); mapping.add(couple); shortestEditDistance = newShortestEditDistance; doStep = true; } } LOGGER.debug("Mappings: " + mapping.size()); return shortestEditDistance; }