Package edu.ucla.sspace.util.primitive

Examples of edu.ucla.sspace.util.primitive.IntIterator


            assert added : "Added duplicate row to the set of selected points";           
            updateNearestCenter(inverseSimilarities, dataPoints,
                                point, simFunc);
  }

        IntIterator iter = selected.iterator();
        DoubleVector[] centroids = new DoubleVector[k];
        for (int i = 0; iter.hasNext(); ++i)
            centroids[i] = dataPoints.getRowVector(iter.nextInt());
        return centroids;
    }
View Full Code Here


     * @return the line graph for the input graph
     */
    public static <E extends Edge, G extends Graph<E>> Graph<Edge>
            toLineGraph(G graph, Indexer<E> edgeIndices) {
        Graph<Edge> lineGraph = new SparseUndirectedGraph();
        IntIterator verts = graph.vertices().iterator();
        while (verts.hasNext()) {
            int v = verts.nextInt();
            Set<E> adjacent = graph.getAdjacencyList(v);
            // For each pair of edges connected to the same vertex, add them as
            // vertices in the line graph and connect them by an edge
            for (E e1 : adjacent) {
                int e1vertex = edgeIndices.index(e1);
View Full Code Here

        return true;
    }

    static int getMaxClass(int v, int[] vertexAssignments, Graph g) {
        IntSet neighbors = g.getNeighbors(v);
        IntIterator iter = neighbors.iterator();
        Counter<Integer> classes = new ObjectCounter<Integer>();
        classes.count(vertexAssignments[v]);
        while (iter.hasNext()) {
            int n = iter.nextInt();
            classes.count(vertexAssignments[n]);
        }

        TIntSet ties = new TIntHashSet();
        int max = 0;
View Full Code Here

                        continue;
                    double sim = simFunc.sim(cc.centerOfMass(), cc2.centerOfMass());
                    if (sim < similarityThreshold)
                        continue;

                    IntIterator iter = cc2.indices().iterator();
                    double similaritySum = 0;
                    while (iter.hasNext()) {
                        DoubleVector v = matrix.getRowVector(iter.next());
                        similaritySum += simFunc.sim(cc2.centerOfMass(), v);
                    }
                    double avgSim = similaritySum / cc2.size();
                   
                    if (avgSim > maxCohesiveness) {
View Full Code Here

                        CandidateCluster fac = facilities.get(j);
                        veryVerbose(LOGGER, "Facility %d had a center of mass at %s",
                                    j, fac.centerOfMass());
                       
                        int clusterId = j;
                        IntIterator iter = fac.indices().iterator();
                        while (iter.hasNext()) {
                            int row = iter.nextInt();
                            assignments[row] =
                                new HardAssignment(clusterId);
                        }
                    }
                    return new Assignments(numClusters, assignments, matrix);                   
                }
                else {
                    verbose(LOGGER, "Had more than %d facilities, " +
                            "consolidating to %d", facilities.size(),
                            numClusters);
                   
                    List<DoubleVector> facilityCentroids =
                        new ArrayList<DoubleVector>(facilities.size());
                    int[] weights = new int[facilities.size()];
                    int i = 0;
                    for (CandidateCluster fac : facilities) {
                        facilityCentroids.add(fac.centerOfMass());
                        weights[i++] = fac.size();
                    }
                    // Wrap the facilities centroids in a matrix for convenience
                    Matrix m = Matrices.asMatrix(facilityCentroids);

                    // Select the initial seed points for reducing the kappa
                    // clusters to k using the generalized ORSS selection
                    // process, which supports data comparisons other than
                    // Euclidean distance
                    GeneralizedOrssSeed orss = new GeneralizedOrssSeed(simFunc);
                    DoubleVector[] centroids = orss.chooseSeeds(numClusters, m);
                    assert nonNullCentroids(centroids)
                        : "ORSS seed returned too few centroids";
                   
                    // This records the assignments of the kappa facilities to
                    // the k centers.  Initially, everyhting is assigned to the
                    // same center and iterations repeat until convergence.
                    int[] facilityAssignments = new int[facilities.size()];
                   
                    // Using those facilities as starting points, run k-means on
                    // the facility centroids until no facilities change their
                    // memebership.
                    int numChanged = 0;
                    int kmeansIters = 0;
                    do {
                        numChanged = 0;
                        // Recompute the new centroids each time
                        DoubleVector[] updatedCentroids =
                            new DoubleVector[numClusters];
                        for (i = 0; i < updatedCentroids.length; ++i)
                            updatedCentroids[i] = new DenseVector(cols);
                        int[] updatedCentroidSizes = new int[numClusters];

                        double similaritySum = 0;
                       
                        // For each CandidateCluster find the most similar centroid
                        i = 0;
                        for (CandidateCluster fac : facilities) {
                            int mostSim = -1;
                            double highestSim = -1;
                            for (int j = 0; j < centroids.length; ++j) {
//                                  System.out.printf("centroids[%d]: %s%n fac.centroid(): %s%n",
//                                                    j, centroids[j],
//                                                    fac.centerOfMass());
                                double sim = simFunc.sim(centroids[j],
                                                         fac.centerOfMass());
                                if (sim > highestSim) {
                                    highestSim = sim;
                                    mostSim = j;
                                }
                            }

                            // For the most similar centroid, update its center
                            // of mass for the next round with the weighted
                            // vector
                            VectorMath.add(updatedCentroids[mostSim],
                                           fac.sum());
                            updatedCentroidSizes[mostSim] += fac.size();
                            int curAssignment = facilityAssignments[i];
                            facilityAssignments[i] = mostSim;
                            similaritySum += highestSim;
                            if (curAssignment != mostSim) {
                                veryVerbose(LOGGER, "Facility %d changed its " +
                                            "centroid from %d to %d",
                                            i, curAssignment, mostSim);
                                numChanged++;
                            }
                            i++;
                        }

                        // Once all the facilities have been assigned to one of
                        // the k-centroids, recompute the centroids by
                        // normalizing the sum of the weighted vectors according
                        // the number of points
                        for (int j = 0; j < updatedCentroids.length; ++j) {
                            DoubleVector v = updatedCentroids[j];
                            int size = updatedCentroidSizes[j];
                            for (int k = 0; k < cols; ++k)
                                v.set(k, v.get(k) / size);
                            // Update this centroid for the next round
                            centroids[j] = v;                           
                        }

                        veryVerbose(LOGGER, "%d centroids swapped their facility",
                                    numChanged);
                    } while (numChanged > 0 &&
                                 ++kmeansIters < MAX_BATCH_KMEANS_ITERS);

                    // Use the final assignments to create assignments for each
                    // of the input data points
                    Assignment[] assignments = new Assignment[rows];
                    for (int j = 0; j < facilityAssignments.length; ++j) {
                        CandidateCluster fac = facilities.get(j);
                        veryVerbose(LOGGER, "Facility %d had a center of mass at %s",
                                    j, fac.centerOfMass());
                       
                        int clusterId = facilityAssignments[j];
                        IntIterator iter = fac.indices().iterator();
                        while (iter.hasNext()) {
                            int row = iter.nextInt();
                            assignments[row] =
                                new HardAssignment(clusterId);
                        }
                    }
                    return new Assignments(numClusters, assignments, matrix);
View Full Code Here

        Element nodes = doc.createElement("nodes");
        graph.appendChild(nodes);
        Element edges = doc.createElement("edges");
        graph.appendChild(edges);

        IntIterator vIter = g.vertices().iterator();
        while (vIter.hasNext()) {
            int vertex = vIter.next();
            Element node = doc.createElement("node");
            String vLabel = (vertexLabels == null)
                ? String.valueOf(vertex)
                : vertexLabels.lookup(vertex);
            if (vLabel == null)
View Full Code Here

        Element nodes = doc.createElement("nodes");
        graph.appendChild(nodes);
        Element edges = doc.createElement("edges");
        graph.appendChild(edges);

        IntIterator vIter = g.vertices().iterator();
        while (vIter.hasNext()) {
            int vertex = vIter.next();
            Element node = doc.createElement("node");
            node.setAttribute("id", String.valueOf(vertex));
            if (useLabels)
                node.setAttribute("label", vertexLabels.lookup(vertex));
            else
View Full Code Here

        Element nodes = doc.createElement("nodes");
        graph.appendChild(nodes);
        Element edges = doc.createElement("edges");
        graph.appendChild(edges);

        IntIterator vIter = g.vertices().iterator();
        while (vIter.hasNext()) {
            int vertex = vIter.next();
            Element node = doc.createElement("node");
            String vLabel = (vertexLabels == null)
                ? String.valueOf(vertex)
                : vertexLabels.lookup(vertex);
            if (vLabel == null)
View Full Code Here

        if (!hasContiguousVertices(g))
            throw new IllegalArgumentException(
                "Vertices must be in continugous order");
       
        double[] centralities = new double[g.order()];
        IntIterator vertexIter = g.vertices().iterator();
        while (vertexIter.hasNext()) {
            int s = vertexIter.nextInt();
            Deque<Integer> S = new ArrayDeque<Integer>();

            // Initialize P to an empty list for each vertex
            List<List<Integer>> P = new ArrayList<List<Integer>>(g.order());
            for (int i = 0; i < g.order(); ++i)
                P.add(new ArrayList<Integer>());

            double[] sigma = new double[g.order()];
            sigma[s] = 1;

            double[] d = new double[g.order()];
            Arrays.fill(d, -1);
            d[s] = 0;
           
            Queue<Integer> Q = new ArrayDeque<Integer>();
            Q.add(s);
            while (!Q.isEmpty()) {
                int v = Q.poll();
                S.offer(v);
                IntIterator neighborIter = g.getNeighbors(v).iterator();
                while (neighborIter.hasNext()) {
                    int w = neighborIter.nextInt();
                    // Check whether this is the first time we've seen vertex w
                    if (d[w] < 0) {
                        Q.offer(w);
                        d[w] = d[v] + 1;
                    }
View Full Code Here

        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(f)));
        Indexer<Integer> vertexMap = new ObjectIndexer<Integer>();
        vertexMap.index(-1); // dummy since Pajek must start at 1
        pw.println("*Vertices " + g.order());
        // Write the vertices, which may be disconnected and must start at 1
        IntIterator iter = g.vertices().iterator();
        while (iter.hasNext()) {
            int v = iter.next();
            pw.println(vertexMap.index(v) + " \"" + vertexIndex.lookup(v) + "\"");
        }
        pw.println("*Edges " + g.size());
        // Write the edges that connect the vertices, noting that Pajek vertices
        // start at 1
View Full Code Here

TOP

Related Classes of edu.ucla.sspace.util.primitive.IntIterator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.