Java Code Examples for gnu.trove.set.TIntSet#contains()

The following examples show how to use gnu.trove.set.TIntSet#contains() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GraphUtil.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Remove from the {@param graph} vertices which are not connected to any edge,
 * and which have no associated object.
 */
public static <V, E> void removeIsolatedVertices(UndirectedGraph<V, E> graph) {
    Objects.requireNonNull(graph, "Graph is null.");

    TIntSet connectedVertices = new TIntHashSet();
    for (int e : graph.getEdges()) {
        connectedVertices.add(graph.getEdgeVertex1(e));
        connectedVertices.add(graph.getEdgeVertex2(e));
    }

    for (int v : graph.getVertices()) {
        if (!connectedVertices.contains(v) && graph.getVertexObject(v) == null) {
            graph.removeVertex(v);
        }
    }
}
 
Example 2
Source File: AbstractNodeBreakerInternalConnectionsTest.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
private InternalConnections findInternalConnectionsTraverseStoppingAtTerminals(VoltageLevel vl) {
    InternalConnections cs = new InternalConnections();

    VoltageLevel.NodeBreakerView topo = vl.getNodeBreakerView();
    int[] nodes = topo.getNodes();
    final TIntSet explored = new TIntHashSet();
    for (int n : nodes) {
        if (explored.contains(n) || topo.getTerminal(n) == null) {
            continue;
        }
        explored.add(n);
        topo.traverse(n, (n1, sw, n2) -> {
            explored.add(n2);
            if (sw == null) {
                cs.add(n1, n2);
            }
            return topo.getTerminal(n2) == null;
        });
    }
    return cs;
}
 
Example 3
Source File: AbstractNodeBreakerInternalConnectionsTest.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
private InternalConnections findInternalConnections(VoltageLevel vl) {
    InternalConnections cs = new InternalConnections();

    VoltageLevel.NodeBreakerView topo = vl.getNodeBreakerView();
    int[] nodes = topo.getNodes();
    final TIntSet explored = new TIntHashSet();
    for (int n : nodes) {
        if (explored.contains(n)) {
            continue;
        }
        explored.add(n);
        topo.traverse(n, (n1, sw, n2) -> {
            explored.add(n2);
            if (sw == null) {
                cs.add(n1, n2);
            }
            return true;
        });
    }
    return cs;
}
 
Example 4
Source File: ToXdr.java    From monsoon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static bitset createPresenceBitset(TIntSet have, int timestampsSize) {
    boolean bits[] = new boolean[timestampsSize];

    for (int i = 0; i < timestampsSize; ++i)
        bits[i] = have.contains(i);
    return bitset(bits);
}
 
Example 5
Source File: AbstractSparseBinaryMatrix.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Returns true if any of the on bits of the specified matrix are
 * matched by the on bits of this matrix. It is allowed that 
 * this matrix have more on bits than the specified matrix.
 * 
 * @param matrix
 * @return
 */
public boolean any(AbstractSparseBinaryMatrix matrix) {
    TIntSet keySet = getSparseSet();
    
    for(int i : matrix.getSparseIndices()) {
        if(keySet.contains(i)) return true;
    }
    return false;
}
 
Example 6
Source File: AbstractSparseBinaryMatrix.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Returns true if any of the on bit indexes of the specified collection are
 * matched by the on bits of this matrix. It is allowed that 
 * this matrix have more on bits than the specified matrix.
 * 
 * @param matrix
 * @return
 */
public boolean any(TIntList onBits) {
    TIntSet keySet = getSparseSet();
    
    for(TIntIterator i = onBits.iterator();i.hasNext();) {
        if(keySet.contains(i.next())) return true;
    }
    return false;
}
 
Example 7
Source File: AbstractSparseBinaryMatrix.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Returns true if any of the on bit indexes of the specified matrix are
 * matched by the on bits of this matrix. It is allowed that 
 * this matrix have more on bits than the specified matrix.
 * 
 * @param matrix
 * @return
 */
public boolean any(int[] onBits) {
    TIntSet keySet = getSparseSet();
    
    for(int i : onBits) {
        if(keySet.contains(i)) return true;
    }
    return false;
}
 
Example 8
Source File: CocktailParty.java    From ambiverse-nlu with Apache License 2.0 4 votes vote down vote up
/**
 * Removes dangling mentions (where no candidate entity has a coherence edge)
 * from gaph. They will influence the minimum weighted degree but can
 * never be improved. Set the solution to the entity with the highest
 * mention-entity weight.
 *
 * @param solution Solution will be updated, setting the correct entity using
 *                local similarity for dangling mentions.
 * @return Node ids of nodes to remove.
 */
private TIntSet removeUnconnectedMentionEntityPairs(Graph g, Map<ResultMention, List<ResultEntity>> solution) {
  TIntSet mentionsToRemove = new TIntHashSet();
  for (int mentionId : g.getMentionNodesIds().values()) {
    GraphNode mentionNode = g.getNode(mentionId);
    Mention mention = (Mention) mentionNode.getNodeData();
    TIntDoubleHashMap entityCandidates = mentionNode.getSuccessors();
    if (entityCandidates.size() == 0) {
      continue;
    }
    // Remove all mentions without any entities that have coherence edges.
    if (g.isLocalMention(mentionId)) {
      logger.debug("local mention removed: " + mentionId + " " + mention);
      mentionsToRemove.add(mentionId);
      GraphTracer.gTracer.addMentionToDangling(g.getName(), mention.getMention(), mention.getCharOffset());
      // Set solution to best local candidate.
      Pair<Integer, Double> bestEntityScore = getBestLocalCandidateAndScore(entityCandidates);
      int bestEntity = bestEntityScore.getKey();
      double score = bestEntityScore.getValue();
      updateSolution(solution, g, mention, bestEntity, score);
    }

  }
  TIntSet entitiesToRemove = new TIntHashSet();
  // Remove entities that are only connected to removed mentions.
  for (int entityId : g.getEntityNodesIds().values()) {
    GraphNode entityNode = g.getNode(entityId);
    TIntDoubleHashMap successors = entityNode.getSuccessors();
    int removedCount = 0;
    for (TIntDoubleIterator itr = successors.iterator(); itr.hasNext(); ) {
      itr.advance();
      int neighborId = itr.key();
      if (mentionsToRemove.contains(neighborId)) {
        ++removedCount;
      }
    }
    if (removedCount == successors.size()) {
      entitiesToRemove.add(entityId);
    }
  }
  // Remove mentions + entity candidates from graph, trace.
  TIntSet nodesToRemove = new TIntHashSet(mentionsToRemove.size() + entitiesToRemove.size());
  nodesToRemove.addAll(mentionsToRemove);
  nodesToRemove.addAll(entitiesToRemove);
  return nodesToRemove;
}
 
Example 9
Source File: MergeCenterClustering.java    From JedAIToolkit with Apache License 2.0 4 votes vote down vote up
@Override
public EquivalenceCluster[] getDuplicates(SimilarityPairs simPairs) {
    initializeData(simPairs);
    
    // add an edge for every pair of entities with a weight higher than the thrshold
    final Queue<SimilarityEdge> SEqueue = new PriorityQueue<>(simPairs.getNoOfComparisons(), new DecSimilarityEdgeComparator());
    final Iterator<Comparison> iterator = simPairs.getPairIterator();
    while (iterator.hasNext()) {
        final Comparison comparison = iterator.next();
        if (threshold < comparison.getUtilityMeasure()) {
            SEqueue.add(new SimilarityEdge(comparison.getEntityId1(), (comparison.getEntityId2() + datasetLimit), comparison.getUtilityMeasure()));
        }
    }
    
    final TIntSet Center = new TIntHashSet();
    final TIntSet NonCenter = new TIntHashSet();
    while (!SEqueue.isEmpty()) {
        final SimilarityEdge se = SEqueue.remove();
        int v1 = se.getModel1Pos();
        int v2 = se.getModel2Pos();
        
        boolean v1IsCenter = Center.contains(v1);
        boolean v2IsCenter = Center.contains(v2);
        boolean v1IsNonCenter = NonCenter.contains(v1);
        boolean v2IsNonCenter = NonCenter.contains(v2);
        
        if (!(v1IsCenter || v2IsCenter || v1IsNonCenter || v2IsNonCenter)) {
            Center.add(v1);
            NonCenter.add(v2);
            similarityGraph.addEdge(v1, v2);
        } else if ((v1IsCenter && v2IsCenter) || (v1IsNonCenter && v2IsNonCenter)) {
            continue;
        } else if (v1IsCenter) {
            NonCenter.add(v2);
            similarityGraph.addEdge(v1, v2);
        } else if (v2IsCenter) {
            NonCenter.add(v1);
            similarityGraph.addEdge(v1, v2);
        }
    }

    return getConnectedComponents();
}
 
Example 10
Source File: CenterClustering.java    From JedAIToolkit with Apache License 2.0 4 votes vote down vote up
@Override
public EquivalenceCluster[] getDuplicates(SimilarityPairs simPairs) {
    initializeData(simPairs);
    
    final float[] edgesWeight = new float[noOfEntities];
    final float[] edgesAttached = new float[noOfEntities];
    final Queue<SimilarityEdge> SEqueue = new PriorityQueue<>(simPairs.getNoOfComparisons(), new DecSimilarityEdgeComparator());

    final Iterator<Comparison> iterator = simPairs.getPairIterator();
    // add a similarity edge to the queue for every pair of entities with a weight higher than the threshold
    while (iterator.hasNext()) { 
        final Comparison comparison = iterator.next();
        if (threshold < comparison.getUtilityMeasure()) {
            SEqueue.add(new SimilarityEdge(comparison.getEntityId1(), comparison.getEntityId2() + datasetLimit, comparison.getUtilityMeasure()));

            edgesWeight[comparison.getEntityId1()] += comparison.getUtilityMeasure();
            edgesWeight[comparison.getEntityId2() + datasetLimit] += comparison.getUtilityMeasure();

            edgesAttached[comparison.getEntityId1()]++;
            edgesAttached[comparison.getEntityId2() + datasetLimit]++;
        }
    }

    final TIntSet Center = new TIntHashSet();
    final TIntSet NonCenter = new TIntHashSet();
    while (!SEqueue.isEmpty()) {
        final SimilarityEdge se = SEqueue.remove();
        int v1 = se.getModel1Pos();
        int v2 = se.getModel2Pos();
        
        boolean v1IsCenter = Center.contains(v1);
        boolean v2IsCenter = Center.contains(v2);
        boolean v1IsNonCenter = NonCenter.contains(v1);
        boolean v2IsNonCenter = NonCenter.contains(v2);

        if (!(v1IsCenter || v2IsCenter || v1IsNonCenter || v2IsNonCenter)) {
            float w1 = edgesWeight[v1] / edgesAttached[v1];
            float w2 = edgesWeight[v2] / edgesAttached[v2];
            if (w1 > w2) {
                Center.add(v1);
                NonCenter.add(v2);
            } else {
                Center.add(v2);
                NonCenter.add(v1);
            }

            similarityGraph.addEdge(v1, v2);
        } else if ((v1IsCenter && v2IsCenter) || (v1IsNonCenter && v2IsNonCenter)) {
            continue;
        } else if (v1IsCenter && !v2IsNonCenter) {
            NonCenter.add(v2);
            similarityGraph.addEdge(v1, v2);
        } else if (v2IsCenter && !v1IsNonCenter) {
            NonCenter.add(v1);
            similarityGraph.addEdge(v1, v2);
        }
    }

    return getConnectedComponents();
}