Esempio n. 1
0
 public static void oneCompany(String Symbol) {
   Double[] close;
   close = CSV_Parser.getClose(Symbol);
   int i = 0;
   /*while(i<close.length)
   {
   	System.out.println(close[i]);
   	++i;
   }*/
   Algorithm.HeadAndShoulders(close);
   Algorithm.InverseHeadAndShoulders(close);
   Algorithm.DoubleTops(close);
 }
Esempio n. 2
0
 private static void printResult(List<Point> pointList, List<Algorithm> algorithms) {
   for (Algorithm algorithm : algorithms) {
     Result r = algorithm.run(pointList);
     r.stop();
     System.err.println(algorithm.toString());
     System.err.println("Time: " + r.getTime());
     System.err.println("Iterations: " + r.getIterationCount());
     for (RoundResult rr : r.getResultsToReport()) {
       System.err.println(rr.printResult());
     }
     //            System.out.println(r.getBestResult().print(true));
     System.err.println();
   }
 }
Esempio n. 3
0
 private static void writeToFile(Algorithm a, RoundResult[] resultsToReport) {
   try {
     PrintWriter pw =
         new PrintWriter(
             "resources/results_"
                 + a.getClass().getSimpleName()
                 + "_"
                 + new SimpleDateFormat("yyyyMMdd_kkmm")
                     .format(new Date(System.currentTimeMillis())));
     for (RoundResult r : resultsToReport) {
       pw.println(r.toString());
     }
     pw.flush();
     pw.close();
   } catch (FileNotFoundException e) {
     System.err.println("File not found: " + e.getMessage());
     for (RoundResult r : resultsToReport) {
       System.out.println(r.toString());
     }
   }
 }
Esempio n. 4
0
  /**
   * Reads the raw data from the SecretKey File, creates a SecretKey from that raw data, reads the
   * raw data from the input File, and then decrypts and saves its contents to the output File.
   *
   * @param input the File to be read and decrypted
   * @param output the File the decrypted data will be saved to
   * @param keyFile the File the SecretKey data will be loaded from
   * @throws InvalidKeyException if the given key material is shorter than 8 bytes or if the given
   *     key is inappropriate for initializing this cipher, or if this cipher is being initialized
   *     for decryption and requires algorithm parameters that cannot be determined from the given
   *     key, or if the given key has a keysize that exceeds the maximum allowable keysize (as
   *     determined from the configured jurisdiction policy files).
   * @throws IOException if any of the files do not exist, are a directory rather than a regular
   *     file, or for some other reason cannot be opened for reading or if an I/O error occurs.
   * @throws IllegalBlockSizeException if the cipher is a block cipher, no padding has been
   *     requested (only in encryption mode), and the total input length of the data processed by
   *     this cipher is not a multiple of block size; or if this encryption algorithm is unable to
   *     process the input data provided.
   * @throws BadPaddingException if the cipher is in decryption mode, and (un)padding has been
   *     requested, but the decrypted data is not bounded by the appropriate padding bytes.
   * @throws NoSuchAlgorithmException if no Provider supports a SecretKeyFactorySpi implementation
   *     for the specified algorithm.
   * @throws InvalidKeySpecException if the given key specification is inappropriate for this
   *     secret-key factory to produce a secret key.
   * @throws UnsupportedOperationException if algorithm is not DES or DESede
   */
  public void decrypt(File input, File output, File keyFile)
      throws InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException,
          NoSuchAlgorithmException, InvalidKeySpecException {
    if (debug) {
      System.out.println("Loading key...");
    }
    FileInputStream fis = null;
    try {
      fis = new FileInputStream(keyFile);
      data = new byte[fis.available()];
      fis.read(data);
    } finally {
      if (fis != null) {
        fis.close();
      }
    }
    switch (Algorithm.valueOf(algorithm)) {
      case DES:
        key = SecretKeyFactory.getInstance(algorithm).generateSecret(new DESKeySpec(data));
        break;
      case DESede:
        key = SecretKeyFactory.getInstance(algorithm).generateSecret(new DESedeKeySpec(data));
        break;
      default:
        throw new UnsupportedOperationException("Unsupported decryption algorithm");
    }

    if (debug) {
      System.out.println("Initializing decryption...");
    }
    cipher.init(Cipher.DECRYPT_MODE, key);
    if (debug) {
      System.out.println("Reading data...");
    }
    fis = null;
    try {
      fis = new FileInputStream(input);
      data = new byte[(int) input.length()];
      fis.read(data);
    } finally {
      if (fis != null) {
        fis.close();
      }
    }
    if (debug) {
      System.out.println("Decrypting data...");
    }
    data = cipher.doFinal(data);
    if (debug) {
      System.out.println("Saving data...");
    }
    FileOutputStream fos = null;
    try {
      fos = new FileOutputStream(output);
      fos.write(data);
    } finally {
      if (fos != null) {
        fos.close();
      }
    }
    if (debug) {
      System.out.println("Decryption complete!");
    }
    data = null;
  }
Esempio n. 5
0
  public static void main(String[] args) throws Throwable {
    DATASET = System.getProperty("dataset", "movielens-pos.json");
    TRAINPERCENT = Double.parseDouble(System.getProperty("trainPercent", "0.8"));
    TESTPERCENT = Double.parseDouble(System.getProperty("testPercent", "0.5"));
    RUNS = Integer.parseInt(System.getProperty("runs", "1"));
    PREDICTIONS = Integer.parseInt(System.getProperty("predictions", "1"));
    // rand = new Random( Integer.parseInt( System.getProperty( "seed", "1" ) ) );
    rand = new Random();

    System.out.println("File: " + DATASET);
    System.out.println("Train Percent: " + TRAINPERCENT);
    System.out.println("Test Percent: " + TESTPERCENT);
    System.out.println("Runs: " + RUNS);
    System.out.println("Predictions: " + PREDICTIONS);
    docs = DatasetOld.loadDataset(DATASET);

    for (Algorithm alg : algs) {
      System.out.print(alg.name + "\t");
      double total = 0.0;

      long trainTime = 0;
      long predictTime = 0;

      for (int run = 0; run < RUNS; run++) {
        ArrayList<HashMap<Integer, Double>> traindocs = new ArrayList<HashMap<Integer, Double>>();
        ArrayList<HashMap<Integer, Double>> testdocs = new ArrayList<HashMap<Integer, Double>>();

        for (HashMap<Integer, Double> doc : docs) {
          if (rand.nextDouble() < TRAINPERCENT) traindocs.add(doc);
          else testdocs.add(doc);
        }

        long startTime = System.nanoTime();
        alg.train(traindocs);
        trainTime += System.nanoTime() - startTime;

        int successes = 0;

        for (HashMap<Integer, Double> testdoc : testdocs) {
          HashMap<Integer, Double> givenwords = new HashMap<Integer, Double>();
          HashSet<Integer> testwords = new HashSet<Integer>();

          for (int word : testdoc.keySet()) {
            if (rand.nextDouble() < TESTPERCENT) {
              testwords.add(word);
            } else {
              givenwords.put(word, testdoc.get(word));
            }
          }

          startTime = System.nanoTime();
          double[] scores = alg.predict(givenwords);
          predictTime += System.nanoTime() - startTime;
          // System.out.println(Arrays.toString( scores ));
          PriorityQueue<Pair> pq = new PriorityQueue<Pair>();

          for (int i = 0; i < scores.length; i++) {
            if (givenwords.containsKey(i)) {
              continue;
            }

            if (pq.size() < PREDICTIONS) {
              pq.add(new Pair(i, scores[i]));
            }
            if (scores[i] > pq.peek().score) {
              pq.poll();
              pq.add(new Pair(i, scores[i]));
            }
          }

          while (!pq.isEmpty()) {
            Pair pair = pq.poll();
            // System.out.println( WordIndex.get( pair.word ) + "\t" + pair.score + "\t" +
            // testwords.contains( pair.word ) );
            if (testwords.contains(pair.word)) {
              successes++;
            }
          }
        }

        total += (double) successes / PREDICTIONS / testdocs.size();
      }

      System.out.println(
          total / RUNS
              + "\t"
              + (trainTime / 1000000000.0 / RUNS)
              + "\t"
              + (predictTime / 1000000000.0 / RUNS));
    }
  }