コード例 #1
0
ファイル: Graph.java プロジェクト: midiablo/CoarseWordNet
 public Graph copy() {
   WeightedArcSet list = new WeightedArcSet();
   Constructor[] cons = WeightedArc.class.getDeclaredConstructors();
   for (int i = 0; i < cons.length; i++) cons[i].setAccessible(true);
   ArcLabelledNodeIterator it = graph.nodeIterator();
   while (it.hasNext()) {
     Integer aux1 = it.nextInt();
     Integer aux2 = null;
     ArcLabelledNodeIterator.LabelledArcIterator suc = it.successors();
     while ((aux2 = suc.nextInt()) != null && aux2 >= 0 && (aux2 < graph.numNodes()))
       try {
         if (commit++ % COMMIT_SIZE == 0) {
           list.commit();
         }
         WeightedArc arc = (WeightedArc) cons[0].newInstance(aux2, aux1, suc.label().getFloat());
         list.add(arc);
       } catch (Exception ex) {
         throw new Error(ex);
       }
   }
   Graph result = new Graph(list.toArray(new WeightedArc[0]));
   result.nodes.clear();
   result.nodesReverse.clear();
   for (Integer n : this.nodes.keySet()) {
     if (commit++ % COMMIT_SIZE == 0) {
       result.commit();
     }
     result.nodesReverse.put(this.nodes.get(n), n);
     result.nodes.put(n, this.nodes.get(n));
   }
   return result;
 }
コード例 #2
0
ファイル: Graph.java プロジェクト: midiablo/CoarseWordNet
 public Graph(WeightedBVGraph graph, String[] names) {
   org.apache.log4j.Logger logger =
       org.apache.log4j.Logger.getLogger("it.unimi.dsi.webgraph.ImmutableGraph");
   logger.setLevel(org.apache.log4j.Level.FATAL);
   if (names.length != graph.numNodes())
     throw new Error("Problem with the list of names for the nodes in the graph.");
   try {
     File auxFile = File.createTempFile("graph-maps-" + System.currentTimeMillis(), "aux");
     auxFile.deleteOnExit();
     RecordManager recMan = RecordManagerFactory.createRecordManager(auxFile.getAbsolutePath());
     nodes = recMan.hashMap("nodes");
     nodesReverse = recMan.hashMap("nodesReverse");
   } catch (IOException ex) {
     throw new Error(ex);
   }
   nodes.clear();
   nodesReverse.clear();
   Constructor[] cons = WeightedArc.class.getDeclaredConstructors();
   for (int i = 0; i < cons.length; i++) cons[i].setAccessible(true);
   this.graph = graph;
   WeightedArcSet list = new WeightedArcSet();
   ArcLabelledNodeIterator it = graph.nodeIterator();
   while (it.hasNext()) {
     if (commit++ % COMMIT_SIZE == 0) {
       commit();
       list.commit();
     }
     Integer aux1 = it.nextInt();
     Integer aux2 = null;
     ArcLabelledNodeIterator.LabelledArcIterator suc = it.successors();
     while ((aux2 = suc.nextInt()) != null && aux2 >= 0 && (aux2 < graph.numNodes()))
       try {
         WeightedArc arc = (WeightedArc) cons[0].newInstance(aux2, aux1, suc.label().getFloat());
         list.add(arc);
         this.nodes.put(aux1, names[aux1]);
         this.nodes.put(aux2, names[aux2]);
         this.nodesReverse.put(names[aux1], aux1);
         this.nodesReverse.put(names[aux2], aux2);
       } catch (Exception ex) {
         throw new Error(ex);
       }
   }
   reverse = new WeightedBVGraph(list.toArray(new WeightedArc[0]));
   numArcs = list.size();
   iterator = nodeIterator();
   try {
     File auxFile = File.createTempFile("graph" + System.currentTimeMillis(), "aux");
     auxFile.deleteOnExit();
     String basename = auxFile.getAbsolutePath();
     store(basename);
   } catch (IOException ex) {
     throw new Error(ex);
   }
   commit();
 }
コード例 #3
0
ファイル: Graph.java プロジェクト: midiablo/CoarseWordNet
 public static Graph merge(Graph g1, Graph g2) {
   int commit = 0;
   WeightedArcSet list = new WeightedArcSet();
   Constructor[] cons = WeightedArc.class.getDeclaredConstructors();
   for (int i = 0; i < cons.length; i++) cons[i].setAccessible(true);
   ArcLabelledNodeIterator it1 = g1.graph.nodeIterator();
   while (it1.hasNext()) {
     Integer aux1 = it1.nextInt();
     ArcLabelledNodeIterator.LabelledArcIterator suc = it1.successors();
     Integer aux2 = null;
     while ((aux2 = suc.nextInt()) != null && aux2 >= 0 && (aux2 < g1.graph.numNodes()))
       try {
         WeightedArc arc = (WeightedArc) cons[0].newInstance(aux1, aux2, suc.label().getFloat());
         list.add(arc);
         if (commit++ % COMMIT_SIZE == 0) {
           list.commit();
         }
       } catch (Exception ex) {
         throw new Error(ex);
       }
   }
   ArcLabelledNodeIterator it2 = g2.graph.nodeIterator();
   while (it2.hasNext()) {
     Integer aux1 = it2.nextInt();
     ArcLabelledNodeIterator.LabelledArcIterator suc = it2.successors();
     Integer aux2 = null;
     while ((aux2 = suc.nextInt()) != null && aux2 >= 0 && (aux2 < g2.graph.numNodes()))
       try {
         int aaux1 = aux1 + g1.numNodes();
         int aaux2 = aux2 + g1.numNodes();
         if (g1.nodes.get(aux1) != null && g1.nodes.get(aux1).equals(g2.nodes.get(aux1)))
           aaux1 = g1.nodesReverse.get(g2.nodes.get(aux1));
         if (g1.nodes.get(aux2) != null && g1.nodes.get(aux2).equals(g2.nodes.get(aux2)))
           aaux2 = g1.nodesReverse.get(g2.nodes.get(aux2));
         WeightedArc arc = (WeightedArc) cons[0].newInstance(aux1, aux2, suc.label().getFloat());
         list.add(arc);
         if (commit++ % COMMIT_SIZE == 0) {
           list.commit();
         }
       } catch (Exception ex) {
         throw new Error(ex);
       }
   }
   Graph result = new Graph(list.toArray(new WeightedArc[0]));
   result.nodes.clear();
   result.nodesReverse.clear();
   for (Integer n : g1.nodes.keySet()) {
     result.nodesReverse.put(g1.nodes.get(n), n);
     result.nodes.put(n, g1.nodes.get(n));
     if (commit++ % COMMIT_SIZE == 0) {
       result.commit();
     }
   }
   for (Integer n : g2.nodes.keySet()) {
     int nn = n + g1.numNodes();
     if (g1.nodes.get(n) != null && g1.nodes.get(n).equals(g2.nodes.get(n)))
       nn = g1.nodesReverse.get(g2.nodes.get(n));
     result.nodesReverse.put(g2.nodes.get(n), nn);
     result.nodes.put(nn, g2.nodes.get(n));
     if (commit++ % COMMIT_SIZE == 0) {
       result.commit();
     }
   }
   result.iterator = result.nodeIterator();
   return result;
 }
コード例 #4
0
  /** Computes the next step of the Power Method. */
  public void step() throws IOException {
    double[] oldRank = rank, newRank = previousRank;
    DoubleArrays.fill(newRank, 0.0);

    // for each node, calculate its outdegree and redistribute its rank among pointed nodes
    double accum = 0.0;

    progressLogger.expectedUpdates = numNodes;
    progressLogger.start("Iteration " + (++iterationNumber) + "...");

    final ArcLabelledNodeIterator nodeIterator = g.nodeIterator();
    int i, outdegree, j, n = numNodes;
    int[] succ;
    Label[] lab;

    while (n-- != 0) {
      i = nodeIterator.nextInt();
      outdegree = nodeIterator.outdegree();

      if (outdegree == 0 || buckets != null && buckets.get(i)) accum += oldRank[i];
      else {
        j = outdegree;
        succ = nodeIterator.successorArray();
        lab = nodeIterator.labelArray();
        while (j-- != 0) {
          newRank[succ[j]] += (oldRank[i] * lab[j].getFloat()) / sumoutweight[i];
        }
      }
      progressLogger.update();
    }
    progressLogger.done();

    final double accumOverNumNodes = accum / numNodes;

    final double oneOverNumNodes = 1.0 / numNodes;
    if (preference != null)
      if (preferentialAdjustment == null)
        for (i = numNodes; i-- != 0; )
          newRank[i] =
              alpha * newRank[i]
                  + (1 - alpha) * preference.getDouble(i)
                  + alpha * accumOverNumNodes;
      else
        for (i = numNodes; i-- != 0; )
          newRank[i] =
              alpha * newRank[i]
                  + (1 - alpha) * preference.getDouble(i)
                  + alpha * accum * preferentialAdjustment.getDouble(i);
    else if (preferentialAdjustment == null)
      for (i = numNodes; i-- != 0; )
        newRank[i] = alpha * newRank[i] + (1 - alpha) * oneOverNumNodes + alpha * accumOverNumNodes;
    else
      for (i = numNodes; i-- != 0; )
        newRank[i] =
            alpha * newRank[i]
                + (1 - alpha) * oneOverNumNodes
                + alpha * accum * preferentialAdjustment.getDouble(i);

    // make the rank just computed the new rank
    rank = newRank;
    previousRank = oldRank;

    // Compute derivatives.
    n = iterationNumber;

    if (subset == null) {
      for (i = 0; i < order.length; i++) {
        final int k = order[i];
        final double alphak = Math.pow(alpha, k);
        final double nFallingK = Util.falling(n, k);
        for (j = 0; j < numNodes; j++)
          derivative[i][j] += nFallingK * (rank[j] - previousRank[j]) / alphak;
      }
    } else {
      for (i = 0; i < order.length; i++) {
        final int k = order[i];
        final double alphak = Math.pow(alpha, k);
        final double nFallingK = Util.falling(n, k);

        for (int t : subset) derivative[i][t] += nFallingK * (rank[t] - previousRank[t]) / alphak;
      }
    }

    // Compute coefficients, if required.

    if (coeffBasename != null) {
      final DataOutputStream coefficients =
          new DataOutputStream(
              new FastBufferedOutputStream(
                  new FileOutputStream(coeffBasename + "-" + (iterationNumber))));
      final double alphaN = Math.pow(alpha, n);
      for (i = 0; i < numNodes; i++) coefficients.writeDouble((rank[i] - previousRank[i]) / alphaN);
      coefficients.close();
    }
  }