コード例 #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
 private NodeIterator advanceIterator(int x) {
   if (x >= graph.numNodes()) throw new Error("Problem with the id for the node.");
   if (!iterator.hasNext() || iterator.nextInt() >= x) iterator = nodeIterator();
   Integer aux = null;
   while ((aux = iterator.nextInt()) != x) {}
   return iterator;
 }
コード例 #3
0
ファイル: Graph.java プロジェクト: midiablo/CoarseWordNet
 protected void finalize() throws Throwable {
   super.finalize();
   nodes.clear();
   nodes.getRecordManager().commit();
   nodes.getRecordManager().close();
   nodesReverse.clear();
   nodesReverse.getRecordManager().commit();
   nodesReverse.getRecordManager().close();
 }
コード例 #4
0
ファイル: Graph.java プロジェクト: midiablo/CoarseWordNet
 public Label prototype() {
   return graph.prototype();
 }
コード例 #5
0
ファイル: Graph.java プロジェクト: midiablo/CoarseWordNet
 public Graph neighbourhoodGraph(int nnodes[], int hops) {
   PrimaryHashMap<Integer, String> nodes;
   PrimaryHashMap<String, Integer> nodesReverse;
   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();
   WeightedArcSet list1 = new WeightedArcSet();
   Int2IntAVLTreeMap map = new Int2IntAVLTreeMap();
   IntSet set = new IntLinkedOpenHashSet();
   int numIterators = 100;
   Constructor[] cons = WeightedArc.class.getDeclaredConstructors();
   for (int i = 0; i < cons.length; i++) cons[i].setAccessible(true);
   for (int n : nnodes) map.put(n, 0);
   NodeIterator its[] = new NodeIterator[numIterators];
   int itNum[] = new int[numIterators];
   for (int n = 0; n < its.length; n++) {
     its[n] = nodeIterator();
     itNum[n] = 0;
   }
   while (map.size() != 0) {
     Integer node = 0;
     for (int n = 0; n < its.length; n++) if (itNum[n] <= node) node = itNum[n];
     node = map.tailMap(node).firstKey();
     if (node == null) map.firstKey();
     NodeIterator it = null;
     Integer aux1 = 0;
     int iit = 0;
     for (int n = 0; n < its.length; n++) {
       if (!its[n].hasNext()) {
         its[n] = nodeIterator();
         itNum[n] = 0;
       }
       if (itNum[n] == node) {
         it = its[n];
         aux1 = itNum[n];
         iit = 0;
         break;
       }
       if (itNum[n] < node && itNum[n] >= aux1) {
         it = its[n];
         aux1 = itNum[n];
         iit = n;
       }
     }
     if (it == null) {
       its[0] = nodeIterator();
       itNum[0] = 0;
       it = its[0];
     }
     while (it != null && (aux1 = it.nextInt()) != null && aux1 >= 0 && aux1 < node) {}
     itNum[iit] = aux1 + 1;
     Integer aux2 = null;
     ArcLabelledNodeIterator.LabelledArcIterator suc = it.successors();
     while ((aux2 = suc.nextInt()) != null && aux2 >= 0 && (aux2 < graph.numNodes()))
       try {
         if (commit++ % COMMIT_SIZE == 0) {
           try {
             nodes.getRecordManager().commit();
           } catch (IOException e) {
             throw new Error(e);
           }
           try {
             nodesReverse.getRecordManager().commit();
           } catch (IOException e) {
             throw new Error(e);
           }
         }
         if (!nodesReverse.containsKey(this.nodes.get(aux1))) {
           nodes.put(nodes.size(), this.nodes.get(aux1));
           nodesReverse.put(this.nodes.get(aux1), nodesReverse.size());
         }
         if (!nodesReverse.containsKey(this.nodes.get(aux2))) {
           nodes.put(nodes.size(), this.nodes.get(aux2));
           nodesReverse.put(this.nodes.get(aux2), nodesReverse.size());
         }
         int aaux1 = nodesReverse.get(this.nodes.get(aux1));
         int aaux2 = nodesReverse.get(this.nodes.get(aux2));
         WeightedArc arc1 =
             (WeightedArc) cons[0].newInstance(aaux1, aaux2, suc.label().getFloat());
         list1.add(arc1);
         if (map.get(node) < hops) {
           if (!set.contains(aux1) && (map.get(aux1) == null || map.get(aux1) > map.get(node) + 1))
             map.put(aux1.intValue(), map.get(node) + 1);
           if (!set.contains(aux2) && (map.get(aux2) == null || map.get(aux2) > map.get(node) + 1))
             map.put(aux2.intValue(), map.get(node) + 1);
         }
       } catch (Exception ex) {
         ex.printStackTrace();
         throw new Error(ex);
       }
     ArcLabelledNodeIterator.LabelledArcIterator anc = it.ancestors();
     while ((aux2 = anc.nextInt()) != null && aux2 >= 0 && (aux2 < graph.numNodes()))
       try {
         if (commit++ % COMMIT_SIZE == 0) {
           try {
             nodes.getRecordManager().commit();
           } catch (IOException e) {
             throw new Error(e);
           }
           try {
             nodesReverse.getRecordManager().commit();
           } catch (IOException e) {
             throw new Error(e);
           }
         }
         if (!nodesReverse.containsKey(this.nodes.get(aux1))) {
           nodes.put(nodes.size(), this.nodes.get(aux1));
           nodesReverse.put(this.nodes.get(aux1), nodesReverse.size());
         }
         if (!nodesReverse.containsKey(this.nodes.get(aux2))) {
           nodes.put(nodes.size(), this.nodes.get(aux2));
           nodesReverse.put(this.nodes.get(aux2), nodesReverse.size());
         }
         int aaux1 = nodesReverse.get(this.nodes.get(aux1));
         int aaux2 = nodesReverse.get(this.nodes.get(aux2));
         WeightedArc arc1 =
             (WeightedArc) cons[0].newInstance(aaux2, aaux1, anc.label().getFloat());
         list1.add(arc1);
         if (map.get(node) < hops) {
           if (!set.contains(aux1) && (map.get(aux1) == null || map.get(aux1) > map.get(node) + 1))
             map.put(aux1.intValue(), map.get(node) + 1);
           if (!set.contains(aux2) && (map.get(aux2) == null || map.get(aux2) > map.get(node) + 1))
             map.put(aux2.intValue(), map.get(node) + 1);
         }
       } catch (Exception ex) {
         ex.printStackTrace();
         throw new Error(ex);
       }
     map.remove(node);
     set.add(node);
   }
   Graph newGraph = new Graph(list1.toArray(new WeightedArc[0]));
   newGraph.nodes.clear();
   newGraph.nodesReverse.clear();
   newGraph.nodes = nodes;
   newGraph.nodesReverse = nodesReverse;
   return newGraph;
 }
コード例 #6
0
ファイル: Graph.java プロジェクト: midiablo/CoarseWordNet
 public int numNodes() {
   return graph.numNodes();
 }
コード例 #7
0
  public static void main(final String[] arg)
      throws IOException, JSAPException, ConfigurationException, ClassNotFoundException {

    SimpleJSAP jsap =
        new SimpleJSAP(
            WeightedPageRankPowerMethod.class.getName(),
            "Computes PageRank of a graph with given graphBasename using the power method."
                + "The resulting doubles are stored in binary form in rankFile."
                + "\n[STOPPING CRITERION] The computation is stopped as soon as two successive iterates have"
                + "an L2-distance smaller than a given threshold (-t option); in any case no more than a fixed"
                + "number of iterations (-i option) is performed.",
            new Parameter[] {
              new FlaggedOption(
                  "alpha",
                  JSAP.DOUBLE_PARSER,
                  Double.toString(WeightedPageRank.DEFAULT_ALPHA),
                  JSAP.NOT_REQUIRED,
                  'a',
                  "alpha",
                  "Damping factor."),
              new FlaggedOption(
                  "maxIter",
                  JSAP.INTEGER_PARSER,
                  Integer.toString(WeightedPageRank.DEFAULT_MAX_ITER),
                  JSAP.NOT_REQUIRED,
                  'i',
                  "max-iter",
                  "Maximum number of iterations."),
              new FlaggedOption(
                  "threshold",
                  JSAP.DOUBLE_PARSER,
                  Double.toString(WeightedPageRank.DEFAULT_THRESHOLD),
                  JSAP.NOT_REQUIRED,
                  't',
                  "threshold",
                  "Threshold to determine whether to stop."),
              new FlaggedOption(
                  "coeff",
                  JSAP.STRING_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.NOT_REQUIRED,
                  'c',
                  "coeff",
                  "Save the k-th coefficient of the Taylor polynomial using this basename."),
              new FlaggedOption(
                      "derivative",
                      JSAP.INTEGER_PARSER,
                      JSAP.NO_DEFAULT,
                      JSAP.NOT_REQUIRED,
                      'd',
                      "derivative",
                      "The order(s) of the the derivative(s) to be computed (>0).")
                  .setAllowMultipleDeclarations(true),
              new FlaggedOption(
                  "preferenceVector",
                  JSAP.STRING_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.NOT_REQUIRED,
                  'p',
                  "preference-vector",
                  "A preference vector stored as a vector of binary doubles."),
              new FlaggedOption(
                  "preferenceObject",
                  JSAP.STRING_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.NOT_REQUIRED,
                  'P',
                  "preference-object",
                  "A preference vector stored as a serialised DoubleList."),
              new FlaggedOption(
                  "startFilename",
                  JSAP.STRING_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.NOT_REQUIRED,
                  '1',
                  "start",
                  "Start vector filename."),
              new FlaggedOption(
                  "buckets",
                  JSAP.STRING_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.NOT_REQUIRED,
                  'b',
                  "buckets",
                  "The buckets of the graph; if supplied, buckets will be treated as dangling nodes."),
              new Switch("offline", 'o', "offline", "use loadOffline() to load the graph"),
              new Switch(
                  "strongly",
                  'S',
                  "strongly",
                  "use the preference vector to redistribute the dangling rank."),
              new Switch(
                  "sortedRank",
                  's',
                  "sorted-ranks",
                  "Store the ranks (from highest to lowest) into <rankBasename>-sorted.ranks."),
              new FlaggedOption(
                  "norm",
                  JSAP.STRING_PARSER,
                  WeightedPageRank.Norm.INFTY.toString(),
                  JSAP.NOT_REQUIRED,
                  'n',
                  "norm",
                  "Norm type. Possible values: " + Arrays.toString(WeightedPageRank.Norm.values())),
              new UnflaggedOption(
                  "graphBasename",
                  JSAP.STRING_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.REQUIRED,
                  JSAP.NOT_GREEDY,
                  "The basename of the graph."),
              new UnflaggedOption(
                  "rankBasename",
                  JSAP.STRING_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.REQUIRED,
                  JSAP.NOT_GREEDY,
                  "The filename where the resulting rank (doubles in binary form) are stored.")
            });

    JSAPResult jsapResult = jsap.parse(arg);
    if (jsap.messagePrinted()) return;

    final boolean offline = jsapResult.getBoolean("offline", false);
    final boolean strongly = jsapResult.getBoolean("strongly", false);
    final boolean sorted = jsapResult.getBoolean("sortedRank", false);
    final int[] order = jsapResult.getIntArray("derivative");
    final String graphBasename = jsapResult.getString("graphBasename");
    final String rankBasename = jsapResult.getString("rankBasename");
    final String buckets = jsapResult.getString("buckets");
    final String startFilename = jsapResult.getString("startFilename", null);
    final String coeffBasename = jsapResult.getString("coeff");
    final String norm = jsapResult.getString("norm");
    final ProgressLogger progressLogger = new ProgressLogger(LOGGER, "nodes");

    ArcLabelledImmutableGraph graph =
        offline
            ? ArcLabelledImmutableGraph.loadOffline(graphBasename, progressLogger)
            : ArcLabelledImmutableGraph.loadSequential(graphBasename, progressLogger);

    DoubleList preference = null;
    String preferenceFilename = null;
    if (jsapResult.userSpecified("preferenceVector"))
      preference =
          DoubleArrayList.wrap(
              BinIO.loadDoubles(preferenceFilename = jsapResult.getString("preferenceVector")));

    if (jsapResult.userSpecified("preferenceObject")) {
      if (jsapResult.userSpecified("preferenceVector"))
        throw new IllegalArgumentException("You cannot specify twice the preference vector");
      preference =
          (DoubleList)
              BinIO.loadObject(preferenceFilename = jsapResult.getString("preferenceObject"));
    }

    if (strongly && preference == null)
      throw new IllegalArgumentException("The 'strongly' option requires a preference vector");

    DoubleList start = null;
    if (startFilename != null) {
      LOGGER.debug("Loading start vector \"" + startFilename + "\"...");
      start = DoubleArrayList.wrap(BinIO.loadDoubles(startFilename));
      LOGGER.debug("done.");
    }

    WeightedPageRankPowerMethod pr = new WeightedPageRankPowerMethod(graph);
    pr.alpha = jsapResult.getDouble("alpha");
    pr.preference = preference;
    pr.buckets = (BitSet) (buckets == null ? null : BinIO.loadObject(buckets));
    pr.stronglyPreferential = strongly;
    pr.start = start;
    pr.norm = WeightedPageRank.Norm.valueOf(norm);
    pr.order = order != null ? order : null;
    pr.coeffBasename = coeffBasename;

    // cycle until we reach maxIter interations or the norm is less than the given threshold
    // (whichever comes first)
    pr.stepUntil(
        or(
            new WeightedPageRank.NormDeltaStoppingCriterion(jsapResult.getDouble("threshold")),
            new WeightedPageRank.IterationNumberStoppingCriterion(jsapResult.getInt("maxIter"))));

    System.err.print("Saving ranks...");
    BinIO.storeDoubles(pr.rank, rankBasename + ".ranks");
    if (pr.numNodes < 100) {
      for (int i = 0; i < pr.rank.length; i++) {
        System.err.println("PageRank[" + i + "]=" + pr.rank[i]);
      }
    }
    Properties prop = pr.buildProperties(graphBasename, preferenceFilename, startFilename);
    prop.save(rankBasename + ".properties");

    if (order != null) {
      System.err.print("Saving derivatives...");
      for (int i = 0; i < order.length; i++)
        BinIO.storeDoubles(pr.derivative[i], rankBasename + ".der-" + order[i]);
    }

    final double[] rank = pr.rank;
    pr = null; // Let us free some memory...
    graph = null;

    if (sorted) {
      System.err.print("Sorting ranks...");
      Arrays.sort(rank);
      final int n = rank.length;
      int i = n / 2;
      double t;
      // Since we need the ranks from highest to lowest, we invert their order.
      while (i-- != 0) {
        t = rank[i];
        rank[i] = rank[n - i - 1];
        rank[n - i - 1] = t;
      }
      System.err.print(" saving sorted ranks...");
      BinIO.storeDoubles(rank, rankBasename + "-sorted.ranks");
      System.err.println(" done.");
    }
  }