use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.
the class ConvexProblems method testP20150908.
/**
* Problem reported to ojalgo-user. A simple bug caused a java.lang.ArithmeticException: / by zero. ojAlgo
* did not handle the case with inequality constraints that are all active (the set of excluded
* constraints was empty).
*/
@Test
public void testP20150908() {
final PrimitiveDenseStore tmpQ = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 2, 0 }, { 0, 2 } });
final PrimitiveDenseStore tmpC = PrimitiveDenseStore.FACTORY.columns(new double[] { 0, 0 });
final PrimitiveDenseStore tmpAI = PrimitiveDenseStore.FACTORY.rows(new double[][] { { -1, -1 } });
final PrimitiveDenseStore tmpBI = PrimitiveDenseStore.FACTORY.columns(new double[] { -1 });
final Builder tmpBuilder = new ConvexSolver.Builder(tmpQ, tmpC).inequalities(tmpAI, tmpBI);
final ConvexSolver tmpSolver = tmpBuilder.build();
final Optimisation.Result tmpResult = tmpSolver.solve();
final PrimitiveDenseStore tmpExpectedSolution = PrimitiveDenseStore.FACTORY.columns(new double[] { 0.5, 0.5 });
final Optimisation.Result tmpExpectedResult = new Optimisation.Result(Optimisation.State.OPTIMAL, 0.5, tmpExpectedSolution);
TestUtils.assertStateNotLessThanOptimal(tmpResult);
TestUtils.assertEquals(tmpExpectedResult, tmpResult);
OptimisationConvexTests.assertDirectAndIterativeEquals(tmpBuilder, null);
}
use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.
the class ConvexProblems method testP20150922.
/**
* <p>
* I recently upgraded to v38.2 of Ojalgo for solving some quadratic programs. It seems that somewhere in
* the code there is an assumption that whenever there are inequality constraints there must be at least
* one equality constraint. My problem has a bunch of inequality constraints but no equality constraints
* and running it gives a "divide by zero" error. Again, this only seems to manifest itself when there are
* inequality constraints but no equality constraints. I have reproduced it below with a simple example.
* </p>
* <p>
* apete. Most likely the same problem as P20150908 (Cannot reproduce the problem with the leatest code.)
* </p>
*/
@Test
public void testP20150922() {
final PrimitiveDenseStore Q = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.0, 0 }, { 0, 1.0 } });
final PrimitiveDenseStore C = PrimitiveDenseStore.FACTORY.columns(new double[] { 0, 0 });
final ConvexSolver.Builder myBuilderI = new ConvexSolver.Builder(Q, C);
final PrimitiveDenseStore AI = PrimitiveDenseStore.FACTORY.rows(new double[] { 1, 1 });
final PrimitiveDenseStore BI = PrimitiveDenseStore.FACTORY.columns(new double[] { 1 });
myBuilderI.inequalities(AI, BI);
final ConvexSolver prob = myBuilderI.build();
// java.lang.ArithmeticException: / by zero
final Result solved = prob.solve();
if (DEBUG) {
BasicLogger.debug(solved);
}
final PrimitiveDenseStore AI2 = PrimitiveDenseStore.FACTORY.rows(new double[] { 1, 1 });
final PrimitiveDenseStore BI2 = PrimitiveDenseStore.FACTORY.columns(new double[] { 2 });
// Discovered that you got (fixed now) a problem if you modify a builder after it has been used to build a solver
myBuilderI.inequalities(AI2, BI2);
final ConvexSolver prob2 = myBuilderI.build();
// java.lang.ArithmeticException: / by zero
final Result solved2 = prob2.solve();
if (DEBUG) {
BasicLogger.debug(solved2);
}
OptimisationConvexTests.assertDirectAndIterativeEquals(myBuilderI, null);
}
use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.
the class ConvexProblems method testP20111205.
/**
* Continuation of P20111129 http://bugzilla.optimatika.se/show_bug.cgi?id=15 Have to turn off validation
* as Q is not positive semidefinite.
* <p>
* 2016-03-07: Initially the solution (from AMPL/LOQO) was stated to be:
* <p>
*
* <pre>
* 1.78684, 0.000326128, 1.78665, 0.000136478, 495.429, 0.00358488, 495.427, 0.00178874, 8.90701, 0.000339811, 8.90684, 0.000174032
* </pre>
* <p>
* The ExpressionsBasedModel can only validate this solution to be correct using a very "poor" accuracy
* context. When ExpressionsBasedModel uses CPLEX as the solver a slightly different solution is returned
* that validates much better. Switched to using that solution as the expected solution in this test:
* <p>
*
* <pre>
* 1.7856570552, 1.216415374E-5, 1.78565097263, 6.08157995E-6, 495.426247828, 2.478968927E-5, 495.426235433, 1.239483719E-5, 8.90673094088, 6.04347562E-6, 8.90672791911, 3.02171321E-6
* </pre>
* </p>
*/
@Test
public void testP20111205() {
final PrimitiveDenseStore tmpAE = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, -1.0, -1.0, 1.0 }, { 1.0, -1.0, -1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 1.0, -1.0, -1.0, 1.0, 0.0, 0.0, 0.0, 0.0 } });
final PrimitiveDenseStore tmpBE = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 0.0 }, { 0.0 }, { 0.0 } });
final PrimitiveDenseStore tmpQ = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 42.58191012032541, -42.58191012032541, 0.0, 0.0, 0.029666091804595635, -0.029666091804595635, 0.0, 0.0, 9.954580659495097, -9.954580659495097, 0.0, 0.0 }, { -42.58191012032541, 42.58191012032541, 0.0, 0.0, -0.029666091804595635, 0.029666091804595635, 0.0, 0.0, -9.954580659495097, 9.954580659495097, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.029666091804595635, -0.029666091804595635, 0.0, 0.0, 0.8774199042430086, -0.8774199042430086, 0.0, 0.0, -3.537087573378497, 3.537087573378497, 0.0, 0.0 }, { -0.029666091804595635, 0.029666091804595635, 0.0, 0.0, -0.8774199042430086, 0.8774199042430086, 0.0, 0.0, 3.537087573378497, -3.537087573378497, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 9.954580659495097, -9.954580659495097, 0.0, 0.0, -3.537087573378497, 3.537087573378497, 0.0, 0.0, 153.76101274121527, -153.76101274121527, 0.0, 0.0 }, { -9.954580659495097, 9.954580659495097, 0.0, 0.0, 3.537087573378497, -3.537087573378497, 0.0, 0.0, -153.76101274121527, 153.76101274121527, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 } });
final PrimitiveDenseStore tmpC = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 185.8491751747291 }, { -192.3021967647291 }, { -6.45302159 }, { -6.45302159 }, { 406.4118818820076 }, { -409.5778277520076 }, { -3.16594587 }, { -3.16594587 }, { -352.0970015985486 }, { 339.11043506854867 }, { -12.986566530000001 }, { -12.986566530000001 } });
final PrimitiveDenseStore tmpAI = PrimitiveDenseStore.FACTORY.rows(new double[][] { { -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0 } });
final PrimitiveDenseStore tmpBI = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 0.0 }, { 0.0 }, { 0.0 }, { 0.0 }, { 0.0 }, { 0.0 }, { 0.0 }, { 0.0 }, { 0.0 }, { 0.0 }, { 0.0 }, { 0.0 } });
// Optimisation.Result from CPLEX (via ExpressionsBasedModel)
final double[] tmpExpected = new double[] { 1.7856570552, 1.216415374E-5, 1.78565097263, 6.08157995E-6, 495.426247828, 2.478968927E-5, 495.426235433, 1.239483719E-5, 8.90673094088, 6.04347562E-6, 8.90672791911, 3.02171321E-6 };
final PrimitiveDenseStore[] tmpMatrices = new PrimitiveDenseStore[6];
tmpMatrices[0] = tmpAE;
tmpMatrices[1] = tmpBE;
tmpMatrices[2] = tmpQ;
tmpMatrices[3] = tmpC;
tmpMatrices[4] = tmpAI;
tmpMatrices[5] = tmpBI;
// The original AMPL/LOQO solution was given with 6 digits precision and never more than 9 decimals
// ojAlgo can only get roughly the same solution
final NumberContext tmpAccuracy = NumberContext.getGeneral(3, 3);
ConvexProblems.builAndTestModel(tmpMatrices, tmpExpected, tmpAccuracy, true);
}
use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.
the class UCLAee236aCase method testRelaxedNodeP01.
/**
* P1
*/
@Test
public void testRelaxedNodeP01() {
final ExpressionsBasedModel tmpModel = UCLAee236aCase.makeOriginalRootModel().relax(true);
tmpModel.getVariable(0).upper(TWO);
final Optimisation.Result tmpResult = tmpModel.minimise();
TestUtils.assertEquals(State.OPTIMAL, tmpResult.getState());
final PrimitiveDenseStore tmpExpX = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 2.00 }, { 2.14 } });
TestUtils.assertEquals(tmpExpX, tmpResult, PRECISION);
TestUtils.assertEquals(-10.43, tmpModel.minimise().getValue(), PRECISION);
}
use of org.ojalgo.matrix.store.PrimitiveDenseStore in project ojAlgo by optimatika.
the class UCLAee236aCase method testRelaxedNodeP09.
/**
* P9
*/
@Test
public void testRelaxedNodeP09() {
final ExpressionsBasedModel tmpModel = UCLAee236aCase.makeOriginalRootModel().relax(true);
tmpModel.getVariable(0).lower(THREE);
tmpModel.getVariable(1).upper(ONE);
tmpModel.getVariable(0).lower(FOUR);
tmpModel.getVariable(1).upper(ZERO);
final Optimisation.Result tmpResult = tmpModel.minimise();
TestUtils.assertEquals(State.OPTIMAL, tmpResult.getState());
final PrimitiveDenseStore tmpExpX = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 4.50 }, { 0.00 } });
TestUtils.assertEquals(tmpExpX, tmpResult, PRECISION);
TestUtils.assertEquals(-9.00, tmpModel.minimise().getValue(), PRECISION);
}
Aggregations