List of usage examples for org.apache.commons.math3.linear MatrixUtils createRealVector
public static RealVector createRealVector(double[] data) throws NoDataException, NullArgumentException
From source file:edu.washington.gs.skyline.model.quantification.WeightedRegression.java
public static double[] weighted(double[][] x, double[] y, double[] weights, boolean intercept) { RealMatrix predictor;/*from w w w . j a va 2 s. c om*/ if (intercept) { int nRows = x.length; int nCols = x[0].length + 1; predictor = MatrixUtils.createRealMatrix(nRows, nCols); for (int iRow = 0; iRow < nRows; iRow++) { predictor.setEntry(iRow, 0, 1.0); for (int iCol = 1; iCol < nCols; iCol++) { predictor.setEntry(iRow, iCol, x[iRow][iCol - 1]); } } } else { predictor = MatrixUtils.createRealMatrix(x); } RealVector responseVector = MatrixUtils.createRealVector(y); RealMatrix weightsMatrix = MatrixUtils.createRealDiagonalMatrix(weights); RealMatrix predictorTransposed = predictor.transpose(); RealMatrix predictorTransposedTimesWeights = predictorTransposed .multiply(weightsMatrix.multiply(predictor)); CholeskyDecomposition choleskyDecomposition = new CholeskyDecomposition(predictorTransposedTimesWeights); RealVector vectorToSolve = predictorTransposed.operate(weightsMatrix.operate(responseVector)); RealVector result = choleskyDecomposition.getSolver().solve(vectorToSolve); return result.toArray(); }
From source file:io.warp10.script.functions.TOVEC.java
@Override public Object apply(WarpScriptStack stack) throws WarpScriptException { Object o = stack.pop();/*from w w w.j av a 2 s.co m*/ if (o instanceof RealMatrix) { RealMatrix matrix = (RealMatrix) o; if (1 != matrix.getColumnDimension()) { throw new WarpScriptException( getName() + " expects a matrix with a single column on top of the stack."); } RealVector vector = matrix.getColumnVector(0); stack.push(vector); return stack; } if (!(o instanceof List)) { throw new WarpScriptException(getName() + " expects a list onto the stack."); } double[] data = new double[((List) o).size()]; for (int i = 0; i < data.length; i++) { Object oo = ((List) o).get(i); if (!(oo instanceof Number)) { throw new WarpScriptException(getName() + " expects a list of numbers onto the stack."); } data[i] = ((Number) oo).doubleValue(); } stack.push(MatrixUtils.createRealVector(data)); return stack; }
From source file:com.idylwood.utils.OptimizationUtils.java
/** * Solves for Markowitz optimal portfolio by means of lagrange multiplier. * Returns null if it does not exist (the matrix is singular) * Otherwise it attempts to find the lowest variance portfolio for * the given <code>portfolio_return</code> * @param covariance Precalculated covariance matrix * @param returns Precalculated vector of returns * @param portfolio_return Return to optimize risk for * @author Harry C Kim// w w w .j a va2 s .c om * @return */ static final double[] MarkowitzSolve(final double[][] covariance, final double[] returns, final double portfolio_return) { if (covariance.length != covariance[0].length) throw new IllegalArgumentException("Covariance needs to be square matrix"); if (returns.length != covariance.length) throw new IllegalArgumentException("Returns must be same length as covariance"); /* for (int i = 0; i < covariance.length; i++) MathUtils.printArray(covariance[i]); System.out.println(); MathUtils.printArray(returns); System.out.println(); */ final int timePoints = covariance.length; final double[][] lagrangeMatrix = new double[timePoints + 2][timePoints + 2]; //b as in Ax = b final double[] b = new double[timePoints + 2]; for (int i = 0; i < timePoints; i++) { for (int j = 0; j < timePoints; j++) { lagrangeMatrix[i][j] = 2 * covariance[i][j]; b[i] = 0; // this is like riskTolerance*returns[i]; but since // returns[i]*weights[i] = portfolio_return it will go away in the derivative } } for (int j = 0; j < timePoints; j++) { lagrangeMatrix[timePoints][j] = returns[j]; lagrangeMatrix[timePoints + 1][j] = 1; lagrangeMatrix[j][timePoints] = returns[j]; lagrangeMatrix[j][timePoints + 1] = 1; } b[timePoints] = portfolio_return; //**** what is the constraint on total expected return? b[timePoints + 1] = 1; /* // Print out lagrangeMatrix augmented with b vector for(int i=0; i<timePoints+2; i++) { for(int j=0; j<timePoints+2;j++) { System.out.print(lagrangeMatrix[i][j] + " "); } System.out.println(b[i]); } */ // TODO use Gaussian elimination solver, may be faster // TODO maybe refactor to use idylblas RealMatrix lagrangeReal = MatrixUtils.createRealMatrix(lagrangeMatrix); RealVector bReal = MatrixUtils.createRealVector(b); SingularValueDecomposition svd = new SingularValueDecomposition(lagrangeReal); if (!svd.getSolver().isNonSingular()) return null; RealVector solution = svd.getSolver().solve(bReal); final double weights[] = new double[timePoints]; // last two elements of solution are just lagrange multipliers for (int i = 0; i < weights.length; i++) weights[i] = solution.getEntry(i); // put these in some test class if (!MathUtils.fuzzyEquals(1, MathUtils.sum(weights))) throw new RuntimeException(); if (!MathUtils.fuzzyEquals(portfolio_return, MathUtils.linearCombination(returns, weights))) throw new RuntimeException(); //The following calculates the risk(variance) for the weights found // final double risk = MathUtils.linearCombination(MathUtils.matrixMultiply(covariance, weights),weights); return weights; }
From source file:com.joptimizer.optimizers.LPPresolverTest.java
public void testFromFile1() throws Exception { log.debug("testFromFile1"); String problemId = "1"; log.debug("problemId: " + problemId); double[] c = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "presolving" + File.separator + "c" + problemId + ".txt"); double[][] A = Utils.loadDoubleMatrixFromFile( "lp" + File.separator + "presolving" + File.separator + "A" + problemId + ".csv", ",".charAt(0)); double[] b = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "presolving" + File.separator + "b" + problemId + ".txt"); double[] lb = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "presolving" + File.separator + "lb" + problemId + ".txt"); double[] ub = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "presolving" + File.separator + "ub" + problemId + ".txt"); double s = 0; try {/*from ww w . j ava 2 s . com*/ s = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "presolving" + File.separator + "s" + problemId + ".txt")[0]; } catch (Exception e) { } double[] expectedSolution = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "presolving" + File.separator + "sol" + problemId + ".txt"); double expectedValue = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "presolving" + File.separator + "value" + problemId + ".txt")[0]; double expectedTolerance = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "presolving" + File.separator + "tolerance" + problemId + ".txt")[0]; expectedTolerance = Math.max(expectedTolerance, MatrixUtils.createRealMatrix(A).operate(MatrixUtils.createRealVector(expectedSolution)) .subtract(MatrixUtils.createRealVector(b)).getNorm()); doPresolving(c, A, b, lb, ub, s, expectedSolution, expectedValue, expectedTolerance); }
From source file:com.joptimizer.optimizers.LPStandardConverterTest.java
/** * Standardization of a problem on the form: * min(c) s.t./* ww w . j a v a2 s . c om*/ * G.x < h * A.x = b * lb <= x <= ub */ public void testCGhAbLbUb1() throws Exception { log.debug("testCGhAbLbUb1"); String problemId = "1"; double[] c = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "standardization" + File.separator + "c" + problemId + ".txt"); double[][] G = Utils.loadDoubleMatrixFromFile( "lp" + File.separator + "standardization" + File.separator + "G" + problemId + ".csv", ",".charAt(0)); double[] h = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "standardization" + File.separator + "h" + problemId + ".txt"); ; double[][] A = Utils.loadDoubleMatrixFromFile( "lp" + File.separator + "standardization" + File.separator + "A" + problemId + ".csv", ",".charAt(0)); double[] b = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "standardization" + File.separator + "b" + problemId + ".txt"); double[] lb = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "standardization" + File.separator + "lb" + problemId + ".txt"); double[] ub = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "standardization" + File.separator + "ub" + problemId + ".txt"); double[] expectedSol = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "standardization" + File.separator + "sol" + problemId + ".txt"); double expectedValue = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "standardization" + File.separator + "value" + problemId + ".txt")[0]; //double expectedTolerance = Utils.loadDoubleArrayFromFile("lp"+File.separator+"standardization"+File.separator+"tolerance"+problemId+".txt")[0]; double expectedTolerance = MatrixUtils.createRealMatrix(A) .operate(MatrixUtils.createRealVector(expectedSol)).subtract(MatrixUtils.createRealVector(b)) .getNorm(); //standard form conversion double unboundedLBValue = Double.NEGATIVE_INFINITY;//this is because in the file the unbounded lb are -Infinity values (not the default value) double unboundedUBValue = Double.POSITIVE_INFINITY;//this is because in the file the unbounded ub are +Infinity values LPStandardConverter lpConverter = new LPStandardConverter(unboundedLBValue, unboundedUBValue); lpConverter.toStandardForm(c, G, h, A, b, lb, ub); int n = lpConverter.getStandardN(); int s = lpConverter.getStandardS(); c = lpConverter.getStandardC().toArray(); A = lpConverter.getStandardA().toArray(); b = lpConverter.getStandardB().toArray(); lb = lpConverter.getStandardLB().toArray(); ub = lpConverter.getStandardUB().toArray(); log.debug("n : " + n); log.debug("s : " + s); log.debug("c : " + ArrayUtils.toString(c)); log.debug("A : " + ArrayUtils.toString(A)); log.debug("b : " + ArrayUtils.toString(b)); log.debug("lb : " + ArrayUtils.toString(lb)); log.debug("ub : " + ArrayUtils.toString(ub)); //check consistency assertEquals(G.length, s); assertEquals(s + lpConverter.getOriginalN(), n); assertEquals(lb.length, n); assertEquals(ub.length, n); //check constraints RealMatrix GOrig = new Array2DRowRealMatrix(G); RealVector hOrig = new ArrayRealVector(h); RealMatrix AStandard = new Array2DRowRealMatrix(A); RealVector bStandard = new ArrayRealVector(b); RealVector expectedSolVector = new ArrayRealVector(expectedSol); RealVector Gxh = GOrig.operate(expectedSolVector).subtract(hOrig);//G.x - h RealVector slackVariables = new ArrayRealVector(s); for (int i = 0; i < s; i++) { slackVariables.setEntry(i, 0. - Gxh.getEntry(i));//the difference from 0 assertTrue(slackVariables.getEntry(i) >= 0.); } RealVector sol = slackVariables.append(expectedSolVector); RealVector Axmb = AStandard.operate(sol).subtract(bStandard); assertEquals(0., Axmb.getNorm(), expectedTolerance); // Utils.writeDoubleArrayToFile(new double[]{s}, "target" + File.separator + "standardS"+problemId+".txt"); // Utils.writeDoubleArrayToFile(c, "target" + File.separator + "standardC"+problemId+".txt"); // Utils.writeDoubleMatrixToFile(A, "target" + File.separator + "standardA"+problemId+".txt"); // Utils.writeDoubleArrayToFile(b, "target" + File.separator + "standardB"+problemId+".txt"); // Utils.writeDoubleArrayToFile(lb, "target" + File.separator + "standardLB"+problemId+".txt"); // Utils.writeDoubleArrayToFile(ub, "target" + File.separator + "standardUB"+problemId+".txt"); }
From source file:com.analog.lyric.dimple.test.solvers.sumproduct.TestSampledFactors.java
/** * Adapted from MATLAB test4 in tests/algoGaussian/testSampledFactors.m *//*www. j a v a 2 s . c o m*/ @Test public void sampledComplexProduct() { // NOTE: test may fail if seed is changed! We keep the number of samples down so that the test doesn't // take too long. Increasing the samples produces better results. testRand.setSeed(42); try (CurrentModel cur = using(new FactorGraph())) { final Complex a = complex("a"); final Complex b = complex("b"); final Complex c = product(a, b); double[] aMean = new double[] { 10, 10 }; RealMatrix aCovariance = randCovariance(2); a.setPrior(new MultivariateNormal(aMean, aCovariance.getData())); double[] bMean = new double[] { -20, 20 }; RealMatrix bCovariance = randCovariance(2); b.setPrior(new MultivariateNormalParameters(bMean, bCovariance.getData())); GaussianRandomGenerator normalGenerator = new GaussianRandomGenerator(testRand); CorrelatedRandomVectorGenerator aGenerator = new CorrelatedRandomVectorGenerator(aMean, aCovariance, 1e-12, normalGenerator); CorrelatedRandomVectorGenerator bGenerator = new CorrelatedRandomVectorGenerator(bMean, bCovariance, 1e-12, normalGenerator); StorelessCovariance expectedCov = new StorelessCovariance(2); final int nSamples = 10000; RealVector expectedMean = MatrixUtils.createRealVector(new double[2]); double[] cSample = new double[2]; for (int i = 0; i < nSamples; ++i) { double[] aSample = aGenerator.nextVector(); double[] bSample = bGenerator.nextVector(); // Compute complex product cSample[0] = aSample[0] * bSample[0] - aSample[1] * bSample[1]; cSample[1] = aSample[0] * bSample[1] + aSample[1] * bSample[0]; expectedMean.addToEntry(0, cSample[0]); expectedMean.addToEntry(1, cSample[1]); expectedCov.increment(cSample); } expectedMean.mapDivideToSelf(nSamples); // normalize SumProductSolverGraph sfg = requireNonNull(cur.graph.setSolverFactory(new SumProductSolver())); sfg.setOption(GibbsOptions.numSamples, nSamples); sfg.solve(); MultivariateNormalParameters cBelief = requireNonNull(c.getBelief()); RealVector observedMean = MatrixUtils.createRealVector(cBelief.getMean()); double scaledMeanDistance = expectedMean.getDistance(observedMean) / expectedMean.getNorm(); // System.out.format("expectedMean = %s\n", expectedMean); // System.out.format("observedMean = %s\n", observedMean); // System.out.println(scaledMeanDistance); assertEquals(0.0, scaledMeanDistance, .02); RealMatrix expectedCovariance = expectedCov.getCovarianceMatrix(); RealMatrix observedCovariance = MatrixUtils.createRealMatrix(cBelief.getCovariance()); RealMatrix diffCovariance = expectedCovariance.subtract(observedCovariance); double scaledCovarianceDistance = diffCovariance.getNorm() / expectedCovariance.getNorm(); // System.out.println(expectedCovariance); // System.out.println(expectedCovariance.getNorm()); // System.out.println(diffCovariance); // System.out.println(diffCovariance.getNorm()); // System.out.println(diffCovariance.getNorm() / expectedCovariance.getNorm()); assertEquals(0.0, scaledCovarianceDistance, .2); } }
From source file:com.joptimizer.optimizers.LPPresolverTest.java
/** * This problem has a deterministic solution. *//*from ww w . j ava 2 s . co m*/ public void testFromFile2() throws Exception { log.debug("testFromFile2"); String problemId = "2"; double[] c = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "presolving" + File.separator + "c" + problemId + ".txt"); double[][] A = Utils.loadDoubleMatrixFromFile( "lp" + File.separator + "presolving" + File.separator + "A" + problemId + ".csv", ",".charAt(0)); double[] b = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "presolving" + File.separator + "b" + problemId + ".txt"); double[] lb = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "presolving" + File.separator + "lb" + problemId + ".txt"); double[] ub = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "presolving" + File.separator + "ub" + problemId + ".txt"); double s = 0; try { s = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "presolving" + File.separator + "s" + problemId + ".txt")[0]; } catch (Exception e) { } double[] expectedSolution = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "presolving" + File.separator + "sol" + problemId + ".txt"); double expectedValue = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "presolving" + File.separator + "value" + problemId + ".txt")[0]; double expectedTolerance = MatrixUtils.createRealMatrix(A) .operate(MatrixUtils.createRealVector(expectedSolution)).subtract(MatrixUtils.createRealVector(b)) .getNorm(); //must be: A pXn with rank(A)=p < n RealMatrix AMatrix = MatrixUtils.createRealMatrix(A); SingularValueDecomposition dec = new SingularValueDecomposition(AMatrix); int rankA = dec.getRank(); log.debug("p: " + AMatrix.getRowDimension()); log.debug("n: " + AMatrix.getColumnDimension()); log.debug("rank: " + rankA); LPPresolver lpPresolver = new LPPresolver(); lpPresolver.setNOfSlackVariables((short) s); lpPresolver.setExpectedSolution(expectedSolution);//this is just for test! lpPresolver.presolve(c, A, b, lb, ub); int n = lpPresolver.getPresolvedN(); //deterministic solution assertEquals(0, n); assertTrue(lpPresolver.getPresolvedC() == null); assertTrue(lpPresolver.getPresolvedA() == null); assertTrue(lpPresolver.getPresolvedB() == null); assertTrue(lpPresolver.getPresolvedLB() == null); assertTrue(lpPresolver.getPresolvedUB() == null); assertTrue(lpPresolver.getPresolvedYlb() == null); assertTrue(lpPresolver.getPresolvedYub() == null); assertTrue(lpPresolver.getPresolvedZlb() == null); assertTrue(lpPresolver.getPresolvedZub() == null); double[] sol = lpPresolver.postsolve(new double[] {}); assertEquals(expectedSolution.length, sol.length); for (int i = 0; i < sol.length; i++) { //log.debug("i: " + i); assertEquals(expectedSolution[i], sol[i], 1.e-9); } }
From source file:com.joptimizer.optimizers.LPStandardConverterTest.java
/** * Standardization (to the strictly standard form) of a problem on the form: * min(c) s.t./* w ww.j a va 2s . c o m*/ * G.x < h * A.x = b * lb <= x <= ub * @TODO: the strict conversion is net yet ready. */ public void xxxtestCGhAbLbUb1Strict() throws Exception { log.debug("testCGhAbLbUb1Strict"); String problemId = "1"; double[] c = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "standardization" + File.separator + "c" + problemId + ".txt"); double[][] G = Utils.loadDoubleMatrixFromFile( "lp" + File.separator + "standardization" + File.separator + "G" + problemId + ".csv", ",".charAt(0)); double[] h = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "standardization" + File.separator + "h" + problemId + ".txt"); ; double[][] A = Utils.loadDoubleMatrixFromFile( "lp" + File.separator + "standardization" + File.separator + "A" + problemId + ".csv", ",".charAt(0)); double[] b = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "standardization" + File.separator + "b" + problemId + ".txt"); double[] lb = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "standardization" + File.separator + "lb" + problemId + ".txt"); double[] ub = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "standardization" + File.separator + "ub" + problemId + ".txt"); double[] expectedSol = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "standardization" + File.separator + "sol" + problemId + ".txt"); double expectedValue = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "standardization" + File.separator + "value" + problemId + ".txt")[0]; double expectedTolerance = MatrixUtils.createRealMatrix(A) .operate(MatrixUtils.createRealVector(expectedSol)).subtract(MatrixUtils.createRealVector(b)) .getNorm(); int nOfSlackVariables = 0; for (int i = 0; i < c.length; i++) { double lbi = lb[i]; int lbCompare = Double.compare(lbi, 0.); if (lbCompare != 0 && !Double.isNaN(lbi)) { nOfSlackVariables++; } if (!Double.isNaN(ub[i])) { nOfSlackVariables++; } } int expectedS = G.length + nOfSlackVariables; //standard form conversion boolean strictlyStandardForm = true; LPStandardConverter lpConverter = new LPStandardConverter(strictlyStandardForm); lpConverter.toStandardForm(c, G, h, A, b, lb, ub); int n = lpConverter.getStandardN(); int s = lpConverter.getStandardS(); c = lpConverter.getStandardC().toArray(); A = lpConverter.getStandardA().toArray(); b = lpConverter.getStandardB().toArray(); lb = lpConverter.getStandardLB().toArray(); ub = (lpConverter.getStandardUB() == null) ? null : ub; log.debug("n : " + n); log.debug("s : " + s); log.debug("c : " + ArrayUtils.toString(c)); log.debug("A : " + ArrayUtils.toString(A)); log.debug("b : " + ArrayUtils.toString(b)); log.debug("lb : " + ArrayUtils.toString(lb)); //log.debug("ub : " + ArrayUtils.toString(ub)); //check consistency assertEquals(expectedS, s); assertEquals(lb.length, n); assertTrue(ub == null); //check constraints RealMatrix GOrig = new Array2DRowRealMatrix(G); RealVector hOrig = new ArrayRealVector(h); RealMatrix AStandard = new Array2DRowRealMatrix(A); RealVector bStandard = new ArrayRealVector(b); RealVector expectedSolVector = new ArrayRealVector(expectedSol); double[] expectedStandardSol = lpConverter.getStandardComponents(expectedSol); RealVector expectedStandardSolVector = new ArrayRealVector(expectedStandardSol); for (int i = 0; i < expectedStandardSolVector.getDimension(); i++) { assertTrue(expectedStandardSolVector.getEntry(i) >= 0.); } RealVector Axmb = AStandard.operate(expectedStandardSolVector).subtract(bStandard); assertEquals(0., Axmb.getNorm(), expectedTolerance); Utils.writeDoubleArrayToFile(new double[] { s }, "target" + File.separator + "standardS_" + problemId + ".txt"); Utils.writeDoubleArrayToFile(c, "target" + File.separator + "standardC_" + problemId + ".txt"); Utils.writeDoubleMatrixToFile(A, "target" + File.separator + "standardA_" + problemId + ".csv"); Utils.writeDoubleArrayToFile(b, "target" + File.separator + "standardB_" + problemId + ".txt"); Utils.writeDoubleArrayToFile(lb, "target" + File.separator + "standardLB_" + problemId + ".txt"); //ub is null Utils.writeDoubleArrayToFile(ub, "target" + File.separator + "standardUB_"+problemId+".txt"); }
From source file:com.joptimizer.optimizers.LPPresolverNetlibTest.java
/** * Tests the presolving of a netlib problem. * If checkExpectedSolution, the presolving is checked step by step against * a (beforehand) known solution of the problem. * NOTE: this known solution might differ from the solution given by the presolver * (e.g. in the presence of a weakly dominated column @see {@link LPPresolver#removeDominatedColumns}, * or if it is calculated with simplex method * or if bounds are not exactly the same), so sometimes it is not a good test. *//*from ww w . j a v a 2 s . c o m*/ public void doTesting(String problemName, boolean checkExpectedSolution, double myExpectedTolerance) throws Exception { log.debug("doTesting: " + problemName); int s = (int) Utils.loadDoubleArrayFromFile("lp" + File.separator + "netlib" + File.separator + problemName + File.separator + "standardS.txt")[0]; double[] c = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "netlib" + File.separator + problemName + File.separator + "standardC.txt"); double[][] A = Utils.loadDoubleMatrixFromFile( "lp" + File.separator + "netlib" + File.separator + problemName + File.separator + "standardA.csv", ",".charAt(0)); double[] b = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "netlib" + File.separator + problemName + File.separator + "standardB.txt"); double[] lb = Utils.loadDoubleArrayFromFile("lp" + File.separator + "netlib" + File.separator + problemName + File.separator + "standardLB.txt"); double[] ub = Utils.loadDoubleArrayFromFile("lp" + File.separator + "netlib" + File.separator + problemName + File.separator + "standardUB.txt"); double[] expectedSolution = Utils.loadDoubleArrayFromFile("lp" + File.separator + "netlib" + File.separator + problemName + File.separator + "standardSolution.txt"); double expectedValue = Utils.loadDoubleArrayFromFile("lp" + File.separator + "netlib" + File.separator + problemName + File.separator + "standardValue.txt")[0]; double expectedTolerance = Utils.loadDoubleArrayFromFile("lp" + File.separator + "netlib" + File.separator + problemName + File.separator + "standardTolerance.txt")[0]; //in order to compare with tha Math results, we must have the same bounds for (int i = 0; i < lb.length; i++) { if (Double.isNaN(lb[i])) { lb[i] = -9999999d; //the same as the notebook value } } for (int i = 0; i < ub.length; i++) { if (Double.isNaN(ub[i])) { ub[i] = +9999999d; //the same as the notebook value } } RealMatrix AMatrix = MatrixUtils.createRealMatrix(A); RealVector bVector = MatrixUtils.createRealVector(b); //expectedTolerance = Math.max(expectedTolerance, AMatrix.operate(MatrixUtils.createRealVector(expectedSolution)).subtract(bVector).getNorm()); expectedTolerance = Math.max(1.e-9, expectedTolerance); // must be: A pXn with rank(A)=p < n QRSparseFactorization qr = new QRSparseFactorization(new SparseDoubleMatrix2D(A)); qr.factorize(); log.debug("p : " + AMatrix.getRowDimension()); log.debug("n : " + AMatrix.getColumnDimension()); log.debug("full rank: " + qr.hasFullRank()); LPPresolver lpPresolver = new LPPresolver(); lpPresolver.setNOfSlackVariables((short) s); if (checkExpectedSolution) { lpPresolver.setExpectedSolution(expectedSolution);// this is just for test! lpPresolver.setExpectedTolerance(myExpectedTolerance);// this is just for test! } // lpPresolver.setAvoidFillIn(true); // lpPresolver.setZeroTolerance(1.e-13); lpPresolver.presolve(c, A, b, lb, ub); int n = lpPresolver.getPresolvedN(); DoubleMatrix1D presolvedC = lpPresolver.getPresolvedC(); DoubleMatrix2D presolvedA = lpPresolver.getPresolvedA(); DoubleMatrix1D presolvedB = lpPresolver.getPresolvedB(); DoubleMatrix1D presolvedLb = lpPresolver.getPresolvedLB(); DoubleMatrix1D presolvedUb = lpPresolver.getPresolvedUB(); DoubleMatrix1D presolvedYlb = lpPresolver.getPresolvedYlb(); DoubleMatrix1D presolvedYub = lpPresolver.getPresolvedYub(); DoubleMatrix1D presolvedZlb = lpPresolver.getPresolvedZlb(); DoubleMatrix1D presolvedZub = lpPresolver.getPresolvedZub(); log.debug("n : " + n); if (log.isDebugEnabled() && n > 0) { log.debug("presolvedC : " + ArrayUtils.toString(presolvedC.toArray())); log.debug("presolvedA : " + ArrayUtils.toString(presolvedA.toArray())); log.debug("presolvedB : " + ArrayUtils.toString(presolvedB.toArray())); log.debug("presolvedLb : " + ArrayUtils.toString(presolvedLb.toArray())); log.debug("presolvedUb : " + ArrayUtils.toString(presolvedUb.toArray())); log.debug("presolvedYlb: " + ArrayUtils.toString(presolvedYlb.toArray())); log.debug("presolvedYub: " + ArrayUtils.toString(presolvedYub.toArray())); log.debug("presolvedZlb: " + ArrayUtils.toString(presolvedZlb.toArray())); log.debug("presolvedZub: " + ArrayUtils.toString(presolvedZub.toArray())); } if (n == 0) { // deterministic problem double[] sol = lpPresolver.postsolve(new double[] {}); assertEquals(expectedSolution.length, sol.length); for (int i = 0; i < sol.length; i++) { // log.debug("i: " + i); assertEquals(expectedSolution[i], sol[i], 1.e-9); } } else { Utils.writeDoubleArrayToFile(presolvedC.toArray(), "target" + File.separator + "presolvedC_" + problemName + ".txt"); Utils.writeDoubleMatrixToFile(presolvedA.toArray(), "target" + File.separator + "presolvedA_" + problemName + ".csv"); Utils.writeDoubleArrayToFile(presolvedB.toArray(), "target" + File.separator + "presolvedB_" + problemName + ".txt"); Utils.writeDoubleArrayToFile(presolvedLb.toArray(), "target" + File.separator + "presolvedLB_" + problemName + ".txt"); Utils.writeDoubleArrayToFile(presolvedUb.toArray(), "target" + File.separator + "presolvedUB_" + problemName + ".txt"); // check objective function double delta = expectedTolerance; RealVector presolvedES = MatrixUtils.createRealVector(lpPresolver.presolve(expectedSolution)); double presolvedEV = MatrixUtils.createRealVector(presolvedC.toArray()).dotProduct(presolvedES);// in general it is different from the optimal value log.debug("presolved expected value: " + presolvedEV); RealVector postsolvedES = MatrixUtils.createRealVector(lpPresolver.postsolve(presolvedES.toArray())); double postsolvedEV = MatrixUtils.createRealVector(c).dotProduct(postsolvedES); //assertEquals(expectedValue, postsolvedEV, delta); assertTrue(Math.abs((expectedValue - postsolvedEV) / expectedValue) < delta); // check postsolved constraints for (int i = 0; i < lb.length; i++) { double di = Double.isNaN(lb[i]) ? -Double.MAX_VALUE : lb[i]; assertTrue(di <= postsolvedES.getEntry(i) + delta); } for (int i = 0; i < ub.length; i++) { double di = Double.isNaN(ub[i]) ? Double.MAX_VALUE : ub[i]; assertTrue(di + delta >= postsolvedES.getEntry(i)); } RealVector Axmb = AMatrix.operate(postsolvedES).subtract(bVector); assertEquals(0., Axmb.getNorm(), 1.5 * expectedTolerance); // check presolved constraints assertEquals(presolvedLb.size(), presolvedES.getDimension()); assertEquals(presolvedUb.size(), presolvedES.getDimension()); AMatrix = MatrixUtils.createRealMatrix(presolvedA.toArray());//reassigned to avoid memory consumption bVector = MatrixUtils.createRealVector(presolvedB.toArray()); for (int i = 0; i < presolvedLb.size(); i++) { double di = Double.isNaN(presolvedLb.getQuick(i)) ? -Double.MAX_VALUE : presolvedLb.getQuick(i); assertTrue(di <= presolvedES.getEntry(i) + delta); } for (int i = 0; i < presolvedUb.size(); i++) { double di = Double.isNaN(presolvedUb.getQuick(i)) ? Double.MAX_VALUE : presolvedUb.getQuick(i); assertTrue(di + delta >= presolvedES.getEntry(i)); } Axmb = AMatrix.operate(presolvedES).subtract(bVector); assertEquals(0., Axmb.getNorm(), 1.5 * expectedTolerance); //check for 0-rows List<Integer> zeroRows = new ArrayList<Integer>(); for (int i = 0; i < presolvedA.rows(); i++) { boolean isNotZero = false; for (int j = 0; !isNotZero && j < presolvedA.columns(); j++) { isNotZero = Double.compare(0., presolvedA.getQuick(i, j)) != 0; } if (!isNotZero) { zeroRows.add(zeroRows.size(), i); } } if (!zeroRows.isEmpty()) { log.debug("All 0 entries in rows " + ArrayUtils.toString(zeroRows)); fail(); } //check for 0-columns List<Integer> zeroCols = new ArrayList<Integer>(); for (int j = 0; j < presolvedA.columns(); j++) { boolean isNotZero = false; for (int i = 0; !isNotZero && i < presolvedA.rows(); i++) { isNotZero = Double.compare(0., presolvedA.getQuick(i, j)) != 0; } if (!isNotZero) { zeroCols.add(zeroCols.size(), j); } } if (!zeroCols.isEmpty()) { log.debug("All 0 entries in columns " + ArrayUtils.toString(zeroCols)); fail(); } // check rank(A): must be A pXn with rank(A)=p < n qr = new QRSparseFactorization(new SparseDoubleMatrix2D(presolvedA.toArray())); qr.factorize(); boolean isFullRank = qr.hasFullRank(); log.debug("p : " + AMatrix.getRowDimension()); log.debug("n : " + AMatrix.getColumnDimension()); log.debug("full rank: " + isFullRank); assertTrue(AMatrix.getRowDimension() < AMatrix.getColumnDimension()); assertTrue(isFullRank); } }
From source file:com.joptimizer.optimizers.LPPresolverTest.java
public void testFromFile4() throws Exception { log.debug("testFromFile4"); String problemId = "4"; log.debug("problemId: " + problemId); double[] c = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "presolving" + File.separator + "c" + problemId + ".txt"); double[][] A = Utils.loadDoubleMatrixFromFile( "lp" + File.separator + "presolving" + File.separator + "A" + problemId + ".csv", ",".charAt(0)); double[] b = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "presolving" + File.separator + "b" + problemId + ".txt"); double[] lb = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "presolving" + File.separator + "lb" + problemId + ".txt"); double[] ub = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "presolving" + File.separator + "ub" + problemId + ".txt"); double s = 0; try {//from www . j ava 2 s . c o m s = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "presolving" + File.separator + "s" + problemId + ".txt")[0]; } catch (Exception e) { } double[] expectedSolution = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "presolving" + File.separator + "sol" + problemId + ".txt"); double expectedValue = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "presolving" + File.separator + "value" + problemId + ".txt")[0]; double expectedTolerance = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "presolving" + File.separator + "tolerance" + problemId + ".txt")[0]; expectedTolerance = Math.max(expectedTolerance, MatrixUtils.createRealMatrix(A).operate(MatrixUtils.createRealVector(expectedSolution)) .subtract(MatrixUtils.createRealVector(b)).getNorm()); doPresolving(c, A, b, lb, ub, s, expectedSolution, expectedValue, expectedTolerance); }