Example #1
0
  /** Solves a standard form LP problem in the form of min(c) s.t. A.x = b lb <= x <= ub */
  protected int optimizeStandardLP(int nOfSlackVariables) throws Exception {
    log.info("optimizeStandardLP");

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

    LPOptimizationResponse lpResponse;
    if (lpRequest.isPresolvingDisabled()) {
      // optimization
      LPPrimalDualMethod opt = new LPPrimalDualMethod(minLBValue, maxUBValue);
      opt.setLPOptimizationRequest(lpRequest);
      if (opt.optimizePresolvedStandardLP() == OptimizationResponse.FAILED) {
        return OptimizationResponse.FAILED;
      }
      lpResponse = opt.getLPOptimizationResponse();
      setLPOptimizationResponse(lpResponse);
    } else {
      // presolving
      LPPresolver lpPresolver = new LPPresolver();
      lpPresolver.setAvoidScaling(lpRequest.isRescalingDisabled());
      lpPresolver.setAvoidFillIn(lpRequest.isAvoidPresolvingFillIn());
      lpPresolver.setAvoidIncreaseSparsity(lpRequest.isAvoidPresolvingIncreaseSparsity());
      testPresolver = lpPresolver; // just for testing
      lpPresolver.setNOfSlackVariables((short) nOfSlackVariables);
      lpPresolver.presolve(getC(), getA(), getB(), getLb(), getUb());
      int presolvedDim = lpPresolver.getPresolvedN();

      if (presolvedDim == 0) {
        // deterministic problem
        log.debug("presolvedDim : " + presolvedDim);
        log.debug("deterministic LP problem");
        lpResponse = new LPOptimizationResponse();
        lpResponse.setReturnCode(OptimizationResponse.SUCCESS);
        lpResponse.setSolution(new double[] {});
      } else {
        // solving the presolved problem
        DoubleMatrix1D presolvedC = lpPresolver.getPresolvedC();
        DoubleMatrix2D presolvedA = lpPresolver.getPresolvedA();
        DoubleMatrix1D presolvedB = lpPresolver.getPresolvedB();
        if (log.isDebugEnabled()) {
          if (lpPresolver.getPresolvedYlb() != null) {
            log.debug("Ylb: " + ArrayUtils.toString(lpPresolver.getPresolvedYlb().toArray()));
            log.debug("Yub: " + ArrayUtils.toString(lpPresolver.getPresolvedYub().toArray()));
          }
          if (lpPresolver.getPresolvedZlb() != null) {
            log.debug("Zlb: " + ArrayUtils.toString(lpPresolver.getPresolvedZlb().toArray()));
            log.debug("Zub: " + ArrayUtils.toString(lpPresolver.getPresolvedZub().toArray()));
          }
        }

        // new LP problem (the presolved problem)
        LPOptimizationRequest presolvedLPRequest = lpRequest.cloneMe();
        presolvedLPRequest.setC(presolvedC);
        presolvedLPRequest.setA(presolvedA);
        presolvedLPRequest.setB(presolvedB);
        presolvedLPRequest.setLb(lpPresolver.getPresolvedLB());
        presolvedLPRequest.setUb(lpPresolver.getPresolvedUB());
        presolvedLPRequest.setYlb(lpPresolver.getPresolvedYlb());
        presolvedLPRequest.setYub(lpPresolver.getPresolvedYub());
        presolvedLPRequest.setZlb(lpPresolver.getPresolvedZlb());
        presolvedLPRequest.setZub(lpPresolver.getPresolvedZub());
        if (getInitialPoint() != null) {
          presolvedLPRequest.setInitialPoint(lpPresolver.presolve(getInitialPoint().toArray()));
        }
        if (getNotFeasibleInitialPoint() != null) {
          presolvedLPRequest.setNotFeasibleInitialPoint(
              lpPresolver.presolve(getNotFeasibleInitialPoint().toArray()));
        }

        // optimization
        // NB: because of rescaling during the presolving phase, minLB and maxUB could have been
        // rescaled
        double rescaledMinLBValue =
            (Double.isNaN(lpPresolver.getMinRescaledLB()))
                ? this.minLBValue
                : lpPresolver.getMinRescaledLB();
        double rescaledMaxUBValue =
            (Double.isNaN(lpPresolver.getMaxRescaledUB()))
                ? this.maxUBValue
                : lpPresolver.getMaxRescaledUB();
        LPPrimalDualMethod opt = new LPPrimalDualMethod(rescaledMinLBValue, rescaledMaxUBValue);
        opt.setLPOptimizationRequest(presolvedLPRequest);
        if (opt.optimizePresolvedStandardLP() == OptimizationResponse.FAILED) {
          return OptimizationResponse.FAILED;
        }
        lpResponse = opt.getLPOptimizationResponse();
      }

      // postsolving
      double[] postsolvedSolution = lpPresolver.postsolve(lpResponse.getSolution());
      lpResponse.setSolution(postsolvedSolution);
      setLPOptimizationResponse(lpResponse);
    }

    return lpResponse.getReturnCode();
  }