Package de.jungblut.math

Examples of de.jungblut.math.DoubleVector


  public final DoubleVector minimize(CostFunction f, DoubleVector pInput,
      int maxIterations, boolean verbose) {
    ExecutorService pool = Executors.newFixedThreadPool(numThreads);
    // setup
    Random random = new Random();
    DoubleVector globalBestPosition = pInput;
    final DoubleVector[] particlePositions = new DoubleVector[numParticles];
    final double[] particlePersonalBestCost = new double[numParticles];
    // we are going to spread the particles a bit
    for (int i = 0; i < numParticles; i++) {
      particlePositions[i] = new DenseDoubleVector(Arrays.copyOf(
View Full Code Here


    @Override
    public Tuple<Double, DoubleVector> call() throws Exception {
      // loop over all particles and calculate new positions
      for (int particleIndex = range.getStart(); particleIndex < range.getEnd(); particleIndex++) {
        DoubleVector currentPosition = particlePositions[particleIndex];
        DoubleVector currentBest = particlePersonalBestPositions[particleIndex];
        DenseDoubleVector vec = new DenseDoubleVector(dim);
        for (int index = 0; index < vec.getDimension(); index++) {
          double value = (phi * currentPosition.get(index)) // inertia
              + (alpha * random.nextDouble() * (currentBest.get(index) - currentPosition
                  .get(index))) // personal memory
              + (beta * random.nextDouble() * (globalBestPosition.get(index) - currentPosition
                  .get(index))); // group memory
          vec.set(index, value);
        }
View Full Code Here

  @Override
  public final DoubleVector minimize(CostFunction f, DoubleVector theta,
      int length, boolean verbose) {

    DoubleVector input = theta;
    int M = 0;
    int i = 0; // zero the run length counter
    int red = 1; // starting point
    int ls_failed = 0; // no previous line search has failed
    final CostGradientTuple evaluateCost = f.evaluateCost(input);
    double f1 = evaluateCost.getCost();
    DoubleVector df1 = evaluateCost.getGradient();
    i = i + (length < 0 ? 1 : 0);
    // search direction is steepest
    DoubleVector s = df1.multiply(-1.0d);

    double d1 = s.multiply(-1.0d).dot(s); // this is the slope
    double z1 = red / (1.0 - d1); // initial step is red/(|s|+1)

    while (i < Math.abs(length)) {// while not finished
      i = i + (length > 0 ? 1 : 0);// count iterations?!
      // make a copy of current values
      DoubleVector X0 = input.deepCopy();
      double f0 = f1;
      DoubleVector df0 = df1.deepCopy();
      // begin line search
      input = input.add(s.multiply(z1));
      final CostGradientTuple evaluateCost2 = f.evaluateCost(input);
      double f2 = evaluateCost2.getCost();
      DoubleVector df2 = evaluateCost2.getGradient();

      i = i + (length < 0 ? 1 : 0); // count epochs
      double d2 = df2.dot(s);
      // initialize point 3 equal to point 1
      double f3 = f1;
      double d3 = d1;
      double z3 = -z1;
      if (length > 0) {
        M = MAX;
      } else {
        M = Math.min(MAX, -length - i);
      }
      // initialize quanteties
      int success = 0;
      double limit = -1;

      while (true) {
        while (((f2 > f1 + z1 * RHO * d1) | (d2 > -SIG * d1)) && (M > 0)) {
          // tighten the bracket
          limit = z1;
          double z2 = 0.0d;
          double A = 0.0d;
          double B = 0.0d;
          if (f2 > f1) {
            // quadratic fit
            z2 = z3 - (0.5 * d3 * z3 * z3) / (d3 * z3 + f2 - f3);
          } else {
            // cubic fit
            A = 6 * (f2 - f3) / z3 + 3 * (d2 + d3);
            B = 3 * (f3 - f2) - z3 * (d3 + 2 * d2);
            // numerical error possible - ok!
            z2 = (Math.sqrt(B * B - A * d2 * z3 * z3) - B) / A;
          }
          if (Double.isNaN(z2) || Double.isInfinite(z2)) {
            // if we had a numerical problem then bisect
            z2 = z3 / 2.0d;
          }
          // don't accept too close to limits
          z2 = Math.max(Math.min(z2, INT * z3), (1 - INT) * z3);
          // update the step
          z1 = z1 + z2;
          input = input.add(s.multiply(z2));
          final CostGradientTuple evaluateCost3 = f.evaluateCost(input);
          f2 = evaluateCost3.getCost();
          df2 = evaluateCost3.getGradient();
          M = M - 1;
          i = i + (length < 0 ? 1 : 0); // count epochs
          d2 = df2.dot(s);
          // z3 is now relative to the location of z2
          z3 = z3 - z2;
        }
        if (f2 > f1 + z1 * RHO * d1 || d2 > -SIG * d1) {
          break; // this is a failure
        } else if (d2 > SIG * d1) {
          success = 1;
          break; // success
        } else if (M == 0) {
          break; // failure
        }
        // make cubic extrapolation
        double A = 6 * (f2 - f3) / z3 + 3 * (d2 + d3);
        double B = 3 * (f3 - f2) - z3 * (d3 + 2 * d2);
        double z2 = -d2 * z3 * z3 / (B + Math.sqrt(B * B - A * d2 * z3 * z3));
        // num prob or wrong sign?
        if (Double.isNaN(z2) || Double.isInfinite(z2) || z2 < 0)
          // if we have no upper limit
          if (limit < -0.5) {
            // the extrapolate the maximum amount
            z2 = z1 * (EXT - 1);
          } else {
            // otherwise bisect
            z2 = (limit - z1) / 2;
          }
        else if ((limit > -0.5) && (z2 + z1 > limit)) {
          // extraplation beyond max?
          z2 = (limit - z1) / 2; // bisect
        } else if ((limit < -0.5) && (z2 + z1 > z1 * EXT)) {
          // extrapolationbeyond limit
          z2 = z1 * (EXT - 1.0); // set to extrapolation limit
        } else if (z2 < -z3 * INT) {
          z2 = -z3 * INT;
        } else if ((limit > -0.5) && (z2 < (limit - z1) * (1.0 - INT))) {
          // too close to the limit
          z2 = (limit - z1) * (1.0 - INT);
        }
        // set point 3 equal to point 2
        f3 = f2;
        d3 = d2;
        z3 = -z2;
        z1 = z1 + z2;
        // update current estimates
        input = input.add(s.multiply(z2));
        final CostGradientTuple evaluateCost3 = f.evaluateCost(input);
        f2 = evaluateCost3.getCost();
        df2 = evaluateCost3.getGradient();
        M = M - 1;
        i = i + (length < 0 ? 1 : 0); // count epochs?!
        d2 = df2.dot(s);
      }// end of line search

      DoubleVector tmp = null;

      if (success == 1) { // if line search succeeded
        f1 = f2;
        if (verbose) {
          LOG.info("Iteration " + i + " | Cost: " + f1);
View Full Code Here

  /**
   * Folds a single matrix into a single vector by rows.
   */
  public static DoubleVector foldMatrix(DoubleMatrix mat) {
    DoubleVector vec = new DenseDoubleVector(mat.getRowCount()
        * mat.getColumnCount());
    int index = 0;
    for (int i = 0; i < mat.getRowCount(); i++) {
      for (int j = 0; j < mat.getColumnCount(); j++) {
        vec.set(index++, mat.get(i, j));
      }
    }

    return vec;
  }
View Full Code Here

  @Override
  public DoubleVector minimize(CostFunction f, DoubleVector theta,
      int maxIterations, boolean verbose) {

    DoubleVector zeros = new DenseDoubleVector(theta.getDimension());
    this.x = theta;
    this.grad = zeros;
    this.newX = theta.deepCopy();
    this.newGrad = zeros;
    this.dir = zeros;
View Full Code Here

      for (int i = count - 1; i >= 0; i--) {
        alphas[i] = -sList.get(i).dot(dir) / roList.get(i);
        addMult(dir, yList.get(i), alphas[i]);
      }

      DoubleVector lastY = yList.get(count - 1);
      double yDotY = lastY.dot(lastY);
      double scalar = roList.get(count - 1) / yDotY;
      scale(dir, scalar);

      for (int i = 0; i < count; i++) {
        double beta = yList.get(i).dot(dir) / roList.get(i);
View Full Code Here

    return val;
  }

  private void shift() {
    DoubleVector nextS = null;
    DoubleVector nextY = null;

    int listSize = sList.size();

    if (listSize < m) {
      nextS = new DenseDoubleVector(x.getDimension());
      nextY = new DenseDoubleVector(x.getDimension());
    }

    if (nextS == null) {
      nextS = sList.get(0);
      sList.remove(0);
      nextY = yList.get(0);
      yList.remove(0);
      roList.removeAt(0);
    }

    addMultInto(nextS, newX, x, -1);
    addMultInto(nextY, newGrad, grad, -1);
    double ro = nextS.dot(nextY);

    sList.add(nextS);
    yList.add(nextY);
    roList.add(ro);

    DoubleVector tmpNewX = newX.deepCopy();
    newX = x.deepCopy();
    x = tmpNewX;

    DoubleVector tmpNewGrad = newGrad.deepCopy();
    newGrad = grad.deepCopy();
    grad = tmpNewGrad;
  }
View Full Code Here

        }
        break;
      }
    }
    double costSum = 0d;
    DoubleVector gradientSum = new DenseDoubleVector(input.getLength());
    try {
      // now collect the results
      for (int i = 0; i < submittedBatches; i++) {
        CostGradientTuple result = completionService.take().get();
        costSum += result.getCost();
        gradientSum = gradientSum.add(result.getGradient());
      }
    } catch (InterruptedException | ExecutionException e) {
      e.printStackTrace();
      // return null so minimizers should fast-fail
      return null;
    }
    // just return an average over the batches
    return new CostGradientTuple(costSum / submittedBatches,
        gradientSum.divide(submittedBatches));
  }
View Full Code Here

      this.min = min;
      this.max = max;
    }

    public DoubleVector closestPoint(DoubleVector t) {
      DoubleVector p = new DenseDoubleVector(t.getDimension());
      for (int i = 0; i < t.getDimension(); ++i) {
        if (t.get(i) <= min.get(i)) {
          p.set(i, min.get(i));
        } else if (t.get(i) >= max.get(i)) {
          p.set(i, max.get(i));
        } else {
          p.set(i, t.get(i));
        }
      }
      return p;
    }
View Full Code Here

      }
      return p;
    }

    public static HyperRectangle infiniteHyperRectangle(int dimension) {
      DoubleVector min = new DenseDoubleVector(dimension);
      DoubleVector max = new DenseDoubleVector(dimension);
      for (int i = 0; i < dimension; ++i) {
        min.set(i, Double.NEGATIVE_INFINITY);
        max.set(i, Double.POSITIVE_INFINITY);
      }

      return new HyperRectangle(min, max);
    }
View Full Code Here

TOP

Related Classes of de.jungblut.math.DoubleVector

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.