private void swap(List y, List z, Random rdm) {
    int count = 0;
    for (int i = 0; i < y.size(); i++) {
      double p = rdm.nextDouble();
      if (p < 0.5) {
        Object t = y.get(i);
        y.set(i, z.get(i));
        z.set(i, t);
        count++;
      }
    } // end for i

    // logger.info("swapped " + count + " out of " + y.size());
  } // end swap
Esempio n. 2
0
    public void reduce(IntWritable key, Iterable<Text> values, Context context)
        throws IOException, InterruptedException {
      List<Double> newCentroid = null;
      int numPoints = 0;

      for (Text value : values) {
        ++numPoints;

        List<Double> p = GetPoint(value.toString());

        if (newCentroid == null) {
          // initialize the new centroid to the first element
          newCentroid = new ArrayList<Double>(p);
        } else {
          for (int i = 0; i < newCentroid.size(); i++) {
            newCentroid.set(i, newCentroid.get(i) + p.get(i));
          }
        }
      }

      // now the newCentroid contains the sum of all the points
      // so to get the average of all the points, we need to
      // divide each entry by the total number of points

      for (int i = 0; i < newCentroid.size(); i++) {
        newCentroid.set(i, newCentroid.get(i) / (double) numPoints);
      }

      // now create a string containing all the new centroid's coordinates

      String s = null;
      for (Double d : newCentroid) {
        if (s == null) {
          s = d.toString();
        } else {
          s += " " + d.toString();
        }
      }

      newCentroids.add(s);

      if (newCentroids.size() == 10) {
        WriteNewCentroids(context);
      }

      // output the centroid ID and the centroid data
      context.write(key, new Text(s));
    }
Esempio n. 3
0
  private void crossBorder() {
    if (isEnd) return;

    List<Block> blockTemp = new ArrayList<>(blocks);
    switch (inputDirection) {
      case 0:
        for (int i = 0; i < blockTemp.size(); i++)
          if (blockTemp.get(i).getY() >= 460) {
            Block temp = blockTemp.get(i);
            temp.setY(0);
            blockTemp.set(i, temp);
          }
        break;

      case 1:
        for (int i = 0; i < blockTemp.size(); i++)
          if (blockTemp.get(i).getX() >= 490) {
            Block temp = blockTemp.get(i);
            temp.setX(0);
            blockTemp.set(i, temp);
          }
        break;

      case 2:
        for (int i = 0; i < blockTemp.size(); i++)
          if (blockTemp.get(i).getY() <= 0) {
            Block temp = blockTemp.get(i);
            temp.setY(450);
            blockTemp.set(i, temp);
          }
        break;

      case 3:
        for (int i = 0; i < blockTemp.size(); i++)
          if (blockTemp.get(i).getX() <= 0) {
            Block temp = blockTemp.get(i);
            temp.setX(480);
            blockTemp.set(i, temp);
          }
        break;
    }

    Deque<Block> blocksTemp = new ArrayDeque<>();

    for (int i = blockTemp.size() - 1; i >= 0; i--) blocksTemp.push(blockTemp.get(i));

    blocks = blocksTemp;
  }
Esempio n. 4
0
 private synchronized void addRRset(Name name, RRset rrset) {
   if (!hasWild && name.isWild()) hasWild = true;
   Object types = data.get(name);
   if (types == null) {
     data.put(name, rrset);
     return;
   }
   int rtype = rrset.getType();
   if (types instanceof List) {
     List list = (List) types;
     for (int i = 0; i < list.size(); i++) {
       RRset set = (RRset) list.get(i);
       if (set.getType() == rtype) {
         list.set(i, rrset);
         return;
       }
     }
     list.add(rrset);
   } else {
     RRset set = (RRset) types;
     if (set.getType() == rtype) data.put(name, rrset);
     else {
       LinkedList list = new LinkedList();
       list.add(set);
       list.add(rrset);
       data.put(name, list);
     }
   }
 }
Esempio n. 5
0
 private ConditionalPermissionInfo setConditionalPermissionInfo(
     String name, ConditionInfo[] conds, PermissionInfo[] perms, boolean firstTry) {
   ConditionalPermissionUpdate update = newConditionalPermissionUpdate();
   List rows = update.getConditionalPermissionInfos();
   ConditionalPermissionInfo newInfo =
       newConditionalPermissionInfo(name, conds, perms, ConditionalPermissionInfo.ALLOW);
   int index = -1;
   if (name != null) {
     for (int i = 0; i < rows.size() && index < 0; i++) {
       ConditionalPermissionInfo info = (ConditionalPermissionInfo) rows.get(i);
       if (name.equals(info.getName())) {
         index = i;
       }
     }
   }
   if (index < 0) {
     // must always add to the beginning (bug 303930)
     rows.add(0, newInfo);
     index = 0;
   } else {
     rows.set(index, newInfo);
   }
   synchronized (lock) {
     if (!update.commit()) {
       if (firstTry)
         // try again
         setConditionalPermissionInfo(name, conds, perms, false);
     }
     return condAdminTable.getRow(index);
   }
 }
 public static void loadDescriptors(
     String pluginsPath,
     List<IdeaPluginDescriptorImpl> result,
     @Nullable StartupProgress progress,
     int pluginsCount) {
   final File pluginsHome = new File(pluginsPath);
   final File[] files = pluginsHome.listFiles();
   if (files != null) {
     int i = result.size();
     for (File file : files) {
       final IdeaPluginDescriptorImpl descriptor = loadDescriptor(file, PLUGIN_XML);
       if (descriptor == null) continue;
       if (progress != null) {
         progress.showProgress(
             descriptor.getName(), PLUGINS_PROGRESS_MAX_VALUE * ((float) ++i / pluginsCount));
       }
       int oldIndex = result.indexOf(descriptor);
       if (oldIndex >= 0) {
         final IdeaPluginDescriptorImpl oldDescriptor = result.get(oldIndex);
         if (StringUtil.compareVersionNumbers(oldDescriptor.getVersion(), descriptor.getVersion())
             < 0) {
           result.set(oldIndex, descriptor);
         }
       } else {
         result.add(descriptor);
       }
     }
   }
 }
 /** set throws an IndexOutOfBoundsException on a negative index */
 public void testSet1_IndexOutOfBoundsException() {
   try {
     List c = emptyArray();
     c.set(-1, "qwerty");
     shouldThrow();
   } catch (IndexOutOfBoundsException e) {
   }
 }
 /** set throws an IndexOutOfBoundsException on a too high index */
 public void testSet2() {
   try {
     List c = emptyArray();
     c.add("asdasd");
     c.add("asdad");
     c.set(100, "qwerty");
     shouldThrow();
   } catch (IndexOutOfBoundsException e) {
   }
 }
 private Character addEnumValue(T value, int position) {
   char ordinal = (char) position;
   if (ordinal != position)
     throw new IndexOutOfBoundsException(
         "Too many values in Enumerated16 field, try calling compact()");
   map.put(value, ordinal);
   if (ordinal == list.size()) list.add(ordinal, value);
   else list.set(ordinal, value);
   return ordinal;
 }
Esempio n. 10
0
 /**
  * Set the given index with the given element, if the SQLJSON object is a JSON array.
  *
  * @param index
  * @param object
  * @return
  */
 @Override
 public void set(int index, Object object) throws SQLException {
   if (!(jsonObject instanceof List)) {
     throw new SQLException("SQLJSON object is not a list");
   } else {
     // this is a hack
     List backingList = ((ValueList) jsonObject).list();
     //noinspection unchecked
     backingList.set(index, object);
   }
 }
Esempio n. 11
0
  // The Apriori Algorithm
  private static List<Itemset> apriori(List<Transaction> transactions, double minsup) {

    if (transactions.isEmpty()) {
      return null;
    }

    // Generate frequent 1-itemset
    List<Integer> occurancesOfItems = new ArrayList<>();
    for (int i = 0; i != transactions.get(0).getNumOfTotalItems(); ++i) {
      occurancesOfItems.add(0);
    }

    for (Transaction transaction : transactions) {
      for (int index : transaction.items()) {
        occurancesOfItems.set(index, occurancesOfItems.get(index) + 1);
      }
    }

    int numOfTransactions = transactions.size();
    List<Itemset> frequentOneItemsets = new ArrayList<>();
    for (int i = 0; i != occurancesOfItems.size(); ++i) {
      if ((double) occurancesOfItems.get(i) / numOfTransactions >= minsup) {
        Itemset itemset = new Itemset(i);
        frequentOneItemsets.add(itemset);
      }
    }

    // A list of itemsets to store the frequent k-itemsets, k = 1, 2, ...
    List<List<Itemset>> listOfFrequentItemsets = new ArrayList<List<Itemset>>();
    listOfFrequentItemsets.add(
        new ArrayList<Itemset>()); // case: k = 0, we fill it with an empty itemsets
    listOfFrequentItemsets.add(frequentOneItemsets); // case: k = 1

    List<Itemset> prunedCandidates;
    List<Itemset> frequentItemsets;
    int k = 1;
    while ((frequentItemsets = listOfFrequentItemsets.get(k)) != null) {
      List<Itemset> newCandidates = generateCandidates(frequentItemsets);
      prunedCandidates = prune(newCandidates, frequentItemsets);
      List<Itemset> candidates = determineFrequentItemsets(prunedCandidates, transactions, minsup);
      listOfFrequentItemsets.add(candidates);
      k++;
    }

    // Union the frequent itemsets as result
    List<Itemset> result = new ArrayList<>();
    for (List<Itemset> list : listOfFrequentItemsets) {
      if (list != null) {
        result.addAll(list);
      }
    }
    return result;
  }
Esempio n. 12
0
 protected void addOrReplace(ParsedMapping parsedMapping) {
   MappingKey key = new MappingKey(parsedMapping);
   for (int i = 0; i < mappings.size(); i++) {
     if (key.equals(new MappingKey(mappings.get(i)))) {
       logger.warn("{}\nis replaced with\n{}", mappings.get(i), parsedMapping);
       mappings.set(i, parsedMapping);
       return;
     }
   }
   logger.debug("Parsed {}", parsedMapping);
   mappings.add(parsedMapping);
 }
Esempio n. 13
0
File: Main.java Progetto: YpGu/gcoev
  /**
   * backward pass 2: update impression features (1) \hat{mu}' (mu_hat_prime_s) (2) \hat{V}'
   * (v_hat_prime_s)
   */
  public static void backward2(boolean update_grad) {
    double c = (1 - lambda) * (1 - lambda) / (sigma * sigma);
    for (int t1 = T - 1; t1 > t0; t1--) {
      int t = t1 - t0;
      //      System.out.println("backward 2;\tt = " + t1);
      if (t != T - 1 - t0) {
        double[][] mu_prime_pre_t = mu_prime_s.get(t - 1); // \mu'^{t-1}  [t-1]
        double[][] mu_hat_prime_t = mu_hat_prime_s.get(t); // \hat{\mu}'^{t}  [t-1]
        double V_prime_pre_t = v_prime_s.get(t - 1); // V'^{t-1}
        double V_hat_prime_t = v_hat_prime_s.get(t); // \hat{V}'{t}

        /* calculate \hat{\mu}' at time t-1 */
        double[][] mu_hat_prime_pre_t = new double[n][K];
        double factor_1 = (sigma * sigma) / (V_prime_pre_t + sigma * sigma);
        double factor_2 = (V_prime_pre_t) / (V_prime_pre_t + sigma * sigma);
        for (int i = 0; i < n; i++)
          for (int k = 0; k < K; k++) {
            mu_hat_prime_pre_t[i][k] =
                factor_1 * mu_prime_pre_t[i][k] + factor_2 * mu_hat_prime_t[i][k];
          }
        /* calculate \hat{V}' at time t-1 */
        double v_hat_prime_pre_t =
            V_prime_pre_t + factor_2 * factor_2 * (V_hat_prime_t - V_prime_pre_t - sigma * sigma);

        /* update \mu and V */
        mu_hat_prime_s.set(t - 1, mu_hat_prime_pre_t);
        v_hat_prime_s.set(t - 1, v_hat_prime_pre_t);

        /* calculate and update grad_mu_hat_prime at time t-1 */
        if (update_grad)
          for (int s = 0; s < T - t0; s++) {
            double[][] grad_mu_prime_pre_t_s = grad_mu_prime_s.get((t - 1) * (T - t0) + s);
            double[][] grad_mu_hat_prime_t_s = grad_mu_hat_prime_s.get(t * (T - t0) + s);
            double[][] grad_mu_hat_prime_pre_t_s = new double[n][K];
            for (int i = 0; i < n; i++)
              for (int k = 0; k < K; k++) {
                grad_mu_hat_prime_pre_t_s[i][k] =
                    factor_1 * grad_mu_prime_pre_t_s[i][k] + factor_2 * grad_mu_hat_prime_t_s[i][k];
              }
            grad_mu_hat_prime_s.set((t - 1) * (T - t0) + s, grad_mu_hat_prime_pre_t_s);
          }
      } else {
        /*
         * initial condition for backward pass:
         *  (1) \hat{\mu}'^{T} = \mu'^{T}
         *  (2) \hat{V}'^{T} = V'^{T}
         *  (3) \hat{grad_mu}'^{T/s} = grad_mu'^{T/s}, \forall s
         */
        mu_hat_prime_s.set(t, mu_prime_s.get(t));
        v_hat_prime_s.set(t, v_prime_s.get(t));
        if (update_grad)
          for (int s = 0; s < T - t0; s++) {
            grad_mu_hat_prime_s.set(t * (T - t0) + s, grad_mu_prime_s.get(t * (T - t0) + s));
          }
      }
      /* end for each t */
    }
  }
 public void compactEnd() {
   final int compactSize = compactIndexUsed.cardinality();
   if (compactSize == map.size()) {
     return;
   }
   for (int i = 1; i < list.size(); i++) {
     if (compactIndexUsed.get(i)) continue;
     // to be removed
     T t = list.get(i);
     list.set(i, null);
     map.remove(t);
     if (addPosition > i) addPosition = i;
   }
 }
Esempio n. 15
0
  /**
   * Load chromosomes from given ID or file name.
   *
   * @param idOrFile Genome ID or file name where chromosome lengths written
   * @return Chromosome lengths
   * @throws IOException if chromosome length file not found
   */
  public static List<Chromosome> loadChromosomes(String idOrFile) throws IOException {

    InputStream is = null;

    try {
      // Note: to get this to work, had to edit Intellij settings
      // so that "?*.sizes" are considered sources to be copied to class path
      is = HiCTools.class.getResourceAsStream(idOrFile + ".chrom.sizes");

      if (is == null) {
        // Not an ID,  see if its a file
        File file = new File(idOrFile);
        if (file.exists()) {
          is = new FileInputStream(file);
        } else {
          throw new FileNotFoundException("Could not find chromosome sizes file for: " + idOrFile);
        }
      }

      List<Chromosome> chromosomes = new ArrayList();
      chromosomes.add(0, null); // Index 0 reserved for "whole genome" psuedo-chromosome

      Pattern pattern = Pattern.compile("\t");
      BufferedReader reader = new BufferedReader(new InputStreamReader(is));
      String nextLine;
      long genomeLength = 0;
      int idx = 1;

      while ((nextLine = reader.readLine()) != null) {
        String[] tokens = pattern.split(nextLine);
        if (tokens.length == 2) {
          String name = tokens[0];
          int length = Integer.parseInt(tokens[1]);
          genomeLength += length;
          chromosomes.add(idx, new Chromosome(idx, name, length));
          idx++;
        } else {
          System.out.println("Skipping " + nextLine);
        }
      }

      // Add the "psuedo-chromosome" All, representing the whole genome.  Units are in kilo-bases
      chromosomes.set(0, new Chromosome(0, "All", (int) (genomeLength / 1000)));

      return chromosomes;
    } finally {
      if (is != null) is.close();
    }
  }
Esempio n. 16
0
  public void updateBean(AlbumBean newBean, AlbumBean oldBean) throws LibraryException {
    synchronized (libraryList) {
      // Find the bean
      int index = libraryList.indexOf(oldBean);
      if (index == -1) {
        LibraryException le = new LibraryException(Utils.getResourceString(ERROR_BEAN_NOT_FOUND));
        throw le;
      }

      // Set the new bean
      libraryList.set(index, newBean);
      setChanged();
      fireLibraryChangeEvent(
          new LibraryChangeEvent(this, new AlbumBean[] {oldBean}, LibraryChangeEvent.UPDATE));
    }
  }
  /** sublists contains elements at indexes offset from their base */
  public void testSubList() {
    List a = populatedArray(10);
    assertTrue(a.subList(1, 1).isEmpty());
    for (int j = 0; j < 9; ++j) {
      for (int i = j; i < 10; ++i) {
        List b = a.subList(j, i);
        for (int k = j; k < i; ++k) {
          assertEquals(new Integer(k), b.get(k - j));
        }
      }
    }

    List s = a.subList(2, 5);
    assertEquals(3, s.size());
    s.set(2, m1);
    assertEquals(a.get(4), m1);
    s.clear();
    assertEquals(7, a.size());
  }
 @Override
 protected void filterForLanguage(List<String> suggestions) {
   if (language.getShortNameWithCountryAndVariant().equals("de-CH")) {
     for (int i = 0; i < suggestions.size(); i++) {
       String s = suggestions.get(i);
       suggestions.set(i, s.replace("ß", "ss"));
     }
   }
   // This is not quite correct as it might remove valid suggestions that start with "-",
   // but without this we get too many strange suggestions that start with "-" for no apparent
   // reason
   // (e.g. for "Gratifikationskrisem" -> "-Gratifikationskrisen"):
   Iterator<String> iterator = suggestions.iterator();
   while (iterator.hasNext()) {
     String suggestion = iterator.next();
     if (suggestion.length() > 1 && suggestion.startsWith("-")) {
       iterator.remove();
     }
   }
 }
Esempio n. 19
0
 private static int readTokensFile(
     String tokensFile, List<String> tokenName, List<Integer> tokenType, List<String> nameOf)
     throws Exception {
   String line, name;
   String[] part;
   int nTokens = 0;
   int type, i;
   BufferedReader in = new BufferedReader(new FileReader(tokensFile));
   while ((line = in.readLine()) != null) {
     part = line.split("=");
     name = part[0];
     type = Integer.parseInt(part[1]);
     tokenName.add(name);
     tokenType.add(type);
     while (nameOf.size() <= type) {
       nameOf.add("NONE");
     }
     nameOf.set(type, name);
     nTokens++;
   }
   in.close();
   return nTokens;
 }
Esempio n. 20
0
  public static void main(String args[]) throws FileNotFoundException, IOException {
    System.out.println("Gathering data...");
    // Use char[] for ease in building strings, despite only using 8 bits.
    int numElements = 65536;
    char[] info = new char[numElements];
    for (int i = 0; i < info.length; i++) {
      if (i == '\n' || i == '\r') info[i] = NEWLINE_CODE;
      else if (i == ' ' || i == '\t' || i == '\f') info[i] = SPACE_CODE;
      else if (i < 128 && Character.isLowerCase((char) i)) info[i] = LOWER_CODE; // Ascii lower case
      else if (i < 128 && Character.isUpperCase((char) i)) info[i] = UPPER_CODE; // Ascii upper case
      else if (i < 128 && Character.isDigit((char) i)) info[i] = DIGIT_CODE; // Ascii digit
      else if (Character.isJavaIdentifierStart((char) i)) info[i] = OTHER_LETTER_CODE;
      else if (Character.isJavaIdentifierPart((char) i)) info[i] = OTHER_DIGIT_CODE;
      else {
        info[i] = BAD_CODE;
        numElements--;
      }
    }

    System.out.println("Compressing tables...");
    int bestShift = 0;
    int bestEst = info.length;
    String bestBlkStr = null;

    for (int i = 3; i < 11; i++) {
      int blkSize = 1 << i;
      Map blocks = new HashMap();
      List blkArray = new ArrayList();
      System.out.print("shift: " + i);

      for (int j = 0; j < info.length; j += blkSize) {
        String key = new String(info, j, blkSize);
        if (blocks.get(key) == null) {
          blkArray.add(key);
          blocks.put(key, new Integer(blkArray.size()));
        }
      }
      int blkNum = blkArray.size();
      int blockLen = blkNum * blkSize;
      System.out.print(" before " + blockLen);

      //
      // Try to pack blkArray, by finding successively smaller matches
      // between heads and tails of blocks.
      //
      for (int j = blkSize - 1; j > 0; j--) {
        Map tails = new HashMap();
        for (int k = 0; k < blkArray.size(); k++) {
          String str = (String) blkArray.get(k);
          if (str == null) continue;
          String tail = str.substring(str.length() - j);
          List l = (List) tails.get(tail);
          if (l == null) tails.put(tail, new LinkedList(Collections.singleton(new Integer(k))));
          else l.add(new Integer(k));
        }

        //
        // Now calculate the heads, and merge overlapping blocks
        //
        block:
        for (int k = 0; k < blkArray.size(); k++) {
          String tomerge = (String) blkArray.get(k);
          if (tomerge == null) continue;
          while (true) {
            String head = tomerge.substring(0, j);
            LinkedList entry = (LinkedList) tails.get(head);
            if (entry == null) continue block;
            Integer other = (Integer) entry.removeFirst();
            if (other.intValue() == k) {
              if (entry.size() > 0) {
                entry.add(other);
                other = (Integer) entry.removeFirst();
              } else {
                entry.add(other);
                continue block;
              }
            }
            if (entry.size() == 0) tails.remove(head);

            //
            // A match was found.
            //
            String merge = blkArray.get(other.intValue()) + tomerge.substring(j);
            blockLen -= j;
            blkNum--;
            if (other.intValue() < k) {
              blkArray.set(k, null);
              blkArray.set(other.intValue(), merge);
              String tail = merge.substring(merge.length() - j);
              List l = (List) tails.get(tail);
              Collections.replaceAll(l, new Integer(k), other);
              continue block;
            }
            blkArray.set(k, merge);
            blkArray.set(other.intValue(), null);
            tomerge = merge;
          }
        }
      }
      StringBuffer blockStr = new StringBuffer(blockLen);
      for (int k = 0; k < blkArray.size(); k++) {
        String str = (String) blkArray.get(k);
        if (str != null) blockStr.append(str);
      }
      if (blockStr.length() != blockLen) throw new Error("Unexpected blockLen " + blockLen);
      int estimate = blockLen + (info.length >> (i - 1));
      System.out.println(" after merge " + blockLen + ": " + estimate + " bytes");
      if (estimate < bestEst) {
        bestEst = estimate;
        bestShift = i;
        bestBlkStr = blockStr.toString();
      }
    }

    int blkSize = 1 << bestShift;
    char[] blocks = new char[info.length / blkSize];
    for (int j = 0; j < info.length; j += blkSize) {
      String key = new String(info, j, blkSize);
      int index = bestBlkStr.indexOf(key);
      if (index == -1) throw new Error("Unexpected index for " + j);
      blocks[j >> bestShift] = (char) (index - j);
    }

    //
    // Process the code.h file
    //
    System.out.println("Generating code.h with shift of " + bestShift);
    PrintStream hfile = new PrintStream(new FileOutputStream("code.h"));
    printHeader(hfile, new String[] {"\"platform.h\""});
    hfile.println("#ifndef code_INCLUDED");
    hfile.println("#define code_INCLUDED");
    hfile.println();
    hfile.println("class Code");
    hfile.println("{");
    hfile.println("    //");
    hfile.println("    // To facilitate the scanning, the character set is partitioned into");
    hfile.println("    // 8 categories using the array CODE. These are described below");
    hfile.println("    // together with some self-explanatory functions defined on CODE.");
    hfile.println("    //");
    hfile.println("    enum {");
    hfile.println("        SHIFT = " + bestShift + ",");
    hfile.println("        NEWLINE_CODE = " + NEWLINE_CODE + ',');
    hfile.println("        SPACE_CODE = " + SPACE_CODE + ',');
    hfile.println("        BAD_CODE = " + BAD_CODE + ',');
    hfile.println("        DIGIT_CODE = " + DIGIT_CODE + ',');
    hfile.println("        OTHER_DIGIT_CODE = " + OTHER_DIGIT_CODE + ',');
    hfile.println("        LOWER_CODE = " + LOWER_CODE + ',');
    hfile.println("        UPPER_CODE = " + UPPER_CODE + ',');
    hfile.println("        OTHER_LETTER_CODE = " + OTHER_LETTER_CODE);
    hfile.println("    };");
    hfile.println();
    hfile.println("    static char codes[" + bestBlkStr.length() + "];");
    hfile.println("    static u2 blocks[" + blocks.length + "];");
    hfile.println();
    hfile.println();
    hfile.println("public:");
    hfile.println();
    hfile.println("    static inline void SetBadCode(wchar_t c)");
    hfile.println("    {");
    hfile.println("        codes[(u2) (blocks[c >> SHIFT] + c)] = BAD_CODE;");
    hfile.println("    }");
    hfile.println();
    hfile.println("    static inline void CodeCheck(wchar_t c)");
    hfile.println("    {");
    hfile.println("        assert((u2) (blocks[c >> SHIFT] + c) < " + bestBlkStr.length() + ");");
    hfile.println("    }");
    hfile.println();
    hfile.println("    static inline bool CodeCheck(void)");
    hfile.println("    {");
    hfile.println("        for (int i = 0; i <= 0xffff; i++)");
    hfile.println("            CodeCheck((wchar_t) i);");
    hfile.println("        return true;");
    hfile.println("    }");
    hfile.println();
    hfile.println("    //");
    hfile.println("    // \\r characters are replaced by \\x0a in Stream::ProcessInput().");
    hfile.println("    //");
    hfile.println("    static inline bool IsNewline(wchar_t c)");
    hfile.println("    {");
    hfile.println("        return c == '\\x0a';");
    hfile.println("    }");
    hfile.println();
    hfile.println("    static inline bool IsSpaceButNotNewline(wchar_t c)");
    hfile.println("    {");
    hfile.println("        return codes[(u2) (blocks[c >> SHIFT] + c)] == SPACE_CODE;");
    hfile.println("    }");
    hfile.println();
    hfile.println("    static inline bool IsSpace(wchar_t c)");
    hfile.println("    {");
    hfile.println("        return codes[(u2) (blocks[c >> SHIFT] + c)] <= SPACE_CODE;");
    hfile.println("    }");
    hfile.println();
    hfile.println("    static inline bool IsDigit(wchar_t c)");
    hfile.println("    {");
    hfile.println("        return codes[(u2) (blocks[c >> SHIFT] + c)] == DIGIT_CODE;");
    hfile.println("    }");
    hfile.println();
    hfile.println("    static inline bool IsOctalDigit(wchar_t c)");
    hfile.println("    {");
    hfile.println("        return c >= U_0 && c <= U_7;");
    hfile.println("    }");
    hfile.println();
    hfile.println("    static inline bool IsHexDigit(wchar_t c)");
    hfile.println("    {");
    hfile.println("        return c <= U_f && (c >= U_a ||");
    hfile.println("                            (c >= U_A && c <= U_F) ||");
    hfile.println("                            (c >= U_0 && c <= U_9));");
    hfile.println("    }");
    hfile.println();
    hfile.println("    static inline bool IsUpper(wchar_t c)");
    hfile.println("    {");
    hfile.println("        return codes[(u2) (blocks[c >> SHIFT] + c)] == UPPER_CODE;");
    hfile.println("    }");
    hfile.println();
    hfile.println("    static inline bool IsLower(wchar_t c)");
    hfile.println("    {");
    hfile.println("        return codes[(u2) (blocks[c >> SHIFT] + c)] == LOWER_CODE;");
    hfile.println("    }");
    hfile.println();
    hfile.println("    static inline bool IsAlpha(wchar_t c)");
    hfile.println("    {");
    hfile.println("        return codes[(u2) (blocks[c >> SHIFT] + c)] >= LOWER_CODE;");
    hfile.println("    }");
    hfile.println();
    hfile.println("    static inline bool IsAlnum(wchar_t c)");
    hfile.println("    {");
    hfile.println("        return codes[(u2) (blocks[c >> SHIFT] + c)] >= DIGIT_CODE;");
    hfile.println("    }");
    hfile.println();
    hfile.println("};");
    hfile.println();
    hfile.println("#endif // code_INCLUDED");
    printFooter(hfile);
    hfile.close();

    //
    // Process the code.cpp file
    //
    System.out.println("Generating code.cpp");
    PrintStream cfile = new PrintStream(new FileOutputStream("code.cpp"));
    printHeader(cfile, new String[] {"\"code.h\""});
    cfile.println("char Code::codes[" + bestBlkStr.length() + "] =");
    cfile.println("{");
    for (int j = 0; j < bestBlkStr.length(); j += 4) {
      for (int k = 0; k < 4; k++) {
        if (k + j >= bestBlkStr.length()) break;
        if (k == 0) cfile.print("   ");
        cfile.print(" " + CODE_NAMES[bestBlkStr.charAt(k + j)] + ",");
      }
      cfile.println();
    }
    cfile.println("};");
    cfile.println();
    cfile.println();
    cfile.println("//");
    cfile.println("// The Blocks vector:");
    cfile.println("//");
    cfile.println("u2 Code::blocks[" + blocks.length + "] =");
    cfile.println("{");
    for (int k = 0; k < blocks.length; k += 9) {
      for (int i = 0; i < 9; i++) {
        if (k + i >= blocks.length) break;
        if (i == 0) cfile.print("   ");
        cfile.print(" 0x" + Integer.toHexString(blocks[k + i]) + ",");
      }
      cfile.println();
    }
    cfile.println("};");
    printFooter(cfile);
    cfile.close();

    //
    // Print statistics.
    //
    System.out.println(
        "Total static storage utilization is " + blocks.length * 2 + " bytes for block lookup");
    System.out.println("   plus " + bestBlkStr.length() + " bytes for the encodings");
    System.out.println(
        "The number of unicode characters legal in Java sourcecode is " + numElements);
  }
Esempio n. 21
0
  public static void main(String[] argv)
      throws IOException, CmdLineParser.UnknownOptionException,
          CmdLineParser.IllegalOptionValueException {

    if (argv.length < 4) {
      System.out.println("Usage: hictools pre <options> <inputFile> <outputFile> <genomeID>");
      System.out.println("  <options>: -d only calculate intra chromosome (diagonal) [false]");
      System.out.println(
          "           : -o calculate densities (observed/expected), write to file [false]");
      System.out.println("           : -t <int> only write cells with count above threshold t [0]");
      System.out.println(
          "           : -c <chromosome ID> only calculate map on specific chromosome");
      System.exit(0);
    }

    Globals.setHeadless(true);

    CommandLineParser parser = new CommandLineParser();
    parser.parse(argv);
    String[] args = parser.getRemainingArgs();

    if (args[0].equals("sort")) {
      AlignmentsSorter.sort(args[1], args[2], null);
    } else if (args[0].equals("pairsToBin")) {
      String ifile = args[1];
      String ofile = args[2];
      String genomeId = args[3];
      List<Chromosome> chromosomes = loadChromosomes(genomeId);
      AsciiToBinConverter.convert(ifile, ofile, chromosomes);
    } else if (args[0].equals("binToPairs")) {
      String ifile = args[1];
      String ofile = args[2];
      AsciiToBinConverter.convertBack(ifile, ofile);
    } else if (args[0].equals("printmatrix")) {
      if (args.length < 5) {
        System.err.println(
            "Usage: hictools printmatrix <observed/oe/pearson> hicFile chr1 chr2 binsize");
        System.exit(-1);
      }
      String type = args[1];
      String file = args[2];
      String chr1 = args[3];
      String chr2 = args[4];
      String binSizeSt = args[5];
      int binSize = 0;
      try {
        binSize = Integer.parseInt(binSizeSt);
      } catch (NumberFormatException e) {
        System.err.println("Integer expected.  Found: " + binSizeSt);
        System.exit(-1);
      }

      dumpMatrix(file, chr1, chr2, binSize, type);

    } else if (args[0].equals("eigenvector")) {
      if (args.length < 4) {
        System.err.println("Usage: hictools eigenvector hicFile chr binsize");
      }
      String file = args[1];
      String chr = args[2];
      String binSizeSt = args[3];
      int binSize = 0;
      try {
        binSize = Integer.parseInt(binSizeSt);
      } catch (NumberFormatException e) {
        System.err.println("Integer expected.  Found: " + binSizeSt);
        System.exit(-1);
      }
      calculateEigenvector(file, chr, binSize);
    } else if (args[0].equals("pre")) {
      String genomeId = "";
      try {
        genomeId = args[3];
      } catch (ArrayIndexOutOfBoundsException e) {
        System.err.println("No genome ID given");
        System.exit(0);
      }
      List<Chromosome> chromosomes = loadChromosomes(genomeId);

      long genomeLength = 0;
      for (Chromosome c : chromosomes) {
        if (c != null) genomeLength += c.getSize();
      }
      chromosomes.set(0, new Chromosome(0, "All", (int) (genomeLength / 1000)));

      String[] tokens = args[1].split(",");
      List<String> files = new ArrayList<String>(tokens.length);

      for (String f : tokens) {
        files.add(f);
      }

      Preprocessor preprocessor = new Preprocessor(new File(args[2]), chromosomes);

      preprocessor.setIncludedChromosomes(parser.getChromosomeOption());
      preprocessor.setCountThreshold(parser.getCountThresholdOption());
      preprocessor.setNumberOfThreads(parser.getThreadedOption());
      preprocessor.setDiagonalsOnly(parser.getDiagonalsOption());
      preprocessor.setLoadDensities(parser.getDensitiesOption());
      preprocessor.preprocess(files);
    }
  }
Esempio n. 22
0
File: Main.java Progetto: YpGu/gcoev
  /**
   * forward pass 1: update intrinsic features (1) mu (mu_s) (2) grad_mu (grad_mu_s) (3) variance V
   * (v_s)
   */
  public static void forward1(boolean update_grad, int iter) {
    /*
       if (iter == 4) {
         int t = 15;
         double[][] h_t = new double[n][K];
         double[][] h_hat_t = new double[n][K];
         double[][] h_prime_t = new double[n][K];
         double[][] h_hat_prime_t = new double[n][K];
         for (int i = 0; i < n; i++) for (int k = 0; k < K; k++) {
    h_t[i][k] = h_s.get(t-1)[i][k];
    h_hat_t[i][k] = h_hat_s.get(t-1)[i][k];
    h_prime_t[i][k] = h_prime_s.get(t-1)[i][k];
    h_hat_prime_t[i][k] = h_hat_prime_s.get(t-1)[i][k];
         }
         h_s.set(t, h_t); h_hat_s.set(t, h_hat_t);
         h_prime_s.set(t, h_prime_t); h_hat_prime_s.set(t, h_hat_prime_t);
       }
       */

    for (int t = 0; t < T - t0; t++) {
      //      System.out.println("forward 1;\tt = " + t1);
      if (t != 0) {
        double delta_t = delta_s.get(t); // delta_t
        double[][] h_hat_t = h_hat_s.get(t); // \hat{h}^t  [t]
        double[][] mu_pre_t = mu_s.get(t - 1); // mu^{t-1} (N*1)
        double V_pre_t = v_s.get(t - 1); // V^{t-1}

        Matrix a = new Matrix(AS.get(t - 1)); // A^{t-1}
        Matrix hprime_pre_t = new Matrix(h_prime_s.get(t - 1)); // h'^{t-1}
        Matrix ave_neighbors = a.times(hprime_pre_t);

        /* calculate \mu */
        double[][] mu_t = new double[n][K];
        double factor_1 =
            (delta_t * delta_t)
                / (delta_t * delta_t + sigma * sigma + (1 - lambda) * (1 - lambda) * V_pre_t);
        double factor_2 =
            (sigma * sigma + (1 - lambda) * (1 - lambda) * V_pre_t)
                / (delta_t * delta_t + sigma * sigma + (1 - lambda) * (1 - lambda) * V_pre_t);
        for (int i = 0; i < n; i++)
          for (int k = 0; k < K; k++) {
            mu_t[i][k] =
                factor_1 * ((1 - lambda) * mu_pre_t[i][k] + lambda * ave_neighbors.get(i, k))
                    + factor_2 * h_hat_t[i][k];
          }
        /* calculate V */
        double V_t = factor_2 * delta_t * delta_t;

        /* update \mu and V */
        mu_s.set(t, mu_t);
        v_s.set(t, V_t);

        /* calculate and update grad_mu */
        if (update_grad)
          for (int s = 0; s < T - t0; s++) {
            double[][] grad_pre_t_s = grad_mu_s.get((t - 1) * (T - t0) + s);
            double[][] grad_t_s = new double[n][K];
            for (int i = 0; i < n; i++)
              for (int k = 0; k < K; k++) {
                grad_t_s[i][k] = factor_1 * (1 - lambda) * grad_pre_t_s[i][k];
                if (t == s) {
                  grad_t_s[i][k] += factor_2;
                }
              }
            grad_mu_s.set(t * (T - t0) + s, grad_t_s);
          }
      } else {
        /* mu, V: random init (keep unchanged) */
        /* grad_mu: set to 0 (keep unchanged) */
      }
      //      Scanner sc = new Scanner(System.in);
      //      int gu; gu = sc.nextInt();
      /* end for each t */
    }
  }
Esempio n. 23
0
  /**
   * Performs flush.
   *
   * @throws GridException If failed.
   */
  private void doFlush() throws GridException {
    lastFlushTime = U.currentTimeMillis();

    List<GridFuture> activeFuts0 = null;

    int doneCnt = 0;

    for (GridFuture<?> f : activeFuts) {
      if (!f.isDone()) {
        if (activeFuts0 == null) activeFuts0 = new ArrayList<>((int) (activeFuts.size() * 1.2));

        activeFuts0.add(f);
      } else {
        f.get();

        doneCnt++;
      }
    }

    if (activeFuts0 == null || activeFuts0.isEmpty()) return;

    while (true) {
      Queue<GridFuture<?>> q = null;

      for (Buffer buf : bufMappings.values()) {
        GridFuture<?> flushFut = buf.flush();

        if (flushFut != null) {
          if (q == null) q = new ArrayDeque<>(bufMappings.size() * 2);

          q.add(flushFut);
        }
      }

      if (q != null) {
        assert !q.isEmpty();

        boolean err = false;

        for (GridFuture fut = q.poll(); fut != null; fut = q.poll()) {
          try {
            fut.get();
          } catch (GridException e) {
            if (log.isDebugEnabled()) log.debug("Failed to flush buffer: " + e);

            err = true;
          }
        }

        if (err)
          // Remaps needed - flush buffers.
          continue;
      }

      doneCnt = 0;

      for (int i = 0; i < activeFuts0.size(); i++) {
        GridFuture f = activeFuts0.get(i);

        if (f == null) doneCnt++;
        else if (f.isDone()) {
          f.get();

          doneCnt++;

          activeFuts0.set(i, null);
        } else break;
      }

      if (doneCnt == activeFuts0.size()) return;
    }
  }
Esempio n. 24
0
  /**
   * Compile with avr-gcc.
   *
   * @param sketch Sketch object to be compiled.
   * @param buildPath Where the temporary files live and will be built from.
   * @param primaryClassName the name of the combined sketch file w/ extension
   * @return true if successful.
   * @throws RunnerException Only if there's a problem. Only then.
   * 
  * [ROBOTIS]Changed prototype to support ARM Cortex-M3 based CM-900 Pandora project
  * 2012-09-26 [email protected]
  * */
  public boolean compile(Sketch sketch, //change return type[ROBOTIS]
                         String buildPath,
                         String primaryClassName,
                         boolean verbose,
                         List<String> ignored) throws RunnerException {
    this.sketch = sketch;
    this.buildPath = buildPath;
    this.primaryClassName = primaryClassName; //예를 들면 cpp파일로 변환된 AnalogReadSerial.cpp
    this.verbose = verbose;
    this.sketchIsCompiled = false;
    System.out.println("Compiler.compile() sketch ="+sketch.getName()+"buildpath ="+buildPath+"primaryClassName ="+primaryClassName);
    // the pms object isn't used for anything but storage
    MessageStream pms = new MessageStream(this);

    String avrBasePath = Base.getAvrBasePath();
    System.out.println("[ROBOTIS]avrBasePath ="+avrBasePath);
    Map<String, String> boardPreferences = Base.getBoardPreferences();
    String core = boardPreferences.get("build.core");
    System.out.println("[ROBOTIS]build.core ="+core);
    if (core == null) {
    	RunnerException re = new RunnerException(_("No board selected; please choose a board from the Tools > Board menu."));
      re.hideStackTrace();
      throw re;
    }
    String corePath;
    
    if (core.indexOf(':') == -1) {
      Target t = Base.getTarget();
      File coreFolder = new File(new File(t.getFolder(), "cores"), core);
      corePath = coreFolder.getAbsolutePath();
      
    } else {
      Target t = Base.targetsTable.get(core.substring(0, core.indexOf(':')));
      File coreFolder = new File(t.getFolder(), "cores");
      coreFolder = new File(coreFolder, core.substring(core.indexOf(':') + 1));
      corePath = coreFolder.getAbsolutePath();
    }

    System.out.println("[ROBOTIS]corePath ="+corePath);
    
    String variant = boardPreferences.get("build.variant");
    String variantPath = null;
    
    if (variant != null) {
      if (variant.indexOf(':') == -1) {
	Target t = Base.getTarget();
	File variantFolder = new File(new File(t.getFolder(), "variants"), variant);
	variantPath = variantFolder.getAbsolutePath();
      } else {
	Target t = Base.targetsTable.get(variant.substring(0, variant.indexOf(':')));
	File variantFolder = new File(t.getFolder(), "variants");
	variantFolder = new File(variantFolder, variant.substring(variant.indexOf(':') + 1));
	variantPath = variantFolder.getAbsolutePath();
      }
    }

    List<File> objectFiles = new ArrayList<File>();

   // 0. include paths for core + all libraries

   sketch.setCompilingProgress(20);
   List includePaths = new ArrayList();
   includePaths.add(corePath);
   if (variantPath != null) includePaths.add(variantPath);
   for (File file : sketch.getImportedLibraries()) {
     includePaths.add(file.getPath());
   }

   // 1. compile the sketch (already in the buildPath)

   sketch.setCompilingProgress(30);
   objectFiles.addAll(
     compileFiles(avrBasePath, buildPath, includePaths,
               findFilesInPath(buildPath, "S", false),
               findFilesInPath(buildPath, "c", false),
               findFilesInPath(buildPath, "cpp", false),
               boardPreferences));
   sketchIsCompiled = true;

   // 2. compile the libraries, outputting .o files to: <buildPath>/<library>/

   sketch.setCompilingProgress(40);
   for (File libraryFolder : sketch.getImportedLibraries()) {
     File outputFolder = new File(buildPath, libraryFolder.getName());
     File utilityFolder = new File(libraryFolder, "utility");
     createFolder(outputFolder);
     // this library can use includes in its utility/ folder
     includePaths.add(utilityFolder.getAbsolutePath());
     objectFiles.addAll(
       compileFiles(avrBasePath, outputFolder.getAbsolutePath(), includePaths,
               findFilesInFolder(libraryFolder, "S", false),
               findFilesInFolder(libraryFolder, "c", false),
               findFilesInFolder(libraryFolder, "cpp", false),
               boardPreferences));
     outputFolder = new File(outputFolder, "utility");
     createFolder(outputFolder);
     objectFiles.addAll(
       compileFiles(avrBasePath, outputFolder.getAbsolutePath(), includePaths,
               findFilesInFolder(utilityFolder, "S", false),
               findFilesInFolder(utilityFolder, "c", false),
               findFilesInFolder(utilityFolder, "cpp", false),
               boardPreferences));
     // other libraries should not see this library's utility/ folder
     includePaths.remove(includePaths.size() - 1);
   }

   // 3. compile the core, outputting .o files to <buildPath> and then
   // collecting them into the core.a library file.

   sketch.setCompilingProgress(50);
  includePaths.clear();
  includePaths.add(corePath);  // include path for core only
  if (variantPath != null) includePaths.add(variantPath);
  List<File> coreObjectFiles =
    compileFiles(avrBasePath, buildPath, includePaths,
              findFilesInPath(corePath, "S", true),
              findFilesInPath(corePath, "c", true),
              findFilesInPath(corePath, "cpp", true),
              boardPreferences);

   String runtimeLibraryName = buildPath + File.separator + "core.a";
   List baseCommandAR = new ArrayList(Arrays.asList(new String[] {
     avrBasePath + "avr-ar",
     "rcs",
     runtimeLibraryName
   }));
   for(File file : coreObjectFiles) {
     List commandAR = new ArrayList(baseCommandAR);
     commandAR.add(file.getAbsolutePath());
     execAsynchronously(commandAR);
   }

    // 4. link it all together into the .elf file
    // For atmega2560, need --relax linker option to link larger
    // programs correctly.
    String optRelax = "";
    String atmega2560 = new String ("atmega2560");
    if ( atmega2560.equals(boardPreferences.get("build.mcu")) ) {
        optRelax = new String(",--relax");
    }
   sketch.setCompilingProgress(60);
    List baseCommandLinker = new ArrayList(Arrays.asList(new String[] {
      avrBasePath + "avr-gcc",
      "-Os",
      "-Wl,--gc-sections"+optRelax,
      "-mmcu=" + boardPreferences.get("build.mcu"),
      "-o",
      buildPath + File.separator + primaryClassName + ".elf"
    }));

    for (File file : objectFiles) {
      baseCommandLinker.add(file.getAbsolutePath());
    }

    baseCommandLinker.add(runtimeLibraryName);
    baseCommandLinker.add("-L" + buildPath);
    baseCommandLinker.add("-lm");

    execAsynchronously(baseCommandLinker);

    List baseCommandObjcopy = new ArrayList(Arrays.asList(new String[] {
      avrBasePath + "avr-objcopy",
      "-O",
      "-R",
    }));
    
    List commandObjcopy;

    // 5. extract EEPROM data (from EEMEM directive) to .eep file.
    sketch.setCompilingProgress(70);
    commandObjcopy = new ArrayList(baseCommandObjcopy);
    commandObjcopy.add(2, "ihex");
    commandObjcopy.set(3, "-j");
    commandObjcopy.add(".eeprom");
    commandObjcopy.add("--set-section-flags=.eeprom=alloc,load");
    commandObjcopy.add("--no-change-warnings");
    commandObjcopy.add("--change-section-lma");
    commandObjcopy.add(".eeprom=0");
    commandObjcopy.add(buildPath + File.separator + primaryClassName + ".elf");
    commandObjcopy.add(buildPath + File.separator + primaryClassName + ".eep");
    execAsynchronously(commandObjcopy);
    
    // 6. build the .hex file
    sketch.setCompilingProgress(80);
    commandObjcopy = new ArrayList(baseCommandObjcopy);
    commandObjcopy.add(2, "ihex");
    commandObjcopy.add(".eeprom"); // remove eeprom data
    commandObjcopy.add(buildPath + File.separator + primaryClassName + ".elf");
    commandObjcopy.add(buildPath + File.separator + primaryClassName + ".hex");
    execAsynchronously(commandObjcopy);
    
    sketch.setCompilingProgress(90);
   
    return true;
  }
Esempio n. 25
0
File: Main.java Progetto: YpGu/gcoev
  public static void test1(String seed) {
    /* read, init data & parameters */
    for (int t = t0; t < T; t++) {
      //      String fileDir = "../../data/graph/" + Integer.toString(t) + ".csv";  // original
      // co-voting dataset
      //      String fileDir = "./data/" + Integer.toString(t) + ".csv";  // artificial toy dataset
      String fileDir =
          "../../data_sm/nips_17/out/"
              + seed
              + "/"
              + Integer.toString(t)
              + ".train.csv"; // nips dataset (smaller)
      Map<Integer, Double> freq = FileParser.readCSVDict(fileDir);

      double[][] G = new double[n][n];
      double[][] A = new double[n][n];
      double[][] mu = new double[n][K];
      double[][] mu_hat = new double[n][K];
      double[][] mu_prime = new double[n][K];
      double[][] mu_hat_prime = new double[n][K];
      double[][] h = new double[n][K];
      double[][] h_hat = new double[n][K];
      FileParser.readCSVGraph(fileDir, freq, G, A);
      for (int i = 0; i < n; i++)
        for (int k = 0; k < K; k++) {
          mu[i][k] = scale_0 * (rand.nextDouble() - 0.5);
          mu_hat[i][k] = scale_0 * (rand.nextDouble() - 0.5);
          mu_prime[i][k] = mu[i][k];
          mu_hat_prime[i][k] = mu_hat[i][k];
          h[i][k] = scale * (rand.nextDouble() - 0.5);
          h_hat[i][k] = scale * (rand.nextDouble() - 0.5);
        }

      GS.add(G);
      AS.add(A);
      mu_s.add(mu);
      mu_hat_s.add(mu_hat);
      mu_prime_s.add(mu_prime);
      mu_hat_prime_s.add(mu_hat_prime);
      h_s.add(h);
      h_prime_s.add(h);
      h_hat_s.add(h_hat);
      h_hat_prime_s.add(h);

      /* for test */
      delta_s.add(delta);
      delta_prime_s.add(delta); // TODO previous: 0.1

      v_s.add(0.1);
      v_hat_s.add(0.1);
      v_prime_s.add(0.1);
      v_hat_prime_s.add(0.1);

      System.out.println("done! t = " + t);
    }

    for (int t = t0; t < T; t++) {
      for (int s = t0; s < T; s++) {
        grad_mu_s.add(new double[n][K]);
        grad_mu_hat_s.add(new double[n][K]);
        grad_mu_prime_s.add(new double[n][K]);
        grad_mu_hat_prime_s.add(new double[n][K]);
      }
      grad_h_hat_s.add(new double[n][K]);
      grad_h_hat_prime_s.add(new double[n][K]);
    }
    /* end initialization */

    /* outer for-loop */
    double old_obj_1 = -1, old_obj_2 = -1;
    for (int iter = 0; iter < MAX_ITER; iter++) {
      //      Scanner sc = new Scanner(System.in); int gu; gu = sc.nextInt();
      System.out.println("====== iter = " + iter + " ======");
      /** intrinsic feature * */
      forward1(true, iter);
      backward1(true);
      compute_gradient1(iter);
      double new_obj_1 = 0;
      /* gradient descent: inner for-loop here */
      int inner_iter_1 = 0;
      while (inner_iter_1 < INNER_ITER) {
        /* update variational parameters \hat{h} using gradient descent */
        for (int t = 0; t < T - t0; t++) {
          double[][] h_hat_t = h_hat_s.get(t);
          double[][] grad_h_hat_t = grad_h_hat_s.get(t);
          for (int i = 0; i < n; i++)
            for (int k = 0; k < K; k++) {
              h_hat_t[i][k] += lr_1 * grad_h_hat_t[i][k];
            }
          h_hat_s.set(t, h_hat_t);
        }
        /* update \hat{\mu} and \hat{V}, since both are function of \hat{h} */
        forward1(false, iter);
        backward1(false);
        double obj1 = compute_objective1();
        if (inner_iter_1 % 10 == 0)
          System.out.println("(1) iter = " + inner_iter_1 + ", obj 1 = " + obj1);
        if (inner_iter_1 != 0 && obj1 < new_obj_1) {
          lr_1 *= 0.8;
          break;
        }
        new_obj_1 = obj1;
        inner_iter_1 += 1;
      }
      if (inner_iter_1 == INNER_ITER) lr_1 *= 2;
      /* sample */
      for (int t = 0; t < T - t0; t++) {
        double[][] samples =
            Operations.sample_multivariate_normal(mu_hat_s.get(t), v_hat_s.get(t), N_SAMPLES);
        double[][] h_t = new double[n][K];
        for (int i = 0; i < n; i++)
          for (int k = 0; k < K; k++) {
            h_t[i][k] = samples[i][k];
          }
        h_s.set(t, h_t);
      }

      /** impression feature * */
      forward2(true);
      backward2(true);
      compute_gradient2(iter);
      double new_obj_2 = 0;
      /* gradient descent: inner for-loop here */
      int inner_iter_2 = 0;
      while (inner_iter_2 < INNER_ITER) {
        /* update \hat{h}' using gradient descent */
        for (int t = 0; t < T - t0; t++) {
          double[][] h_hat_prime_t = h_hat_prime_s.get(t);
          double[][] grad_h_hat_prime_t = grad_h_hat_prime_s.get(t);
          for (int i = 0; i < n; i++)
            for (int k = 0; k < K; k++) {
              h_hat_prime_t[i][k] += lr_2 * grad_h_hat_prime_t[i][k];
            }
          h_hat_prime_s.set(t, h_hat_prime_t);
        }
        /* update \hat{\mu}' and \hat{V}', since both are function of \hat{h}' */
        forward2(false);
        backward2(false);
        double obj2 = compute_objective2();
        if (inner_iter_2 % 10 == 0)
          System.out.println("(2) iter = " + inner_iter_2 + ", obj 2 = " + obj2);
        if (inner_iter_2 != 0 && obj2 < new_obj_2) {
          lr_2 *= 0.8;
          break;
        }
        new_obj_2 = obj2;
        inner_iter_2 += 1;
      }
      if (inner_iter_2 == INNER_ITER) lr_2 *= 2;
      /* sample */
      for (int t = 0; t < T - t0; t++) {
        double[][] samples =
            Operations.sample_multivariate_normal(
                mu_hat_prime_s.get(t), v_hat_prime_s.get(t), N_SAMPLES);
        double[][] h_prime_t = new double[n][K];
        for (int i = 0; i < n; i++)
          for (int k = 0; k < K; k++) {
            h_prime_t[i][k] = samples[i][k];
          }
        h_prime_s.set(t, h_prime_t);
      }

      /** output * */
      for (int t = 0; t < T - t0; t++) {
        double[][] h_t = h_s.get(t);
        double[][] h_prime_t = h_prime_s.get(t);
        /* output filename:
         *    ./res/<seed>_<sigma>/h_<time>_<iter>.txt
         */
        FileParser.output(
            h_t, "./res/" + seed + "_" + delta_str + "/h_" + (t + t0) + "_" + iter + ".txt");
        FileParser.output(
            h_prime_t,
            "./res/" + seed + "_" + delta_str + "/h_p_" + (t + t0) + "_" + iter + ".txt");
      }

      /* check convergence */
      double diff_1 = -(new_obj_1 - old_obj_1) / old_obj_1;
      double diff_2 = -(new_obj_2 - old_obj_2) / old_obj_2;
      if (iter != 0 && diff_1 < 1e-6 && diff_2 < 1e-6) {
        System.out.println("diff_1 = " + diff_1);
        System.out.println("diff_2 = " + diff_2);
        break;
      }
      old_obj_1 = new_obj_1;
      old_obj_2 = new_obj_2;
    }
  }
Esempio n. 26
0
File: Main.java Progetto: YpGu/gcoev
  /** forward pass 2: update impression features (1) mu' (mu_prime_s) (2) variance V' (v_prime_s) */
  public static void forward2(boolean update_grad) {
    for (int t1 = t0; t1 < T; t1++) {
      int t = t1 - t0;
      //      System.out.println("forward 2;\tt = " + t1);
      if (t != 0) {
        double delta_t_prime = delta_prime_s.get(t); // delta'_t
        double[][] mu_prime_pre_t = mu_prime_s.get(t - 1); // mu'^{t-1}  [t]
        double[][] h_hat_prime_t = h_hat_prime_s.get(t); // \hat{h}'^{t} (n*1)
        double V_prime_pre_t = v_prime_s.get(t - 1); // V'^{t-1} [t]

        /* calculate \mu */
        double[][] mu_prime_t = new double[n][K];
        double factor_1 =
            (delta_t_prime * delta_t_prime)
                / (delta_t_prime * delta_t_prime + sigma * sigma + V_prime_pre_t);
        double factor_2 =
            (sigma * sigma + V_prime_pre_t)
                / (delta_t_prime * delta_t_prime + sigma * sigma + V_prime_pre_t);
        for (int i = 0; i < n; i++)
          for (int k = 0; k < K; k++) {
            mu_prime_t[i][k] = factor_1 * mu_prime_pre_t[i][k] + factor_2 * h_hat_prime_t[i][k];
          }
        /* calculate V */
        double v_prime_t = factor_2 * delta_t_prime * delta_t_prime;

        /* update */
        mu_prime_s.set(t, mu_prime_t);
        v_prime_s.set(t, v_prime_t);

        /* calculate and update grad_mu_prime */
        if (update_grad)
          for (int s = 0; s < T - t0; s++) {
            double[][] grad_mu_prime_pre_t_s = grad_mu_prime_s.get((t - 1) * (T - t0) + s);
            double[][] grad_mu_prime_t_s = new double[n][K];
            for (int i = 0; i < n; i++)
              for (int k = 0; k < K; k++) {
                grad_mu_prime_t_s[i][k] = factor_1 * grad_mu_prime_pre_t_s[i][k];
                if (s == t) {
                  grad_mu_prime_t_s[i][k] += factor_2;
                }
              }
            grad_mu_prime_s.set(t * (T - t0) + s, grad_mu_prime_t_s);
          }
      } else {
        /* for \mu'_i: ignore the first term in the summation */
        /* for V':
         *    option 1: use v_prime_init for all i
         *    option 2: ignore v_prime_{t-1}
         */
        /*
        double delta_t_prime = delta_prime_s.get(t);	      // delta'_t
        double[][] h_hat_prime_t = h_prime_s.get(t);   // \hat{h}'^{t} (n*1)
        */
        /* calculate new round of parameters */
        /*
        double[] mu_prime_t = new double[n];
        for (int i = 0; i < n; i++) {
          double c = (sigma*sigma) / (sigma*sigma + delta_t_prime*delta_t_prime);
          mu_prime_t[i] = c * h_hat_prime_t[i][0];
        }
        double v_prime_t = v_prime_init;
        */
        /* update */
        /*
        mu_prime_s.set(t, mu_prime_t);
        v_prime_s.set(t, v_prime_t);
        */
      }
      /* end for each t */
    }
  }
Esempio n. 27
0
File: Main.java Progetto: YpGu/gcoev
  /*
   * backward pass 1: update
   *  (1) \hat{mu} (mu_hat_s)
   *  (2) \hat{grad_mu} (grad_mu_hat_s)
   *  (3) \hat{V} (v_hat_s)
   */
  public static void backward1(boolean update_grad) {
    for (int t1 = T - 1; t1 > t0; t1--) {
      int t = t1 - t0;
      //      System.out.println("backward 1;\tt = " + t1);
      if (t != T - 1 - t0) {
        double V_pre_t = v_s.get(t - 1); // V^{t-1}
        double V_hat_t = v_hat_s.get(t); // \hat{V}^{t}
        double[][] mu_pre_t = mu_s.get(t - 1); // \mu^{t-1}
        double[][] mu_hat_t = mu_hat_s.get(t); // \hat{\mu}^{t}  [t-1]

        Matrix A_pre_t = new Matrix(AS.get(t - 1)); // A^{t-1}
        Matrix hprime_pre_t = new Matrix(h_prime_s.get(t - 1)); // h'^{t-1}
        Matrix ave_neighbors = A_pre_t.times(hprime_pre_t); // n * 1

        /* calculate \hat{\mu} at time t-1 */
        double factor_1 =
            (1 - lambda) * V_pre_t / (sigma * sigma + (1 - lambda) * (1 - lambda) * V_pre_t);
        double factor_2 = (sigma * sigma) / (sigma * sigma + (1 - lambda) * (1 - lambda) * V_pre_t);
        double[][] mu_hat_pre_t = new double[n][K];
        for (int i = 0; i < n; i++)
          for (int k = 0; k < K; k++) {
            mu_hat_pre_t[i][k] =
                factor_1 * (mu_hat_t[i][k] - lambda * ave_neighbors.get(i, k))
                    + factor_2 * mu_pre_t[i][k];
          }
        /* calculate \hat{V} at time t-1 */
        double V_hat_pre_t =
            V_pre_t
                + factor_1
                    * factor_1
                    * (V_hat_t - (1 - lambda) * (1 - lambda) * V_pre_t - (sigma * sigma));

        /* update \mu and V */
        mu_hat_s.set(t - 1, mu_hat_pre_t);
        v_hat_s.set(t - 1, V_hat_pre_t);

        /* calculate and update grad_mu_hat at time t-1 */
        if (update_grad)
          for (int s = 0; s < T - t0; s++) {
            double[][] grad_hat_t_s = grad_mu_hat_s.get(t * (T - t0) + s);
            double[][] grad_pre_t_s = grad_mu_s.get((t - 1) * (T - t0) + s);
            double[][] grad_hat_pre_t_s = new double[n][K];
            for (int i = 0; i < n; i++)
              for (int k = 0; k < K; k++) {
                grad_hat_pre_t_s[i][k] =
                    factor_1 * grad_hat_t_s[i][k] + factor_2 * grad_pre_t_s[i][k];
              }
            grad_mu_hat_s.set((t - 1) * (T - t0) + s, grad_hat_pre_t_s);
          }
      } else {
        /*
         * initial condition for backward pass:
         *  (1) \hat{mu}^{T} = mu^{T}
         *  (2) \hat{V}^{T} = V^{T}
         *  (3) \hat{grad_mu}^{T/s} = grad_mu^{T/s}, \forall s
         */
        mu_hat_s.set(t, mu_s.get(t));
        v_hat_s.set(t, v_s.get(t));
        if (update_grad)
          for (int s = 0; s < T - t0; s++) {
            grad_mu_hat_s.set(t * (T - t0) + s, grad_mu_s.get(t * (T - t0) + s));
          }
      }
      //      Scanner sc = new Scanner(System.in);
      //      int gu; gu = sc.nextInt();
      /* end for each t */
    }
  }
Esempio n. 28
0
  /**
   * SIGH...
   *
   * @return
   */
  public BNF toBNF() {

    // first thing first, make a deep copy of current EBNF
    // then play / return with the copy
    EBNF ebnf = clone();

    // auto generate new non terminal names
    StringIterator quanNames = new StringIterator("quan_{000}");
    int oldHash = ebnf.hashCode();
    int newHash = ebnf.hashCode();
    boolean exceptionTriggered;

    do {
      oldHash = newHash;
      exceptionTriggered = false;

      Iterator<NonTerminal> i = ebnf.nonTerminals.keySet().iterator();

      try {

        // travel through all non terminals
        while (i.hasNext()) {
          NonTerminal iii = i.next();
          Iterator<? extends EBNFBranch> j = ebnf.nonTerminals.get(iii).iterator();

          // travel through all branches for each non terminal
          while (j.hasNext()) {
            EBNFBranch jjj = j.next();
            List l = jjj.getElementList();

            // travel through all elements for each branch
            for (int n = 0; n < l.size(); n++) {
              if (l.get(n) instanceof Quantification) {

                // found a quantification
                Quantification q = ((Quantification) l.get(n));

                // substitude it with a new non terminal
                NonTerminal newnt = new NonTerminal(quanNames.next());

                l.set(n, newnt);

                // modify the element list for the new nt a little bit
                List<EBNFBranch> li = (List<EBNFBranch>) q.getBranchList();

                if ((q.getMaxOccurrance() == 1) && (q.getMinOccurrance() == 0)) {

                  // [..]
                  boolean containsEpsilon = false;

                  for (int k = 0; k < li.size(); k++) {
                    if (Terminal.epsilon.equals(li.get(k).getElementList().get(0))) {

                      // this branch contains epsilon
                      // (branches that contain epsilon contains only 1 element)
                      containsEpsilon = true;
                    }
                  }

                  // add new epsilon branch if currently don't have one
                  if (!containsEpsilon) {
                    li.add(new EBNFBranch());
                  }
                } else if ((q.getMaxOccurrance() == -1) && (q.getMinOccurrance() == 0)) {
                  boolean containsEpsilon = false;

                  // {...}
                  for (int k = 0; k < li.size(); k++) {
                    if (Terminal.epsilon.equals(li.get(k).getElementList().get(0))) {
                      containsEpsilon = true;

                      // keep the only one epsilon branch untouched
                    } else {

                      // add the new non terminal to the end of
                      // each non epsilon branch
                      li.get(k).getElementList().add(newnt);
                    }
                  }

                  if (!containsEpsilon) {
                    li.add(new EBNFBranch());
                  }
                }

                // add the new non terminal to new EBNF
                ebnf.nonTerminals.put(newnt, li);
              }
            }
          }
        }
      } catch (ConcurrentModificationException e) {
        exceptionTriggered = true;
      }

      newHash = ebnf.hashCode();
    } while ((newHash != oldHash) || exceptionTriggered);

    return new BNF(ebnf.startSymbol, ebnf.nonTerminals);
  }
 /** set changes the element at the given index */
 public void testSet() {
   List full = populatedArray(3);
   assertEquals(two, full.set(2, four));
   assertEquals(4, ((Integer) full.get(2)).intValue());
 }
Esempio n. 30
0
 public void setLine(int index, TicketLineInfo oLine) {
   oLine.setTicket(m_sId, index);
   m_aLines.set(index, oLine);
 }