Beispiel #1
0
  public double getNearestInRangeDOFVal(
      double startVal, double min, double max, DoubleMatrix1D x, int dof, int[] indices) {
    // Return the nearest value of DOF #dof in x to startVal that will make this GenCoord (operating
    // on
    // the given indices of x) return a value in the range[min,max]
    // If there is no such value, NaN is returned
    switch (type) {
      case REGULAR:
        if (startVal < min) return min;
        else if (startVal > max) return max;
        else // startVal is in range already
        return startVal;

      case SUMSQ:
        double sum = 0;
        for (int q : indices) sum += Math.pow(x.get(q), 2);
        double minsq = Math.pow(min, 2);
        if (sum
            < minsq) { // Increase the absolute value of x.get(dof) until it makes sum equal to
                       // min^2
          double absAns = Math.sqrt(Math.pow(min, 2) - (sum - Math.pow(startVal, 2)));
          if (startVal > 0) return absAns;
          else return -absAns;
        }
        double maxsq = Math.pow(max, 2);
        if (sum
            > maxsq) { // Decrease the absolute value of x.get(dof) until it makes sum equal to
                       // min^2
          double absAns = Math.sqrt(Math.pow(max, 2) - (sum - Math.pow(startVal, 2)));
          if (startVal > 0) return absAns;
          else return -absAns;
        } else // In range already
        return startVal;

      case LINCOMB:
        double ans = 0;
        int DOFLocalInd = -1; // Index of dof in the "indices" array
        for (int a = 0; a < coeffs.length; a++) {
          if (indices[a] == dof) {
            ans += coeffs[a] * startVal;
            DOFLocalInd = a;
          } else ans += coeffs[a] * x.get(indices[a]);
        }
        if (ans < min) {
          if (coeffs[DOFLocalInd] == 0) return Double.NaN;
          return startVal + (min - ans) / coeffs[DOFLocalInd];
        } else if (ans > max) {
          if (coeffs[DOFLocalInd] == 0) return Double.NaN;
          return startVal + (max - ans) / coeffs[DOFLocalInd];
        } else // In range already
        return startVal;

      default:
        System.err.println("ERROR: Unrecognized generalized coordinate type: " + type);
        System.exit(1);
        return 0;
    }
  }
  private double multiLL(DoubleMatrix2D coeffs, Node dep, List<Node> indep) {

    DoubleMatrix2D indepData =
        factory2D.make(internalData.subsetColumns(indep).getDoubleData().toArray());
    List<Node> depList = new ArrayList<>();
    depList.add(dep);
    DoubleMatrix2D depData =
        factory2D.make(internalData.subsetColumns(depList).getDoubleData().toArray());

    int N = indepData.rows();
    DoubleMatrix2D probs =
        Algebra.DEFAULT.mult(factory2D.appendColumns(factory2D.make(N, 1, 1.0), indepData), coeffs);

    probs =
        factory2D
            .appendColumns(factory2D.make(indepData.rows(), 1, 1.0), probs)
            .assign(Functions.exp);
    double ll = 0;
    for (int i = 0; i < N; i++) {
      DoubleMatrix1D curRow = probs.viewRow(i);
      curRow.assign(Functions.div(curRow.zSum()));
      ll += Math.log(curRow.get((int) depData.get(i, 0)));
    }
    return ll;
  }
Beispiel #3
0
  public double eval(DoubleMatrix1D x, int[] indices) {
    // The "regular" coordinates to be used have the indicated indices in x

    switch (type) {
      case REGULAR:
        return x.get(indices[0]);
      case SUMSQ:
        double sum = 0;
        for (int q : indices) sum += Math.pow(x.get(q), 2);
        return Math.sqrt(sum);
      case LINCOMB:
        double ans = 0;
        for (int a = 0; a < coeffs.length; a++) ans += coeffs[a] * x.get(indices[a]);
        return ans;
      default:
        System.err.println("ERROR: Unrecognized generalized coordinate type: " + type);
        System.exit(1);
        return 0;
    }
  }
 /** @see "Convex Optimization, p. 610" */
 protected DoubleMatrix1D rDual(DoubleMatrix1D gradF0X, DoubleMatrix1D L, DoubleMatrix1D V) {
   // m1 = GradFiX[T].L + gradF0X
   DoubleMatrix1D m1 = F1.make(getDim());
   for (int i = 0; i < getDim(); i++) {
     double m = 0;
     m += -L.getQuick(i);
     m += L.getQuick(getDim() + i);
     m1.setQuick(i, m + gradF0X.get(i));
   }
   if (getMeq() == 0) {
     return m1;
   }
   return ColtUtils.zMultTranspose(getA(), V, m1, 1.);
 }
  public static DoubleMatrix1D normalizeVector(DoubleMatrix1D vector) {
    ArrayList<Double> vl = new ArrayList<Double>();

    for (int i = 0; i < vector.size(); i++) {
      vl.add(Math.pow(vector.get(i), 2));
    }

    vl = normalizeVector(vl);

    DoubleMatrix1D rt = DoubleFactory1D.sparse.make(vl.size());

    for (int i = 0; i < vector.size(); i++) {
      rt.set(i, vl.get(i));
    }

    return rt;
  }
  public static boolean isInScope(
      final DoubleMatrix1D position,
      final DoubleMatrix1D dimensions,
      final DoubleMatrix1D feature) {
    if (position.size() != dimensions.size() || position.size() != feature.size()) {
      throw new IllegalArgumentException(
          "feature, position and dimensions" + "must have the same size");
    }

    // is the feature inside the tile (including boundaries)
    for (int i = 0; i < feature.size(); i++) {
      if (!(feature.get(i) >= position.get(i) - dimensions.get(i) / 2
          && feature.get(i) <= position.get(i) + dimensions.get(i) / 2)) {
        return false;
      }
    }
    return true;
  }
 public void decrementDisp(double x, double y) {
   disp.set(0, disp.get(0) - x);
   disp.set(1, disp.get(1) - y);
 }
 public void incrementDisp(double x, double y) {
   disp.set(0, disp.get(0) + x);
   disp.set(1, disp.get(1) + y);
 }
 public double getYDisp() {
   return disp.get(1);
 }
 public double getXDisp() {
   return disp.get(0);
 }
  /**
   * Returns the best cut of a graph w.r.t. the degree of dissimilarity between points of different
   * partitions and the degree of similarity between points of the same partition.
   *
   * @param W the weight matrix of the graph
   * @return an array of two elements, each of these contains the points of a partition
   */
  protected static int[][] bestCut(DoubleMatrix2D W) {
    int n = W.columns();
    // Builds the diagonal matrices D and D^(-1/2) (represented as their diagonals)
    DoubleMatrix1D d = DoubleFactory1D.dense.make(n);
    DoubleMatrix1D d_minus_1_2 = DoubleFactory1D.dense.make(n);
    for (int i = 0; i < n; i++) {
      double d_i = W.viewRow(i).zSum();
      d.set(i, d_i);
      d_minus_1_2.set(i, 1 / Math.sqrt(d_i));
    }
    DoubleMatrix2D D = DoubleFactory2D.sparse.diagonal(d);

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

    DoubleMatrix2D X = D.copy();

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

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

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

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

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

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

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

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

    return partition;
  }
Beispiel #12
0
  /**
   * Solves a presolved standard form LP problem in the form of min(c) s.t. A.x = b lb <= x <= ub
   */
  protected int optimizePresolvedStandardLP() throws Exception {
    log.info("optimizePresolvedStandardLP");

    long tStart = System.currentTimeMillis();

    LPOptimizationRequest lpRequest = getLPOptimizationRequest();
    if (log.isDebugEnabled() && lpRequest.isDumpProblem()) {
      log.debug("LP problem: " + lpRequest.toString());
    }

    if (this.dim <= -1) {
      if (getLb().size() != getUb().size()) {
        log.error("Lower and upper bounds must have the same dimension");
        throw new IllegalArgumentException("Lower and upper bounds must have the same dimension");
      }
      this.dim = getLb().size();
      double minDeltaBoundsValue = Double.MAX_VALUE;
      int minDeltaBoundsIndex = -1;
      for (int i = 0; i < getDim(); i++) {
        double deltai = getUb().getQuick(i) - getLb().getQuick(i);
        if (deltai < minDeltaBoundsValue) {
          minDeltaBoundsValue = deltai;
          minDeltaBoundsIndex = i;
        }
      }
      log.info("min delta bounds index: " + minDeltaBoundsIndex);
      log.info("min delta bounds value: " + minDeltaBoundsValue);
    }
    // this.boundedLb = new boolean[getDim()];
    // this.boundedUb = new boolean[getDim()];
    //		for(int i=0; i<getDim(); i++){
    //			if(!isLbUnbounded(getLb().getQuick(i))){
    //				boundedLb[i] = true;
    //				nOfBoundedLb++;
    //			}
    //			if(!isUbUnbounded(getUb().getQuick(i))){
    //				boundedUb[i] = true;
    //				nOfBoundedUb++;
    //			}
    //		}
    this.meq = (this.meq > -1) ? this.meq : ((getA() != null) ? getA().rows() : 0);
    // this.mieq = (this.mieq>-1)? this.mieq : (nOfBoundedLb+nOfBoundedUb);
    this.mieq = (this.mieq > -1) ? this.mieq : (2 * getDim());

    if (log.isDebugEnabled()) {
      log.debug("dim : " + getDim());
      log.debug("meq : " + getMeq());
      log.debug("mieq: " + getMieq());
    }

    LPOptimizationResponse lpResponse = new LPOptimizationResponse();

    DoubleMatrix1D X0 = getInitialPoint();
    if (X0 == null) {
      DoubleMatrix1D X0NF = getNotFeasibleInitialPoint();
      if (X0NF != null) {
        double rPriX0NFNorm = Math.sqrt(ALG.norm2(rPri(X0NF)));
        DoubleMatrix1D fiX0NF = getFi(X0NF);
        int maxIndex = Utils.getMaxIndex(fiX0NF);
        double maxValue = fiX0NF.get(maxIndex);
        if (log.isDebugEnabled()) {
          log.debug("rPriX0NFNorm :  " + rPriX0NFNorm);
          log.debug("X0NF         :  " + ArrayUtils.toString(X0NF.toArray()));
          log.debug("fiX0NF       :  " + ArrayUtils.toString(fiX0NF.toArray()));
        }
        if (maxValue < 0 && rPriX0NFNorm <= getToleranceFeas()) {
          // the provided not-feasible starting point is already feasible
          log.debug("the provided initial point is already feasible");
          X0 = X0NF;
        }
      }
      if (X0 == null) {
        BasicPhaseILPPDM bf1 = new BasicPhaseILPPDM(this);
        X0 = bf1.findFeasibleInitialPoint();
      }
    }

    // check X0 feasibility
    DoubleMatrix1D fiX0 = getFi(X0);
    int maxIndex = Utils.getMaxIndex(fiX0);
    double maxValue = fiX0.get(maxIndex);
    double rPriX0Norm = Math.sqrt(ALG.norm2(rPri(X0)));
    if (maxValue >= 0. || rPriX0Norm > getToleranceFeas()) { // must be fi STRICTLY < 0
      log.warn("rPriX0Norm  : " + rPriX0Norm);
      log.warn("ineqX0      : " + ArrayUtils.toString(fiX0.toArray()));
      log.warn("max ineq index: " + maxIndex);
      log.warn("max ineq value: " + maxValue);
      // the point must be INTERNAL, fi are used as denominators
      throw new Exception("initial point must be strictly feasible");
    }

    DoubleMatrix1D V0 = F1.make(getMeq());
    if (getYlb() != null && getYub() != null) {
      // NB: the Lagrangian multipliers for eq. constraints used in this interior point method (v)
      // are the opposite of the Lagrangian  multipliers for eq. constraints used in the presolver
      // (y)
      // and so Ylb<=y<=Yub becomes -Yub<=v<=-Ylb
      for (int i = 0; i < getMeq(); i++) {
        double v0i = 0;
        if (!isLbUnbounded(getYlb().getQuick(i))) {
          if (!isUbUnbounded(getYub().getQuick(i))) {
            v0i = -(getYub().getQuick(i) + getYlb().getQuick(i)) / 2;
          } else {
            v0i = -getYlb().getQuick(i);
          }
        } else {
          if (!isUbUnbounded(getYub().getQuick(i))) {
            v0i = -getYub().getQuick(i);
          } else {
            v0i = 0;
          }
        }
        V0.setQuick(i, v0i);
      }
    }

    DoubleMatrix1D L0 = getInitialLagrangian();
    if (L0 != null) {
      for (int j = 0; j < L0.size(); j++) {
        // must be >0
        if (L0.get(j) <= 0) {
          throw new IllegalArgumentException("initial lagrangian must be strictly > 0");
        }
      }
    } else {
      L0 = F1.make(getMieq(), 1.); // must be >0 strictly
      if (getZlb() != null && getZub() != null) {
        // Zlb<= L <=Zub, meaning that:
        // zlb[i] and zub[i] are the bounds on the Lagrangian of the constraint associated with
        // lb[i]<x[i]<ub[i]
        // note that zlb.size = zub.size = lb.size = ub.size (and = n of variables of the problem (=
        // getDim())
        // and that L.size = nOfBoundedLb + nOfBoundedUb (and in general < 2*getDim())
        int cntLB = 0;
        int cntUB = 0;
        for (int i = 0; i < getDim(); i++) {
          double zlbi =
              (isLbUnbounded(getZlb().getQuick(i))) ? 0 : getZlb().getQuick(i); // L must be > 0
          double zubi = (isUbUnbounded(getZub().getQuick(i))) ? 1 : getZub().getQuick(i);
          L0.setQuick(cntLB, (zubi - zlbi) / 2);
          cntLB++;
          L0.setQuick(getDim() + cntUB, (zubi - zlbi) / 2);
          cntUB++;
        }
      } else {
        // inequalities comes from the pairs lower bounds-upper bounds
        // in the calculation of the H matrix fro the KKT system, each pairs gives terms of the
        // form:
        // t = tl + tu
        // tl = -L[i] / fi[i] for the lower bound
        // tu =  L[dim+i] / fi[dim+i] for the upper bound
        // we want t = 1, and hence
        // L[i] > -cc * fi[i]
        // L[dim+i] = (1 + L[i] / fi[i]) * fi[dim+i]
        //				double cc = 10;
        //				int nOfLB = getMieq()/2;
        //				for (int i = 0; i < nOfLB; i++) {
        //					L0.setQuick(i, -cc * fiX0.getQuick(i));
        //					L0.setQuick(nOfLB + i, (1 - 10)	* fiX0.getQuick(nOfLB + i));
        //					double sum = -L0.getQuick(i)/fiX0.getQuick(i)+L0.getQuick(nOfLB +
        // i)/fiX0.getQuick(nOfLB + i);
        //					log.debug("sum["+i+"]:  " + sum);
        //				}
      }
    }
    if (log.isDebugEnabled()) {
      log.debug("X0:  " + ArrayUtils.toString(X0.toArray()));
      log.debug("V0:  " + ArrayUtils.toString(V0.toArray()));
      log.debug("L0:  " + ArrayUtils.toString(L0.toArray()));
    }
    if (log.isInfoEnabled()) {
      log.info("toleranceFeas:  " + getToleranceFeas());
      log.info("tolerance    :  " + getTolerance());
    }

    DoubleMatrix1D X = X0;
    DoubleMatrix1D V = V0;
    DoubleMatrix1D L = L0;
    double previousF0X = Double.NaN;
    double previousRPriXNorm = Double.NaN;
    double previousRDualXLVNorm = Double.NaN;
    double previousSurrDG = Double.NaN;
    double t;
    int iteration = 0;
    // List<DoubleMatrix1D> XList = new ArrayList<DoubleMatrix1D>();
    // List<double[]> SList = new ArrayList<double[]>();
    while (true) {

      iteration++;
      // iteration limit condition
      if (iteration == getMaxIteration() + 1) {
        lpResponse.setReturnCode(OptimizationResponse.FAILED);
        log.error("Max iterations limit reached");
        throw new Exception("Max iterations limit reached");
      }

      // XList.add(XList.size(), X);
      double F0X = getF0(X);
      if (log.isInfoEnabled()) {
        log.info("iteration: " + iteration);
        log.info("f0(X)=" + F0X);
      }
      if (log.isDebugEnabled()) {
        log.debug("X=" + ArrayUtils.toString(X.toArray()));
        log.debug("L=" + ArrayUtils.toString(L.toArray()));
        log.debug("V=" + ArrayUtils.toString(V.toArray()));
      }

      // determine functions evaluations
      DoubleMatrix1D gradF0X = getGradF0(X);
      DoubleMatrix1D fiX = getFi(X);
      log.debug("fiX=" + ArrayUtils.toString(fiX.toArray()));
      // DoubleMatrix2D GradFiX = getGradFi(X);
      // DoubleMatrix2D GradFiXOLD = getGradFiOLD(X);

      // determine t
      double surrDG = getSurrogateDualityGap(fiX, L);
      t = getMu() * getMieq() / surrDG;
      log.debug("t:  " + t);

      // determine residuals
      DoubleMatrix1D rPriX = rPri(X);
      DoubleMatrix1D rCentXLt = rCent(fiX, L, t);
      DoubleMatrix1D rDualXLV = rDual(gradF0X, L, V);
      // DoubleMatrix1D rDualXLVOLD = rDualOLD(GradFiXOLD, gradF0X, L, V);
      // log.debug("delta: " + ALG.normInfinity(rDualXLVOLD.assign(rDualXLV, Functions.minus)));
      double rPriXNorm = Math.sqrt(ALG.norm2(rPriX));
      double rCentXLtNorm = Math.sqrt(ALG.norm2(rCentXLt));
      double rDualXLVNorm = Math.sqrt(ALG.norm2(rDualXLV));
      double normRXLVt =
          Math.sqrt(Math.pow(rPriXNorm, 2) + Math.pow(rCentXLtNorm, 2) + Math.pow(rDualXLVNorm, 2));
      // @TODO: set log.debug not log.info
      log.info("rPri  norm: " + rPriXNorm);
      log.info("rCent norm: " + rCentXLtNorm);
      log.info("rDual norm: " + rDualXLVNorm);
      log.info("surrDG    : " + surrDG);

      // custom exit condition
      if (checkCustomExitConditions(X)) {
        lpResponse.setReturnCode(OptimizationResponse.SUCCESS);
        break;
      }

      // exit condition
      if (rPriXNorm <= getToleranceFeas()
          && rDualXLVNorm <= getToleranceFeas()
          && surrDG <= getTolerance()) {
        lpResponse.setReturnCode(OptimizationResponse.SUCCESS);
        break;
      }

      // progress conditions
      if (isCheckProgressConditions()) {
        if (!Double.isNaN(previousRPriXNorm)
            && !Double.isNaN(previousRDualXLVNorm)
            && !Double.isNaN(previousSurrDG)) {
          if ((previousRPriXNorm <= rPriXNorm && rPriXNorm >= getToleranceFeas())
              || (previousRDualXLVNorm <= rDualXLVNorm && rDualXLVNorm >= getToleranceFeas())) {
            log.error("No progress achieved, exit iterations loop without desired accuracy");
            lpResponse.setReturnCode(OptimizationResponse.FAILED);
            throw new Exception(
                "No progress achieved, exit iterations loop without desired accuracy");
          }
        }
        previousRPriXNorm = rPriXNorm;
        previousRDualXLVNorm = rDualXLVNorm;
        previousSurrDG = surrDG;
      }

      // compute primal-dual search direction
      // a) prepare 11.55 system
      DoubleMatrix2D Hpd = GradLSum(L, fiX);
      // DoubleMatrix2D HpdOLD = GradLSumOLD(GradFiXOLD, L, fiX);
      // log.debug("delta: " + ALG.normInfinity(HpdOLD.assign(Hpd, Functions.minus)));
      DoubleMatrix1D gradSum = gradSum(t, fiX);
      DoubleMatrix1D g = null;
      // if(getAT()==null){
      if (getA() == null) {
        g = ColtUtils.add(gradF0X, gradSum);
      } else {
        // g = ColtUtils.add(ColtUtils.add(gradF0X, gradSum), ALG.mult(getAT(), V));
        g =
            ColtUtils.add(
                ColtUtils.add(gradF0X, gradSum),
                ColtUtils.zMultTranspose(getA(), V, F1.make(getDim()), 0));
      }

      // b) solving 11.55 system
      if (this.kktSolver == null) {
        this.kktSolver = new UpperDiagonalHKKTSolver(getDim(), lpRequest.isRescalingDisabled());
        // this.kktSolver = new DiagonalHKKTSolver(getDim(), lpRequest.isRescalingDisabled());
      }
      if (isCheckKKTSolutionAccuracy()) {
        kktSolver.setCheckKKTSolutionAccuracy(true);
        kktSolver.setToleranceKKT(getToleranceKKT());
      }
      kktSolver.setHMatrix(Hpd);
      kktSolver.setGVector(g);
      if (getA() != null) {
        kktSolver.setAMatrix(getA());
        // kktSolver.setATMatrix(getAT());
        kktSolver.setHVector(rPriX);
        //				if(rPriXNorm > getToleranceFeas()){
        //					kktSolver.setHVector(rPriX);
        //				}
      }
      DoubleMatrix1D[] sol = kktSolver.solve();
      DoubleMatrix1D stepX = sol[0];
      // double[] signa = new double[stepX.size()];
      //			for(int p=0; p<stepX.size(); p++){
      //				signa[p] =	Math.signum(stepX.getQuick(p));
      //			}
      // SList.add(SList.size(), signa);
      DoubleMatrix1D stepV = (sol[1] != null) ? sol[1] : F1.make(0);
      if (log.isDebugEnabled()) {
        log.debug("stepX: " + ArrayUtils.toString(stepX.toArray()));
        log.debug("stepV: " + ArrayUtils.toString(stepV.toArray()));
      }

      // c) solving for L
      DoubleMatrix1D stepL = F1.make(getMieq());
      DoubleMatrix1D gradFiStepX = gradFiStepX(stepX);
      for (int i = 0; i < getMieq(); i++) {
        stepL.setQuick(
            i, (-L.getQuick(i) * gradFiStepX.getQuick(i) + rCentXLt.getQuick(i)) / fiX.getQuick(i));
      }
      if (log.isDebugEnabled()) {
        log.debug("stepL: " + ArrayUtils.toString(stepL.toArray()));
      }

      // line search and update
      // a) sMax computation
      double sMax = Double.MAX_VALUE;
      for (int j = 0; j < getMieq(); j++) {
        if (stepL.get(j) < 0) {
          sMax = Math.min(-L.get(j) / stepL.get(j), sMax);
        }
      }
      sMax = Math.min(1, sMax);
      double s = 0.99 * sMax;
      // b) backtracking with f
      DoubleMatrix1D X1 = F1.make(X.size());
      DoubleMatrix1D L1 = F1.make(L.size());
      DoubleMatrix1D V1 = F1.make(V.size());
      DoubleMatrix1D fiX1 = null;
      DoubleMatrix1D gradF0X1 = null;
      // DoubleMatrix2D GradFiX1 = null;
      // DoubleMatrix2D GradFiX1 = null;
      DoubleMatrix1D rPriX1 = null;
      DoubleMatrix1D rCentX1L1t = null;
      DoubleMatrix1D rDualX1L1V1 = null;
      int cnt = 0;
      boolean areAllNegative = true;
      while (cnt < 500) {
        cnt++;
        // X1 = X + s*stepX
        X1 = stepX.copy().assign(Mult.mult(s)).assign(X, Functions.plus);
        DoubleMatrix1D ineqValueX1 = getFi(X1);
        areAllNegative = true;
        for (int j = 0; areAllNegative && j < getMieq(); j++) {
          areAllNegative = (Double.compare(ineqValueX1.get(j), 0.) < 0);
        }
        if (areAllNegative) {
          break;
        }
        s = getBeta() * s;
      }

      if (!areAllNegative) {
        // exited from the feasible region
        throw new Exception("Optimization failed: impossible to remain within the faesible region");
      }

      log.debug("s: " + s);
      // c) backtracking with norm
      double previousNormRX1L1V1t = Double.NaN;
      cnt = 0;
      while (cnt < 500) {
        cnt++;
        X1 = ColtUtils.add(X, stepX, s);
        L1 = ColtUtils.add(L, stepL, s);
        V1 = ColtUtils.add(V, stepV, s);

        if (isInDomainF0(X1)) {
          fiX1 = getFi(X1);
          gradF0X1 = getGradF0(X1);
          // GradFiX1 = getGradFi(X1);

          rPriX1 = rPri(X1);
          rCentX1L1t = rCent(fiX1, L1, t);
          rDualX1L1V1 = rDual(gradF0X1, L1, V1);
          double normRX1L1V1t =
              Math.sqrt(ALG.norm2(rPriX1) + ALG.norm2(rCentX1L1t) + ALG.norm2(rDualX1L1V1));
          // log.debug("normRX1L1V1t: "+normRX1L1V1t);
          if (normRX1L1V1t <= (1 - getAlpha() * s) * normRXLVt) {
            break;
          }

          if (!Double.isNaN(previousNormRX1L1V1t)) {
            if (previousNormRX1L1V1t <= normRX1L1V1t) {
              log.warn("No progress achieved in backtracking with norm");
              break;
            }
          }
          previousNormRX1L1V1t = normRX1L1V1t;
        }

        s = getBeta() * s;
        // log.debug("s: " + s);
      }

      // update
      X = X1;
      V = V1;
      L = L1;
    }

    //		if(lpRequest.isCheckOptimalDualityConditions()){
    //			//check duality conditions:
    //			if(!checkDualityConditions(X, L, V)){
    //				log.error("duality conditions not satisfied");
    //				lpResponse.setReturnCode(OptimizationResponse.FAILED);
    //				throw new Exception("duality conditions not satisfied");
    //			}
    //		}

    if (lpRequest.isCheckOptimalLagrangianBounds()) {
      // check equality constraints Lagrangian bounds
      //			if(!checkEqConstraintsLagrangianBounds(V)){
      //				log.error("equality constraints Lagrangian multipliers bounds not satisfied");
      //				lpResponse.setReturnCode(OptimizationResponse.FAILED);
      //				throw new Exception("equality constraints Lagrangian multipliers bounds not satisfied");
      //			}

      // check inequality constraints Lagrangian bounds
      //			if(!checkIneqConstraintsLagrangianBounds(X, L)){
      //				log.error("inequality constraints Lagrangian multipliers bounds not satisfied");
      //				lpResponse.setReturnCode(OptimizationResponse.FAILED);
      //				throw new Exception("inequality constraints Lagrangian multipliers bounds not
      // satisfied");
      //			}
    }

    long tStop = System.currentTimeMillis();
    log.debug("time: " + (tStop - tStart));
    log.debug("sol : " + ArrayUtils.toString(X.toArray()));
    log.debug("ret code: " + lpResponse.getReturnCode());
    //		log.debug("XList : " + ArrayUtils.toString(XList));
    //		for(int s=0; s<SList.size(); s++){
    //			log.debug("SList : " + ArrayUtils.toString(SList.get(s)));
    //		}
    lpResponse.setSolution(X.toArray());
    setLPOptimizationResponse(lpResponse);
    return lpResponse.getReturnCode();
  }
Beispiel #13
0
  public String classify(Collection<Rules> cr, DoubleMatrix1D feature) throws GggpExceptionImpl {

    boolean evaluation;
    Iterator<Rules> itr;
    String res = null;
    Map<String, Double> aux;
    int normalClass;
    int injuryClass;
    Rules r;

    if ((cr != null) && (!cr.isEmpty()) && (feature != null)) {

      aux = new ConcurrentHashMap<String, Double>();

      /** The features's real values are from 0 to 17. */
      aux.put((new String("secDifTorMax").toUpperCase()), feature.get(0));
      aux.put((new String("secDifAngTorMax").toUpperCase()), feature.get(1));
      aux.put((new String("secDifTorMin").toUpperCase()), feature.get(2));
      aux.put((new String("secDifAngTorMin").toUpperCase()), feature.get(3));
      aux.put((new String("torMax").toUpperCase()), feature.get(4));
      aux.put((new String("angTorMax").toUpperCase()), feature.get(5));
      aux.put((new String("timTorMax").toUpperCase()), feature.get(6));
      aux.put((new String("torMin").toUpperCase()), feature.get(7));
      aux.put((new String("angTorMin").toUpperCase()), feature.get(8));
      aux.put((new String("timTorMin").toUpperCase()), feature.get(9));
      aux.put((new String("timAvgTorMaxExt").toUpperCase()), feature.get(10));
      aux.put((new String("desAvgTimMaxExt").toUpperCase()), feature.get(11));
      aux.put((new String("timAvgTorMinFlx").toUpperCase()), feature.get(12));
      aux.put((new String("desAvgTimMinFlx").toUpperCase()), feature.get(13));
      aux.put((new String("torAvgExt").toUpperCase()), feature.get(14));
      aux.put((new String("desAvgTorExt").toUpperCase()), feature.get(15));
      aux.put((new String("torAvgFlx").toUpperCase()), feature.get(16));
      aux.put((new String("desAvgTorFlx").toUpperCase()), feature.get(17));

      itr = cr.iterator();

      normalClass = 0;
      injuryClass = 0;

      while (itr.hasNext()) {

        r = itr.next();

        evaluation = r.evaluatesRule(aux, r.getAntecedent());

        if (evaluation) {

          if (((r.getConsecuent()).toUpperCase()).equals("NORMAL")) {

            normalClass++;

          } else {

            injuryClass++;
          }
        }
      }

      if (normalClass > injuryClass) {
        res = new String("NORMAL");
      } else {
        if (normalClass < injuryClass) {
          res = new String("INJURY");
        } else {
          res = new String("INDEFINITE");
        }
      }
    }

    return res;
  }
Beispiel #14
0
  protected double computeFunctionGradientLL(double lambda[], double grad[]) {
    double logli = 0;
    try {
      for (int f = 0; f < lambda.length; f++) {
        grad[f] = -1 * lambda[f] * params.invSigmaSquare;
        logli -= ((lambda[f] * lambda[f]) * params.invSigmaSquare) / 2;
      }
      diter.startScan();
      if (featureGenCache != null) featureGenCache.startDataScan();
      for (int numRecord = 0; diter.hasNext(); numRecord++) {
        DataSequence dataSeq = (DataSequence) diter.next();
        if (featureGenCache != null) featureGenCache.nextDataIndex();
        if (params.debugLvl > 1) {
          Util.printDbg("Read next seq: " + numRecord + " logli " + logli);
        }
        alpha_Y.assign(0);
        for (int f = 0; f < lambda.length; f++) ExpF[f] = RobustMath.LOG0;

        if ((beta_Y == null) || (beta_Y.length < dataSeq.length())) {
          beta_Y = new DenseDoubleMatrix1D[2 * dataSeq.length()];
          for (int i = 0; i < beta_Y.length; i++) beta_Y[i] = new DenseDoubleMatrix1D(numY);
        }
        // compute beta values in a backward scan.
        // also scale beta-values to 1 to avoid numerical problems.
        beta_Y[dataSeq.length() - 1].assign(0);
        for (int i = dataSeq.length() - 1; i > 0; i--) {
          if (params.debugLvl > 2) {
            /*  Util.printDbg("Features fired");
            featureGenerator.startScanFeaturesAt(dataSeq, i);
            while (featureGenerator.hasNext()) {
            Feature feature = featureGenerator.next();
            Util.printDbg(feature.toString());
            }
            */
          }

          // compute the Mi matrix
          initMDone =
              computeLogMi(
                  featureGenerator, lambda, dataSeq, i, Mi_YY, Ri_Y, false, reuseM, initMDone);
          tmp_Y.assign(beta_Y[i]);
          tmp_Y.assign(Ri_Y, sumFunc);
          RobustMath.logMult(Mi_YY, tmp_Y, beta_Y[i - 1], 1, 0, false, edgeGen);
        }

        double thisSeqLogli = 0;
        for (int i = 0; i < dataSeq.length(); i++) {
          // compute the Mi matrix
          initMDone =
              computeLogMi(
                  featureGenerator, lambda, dataSeq, i, Mi_YY, Ri_Y, false, reuseM, initMDone);
          // find features that fire at this position..
          featureGenerator.startScanFeaturesAt(dataSeq, i);

          if (i > 0) {
            tmp_Y.assign(alpha_Y);
            RobustMath.logMult(Mi_YY, tmp_Y, newAlpha_Y, 1, 0, true, edgeGen);
            newAlpha_Y.assign(Ri_Y, sumFunc);
          } else {
            newAlpha_Y.assign(Ri_Y);
          }

          while (featureGenerator.hasNext()) {
            Feature feature = featureGenerator.next();
            int f = feature.index();

            int yp = feature.y();
            int yprev = feature.yprev();
            float val = feature.value();

            if ((dataSeq.y(i) == yp)
                && (((i - 1 >= 0) && (yprev == dataSeq.y(i - 1))) || (yprev < 0))) {
              grad[f] += val;
              thisSeqLogli += val * lambda[f];
              if (params.debugLvl > 2) {
                System.out.println("Feature fired " + f + " " + feature);
              }
            }

            if (yprev < 0) {
              ExpF[f] =
                  RobustMath.logSumExp(
                      ExpF[f], newAlpha_Y.get(yp) + RobustMath.log(val) + beta_Y[i].get(yp));
            } else {
              ExpF[f] =
                  RobustMath.logSumExp(
                      ExpF[f],
                      alpha_Y.get(yprev)
                          + Ri_Y.get(yp)
                          + Mi_YY.get(yprev, yp)
                          + RobustMath.log(val)
                          + beta_Y[i].get(yp));
            }
          }
          alpha_Y.assign(newAlpha_Y);

          if (params.debugLvl > 2) {
            System.out.println("Alpha-i " + alpha_Y.toString());
            System.out.println("Ri " + Ri_Y.toString());
            System.out.println("Mi " + Mi_YY.toString());
            System.out.println("Beta-i " + beta_Y[i].toString());
          }
        }
        double lZx = RobustMath.logSumExp(alpha_Y);
        thisSeqLogli -= lZx;
        logli += thisSeqLogli;
        // update grad.
        for (int f = 0; f < grad.length; f++) {
          grad[f] -= RobustMath.exp(ExpF[f] - lZx);
        }
        if (params.debugLvl > 1) {
          System.out.println(
              "Sequence "
                  + thisSeqLogli
                  + " logli "
                  + logli
                  + " log(Zx) "
                  + lZx
                  + " Zx "
                  + Math.exp(lZx));
        }
      }
      if (params.debugLvl > 2) {
        for (int f = 0; f < lambda.length; f++) System.out.print(lambda[f] + " ");
        System.out.println(" :x");
        for (int f = 0; f < lambda.length; f++) System.out.print(grad[f] + " ");
        System.out.println(" :g");
      }

      if (params.debugLvl > 0)
        Util.printDbg(
            "Iteration "
                + icall
                + " log-likelihood "
                + logli
                + " norm(grad logli) "
                + norm(grad)
                + " norm(x) "
                + norm(lambda));

    } catch (Exception e) {
      System.out.println("Alpha-i " + alpha_Y.toString());
      System.out.println("Ri " + Ri_Y.toString());
      System.out.println("Mi " + Mi_YY.toString());

      e.printStackTrace();
      System.exit(0);
    }
    return logli;
  }
Beispiel #15
0
  protected double computeFunctionGradient(double lambda[], double grad[]) {
    initMDone = false;

    if (params.trainerType.equals("ll")) return computeFunctionGradientLL(lambda, grad);
    double logli = 0;
    try {
      for (int f = 0; f < lambda.length; f++) {
        grad[f] = -1 * lambda[f] * params.invSigmaSquare;
        logli -= ((lambda[f] * lambda[f]) * params.invSigmaSquare) / 2;
      }
      boolean doScaling = params.doScaling;

      diter.startScan();
      if (featureGenCache != null) featureGenCache.startDataScan();
      int numRecord = 0;
      for (numRecord = 0; diter.hasNext(); numRecord++) {
        DataSequence dataSeq = (DataSequence) diter.next();
        if (featureGenCache != null) featureGenCache.nextDataIndex();
        if (params.debugLvl > 1) {
          Util.printDbg("Read next seq: " + numRecord + " logli " + logli);
        }
        alpha_Y.assign(1);
        for (int f = 0; f < lambda.length; f++) ExpF[f] = 0;

        if ((beta_Y == null) || (beta_Y.length < dataSeq.length())) {
          beta_Y = new DenseDoubleMatrix1D[2 * dataSeq.length()];
          for (int i = 0; i < beta_Y.length; i++) beta_Y[i] = new DenseDoubleMatrix1D(numY);

          scale = new double[2 * dataSeq.length()];
        }
        // compute beta values in a backward scan.
        // also scale beta-values to 1 to avoid numerical problems.
        scale[dataSeq.length() - 1] = (doScaling) ? numY : 1;
        beta_Y[dataSeq.length() - 1].assign(1.0 / scale[dataSeq.length() - 1]);
        for (int i = dataSeq.length() - 1; i > 0; i--) {
          if (params.debugLvl > 2) {
            Util.printDbg("Features fired");
            // featureGenerator.startScanFeaturesAt(dataSeq, i);
            // while (featureGenerator.hasNext()) {
            // Feature feature = featureGenerator.next();
            // Util.printDbg(feature.toString());
            // }
          }

          // compute the Mi matrix
          initMDone =
              computeLogMi(
                  featureGenerator, lambda, dataSeq, i, Mi_YY, Ri_Y, true, reuseM, initMDone);
          tmp_Y.assign(beta_Y[i]);
          tmp_Y.assign(Ri_Y, multFunc);
          RobustMath.Mult(Mi_YY, tmp_Y, beta_Y[i - 1], 1, 0, false, edgeGen);
          //		Mi_YY.zMult(tmp_Y, beta_Y[i-1]);

          // need to scale the beta-s to avoid overflow
          scale[i - 1] = doScaling ? beta_Y[i - 1].zSum() : 1;
          if ((scale[i - 1] < 1) && (scale[i - 1] > -1)) scale[i - 1] = 1;
          constMultiplier.multiplicator = 1.0 / scale[i - 1];
          beta_Y[i - 1].assign(constMultiplier);
        }

        double thisSeqLogli = 0;
        for (int i = 0; i < dataSeq.length(); i++) {
          // compute the Mi matrix
          initMDone =
              computeLogMi(
                  featureGenerator, lambda, dataSeq, i, Mi_YY, Ri_Y, true, reuseM, initMDone);
          // find features that fire at this position..
          featureGenerator.startScanFeaturesAt(dataSeq, i);

          if (i > 0) {
            tmp_Y.assign(alpha_Y);
            RobustMath.Mult(Mi_YY, tmp_Y, newAlpha_Y, 1, 0, true, edgeGen);
            //		Mi_YY.zMult(tmp_Y, newAlpha_Y,1,0,true);
            newAlpha_Y.assign(Ri_Y, multFunc);
          } else {
            newAlpha_Y.assign(Ri_Y);
          }
          while (featureGenerator.hasNext()) {
            Feature feature = featureGenerator.next();
            int f = feature.index();

            int yp = feature.y();
            int yprev = feature.yprev();
            float val = feature.value();
            if ((dataSeq.y(i) == yp)
                && (((i - 1 >= 0) && (yprev == dataSeq.y(i - 1))) || (yprev < 0))) {
              grad[f] += val;
              thisSeqLogli += val * lambda[f];
            }
            if (yprev < 0) {
              ExpF[f] += newAlpha_Y.get(yp) * val * beta_Y[i].get(yp);
            } else {
              ExpF[f] +=
                  alpha_Y.get(yprev)
                      * Ri_Y.get(yp)
                      * Mi_YY.get(yprev, yp)
                      * val
                      * beta_Y[i].get(yp);
            }
          }

          alpha_Y.assign(newAlpha_Y);
          // now scale the alpha-s to avoid overflow problems.
          constMultiplier.multiplicator = 1.0 / scale[i];
          alpha_Y.assign(constMultiplier);

          if (params.debugLvl > 2) {
            System.out.println("Alpha-i " + alpha_Y.toString());
            System.out.println("Ri " + Ri_Y.toString());
            System.out.println("Mi " + Mi_YY.toString());
            System.out.println("Beta-i " + beta_Y[i].toString());
          }
        }
        double Zx = alpha_Y.zSum();
        thisSeqLogli -= log(Zx);
        // correct for the fact that alpha-s were scaled.
        for (int i = 0; i < dataSeq.length(); i++) {
          thisSeqLogli -= log(scale[i]);
        }

        logli += thisSeqLogli;
        // update grad.
        for (int f = 0; f < grad.length; f++) grad[f] -= ExpF[f] / Zx;

        if (params.debugLvl > 1) {
          System.out.println(
              "Sequence "
                  + thisSeqLogli
                  + " logli "
                  + logli
                  + " log(Zx) "
                  + Math.log(Zx)
                  + " Zx "
                  + Zx);
        }
      }
      if (params.debugLvl > 2) {
        for (int f = 0; f < lambda.length; f++) System.out.print(lambda[f] + " ");
        System.out.println(" :x");
        for (int f = 0; f < lambda.length; f++)
          System.out.println(featureGenerator.featureName(f) + " " + grad[f] + " ");
        System.out.println(" :g");
      }

      if (params.debugLvl > 0)
        Util.printDbg(
            "Iter "
                + icall
                + " log likelihood "
                + logli
                + " norm(grad logli) "
                + norm(grad)
                + " norm(x) "
                + norm(lambda));
      if (icall == 0) {
        System.out.println("Number of training records" + numRecord);
      }
    } catch (Exception e) {
      System.out.println("Alpha-i " + alpha_Y.toString());
      System.out.println("Ri " + Ri_Y.toString());
      System.out.println("Mi " + Mi_YY.toString());

      e.printStackTrace();
      System.exit(0);
    }
    return logli;
  }