Exemplo n.º 1
0
  // Lit une ligne du fichier en ignorant les lignes blanches
  // et les commentaires.
  private String readOneLine(BufferedReader input) {
    String line;

    try {
      while (true) {
        line = input.readLine();
        if (line == null) return null;
        line = line.trim();

        // On saute les lignes blanches.
        if (line.length() == 0) {
          line = input.readLine();
          continue;
        }

        // On saute les lignes de commentaires
        if (line.charAt(0) == '#') {
          line = input.readLine();
          continue;
        }

        // On elimine les commentaires a la fin de la ligne
        int index = line.indexOf('#');
        if (index >= 0) {
          line = line.substring(0, index).trim();
        }
        return line;
      }
    } catch (IOException e) {
      System.err.println(e);
      System.exit(1);
    }
    return null;
  }
Exemplo n.º 2
0
  // Initialise les variables, et appelle fillCyclesLFSR()
  private void init() {
    k1 = nocoeff1[0];
    posa1 = new int[nbcoeff1];
    shifta1 = new int[nbcoeff1];
    for (int i = 0; i < nbcoeff1; ++i) {
      posa1[i] = k1 - nocoeff1[i + 1] - 1;
      shifta1[i] = 1 << posa1[i];
    }

    if (J == 2) {
      k2 = nocoeff2[0];
      posa2 = new int[nbcoeff2];
      shifta2 = new int[nbcoeff2];
      for (int i = 0; i < nbcoeff2; ++i) {
        posa2[i] = k2 - nocoeff2[i + 1] - 1;
        shifta2[i] = 1 << posa2[i];
      }
    }

    if (k1 + k2 > 31) {
      System.err.println("The degree of the combined polynomials must be < 31");
      System.err.println("k1 = " + k1 + " k2 = " + k2);
      System.exit(1);
    }

    maskX1 = (1 << k1) - 1;
    maskX2 = (1 << k2) - 1;
    maskX1Shift = maskX1 << k2;
    maskX = (1 << (k1 + k2)) - 1;

    numBits = k1 + k2;
    normFactor = 1.0 / (1L << numBits);

    fillCyclesLFSR();
  }
  /**
   * Returns the best cut of a graph w.r.t. the degree of dissimilarity between points of different
   * partitions and the degree of similarity between points of the same partition.
   *
   * @param W the weight matrix of the graph
   * @return an array of two elements, each of these contains the points of a partition
   */
  protected static int[][] bestCut(DoubleMatrix2D W) {
    int n = W.columns();
    // Builds the diagonal matrices D and D^(-1/2) (represented as their diagonals)
    DoubleMatrix1D d = DoubleFactory1D.dense.make(n);
    DoubleMatrix1D d_minus_1_2 = DoubleFactory1D.dense.make(n);
    for (int i = 0; i < n; i++) {
      double d_i = W.viewRow(i).zSum();
      d.set(i, d_i);
      d_minus_1_2.set(i, 1 / Math.sqrt(d_i));
    }
    DoubleMatrix2D D = DoubleFactory2D.sparse.diagonal(d);

    // System.out.println("DoubleMatrix2D :\n"+D.toString());

    DoubleMatrix2D X = D.copy();

    // System.out.println("DoubleMatrix2D copy :\n"+X.toString());

    // X = D^(-1/2) * (D - W) * D^(-1/2)
    X.assign(W, Functions.minus);
    // System.out.println("DoubleMatrix2D X: (D-W) :\n"+X.toString());
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        X.set(i, j, X.get(i, j) * d_minus_1_2.get(i) * d_minus_1_2.get(j));

    // Computes the eigenvalues and the eigenvectors of X
    EigenvalueDecomposition e = new EigenvalueDecomposition(X);
    DoubleMatrix1D lambda = e.getRealEigenvalues();

    // Selects the eigenvector z_2 associated with the second smallest eigenvalue
    // Creates a map that contains the pairs <index, eigenvalue>
    AbstractIntDoubleMap map = new OpenIntDoubleHashMap(n);
    for (int i = 0; i < n; i++) map.put(i, Math.abs(lambda.get(i)));
    IntArrayList list = new IntArrayList();
    // Sorts the map on the value
    map.keysSortedByValue(list);
    // Gets the index of the second smallest element
    int i_2 = list.get(1);

    // y_2 = D^(-1/2) * z_2
    DoubleMatrix1D y_2 = e.getV().viewColumn(i_2).copy();
    y_2.assign(d_minus_1_2, Functions.mult);

    // Creates a map that contains the pairs <i, y_2[i]>
    map.clear();
    for (int i = 0; i < n; i++) map.put(i, y_2.get(i));
    // Sorts the map on the value
    map.keysSortedByValue(list);
    // Search the element in the map previuosly ordered that minimizes the cut
    // of the partition
    double best_cut = Double.POSITIVE_INFINITY;
    int[][] partition = new int[2][];

    // The array v contains all the elements of the graph ordered by their
    // projection on vector y_2
    int[] v = list.elements();
    // For each admissible splitting point i
    for (int i = 1; i < n; i++) {
      // The array a contains all the elements that have a projection on vector
      // y_2 less or equal to the one of i-th element
      // The array b contains the remaining elements
      int[] a = new int[i];
      int[] b = new int[n - i];
      System.arraycopy(v, 0, a, 0, i);
      System.arraycopy(v, i, b, 0, n - i);
      double cut = Ncut(W, a, b, v);
      if (cut < best_cut) {
        best_cut = cut;
        partition[0] = a;
        partition[1] = b;
      }
    }

    // System.out.println("Partition:");
    // UtilsJS.printMatrix(partition);

    return partition;
  }
 /**
  * Merges two sets of points represented as integer vectors. The sets are not overlapped.
  *
  * @param a the first set of points
  * @param b the second set of points
  * @return the union of the two sets
  */
 protected static int[] merge(int[] a, int[] b) {
   int[] v = new int[a.length + b.length];
   System.arraycopy(a, 0, v, 0, a.length);
   System.arraycopy(b, 0, v, a.length, b.length);
   return v;
 }
Exemplo n.º 5
0
  // Lit le fichier contenant les polynomes pour plusieurs generateurs.
  // no indique le choix du generateur dans le fichier.
  private void readFile(String fileName, int no) {
    BufferedReader input = null;
    try {
      if ((new File(fileName)).exists()) {
        input = new BufferedReader(new FileReader(fileName));
      } else {
        DataInputStream dataInput;
        dataInput =
            new DataInputStream(
                F2wStructure.class
                    .getClassLoader()
                    .getResourceAsStream("umontreal/ssj/hups/dataLFSR/" + fileName));
        input = new BufferedReader(new InputStreamReader(dataInput));
      }
    } catch (FileNotFoundException e) {
      System.err.println("File " + fileName + " not found" + PrintfFormat.NEWLINE);
      System.exit(1);
    }

    // La numerotation des generateurs debute a 1
    if (no < 1) no = 1;

    int[] numbers;

    // La premiere ligne valide est le nombre de polynomes
    String line = readOneLine(input);
    numbers = lineToNumbers(line);
    J = numbers[0];
    if (J != 1 && J != 2) {
      System.err.println(
          "Error: J = "
              + J
              + PrintfFormat.NEWLINE
              + "CycleBasedLFSR works only for the cases of one or two polynomials");
      System.exit(1);
    }

    // Nombre ne lignes qu'il faut sauter avant de lire les polynomes.
    int nbLines = (J + 1) * (no - 1);

    // On saute nbLines valides
    for (int i = 0; i < nbLines; i++) {
      line = readOneLine(input);
      if (line == null) {
        // Il n'y a aucune ligne qui correspond a no.
        System.err.println(
            "Error CycleBasedLFSR:"
                + PrintfFormat.NEWLINE
                + " no data in file "
                + fileName
                + " for "
                + no
                + "-th LFSR");
        System.exit(1);
      }
    }

    // Lecture des valeurs des J steps.
    line = readOneLine(input);
    if (line == null) {
      // Il n'y a aucune ligne qui correspond a no.
      System.err.println(
          "Error CycleBasedLFSR:"
              + PrintfFormat.NEWLINE
              + " no data in file "
              + fileName
              + " for "
              + no
              + "-th LFSR");
      System.exit(1);
    }
    numbers = lineToNumbers(line);

    // Lecture des J polynomes.
    if (J == 1) {
      step1 = numbers[0];
      line = readOneLine(input);
      nocoeff1 = lineToNumbers(line);
      nbcoeff1 = nocoeff1.length - 1;
    } else if (J == 2) {
      step1 = numbers[0];
      step2 = numbers[1];
      line = readOneLine(input);
      nocoeff1 = lineToNumbers(line);
      nbcoeff1 = nocoeff1.length - 1;
      line = readOneLine(input);
      nocoeff2 = lineToNumbers(line);
      nbcoeff2 = nocoeff2.length - 1;
    }
  }