// the naive way of doing things - interesting as a discussion starter
  public static void main(String[] args) {
    for (int size = 1; size < 1000; size++) {
      double[] array = DoubleStream.generate(Math::random).limit(size).toArray();
      double[] copiedArray = Arrays.copyOf(array, size);
      long parallelNanos =
          Timing.time(() -> Arrays.parallelSort(copiedArray)).getDuration().toNanos();
      long singleNanos = Timing.time(() -> Arrays.sort(array)).getDuration().toNanos();

      System.out.println("Size:" + size);
      System.out.println("Single threaded: " + singleNanos);
      System.out.println("Parallel: " + parallelNanos);
      System.out.println();
      if (singleNanos > parallelNanos) {
        return;
      }
    }
  }
  @Override
  public List<T> aggregate(ListOfRanks<T> ranks) {
    int m = ranks.countOfItems();
    double n_ = DoubleStream.of(ranks.weights()).sum();
    double subtrahend = 1 / (double) m;
    double subtrahend2 = 1 / (m * n_);

    double medium = n_ / 2.;
    double[][] ma1 = new double[m][m];
    double[][] ma2 = new double[m][m];
    double[][] ma3 = new double[m][m];

    for (int u = 0; u < m; u++) {
      for (int v = 0; v < m; v++) {
        final int finalU = u;
        final int finalV = v;

        double c =
            ranks
                .stream()
                .filter(rank -> rank.position(finalU) > rank.position(finalV))
                .mapToDouble(rank -> rank.weight)
                .sum();

        if (c > 0) { // MC1
          ma1[u][v] = subtrahend;
        }

        if (c >= medium) { // MC2
          ma2[u][v] = subtrahend;
        }

        ma3[u][v] = c * subtrahend2; // MC3
      }
    }

    double[][] ma;
    switch (type) {
      case MC1:
        ma = ma1;
        break;
      case MC2:
        ma = ma2;
        break;
      default:
        ma = ma3;
    }

    for (int i = 0; i < m; i++) {
      ma[i][i] = 1 - DoubleStream.of(ma[i]).sum();
    }

    double[] weights = findStationaryDistribution(transfer(ma, 0.05));

    return IntStream.range(0, m)
        .mapToObj(i -> Pair.of(weights[i], ranks.itemByNumber(i)))
        .sorted((o1, o2) -> -Double.compare(o1.first, o2.first))
        .map(pair -> pair.second)
        .collect(Collectors.toList());
  }
示例#3
0
 public static void printStats(double[] values) {
   System.out.println("Min;" + DoubleStream.of(values).min().getAsDouble());
   System.out.println("Max;" + DoubleStream.of(values).max().getAsDouble());
   System.out.println("Mean;" + new Mean().evaluate(values));
   System.out.println("Median;" + new Median().evaluate(values));
   System.out.println("Std. Dev.;" + new StandardDeviation().evaluate(values));
 }
  public static void main(String... args) {

    /*
    -------------------------------------------------------------------------
    From obj to ...
    -------------------------------------------------------------------------
    */
    Stream<String> streamOfStrings = Stream.of("un", "deux", "trois");
    Function<String, StringBuilder> function = StringBuilder::new;
    streamOfStrings.map(function) /*.forEach(System.out::println)*/;
    streamOfStrings = Stream.of("un", "deux", "trois");
    ToIntFunction<String> toIntFunction = String::length;
    IntStream streamOfInts = streamOfStrings.mapToInt(toIntFunction);
    streamOfStrings = Stream.of("un", "deux", "trois");
    ToDoubleFunction<String> toDoubleFunction = String::length;
    DoubleStream streamOfDoubles = streamOfStrings.mapToDouble(toDoubleFunction);
    streamOfStrings = Stream.of("un", "deux", "trois");
    ToLongFunction<String> toLongFunction = String::length;
    LongStream streamOfLongs = streamOfStrings.mapToLong(toLongFunction);

    /*
    -------------------------------------------------------------------------
    From int to ...
    -------------------------------------------------------------------------
    */
    IntUnaryOperator plusplus = i -> i++;
    IntStream.of(1, 2, 3).map(plusplus)
    /*.forEach(x->System.out.println(x))*/ ;
    IntFunction<String> intFunction = i -> "" + i;
    IntStream.of(1, 2, 3).mapToObj(intFunction)
    /*.forEach(System.out::println)*/ ;
    IntToDoubleFunction itdf = i -> i;
    IntStream.of(1, 2, 3).mapToDouble(itdf)
    /*.forEach(System.out::println)*/ ;
    IntToLongFunction itlf = i -> i;
    IntStream.of(1, 2, 3).mapToLong(itlf)
    /*.forEach(System.out::println)*/ ;

    /*
    -------------------------------------------------------------------------
    From long to ...
    -------------------------------------------------------------------------
    */
    LongUnaryOperator times = l -> l * l;
    LongStream.of(1L, 2L, 3L).map(times)
    /*.forEach(System.out::println)*/ ;
    LongFunction<String> lf = l -> "toto";
    LongStream.of(1L, 2L, 3L).mapToObj(lf);

    /*
    -------------------------------------------------------------------------
    From double to ...
    -------------------------------------------------------------------------
    */
    DoubleToIntFunction dtif = d -> (int) d;
    DoubleStream.of(1.3, 1.5, 1.6).mapToInt(dtif).forEach(System.out::println);
  }
  @Override
  public void processDocument(Document doc) {
    doc.addFeatureSet(FEATURE_SET, usedForClassification);
    clusters.forEach(
        (i, c) -> {
          DoubleStream stream =
              c.stream()
                  .mapToDouble(
                      (fn) -> doc.getFeature(fn) == null ? 0 : doc.getFeature(fn).doubleValue());
          Double v = 0.0;
          if (isMaxCombiner) {
            v = stream.max().orElse(0);
          } else { // if isSumCombiner
            v = stream.sum();
          }

          if (v > 0) {
            doc.getFeatureSet(FEATURE_SET).add(i.toString(), v);
          }
        });
  }
  public static void main(String[] args) {

    // Page 45
    // ========

    IntStream.iterate(1, i -> i * 2).limit(10).forEachOrdered(System.out::println);

    IntStream.range(1, 6).forEach(System.out::print); // prints 123456

    // Page 46
    // ========

    List<Integer> intList = Arrays.asList(1, 2, 3);

    OptionalDouble maxDistance =
        intList.stream().map(i -> new Point(i % 3, i / 3)).mapToDouble(p -> p.distance(0, 0)).max();

    DoubleStream ds =
        intList.stream().map(i -> new Point(i % 3, i / 3)).mapToDouble(p -> p.distance(0, 0));

    OptionalDouble maxDistance1 = ds.max();

    // Page 48
    // ========

    Optional<Integer> max =
        Arrays.asList(1, 2, 3, 4, 5).stream().map(i -> i + 1).max(Integer::compareTo);

    // Page 49
    // ========

    OptionalInt max1 = IntStream.rangeClosed(1, 5).map(i -> i + 1).max();

    DoubleStream ds1 = IntStream.rangeClosed(1, 10).asDoubleStream();

    Stream<Integer> is = IntStream.rangeClosed(1, 10).boxed();

    Stream<Integer> integerStream = Stream.of(1, 2);
    IntStream is1 = integerStream.mapToInt(Integer::intValue);
  }
  /**
   * Randomly initializes this GaussianMixture for a given number of terms.
   *
   * @param random a {@link java.util.Random} object.
   * @param numTerms a number of terms.
   */
  public void randomInitialization(Random random, int numTerms) {

    this.coefficients = new double[numTerms];
    this.terms = new ArrayList<>(numTerms);
    for (int k = 0; k < numTerms; k++) {
      this.coefficients[k] = random.nextDouble();

      Normal aux = new Normal(this.var);
      aux.setMean(5 * random.nextGaussian());
      aux.setVariance(random.nextDouble());

      this.terms.add(aux);
    }
    ;
    DoubleStream aux = Arrays.stream(this.coefficients);
    double suma = aux.sum();
    aux = Arrays.stream(this.coefficients);
    this.coefficients = aux.map(x -> x / suma).toArray();

    // System.out.println(coefficients);
    // this.coefficients = this.coefficients / .map().sum();

  }
  private <E extends RuntimeException> void assertDoubleToLongFunction(
      DoubleToLongFunction test, Class<E> type) {
    assertNotNull(test);
    try {
      test.applyAsLong(0.0);
      fail();
    } catch (RuntimeException e) {
      assertException(type, e, "0.0");
    }

    try {
      DoubleStream.of(1.0, 2.0, 3.0).mapToLong(test);
    } catch (RuntimeException e) {
      assertException(type, e, "1.0");
    }
  }
示例#9
0
 /**
  * Check if the FlyWheel has reached a stable speed
  *
  * @param tolerance Stabilization check tolerance
  * @return Stabilization status
  */
 public boolean isStabilized(double tolerance) {
   return (DoubleStream.of(errors).sum() / 30) < tolerance;
 }
示例#10
0
 public DoubleStream getCoordinates(float factor) {
   return DoubleStream.of(factor * x, factor * y, factor * z);
 }
示例#11
0
 public DoubleStream getCoordinates() {
   return DoubleStream.of(x, y, z);
 }
示例#12
0
  /** @param args the command line arguments */
  public static void main(String[] args) {

    int dimension = 10;
    int NP = 100;
    int MAXFES = 10 * NP;
    int funcNumber = 14;
    TestFunction tf = new Schwefel();
    int H = 10;
    long seed = 10304050L;
    util.random.Random generator;

    S_SHADE shade;

    int runs = 2;
    double[] bestArray = new double[runs];

    for (int k = 0; k < runs; k++) {

      generator = new util.random.UniformRandomSeed(seed);
      shade = new S_SHADE(dimension, MAXFES, tf, H, NP, generator);

      shade.run();

      //            PrintWriter writer;
      //
      //            try {
      //                writer = new PrintWriter("CEC2015-" + funcNumber + "-shade" + k + ".txt",
      // "UTF-8");
      //
      //                writer.print("{");
      //
      //                for (int i = 0; i < shade.getBestHistory().size(); i++) {
      //
      //                    writer.print(String.format(Locale.US, "%.10f",
      // shade.getBestHistory().get(i).fitness));
      //
      //                    if (i != shade.getBestHistory().size() - 1) {
      //                        writer.print(",");
      //                    }
      //
      //                }
      //
      //                writer.print("}");
      //
      //                writer.close();
      //
      //            } catch (FileNotFoundException | UnsupportedEncodingException ex) {
      //                Logger.getLogger(ShaDE.class.getName()).log(Level.SEVERE, null, ex);
      //            }

      bestArray[k] = shade.getBest().fitness - tf.optimum();
      System.out.println(shade.getBest().fitness - tf.optimum());
      System.out.println(Arrays.toString(shade.getBest().vector));
    }

    System.out.println("=================================");
    System.out.println("Min: " + DoubleStream.of(bestArray).min().getAsDouble());
    System.out.println("Max: " + DoubleStream.of(bestArray).max().getAsDouble());
    System.out.println("Mean: " + new Mean().evaluate(bestArray));
    System.out.println("Median: " + new Median().evaluate(bestArray));
    System.out.println("Std. Dev.: " + new StandardDeviation().evaluate(bestArray));
    System.out.println("=================================");
  }
示例#13
0
 public ScalarSequence(DoubleStream in) {
   x = in.toArray();
   index = capacity = x.length;
   calcStats();
 }