use of org.ojalgo.matrix.store.RawStore in project ojAlgo by optimatika.
the class RawLDL method doSolve.
private MatrixStore<Double> doSolve(final Collectable<Double, ? super PhysicalStore<Double>> rhs, final PhysicalStore<Double> preallocated) {
rhs.supplyTo(preallocated);
final RawStore tmpBody = this.getRawInPlaceStore();
preallocated.substituteForwards(tmpBody, true, false, false);
for (int i = 0; i < preallocated.countRows(); i++) {
preallocated.modifyRow(i, 0, DIVIDE.second(tmpBody.doubleValue(i, i)));
}
preallocated.substituteBackwards(tmpBody, true, true, false);
return preallocated;
}
use of org.ojalgo.matrix.store.RawStore in project ojAlgo by optimatika.
the class RawLDL method decompose.
public boolean decompose(final Access2D.Collectable<Double, ? super PhysicalStore<Double>> matrix) {
final double[][] retVal = this.reset(matrix, false);
final RawStore tmpRawInPlaceStore = this.getRawInPlaceStore();
matrix.supplyTo(tmpRawInPlaceStore);
return this.doDecompose(retVal, tmpRawInPlaceStore);
}
use of org.ojalgo.matrix.store.RawStore in project ojAlgo by optimatika.
the class RawLU method doGetInverse.
private MatrixStore<Double> doGetInverse(final PhysicalStore<Double> preallocated) {
final int[] tmpPivotOrder = myPivot.getOrder();
final int tmpRowDim = this.getRowDim();
for (int i = 0; i < tmpRowDim; i++) {
preallocated.set(i, tmpPivotOrder[i], ONE);
}
final RawStore tmpBody = this.getRawInPlaceStore();
preallocated.substituteForwards(tmpBody, true, false, !myPivot.isModified());
preallocated.substituteBackwards(tmpBody, false, false, false);
return preallocated;
}
use of org.ojalgo.matrix.store.RawStore in project ojAlgo by optimatika.
the class ConvexProblems method testP20140522.
/**
* I’ve been using the QuadraticSolver for a while, and suddenly stumbled over an unexpected failure to
* solve a problem. The solution state was APPROXIMATE and the solution was not correct. I tested the same
* system with another QP-solver and got the result I expected. I then condensed the problem as much as I
* could and made a test out of it (see below). To my surprise the test sometimes fails and sometimes
* passes(!). I’ve been running this test (alone) in TestNG. I’m using Ojalgo v35 and Java 1.7.55. The Q
* matrix is positive definite.
*
* @see "http://bugzilla.optimatika.se/show_bug.cgi?id=210"
*/
@Test
public void testP20140522() {
final double[][] q = new double[][] { { 49.0, 31.0, 17.0, 6.0 }, { 31.0, 25.0, 13.0, 5.0 }, { 17.0, 13.0, 11.0, 3.5 }, { 6.0, 5.0, 3.5, 4.0 } };
final RawStore JamaQ = RawStore.FACTORY.rows(q);
final double[] c = new double[] { 195.0, 59.0, -1.8, -11.7 };
final RawStore JamaC = RawStore.FACTORY.columns(c);
final double[][] ai = new double[][] { { 1.0, 0.0, 0.0, 0.0 }, { -1.0, 0.0, 0.0, 0.0 }, { 1.0, 1.0, 0.0, 0.0 }, { -1.0, -1.0, 0.0, 0.0 }, { 1.0, 1.0, 1.0, 0.0 }, { -1.0, -1.0, -1.0, 0.0 }, { 0.1, 0.0, 0.0, 0.0 }, { 0.01, 0.0, 0.0, 0.0 }, { 0.18, 0.1, 0.0, 0.0 }, { -0.01, 0.0, 0.0, 0.0 }, { -0.183, -0.1, 0.0, 0.0 }, { 0.0283, 0.01, 0.0, 0.0 }, { 0.25, 0.183, 0.1, 0.0 } };
final RawStore JamaAI = RawStore.FACTORY.rows(ai);
final double[] bi = new double[] { 0.13, 0.87, 0.18, 0.82, 0.23, 0.77, -0.04, 99.67, -0.06, 100.33, 1.06, 99.62, -0.08 };
final RawStore JamaBI = RawStore.FACTORY.columns(bi);
Optimisation.Result result = null;
try {
final ConvexSolver.Builder qsBuilder = new ConvexSolver.Builder(JamaQ, JamaC);
qsBuilder.inequalities(JamaAI, JamaBI);
final ConvexSolver qSolver = qsBuilder.build();
// qSolver.options.debug(ConvexSolver.class);
result = qSolver.solve();
OptimisationConvexTests.assertDirectAndIterativeEquals(qsBuilder, null);
} catch (final Exception e) {
e.printStackTrace();
assert false;
}
final CompoundFunction<Double> tmpObj = CompoundFunction.makePrimitive(JamaQ.multiply(0.5), JamaC.multiply(-1.0));
TestUtils.assertEquals(State.OPTIMAL, result.getState());
final int numElm = (int) result.count();
final double[] expectedSolution = new double[] { -0.4, 0.12, -0.0196, -2.45785 };
tmpObj.invoke(Access1D.wrap(expectedSolution));
tmpObj.invoke(Access1D.asPrimitive1D(result));
JamaBI.subtract(JamaAI.multiply(PrimitiveDenseStore.FACTORY.columns(expectedSolution)));
JamaBI.subtract(JamaAI.multiply(PrimitiveDenseStore.FACTORY.columns(result)));
for (int i = 0; i < numElm; i++) {
TestUtils.assertEquals(expectedSolution[i], result.doubleValue(i), 1e-4);
}
}
use of org.ojalgo.matrix.store.RawStore in project ojAlgo by optimatika.
the class P20150809 method buildModel.
static ConvexSolver buildModel(final boolean identity, final boolean addDummyConstraints) {
if (OptimisationConvexTests.DEBUG) {
if (!identity && !addDummyConstraints) {
System.out.println("Zero Q matrix and no constraints -------------------------!");
} else if (!identity) {
System.out.println("Zero Q matrix and constraints -------------------------!");
} else if (!addDummyConstraints) {
System.out.println("Identity Q matrix and no constraints -------------------------!");
} else {
System.out.println("Identity Q matrix and constraints -------------------------!");
}
}
final double[] C = new double[] { 0.12, -0.05, 0.08, 0.07 };
final RawStore cov = new RawStore(4, 4);
if (identity) {
for (int i = 0; i < 4; i++) {
cov.set(i, i, 1.0);
}
}
final RawStore linPart = new RawStore(C, 4);
ConvexSolver.Builder builder = ConvexSolver.getBuilder(cov, linPart);
if (addDummyConstraints) {
final RawStore ineq = RawStore.FACTORY.rows(new double[][] { { -1.0, 0.0, 0.0, 0.0 }, { 0.0, -1.0, 0.0, 0.0 }, { 0.0, 0.0, -1.0, 0.0 }, { 0.0, 0.0, 0.0, -1.0 }, { 1.0, 0.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0, 0.0 }, { 0.0, 0.0, 0.0, 1.0 } });
final RawStore coeff = RawStore.FACTORY.columns(new double[][] { { 99999, 99999, 99999, 99999, 99999, 99999, 99999, 99999 } });
builder = builder.inequalities(ineq, coeff);
}
final Optimisation.Options opts = new Optimisation.Options();
opts.iterations_abort = 10000;
opts.iterations_suffice = 100;
if (OptimisationConvexTests.DEBUG) {
opts.debug(ConvexSolver.class);
}
return builder.build(opts);
}
Aggregations