Search in sources :

Example 1 with ProgrammingError

use of org.ojalgo.ProgrammingError in project ojAlgo by optimatika.

the class CoordinationSet method prune.

/**
 * @return A new CoordinationSet where all series have the same first and last keys.
 */
public CoordinationSet<N> prune() {
    final CoordinationSet<N> retVal = new CoordinationSet<>(this.getResolution());
    final CalendarDate tmpFirstKey = this.getLatestFirstKey();
    final CalendarDate tmpLastKey = this.getEarliestLastKey();
    if (tmpLastKey.compareTo(tmpFirstKey) != -1) {
        for (final CalendarDateSeries<N> tmpSeries : this.values()) {
            final CalendarDateSeries<N> tmpSubMap = tmpSeries.subMap(tmpFirstKey, true, tmpLastKey, true);
            retVal.put(tmpSubMap);
        }
    }
    final CalendarDate tmpEarliestFirstKey = retVal.getEarliestFirstKey();
    final CalendarDate tmpLatestFirstKey = retVal.getLatestFirstKey();
    final CalendarDate tmpEarliestLastKey = retVal.getEarliestLastKey();
    final CalendarDate tmpLatestLastKey = retVal.getLatestLastKey();
    if ((tmpEarliestFirstKey == null) || !tmpEarliestFirstKey.equals(tmpFirstKey)) {
        throw new ProgrammingError("Something went wrong!");
    }
    if ((tmpLatestFirstKey == null) || !tmpLatestFirstKey.equals(tmpFirstKey)) {
        throw new ProgrammingError("Something went wrong!");
    }
    if ((tmpEarliestLastKey == null) || !tmpEarliestLastKey.equals(tmpLastKey)) {
        throw new ProgrammingError("Something went wrong!");
    }
    if ((tmpLatestLastKey == null) || !tmpLatestLastKey.equals(tmpLastKey)) {
        throw new ProgrammingError("Something went wrong!");
    }
    return retVal;
}
Also used : CalendarDate(org.ojalgo.type.CalendarDate) ProgrammingError(org.ojalgo.ProgrammingError)

Example 2 with ProgrammingError

use of org.ojalgo.ProgrammingError in project ojAlgo by optimatika.

the class MultiplyBoth method invokeGeneric.

static <N extends Number & Scalar<N>> void invokeGeneric(final ElementsConsumer<N> product, final int firstRow, final int rowLimit, final Access1D<N> left, final int complexity, final Access1D<N> right) {
    @SuppressWarnings("unchecked") final Class<N> componenetType = (Class<N>) left.get(0L).getClass();
    final N zero;
    try {
        zero = componenetType.newInstance();
    } catch (InstantiationException | IllegalAccessException exception) {
        exception.printStackTrace();
        throw new ProgrammingError(exception);
    }
    final int tmpRowDim = (int) (left.count() / complexity);
    final int tmpColDim = (int) (right.count() / complexity);
    @SuppressWarnings("unchecked") final N[] tmpLeftRow = (N[]) Array.newInstance(componenetType, complexity);
    N tmpVal;
    int tmpFirst = 0;
    int tmpLimit = complexity;
    for (int i = firstRow; i < rowLimit; i++) {
        final int tmpFirstInRow = MatrixUtils.firstInRow(left, i, 0);
        final int tmpLimitOfRow = MatrixUtils.limitOfRow(left, i, complexity);
        for (int c = tmpFirstInRow; c < tmpLimitOfRow; c++) {
            tmpLeftRow[c] = left.get(i + (c * tmpRowDim));
        }
        for (int j = 0; j < tmpColDim; j++) {
            final int tmpColBase = j * complexity;
            tmpFirst = MatrixUtils.firstInColumn(right, j, tmpFirstInRow);
            tmpLimit = MatrixUtils.limitOfColumn(right, j, tmpLimitOfRow);
            tmpVal = zero;
            for (int c = tmpFirst; c < tmpLimit; c++) {
                tmpVal = tmpVal.add(tmpLeftRow[c].multiply(right.get(c + tmpColBase))).get();
            }
            product.set(i, j, tmpVal);
        }
    }
}
Also used : ProgrammingError(org.ojalgo.ProgrammingError)

Example 3 with ProgrammingError

use of org.ojalgo.ProgrammingError in project ojAlgo by optimatika.

the class ConvexProblems method testP20140109.

/**
 * <p>
 * I tried to use ojAlgo to implement a norm minimization problem, but the solver fails even for very
 * simple instances. The following example is one particular simple instance. Q is the identity matrix, C
 * the zero vector. The constraints express that the solution is a probability function (AE for
 * normalization and AI for non-negativity). Q is positive definite and the solution should be (0.5, 0.5),
 * but qSolver fails.
 * </p>
 * <p>
 * apete: The problem was incorrectly specified with a transposed "C" vector. Modified the builder to
 * actually throw an exception.
 * </p>
 */
@Test
public void testP20140109() {
    final PrimitiveDenseStore tmpQ = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1, 0 }, { 0, 1 } });
    final PrimitiveDenseStore tmpC = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 0, 0 } });
    final PrimitiveDenseStore tmpAE = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1, 1 } });
    final PrimitiveDenseStore tmpBE = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1 } });
    final PrimitiveDenseStore tmpAI = PrimitiveDenseStore.FACTORY.rows(new double[][] { { -1, 0 }, { 0, -1 } });
    final PrimitiveDenseStore tmpBI = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 0 }, { 0 } });
    try {
        final ConvexSolver qSolver = new ConvexSolver.Builder(tmpQ, tmpC).equalities(tmpAE, tmpBE).inequalities(tmpAI, tmpBI).build();
        // qSolver.options.debug(ConvexSolver.class);
        final Optimisation.Result tmpResult = qSolver.solve();
        // Shouldn't get this far. There should be an exception
        TestUtils.assertStateLessThanFeasible(tmpResult);
        TestUtils.fail();
    } catch (final ProgrammingError exception) {
        TestUtils.assertTrue("Yes!", true);
    }
    // ... and check that the correctly defined problem does solve.
    final ConvexSolver tmpCorrectSolver = new ConvexSolver.Builder(tmpQ, tmpC.transpose()).equalities(tmpAE, tmpBE).inequalities(tmpAI, tmpBI).build();
    final Optimisation.Result tmpResult = tmpCorrectSolver.solve();
    TestUtils.assertStateNotLessThanOptimal(tmpResult);
    TestUtils.assertEquals(Primitive64Array.wrap(new double[] { 0.5, 0.5 }), tmpResult);
}
Also used : Builder(org.ojalgo.optimisation.convex.ConvexSolver.Builder) Result(org.ojalgo.optimisation.Optimisation.Result) Optimisation(org.ojalgo.optimisation.Optimisation) ProgrammingError(org.ojalgo.ProgrammingError) PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore) Test(org.junit.jupiter.api.Test)

Example 4 with ProgrammingError

use of org.ojalgo.ProgrammingError in project ojAlgo by optimatika.

the class DivideAndConquer method divide.

final void divide(final int first, final int limit, final int threshold, final int workers) {
    final int count = limit - first;
    if ((count > threshold) && (workers > 1)) {
        final int split = first + (count / 2);
        final int nextWorkers = workers / 2;
        final Future<?> firstPart = DaemonPoolExecutor.INSTANCE.submit(() -> this.divide(first, split, threshold, nextWorkers));
        final Future<?> secondPart = DaemonPoolExecutor.INSTANCE.submit(() -> this.divide(split, limit, threshold, nextWorkers));
        try {
            firstPart.get();
            secondPart.get();
        } catch (final InterruptedException | ExecutionException exception) {
            exception.printStackTrace();
            throw new ProgrammingError(exception);
        }
    } else {
        this.conquer(first, limit);
    }
}
Also used : ProgrammingError(org.ojalgo.ProgrammingError) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

ProgrammingError (org.ojalgo.ProgrammingError)4 ExecutionException (java.util.concurrent.ExecutionException)1 Test (org.junit.jupiter.api.Test)1 PrimitiveDenseStore (org.ojalgo.matrix.store.PrimitiveDenseStore)1 Optimisation (org.ojalgo.optimisation.Optimisation)1 Result (org.ojalgo.optimisation.Optimisation.Result)1 Builder (org.ojalgo.optimisation.convex.ConvexSolver.Builder)1 CalendarDate (org.ojalgo.type.CalendarDate)1