Search in sources :

Example 6 with PrimitiveDenseStore

use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.

the class MatrixUtils method makeSPD.

/**
 * Make a random symmetric positive definite matrix
 */
public static PrimitiveDenseStore makeSPD(final int dim) {
    final double[] tmpRandom = new double[dim];
    final PrimitiveDenseStore retVal = PrimitiveDenseStore.FACTORY.makeZero(dim, dim);
    for (int i = 0; i < dim; i++) {
        tmpRandom[i] = Math.random();
        for (int j = 0; j < i; j++) {
            retVal.set(i, j, tmpRandom[i] * tmpRandom[j]);
            retVal.set(j, i, tmpRandom[j] * tmpRandom[i]);
        }
        retVal.set(i, i, tmpRandom[i] + 1.0);
    }
    return retVal;
}
Also used : PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore)

Example 7 with PrimitiveDenseStore

use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.

the class MatrixUtils method getComplexReal.

/**
 * Extracts the real part of the ComplexNumber elements to a new primitive double valued matrix.
 */
public static PrimitiveDenseStore getComplexReal(final Access2D<ComplexNumber> arg) {
    final long tmpRows = arg.countRows();
    final long tmpColumns = arg.countColumns();
    final PrimitiveDenseStore retVal = PrimitiveDenseStore.FACTORY.makeZero(tmpRows, tmpColumns);
    MatrixUtils.copyComplexReal(arg, retVal);
    return retVal;
}
Also used : PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore)

Example 8 with PrimitiveDenseStore

use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.

the class DirectASS method performIteration.

@SuppressWarnings("deprecation")
@Override
protected void performIteration() {
    if (this.isDebug()) {
        this.log("\nPerformIteration {}", 1 + this.countIterations());
        this.log(this.toActivatorString());
    }
    this.getConstraintToInclude();
    this.setConstraintToInclude(-1);
    final int[] incl = this.getIncluded();
    final int[] excl = this.getExcluded();
    boolean solved = false;
    final int numbConstr = this.countIterationConstraints();
    final int numbVars = this.countVariables();
    final PrimitiveDenseStore iterX = this.getIterationX();
    final PrimitiveDenseStore iterL = PrimitiveDenseStore.FACTORY.makeZero(numbConstr, 1L);
    final PrimitiveDenseStore soluL = this.getSolutionL();
    if ((numbConstr < numbVars) && (solved = this.isSolvableQ())) {
        if (numbConstr == 0L) {
            // Unconstrained - can happen when there are no equality constraints and all inequalities are inactive
            iterX.fillMatching(this.getInvQC());
        } else {
            // Actual/normal optimisation problem
            final MatrixStore<Double> iterA = this.getIterationA();
            final MatrixStore<Double> iterB = this.getIterationB();
            final MatrixStore<Double> iterC = this.getIterationC();
            final MatrixStore<Double> tmpInvQAT = this.getSolutionQ(iterA.transpose());
            // TODO Only 1 column change inbetween active set iterations (add or remove 1 column)
            // BasicLogger.debug("tmpInvQAT", tmpInvQAT);
            // Negated Schur complement
            final ElementsSupplier<Double> tmpS = tmpInvQAT.premultiply(iterA);
            if (this.isDebug()) {
                BasicLogger.debug("Negated Schur complement: " + Arrays.toString(incl), tmpS.get());
            }
            if (solved = this.computeGeneral(tmpS)) {
                this.getSolutionGeneral(this.getInvQC().premultiply(iterA).operateOnMatching(SUBTRACT, iterB), iterL);
                if (this.isDebug()) {
                    this.log("Relative error {} in solution for L={}", PrimitiveMath.NaN, iterL);
                }
                final ElementsSupplier<Double> tmpRHS = iterL.premultiply(iterA.transpose()).operateOnMatching(iterC, SUBTRACT);
                this.getSolutionQ(tmpRHS, iterX);
            }
        }
    }
    if (!solved) {
        // The above failed, try solving the full KKT system instaed
        final PrimitiveDenseStore tmpXL = PrimitiveDenseStore.FACTORY.makeZero(numbVars + numbConstr, 1L);
        if (solved = this.solveFullKKT(tmpXL)) {
            iterX.fillMatching(tmpXL.logical().limits(numbVars, 1).get());
            iterL.fillMatching(tmpXL.logical().offsets(numbVars, 0).get());
        }
    }
    soluL.fillAll(0.0);
    if (solved) {
        for (int i = 0; i < this.countEqualityConstraints(); i++) {
            soluL.set(i, iterL.doubleValue(i));
        }
        for (int i = 0; i < incl.length; i++) {
            soluL.set(this.countEqualityConstraints() + incl[i], iterL.doubleValue(this.countEqualityConstraints() + i));
        }
    }
    this.handleIterationResults(solved, iterX, incl, excl);
}
Also used : PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore)

Example 9 with PrimitiveDenseStore

use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.

the class QPESolver method performIteration.

@Override
protected void performIteration() {
    this.getIterationQ();
    final MatrixStore<Double> tmpIterC = this.getIterationC();
    final MatrixStore<Double> tmpIterA = this.getIterationA();
    final MatrixStore<Double> tmpIterB = this.getIterationB();
    boolean solved = false;
    final PrimitiveDenseStore tmpIterX = myIterationX;
    final PrimitiveDenseStore tmpIterL = FACTORY.makeZero(tmpIterA.countRows(), 1L);
    if ((tmpIterA.countRows() < tmpIterA.countColumns()) && (solved = this.isSolvableQ())) {
        // Q is SPD
        // Actual/normal optimisation problem
        final MatrixStore<Double> tmpInvQAT = this.getSolutionQ(tmpIterA.transpose());
        // TODO Only 1 column change inbetween active set iterations (add or remove 1 column)
        // Negated Schur complement
        final MatrixStore<Double> tmpS = tmpIterA.multiply(tmpInvQAT);
        // TODO Symmetric, only need to calculate halv the Schur complement
        if (solved = this.computeGeneral(tmpS)) {
            // tmpX temporarely used to store tmpInvQC
            // TODO Constant if C doesn't change
            final MatrixStore<Double> tmpInvQC = this.getSolutionQ(tmpIterC, tmpIterX);
            this.getSolutionGeneral(tmpIterA.multiply(tmpInvQC).subtract(tmpIterB), tmpIterL);
            this.getSolutionQ(tmpIterC.subtract(tmpIterA.transpose().multiply(tmpIterL)), tmpIterX);
        }
    }
    if (!solved) {
        // The above failed, try solving the full KKT system instaed
        final PrimitiveDenseStore tmpXL = FACTORY.makeZero(this.countVariables() + this.countIterationConstraints(), 1L);
        if (solved = this.solveFullKKT(tmpXL)) {
            tmpIterX.fillMatching(tmpXL.logical().limits(this.countVariables(), 1).get());
            tmpIterL.fillMatching(tmpXL.logical().offsets(this.countVariables(), 0).get());
        }
    }
    if (solved) {
        this.setState(State.OPTIMAL);
        if (myFeasible) {
            this.getSolutionX().modifyMatching(PrimitiveFunction.ADD, tmpIterX);
        } else {
            this.getSolutionX().fillMatching(tmpIterX);
        }
    // this.getLE().fillMatching(tmpIterL);
    } else {
        if (myFeasible) {
            this.setState(State.FEASIBLE);
        } else {
            this.setState(State.INFEASIBLE);
            this.getSolutionX().fillAll(ZERO);
        }
    }
}
Also used : PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore)

Example 10 with PrimitiveDenseStore

use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.

the class Quaternion method toRotationMatrix.

public MatrixStore<Double> toRotationMatrix() {
    final PrimitiveDenseStore retVal = PrimitiveDenseStore.FACTORY.makeZero(3L, 3L);
    final double s = myScalar;
    final double ss = s * s;
    final double ii = i * i;
    final double jj = j * j;
    final double kk = k * k;
    double tmp1;
    double tmp2;
    final double invs = 1.0 / (ii + jj + kk + ss);
    final double r00 = ((ii + ss) - (jj + kk)) * invs;
    final double r11 = ((jj + ss) - (ii + kk)) * invs;
    final double r22 = ((kk + ss) - (ii + jj)) * invs;
    tmp1 = i * j;
    tmp2 = k * s;
    final double r10 = 2.0 * (tmp1 + tmp2) * invs;
    final double r01 = 2.0 * (tmp1 - tmp2) * invs;
    tmp1 = i * k;
    tmp2 = j * s;
    final double r20 = 2.0 * (tmp1 - tmp2) * invs;
    final double r02 = 2.0 * (tmp1 + tmp2) * invs;
    tmp1 = j * k;
    tmp2 = i * s;
    final double r21 = 2.0 * (tmp1 + tmp2) * invs;
    final double r12 = 2.0 * (tmp1 - tmp2) * invs;
    retVal.set(0L, r00);
    retVal.set(1L, r10);
    retVal.set(2L, r20);
    retVal.set(3L, r01);
    retVal.set(4L, r11);
    retVal.set(5L, r21);
    retVal.set(6L, r02);
    retVal.set(7L, r12);
    retVal.set(8L, r22);
    return retVal;
}
Also used : PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore)

Aggregations

PrimitiveDenseStore (org.ojalgo.matrix.store.PrimitiveDenseStore)72 Test (org.junit.jupiter.api.Test)37 Optimisation (org.ojalgo.optimisation.Optimisation)16 NumberContext (org.ojalgo.type.context.NumberContext)15 ExpressionsBasedModel (org.ojalgo.optimisation.ExpressionsBasedModel)12 ComparableToDouble (org.ojalgo.type.keyvalue.ComparableToDouble)7 Result (org.ojalgo.optimisation.Optimisation.Result)6 ComplexNumber (org.ojalgo.scalar.ComplexNumber)4 SimultaneousPrimitive (org.ojalgo.matrix.decomposition.HermitianEvD.SimultaneousPrimitive)3 PhysicalStore (org.ojalgo.matrix.store.PhysicalStore)3 Builder (org.ojalgo.optimisation.convex.ConvexSolver.Builder)3 IntRowColumn (org.ojalgo.access.Structure2D.IntRowColumn)2 ComplexDenseStore (org.ojalgo.matrix.store.ComplexDenseStore)2 LinearSolver (org.ojalgo.optimisation.linear.LinearSolver)2 Normal (org.ojalgo.random.Normal)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Disabled (org.junit.jupiter.api.Disabled)1 Tag (org.junit.jupiter.api.Tag)1 ProgrammingError (org.ojalgo.ProgrammingError)1