private void extractSolution(final QuadraticSolver aSolver) {

    final MatrixStore<Double> tmpSolutionX = aSolver.getSolutionX();

    final int tmpCountVariables = this.countVariables();
    final int tmpCountEqualityConstraints = this.countEqualityConstraints();

    for (int i = 0; i < tmpCountVariables; i++) {
      this.setX(i, tmpSolutionX.doubleValue(i));
    }

    for (int i = 0; i < tmpCountEqualityConstraints; i++) {
      this.setLE(i, tmpSolutionX.doubleValue(tmpCountVariables + i));
    }
  }
Beispiel #2
0
  @Override
  protected MatrixStore<Double> extractSolution() {

    final ExpressionsBasedModel tmpModel = this.getModel();

    if (tmpModel != null) {

      final List<Variable> tmpFreeVariables = tmpModel.getFreeVariables();
      final Set<Index> tmpFixedVariables = tmpModel.getFixedVariables();

      final PrimitiveDenseStore retVal =
          PrimitiveDenseStore.FACTORY.makeZero(
              tmpFixedVariables.size() + tmpFreeVariables.size(), 1);

      for (final Index tmpVariable : tmpFixedVariables) {
        retVal.set(
            tmpVariable.index, 0, tmpModel.getVariable(tmpVariable.index).getValue().doubleValue());
      }

      final MatrixStore<Double> tmpSolutionX = this.getSolutionX();
      for (int i = 0; i < tmpFreeVariables.size(); i++) {
        final Variable tmpVariable = tmpFreeVariables.get(i);
        final int tmpIndexOf = tmpModel.indexOf(tmpVariable);
        retVal.set(tmpIndexOf, 0, tmpSolutionX.doubleValue(i));
      }

      return retVal;

    } else {

      return this.getSolutionX().copy();
    }
  }
  @Override
  protected boolean validate() {

    boolean retVal = true;
    this.setState(State.VALID);

    try {

      final MatrixStore<Double> tmpQ = this.getQ();

      final Cholesky<Double> tmpCholesky = CholeskyDecomposition.makePrimitive();
      tmpCholesky.compute(tmpQ, true);

      if (!tmpCholesky.isSPD()) {
        // Not positive definite. Check if at least positive semidefinite.

        final Eigenvalue<Double> tmpEigenvalue = EigenvalueDecomposition.makePrimitive(true);
        tmpEigenvalue.compute(tmpQ, true);

        final MatrixStore<Double> tmpD = tmpEigenvalue.getD();

        for (int ij = 0; retVal && (ij < tmpD.getMinDim()); ij++) {
          if (tmpD.doubleValue(ij, ij) < ZERO) {
            retVal = false;
            this.setState(State.INVALID);
          }
        }
      }

      if (retVal) {
        // Q ok, check AE

        //                final MatrixStore<Double> tmpAE = this.getAE();
        //
        //                final LU<Double> tmpLU = LUDecomposition.makePrimitive();
        //                tmpLU.compute(tmpAE);
        //
        //                if (tmpLU.getRank() != tmpAE.getRowDim()) {
        //                    retVal = false;
        //                    this.setState(State.INVALID);
        //                }
      }

    } catch (final Exception ex) {

      retVal = false;
      this.setState(State.FAILED);
    }

    return retVal;
  }