List of usage examples for org.apache.commons.math3.linear ArrayRealVector ArrayRealVector
public ArrayRealVector(ArrayRealVector v) throws NullArgumentException
From source file:com.joptimizer.optimizers.LPPrimalDualMethodTest.java
/** * Problem in the form//from w w w. jav a 2s . c om * min(c.x) s.t. * A.x = b * lb <= x <= ub * * This problem involves recursive column duplicate reductions. * This is a good for testing with a small size problem. */ public void testCAbLbUb5() throws Exception { log.debug("testCAbLbUb5"); String problemId = "5"; log.debug("problemId: " + problemId); double[] c = Utils.loadDoubleArrayFromFile("lp" + File.separator + "c" + problemId + ".txt"); double[][] A = Utils.loadDoubleMatrixFromFile("lp" + File.separator + "A" + problemId + ".csv", ",".charAt(0)); double[] b = Utils.loadDoubleArrayFromFile("lp" + File.separator + "b" + problemId + ".txt"); double[] lb = Utils.loadDoubleArrayFromFile("lp" + File.separator + "lb" + problemId + ".txt"); double[] ub = Utils.loadDoubleArrayFromFile("lp" + File.separator + "ub" + problemId + ".txt"); double[] expectedSol = Utils.loadDoubleArrayFromFile("lp" + File.separator + "sol" + problemId + ".txt"); double expectedValue = Utils .loadDoubleArrayFromFile("lp" + File.separator + "value" + problemId + ".txt")[0]; LPOptimizationRequest or = new LPOptimizationRequest(); or.setC(c); or.setA(A); or.setB(b); or.setLb(lb); or.setUb(ub); or.setCheckKKTSolutionAccuracy(true); // or.setToleranceKKT(1.e-7); // or.setToleranceFeas(1.E-7); // or.setTolerance(1.E-7); or.setDumpProblem(true); //optimization LPPrimalDualMethod opt = new LPPrimalDualMethod(); opt.setLPOptimizationRequest(or); int returnCode = opt.optimize(); if (returnCode == OptimizationResponse.FAILED) { fail(); } LPOptimizationResponse response = opt.getLPOptimizationResponse(); double[] sol = response.getSolution(); RealVector cVector = new ArrayRealVector(c); RealVector solVector = new ArrayRealVector(sol); double value = cVector.dotProduct(solVector); log.debug("sol : " + ArrayUtils.toString(sol)); log.debug("value : " + value); //check constraints assertEquals(lb.length, sol.length); assertEquals(ub.length, sol.length); RealVector x = MatrixUtils.createRealVector(sol); RealMatrix AMatrix = MatrixUtils.createRealMatrix(A); RealVector bvector = MatrixUtils.createRealVector(b); for (int i = 0; i < lb.length; i++) { double di = Double.isNaN(lb[i]) ? -Double.MAX_VALUE : lb[i]; assertTrue(di <= x.getEntry(i)); } for (int i = 0; i < ub.length; i++) { double di = Double.isNaN(ub[i]) ? Double.MAX_VALUE : ub[i]; assertTrue(di >= x.getEntry(i)); } RealVector Axb = AMatrix.operate(x).subtract(bvector); assertEquals(0., Axb.getNorm(), or.getToleranceFeas()); //check value assertEquals(expectedValue, value, or.getTolerance()); }
From source file:lirmm.inria.fr.math.BigSparseRealMatrixTest.java
@Test public void testSetColumnVector() { RealMatrix m = new BigSparseRealMatrix(subTestData); RealVector mColumn3 = columnToVector(subColumn3); Assert.assertNotSame(mColumn3, m.getColumnVector(1)); m.setColumnVector(1, mColumn3);//from w w w. j a v a 2 s .c om Assert.assertEquals(mColumn3, m.getColumnVector(1)); try { m.setColumnVector(-1, mColumn3); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setColumnVector(0, new ArrayRealVector(5)); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } }
From source file:com.joptimizer.optimizers.LPPrimalDualMethodTest.java
/** * Problem in the form//from w ww . j a v a 2 s . c o m * min(c.x) s.t. * A.x = b * lb <= x <= ub * * This problem involves column duplicate reduction. * This is a good for testing with a small size problem. */ public void testCAbLbUb6() throws Exception { log.debug("testCAbLbUb6"); String problemId = "6"; log.debug("problemId: " + problemId); double[] c = Utils.loadDoubleArrayFromFile("lp" + File.separator + "c" + problemId + ".txt"); double[][] A = Utils.loadDoubleMatrixFromFile("lp" + File.separator + "A" + problemId + ".csv", ",".charAt(0)); double[] b = Utils.loadDoubleArrayFromFile("lp" + File.separator + "b" + problemId + ".txt"); double[] lb = Utils.loadDoubleArrayFromFile("lp" + File.separator + "lb" + problemId + ".txt"); double[] ub = Utils.loadDoubleArrayFromFile("lp" + File.separator + "ub" + problemId + ".txt"); double[] expectedSol = Utils.loadDoubleArrayFromFile("lp" + File.separator + "sol" + problemId + ".txt"); double expectedvalue = Utils .loadDoubleArrayFromFile("lp" + File.separator + "value" + problemId + ".txt")[0]; LPOptimizationRequest or = new LPOptimizationRequest(); or.setC(c); or.setA(A); or.setB(b); or.setLb(lb); or.setUb(ub); or.setCheckKKTSolutionAccuracy(true); // or.setToleranceKKT(1.e-7); // or.setToleranceFeas(1.E-7); // or.setTolerance(1.E-7); or.setDumpProblem(true); //optimization LPPrimalDualMethod opt = new LPPrimalDualMethod(); opt.setLPOptimizationRequest(or); int returnCode = opt.optimize(); if (returnCode == OptimizationResponse.FAILED) { fail(); } LPOptimizationResponse response = opt.getLPOptimizationResponse(); double[] sol = response.getSolution(); RealVector cVector = new ArrayRealVector(c); RealVector solVector = new ArrayRealVector(sol); double value = cVector.dotProduct(solVector); log.debug("sol : " + ArrayUtils.toString(sol)); log.debug("value : " + value); //check constraints assertEquals(lb.length, sol.length); assertEquals(ub.length, sol.length); RealVector x = MatrixUtils.createRealVector(sol); RealMatrix AMatrix = MatrixUtils.createRealMatrix(A); RealVector bvector = MatrixUtils.createRealVector(b); for (int i = 0; i < lb.length; i++) { double di = Double.isNaN(lb[i]) ? -Double.MAX_VALUE : lb[i]; assertTrue(di <= x.getEntry(i)); } for (int i = 0; i < ub.length; i++) { double di = Double.isNaN(ub[i]) ? Double.MAX_VALUE : ub[i]; assertTrue(di >= x.getEntry(i)); } RealVector Axb = AMatrix.operate(x).subtract(bvector); assertEquals(0., Axb.getNorm(), or.getToleranceFeas()); //check value assertEquals(expectedvalue, value, or.getTolerance()); }
From source file:com.joptimizer.optimizers.LPPrimalDualMethodTest.java
/** * Problem in the form//from w ww. j a v a 2 s . c o m * min(c.x) s.t. * G.x < h * A.x = b * lb <= x <= ub * */ public void testCGhAbLbUb7() throws Exception { log.debug("testCGhAbLbUb7"); String problemId = "7"; log.debug("problemId: " + problemId); double[] c = Utils.loadDoubleArrayFromFile("lp" + File.separator + "c" + problemId + ".txt"); double[][] G = Utils.loadDoubleMatrixFromFile("lp" + File.separator + "G" + problemId + ".csv", ",".charAt(0)); double[] h = Utils.loadDoubleArrayFromFile("lp" + File.separator + "h" + problemId + ".txt"); ; double[][] A = Utils.loadDoubleMatrixFromFile("lp" + File.separator + "A" + problemId + ".csv", ",".charAt(0)); double[] b = Utils.loadDoubleArrayFromFile("lp" + File.separator + "b" + problemId + ".txt"); double[] lb = Utils.loadDoubleArrayFromFile("lp" + File.separator + "lb" + problemId + ".txt"); double[] ub = Utils.loadDoubleArrayFromFile("lp" + File.separator + "ub" + problemId + ".txt"); double[] expectedSol = Utils.loadDoubleArrayFromFile("lp" + File.separator + "sol" + problemId + ".txt"); double expectedvalue = Utils .loadDoubleArrayFromFile("lp" + File.separator + "value" + problemId + ".txt")[0]; //the unbounded bounds are saved on the files with NaN values, so substitute them with acceptable values lb = Utils.replaceValues(lb, Double.NaN, LPPrimalDualMethod.DEFAULT_MIN_LOWER_BOUND); ub = Utils.replaceValues(ub, Double.NaN, LPPrimalDualMethod.DEFAULT_MAX_UPPER_BOUND); LPOptimizationRequest or = new LPOptimizationRequest(); or.setC(c); or.setG(G); or.setH(h); or.setA(A); or.setB(b); or.setLb(lb); or.setUb(ub); or.setCheckKKTSolutionAccuracy(true); // or.setToleranceKKT(1.e-7); // or.setToleranceFeas(1.E-7); // or.setTolerance(1.E-7); or.setDumpProblem(true); //or.setPresolvingDisabled(true); //or.setRescalingDisabled(true); //optimization LPPrimalDualMethod opt = new LPPrimalDualMethod(); opt.setLPOptimizationRequest(or); int returnCode = opt.optimize(); if (returnCode == OptimizationResponse.FAILED) { fail(); } LPOptimizationResponse response = opt.getLPOptimizationResponse(); double[] sol = response.getSolution(); RealVector cVector = new ArrayRealVector(c); RealVector solVector = new ArrayRealVector(sol); double value = cVector.dotProduct(solVector); log.debug("sol : " + ArrayUtils.toString(sol)); log.debug("value : " + value); //check constraints assertEquals(lb.length, sol.length); assertEquals(ub.length, sol.length); RealVector x = MatrixUtils.createRealVector(sol); RealMatrix GMatrix = MatrixUtils.createRealMatrix(G); RealVector hvector = MatrixUtils.createRealVector(h); RealMatrix AMatrix = MatrixUtils.createRealMatrix(A); RealVector bvector = MatrixUtils.createRealVector(b); for (int i = 0; i < lb.length; i++) { assertTrue(lb[i] <= x.getEntry(i)); } for (int i = 0; i < ub.length; i++) { double di = Double.isNaN(lb[i]) ? -Double.MAX_VALUE : lb[i]; assertTrue(di <= x.getEntry(i)); } RealVector Gxh = GMatrix.operate(x).subtract(hvector); for (int i = 0; i < Gxh.getDimension(); i++) { double di = Double.isNaN(ub[i]) ? Double.MAX_VALUE : ub[i]; assertTrue(di >= x.getEntry(i)); } RealVector Axb = AMatrix.operate(x).subtract(bvector); assertEquals(0., Axb.getNorm(), or.getToleranceFeas()); assertEquals(expectedSol.length, sol.length); for (int i = 0; i < sol.length; i++) { assertEquals(expectedSol[0], sol[0], or.getTolerance()); } assertEquals(expectedvalue, value, or.getTolerance()); }
From source file:com.joptimizer.optimizers.LPPrimalDualMethodTest.java
/** * Problem in the form/*ww w . j a va 2s .co m*/ * min(c.x) s.t. * A.x = b * lb <= x <= ub */ public void testCAbLbUb8() throws Exception { log.debug("testCAbLbUb8"); String problemId = "8"; log.debug("problemId: " + problemId); double[] c = Utils.loadDoubleArrayFromFile("lp" + File.separator + "c" + problemId + ".txt"); double[][] A = Utils.loadDoubleMatrixFromFile("lp" + File.separator + "A" + problemId + ".csv", ",".charAt(0)); double[] b = Utils.loadDoubleArrayFromFile("lp" + File.separator + "b" + problemId + ".txt"); double[] lb = Utils.loadDoubleArrayFromFile("lp" + File.separator + "lb" + problemId + ".txt"); double[] ub = Utils.loadDoubleArrayFromFile("lp" + File.separator + "ub" + problemId + ".txt"); double[] expectedSol = Utils.loadDoubleArrayFromFile("lp" + File.separator + "sol" + problemId + ".txt"); double expectedvalue = Utils .loadDoubleArrayFromFile("lp" + File.separator + "value" + problemId + ".txt")[0]; //the unbounded bounds are saved on the files with NaN values, so substitute them with acceptable values lb = Utils.replaceValues(lb, Double.NaN, LPPrimalDualMethod.DEFAULT_MIN_LOWER_BOUND); ub = Utils.replaceValues(ub, Double.NaN, LPPrimalDualMethod.DEFAULT_MAX_UPPER_BOUND); LPOptimizationRequest or = new LPOptimizationRequest(); or.setC(c); or.setA(A); or.setB(b); or.setLb(lb); or.setUb(ub); or.setCheckKKTSolutionAccuracy(true); // or.setToleranceKKT(1.e-7); // or.setToleranceFeas(1.E-7); // or.setTolerance(1.E-7); or.setDumpProblem(true); or.setRescalingDisabled(true); //optimization LPPrimalDualMethod opt = new LPPrimalDualMethod(); opt.setLPOptimizationRequest(or); int returnCode = opt.optimize(); if (returnCode == OptimizationResponse.FAILED) { fail(); } LPOptimizationResponse response = opt.getLPOptimizationResponse(); double[] sol = response.getSolution(); RealVector cVector = new ArrayRealVector(c); RealVector solVector = new ArrayRealVector(sol); double value = cVector.dotProduct(solVector); log.debug("sol : " + ArrayUtils.toString(sol)); log.debug("value : " + value); //check constraints assertEquals(lb.length, sol.length); assertEquals(ub.length, sol.length); RealVector x = MatrixUtils.createRealVector(sol); RealMatrix AMatrix = MatrixUtils.createRealMatrix(A); RealVector bvector = MatrixUtils.createRealVector(b); for (int i = 0; i < lb.length; i++) { double di = Double.isNaN(lb[i]) ? -Double.MAX_VALUE : lb[i]; assertTrue(di <= x.getEntry(i)); } for (int i = 0; i < ub.length; i++) { double di = Double.isNaN(ub[i]) ? Double.MAX_VALUE : ub[i]; assertTrue(di >= x.getEntry(i)); } RealVector Axb = AMatrix.operate(x).subtract(bvector); assertEquals(0., Axb.getNorm(), or.getToleranceFeas()); //check value assertEquals(expectedvalue, value, or.getTolerance()); }
From source file:com.joptimizer.optimizers.LPPrimalDualMethodTest.java
/** * Problem in the form//w w w . j av a 2s .c o m * min(c.x) s.t. * G.x < h * A.x = b * lb <= x <= ub * */ public void testCGhAbLbUb10() throws Exception { log.debug("testCGhAbLbUb10"); String problemId = "10"; log.debug("problemId: " + problemId); double[] c = Utils.loadDoubleArrayFromFile("lp" + File.separator + "c" + problemId + ".txt"); double[][] G = Utils.loadDoubleMatrixFromFile("lp" + File.separator + "G" + problemId + ".csv", ",".charAt(0)); double[] h = Utils.loadDoubleArrayFromFile("lp" + File.separator + "h" + problemId + ".txt"); ; double[][] A = Utils.loadDoubleMatrixFromFile("lp" + File.separator + "A" + problemId + ".csv", ",".charAt(0)); double[] b = Utils.loadDoubleArrayFromFile("lp" + File.separator + "b" + problemId + ".txt"); double[] lb = Utils.loadDoubleArrayFromFile("lp" + File.separator + "lb" + problemId + ".txt"); double[] ub = Utils.loadDoubleArrayFromFile("lp" + File.separator + "ub" + problemId + ".txt"); double[] expectedSol = Utils.loadDoubleArrayFromFile("lp" + File.separator + "sol" + problemId + ".txt"); double expectedvalue = Utils .loadDoubleArrayFromFile("lp" + File.separator + "value" + problemId + ".txt")[0]; //the unbounded bounds are saved on the files with NaN values, so substitute them with acceptable values lb = Utils.replaceValues(lb, Double.NaN, LPPrimalDualMethod.DEFAULT_MIN_LOWER_BOUND); ub = Utils.replaceValues(ub, Double.NaN, LPPrimalDualMethod.DEFAULT_MAX_UPPER_BOUND); LPOptimizationRequest or = new LPOptimizationRequest(); or.setC(c); or.setG(G); or.setH(h); or.setA(A); or.setB(b); or.setLb(lb); or.setUb(ub); or.setCheckKKTSolutionAccuracy(true); // or.setToleranceKKT(1.e-7); // or.setToleranceFeas(1.E-7); // or.setTolerance(1.E-7); or.setDumpProblem(true); //or.setPresolvingDisabled(true); //or.setRescalingDisabled(true); //optimization LPPrimalDualMethod opt = new LPPrimalDualMethod(); opt.setLPOptimizationRequest(or); int returnCode = opt.optimize(); if (returnCode == OptimizationResponse.FAILED) { fail(); } LPOptimizationResponse response = opt.getLPOptimizationResponse(); double[] sol = response.getSolution(); RealVector cVector = new ArrayRealVector(c); RealVector solVector = new ArrayRealVector(sol); double value = cVector.dotProduct(solVector); log.debug("sol : " + ArrayUtils.toString(sol)); log.debug("value : " + value); //check constraints assertEquals(lb.length, sol.length); assertEquals(ub.length, sol.length); RealVector x = MatrixUtils.createRealVector(sol); RealMatrix GMatrix = MatrixUtils.createRealMatrix(G); RealVector hvector = MatrixUtils.createRealVector(h); RealMatrix AMatrix = MatrixUtils.createRealMatrix(A); RealVector bvector = MatrixUtils.createRealVector(b); for (int i = 0; i < lb.length; i++) { assertTrue(lb[i] <= x.getEntry(i)); } for (int i = 0; i < ub.length; i++) { double di = Double.isNaN(lb[i]) ? -Double.MAX_VALUE : lb[i]; assertTrue(di <= x.getEntry(i)); } RealVector Gxh = GMatrix.operate(x).subtract(hvector); for (int i = 0; i < Gxh.getDimension(); i++) { double di = Double.isNaN(ub[i]) ? Double.MAX_VALUE : ub[i]; assertTrue(di >= x.getEntry(i)); } RealVector Axb = AMatrix.operate(x).subtract(bvector); assertEquals(0., Axb.getNorm(), or.getToleranceFeas()); assertEquals(expectedSol.length, sol.length); // for(int i=0; i<sol.length; i++){ // assertEquals(expectedSol[0], sol[0], or.getTolerance()); // } assertEquals(expectedvalue, value, or.getTolerance()); }
From source file:com.joptimizer.optimizers.LPPrimalDualMethodTest.java
/** * Problem in the form//from w ww. ja va 2 s . c om * min(c.x) s.t. * G.x < h * A.x = b * lb <= x <= ub * */ public void testCGhAbLbUb11() throws Exception { log.debug("testCGhAbLbUb11"); String problemId = "10"; log.debug("problemId: " + problemId); double[] c = Utils.loadDoubleArrayFromFile("lp" + File.separator + "c" + problemId + ".txt"); double[][] G = Utils.loadDoubleMatrixFromFile("lp" + File.separator + "G" + problemId + ".csv", ",".charAt(0)); double[] h = Utils.loadDoubleArrayFromFile("lp" + File.separator + "h" + problemId + ".txt"); ; double[][] A = Utils.loadDoubleMatrixFromFile("lp" + File.separator + "A" + problemId + ".csv", ",".charAt(0)); double[] b = Utils.loadDoubleArrayFromFile("lp" + File.separator + "b" + problemId + ".txt"); double[] lb = Utils.loadDoubleArrayFromFile("lp" + File.separator + "lb" + problemId + ".txt"); double[] ub = Utils.loadDoubleArrayFromFile("lp" + File.separator + "ub" + problemId + ".txt"); double[] expectedSol = Utils.loadDoubleArrayFromFile("lp" + File.separator + "sol" + problemId + ".txt"); double expectedValue = Utils .loadDoubleArrayFromFile("lp" + File.separator + "value" + problemId + ".txt")[0]; //the unbounded bounds are saved on the files with NaN values, so substitute them with acceptable values lb = Utils.replaceValues(lb, Double.NaN, LPPrimalDualMethod.DEFAULT_MIN_LOWER_BOUND); ub = Utils.replaceValues(ub, Double.NaN, LPPrimalDualMethod.DEFAULT_MAX_UPPER_BOUND); //check expected sol RealVector expectedX = MatrixUtils.createRealVector(expectedSol); RealMatrix GMatrix = MatrixUtils.createRealMatrix(G); RealVector hvector = MatrixUtils.createRealVector(h); RealMatrix AMatrix = MatrixUtils.createRealMatrix(A); RealVector bvector = MatrixUtils.createRealVector(b); for (int i = 0; i < lb.length; i++) { assertTrue(lb[i] <= expectedX.getEntry(i)); } RealVector Gxh = GMatrix.operate(expectedX).subtract(hvector); for (int i = 0; i < Gxh.getDimension(); i++) { assertTrue(Gxh.getEntry(i) <= 0); } RealVector Axb = AMatrix.operate(expectedX).subtract(bvector); assertEquals(0., Axb.getNorm(), 1.E-6); LPOptimizationRequest or = new LPOptimizationRequest(); or.setC(c); or.setG(G); or.setH(h); or.setA(A); or.setB(b); or.setLb(lb); or.setUb(ub); or.setCheckKKTSolutionAccuracy(true); or.setDumpProblem(true); //or.setPresolvingDisabled(true); //or.setRescalingDisabled(true); //optimization LPPrimalDualMethod opt = new LPPrimalDualMethod(); opt.setLPOptimizationRequest(or); int returnCode = opt.optimize(); if (returnCode == OptimizationResponse.FAILED) { fail(); } LPOptimizationResponse response = opt.getLPOptimizationResponse(); double[] sol = response.getSolution(); RealVector cVector = new ArrayRealVector(c); RealVector solVector = new ArrayRealVector(sol); double value = cVector.dotProduct(solVector); log.debug("sol : " + ArrayUtils.toString(sol)); log.debug("value : " + value); log.debug("expectedValue : " + expectedValue); assertEquals(expectedValue, value, or.getTolerance()); }
From source file:com.joptimizer.optimizers.JOptimizerTest.java
public void testLinearProgramming7D() throws Exception { Log.d(MainActivity.JOPTIMIZER_LOGTAG, "testLinearProgramming7D"); double[] CVector = new double[] { 0.0, 0.0, 0.0, 1.0, 0.833, 0.833, 0.833 }; double[][] AMatrix = new double[][] { { 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0 } }; double[] BVector = new double[] { 1.0 }; double[][] GMatrix = new double[][] { { 0.014, 0.009, 0.021, 1.0, 1.0, 0.0, 0.0 }, { 0.001, 0.002, -0.002, 1.0, 0.0, 1.0, 0.0 }, { 0.003, -0.005, 0.002, 1.0, 0.0, 0.0, 1.0 }, { 0.006, 0.002, 0.007, 0.0, 0.0, 0.0, 0.0 }, { 1.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, 1.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, 1.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }, { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 } }; double[] HVector = new double[] { 0.0, 0.0, 0.0, 0.0010, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; // Objective function (plane) LinearMultivariateRealFunction objectiveFunction = new LinearMultivariateRealFunction(CVector, 0.0); //inequalities (polyhedral feasible set -G.X-H<0 ) ConvexMultivariateRealFunction[] inequalities = new ConvexMultivariateRealFunction[GMatrix.length]; for (int i = 0; i < GMatrix.length; i++) { inequalities[i] = new LinearMultivariateRealFunction( new ArrayRealVector(GMatrix[i]).mapMultiply(-1.).toArray(), -HVector[i]); }//from w ww . j av a 2 s . c o m //optimization problem OptimizationRequest or = new OptimizationRequest(); or.setF0(objectiveFunction); or.setFi(inequalities); or.setA(AMatrix); or.setB(BVector); or.setInitialPoint(new double[] { 0.25, 0.25, 0.5, 0.01, 0.01, 0.01, 0.01 }); //optimization JOptimizer opt = new JOptimizer(); opt.setOptimizationRequest(or); int returnCode = opt.optimize(); if (returnCode == OptimizationResponse.FAILED) { fail(); } OptimizationResponse response = opt.getOptimizationResponse(); double[] sol = response.getSolution(); Log.d(MainActivity.JOPTIMIZER_LOGTAG, "sol: " + ArrayUtils.toString(sol)); Log.d(MainActivity.JOPTIMIZER_LOGTAG, "value : " + objectiveFunction.value(sol)); assertTrue(sol[0] > 0); assertTrue(sol[1] > 0); assertTrue(sol[2] > 0); assertTrue(sol[4] > 0); assertTrue(sol[5] > 0); assertTrue(sol[6] > 0); assertEquals(sol[0] + sol[1] + sol[2], 1., 0.00000001); assertTrue(0.006 * sol[0] + 0.002 * sol[1] + 0.007 * sol[2] > 0.0010); }
From source file:com.itemanalysis.psychometrics.optimization.BOBYQAOptimizer.java
/** * The arguments N, NPT, XPT, XOPT, BMAT, ZMAT, NDIM, SL and SU all have * the same meanings as the corresponding arguments of BOBYQB. * KOPT is the index of the optimal interpolation point. * KNEW is the index of the interpolation point that is going to be moved. * ADELT is the current trust region bound. * XNEW will be set to a suitable new position for the interpolation point * XPT(KNEW,.). Specifically, it satisfies the SL, SU and trust region * bounds and it should provide a large denominator in the next call of * UPDATE. The step XNEW-XOPT from XOPT is restricted to moves along the * straight lines through XOPT and another interpolation point. * XALT also provides a large value of the modulus of the KNEW-th Lagrange * function subject to the constraints that have been mentioned, its main * difference from XNEW being that XALT-XOPT is a constrained version of * the Cauchy step within the trust region. An exception is that XALT is * not calculated if all components of GLAG (see below) are zero. * ALPHA will be set to the KNEW-th diagonal element of the H matrix. * CAUCHY will be set to the square of the KNEW-th Lagrange function at * the step XALT-XOPT from XOPT for the vector XALT that is returned, * except that CAUCHY is set to zero if XALT is not calculated. * GLAG is a working space vector of length N for the gradientAt of the * KNEW-th Lagrange function at XOPT. * HCOL is a working space vector of length NPT for the second derivative * coefficients of the KNEW-th Lagrange function. * W is a working space vector of length 2N that is going to hold the * constrained Cauchy step from XOPT of the Lagrange function, followed * by the downhill version of XALT when the uphill step is calculated. * * Set the first NPT components of W to the leading elements of the * KNEW-th column of the H matrix./*from w w w.jav a 2s . co m*/ * @param knew * @param adelt */ private double[] altmov(int knew, double adelt) { printMethod(); // XXX final int n = currentBest.getDimension(); final int npt = numberOfInterpolationPoints; final ArrayRealVector glag = new ArrayRealVector(n); final ArrayRealVector hcol = new ArrayRealVector(npt); final ArrayRealVector work1 = new ArrayRealVector(n); final ArrayRealVector work2 = new ArrayRealVector(n); for (int k = 0; k < npt; k++) { hcol.setEntry(k, ZERO); } for (int j = 0, max = npt - n - 1; j < max; j++) { final double tmp = zMatrix.getEntry(knew, j); for (int k = 0; k < npt; k++) { hcol.setEntry(k, hcol.getEntry(k) + tmp * zMatrix.getEntry(k, j)); } } final double alpha = hcol.getEntry(knew); final double ha = HALF * alpha; // Calculate the gradientAt of the KNEW-th Lagrange function at XOPT. for (int i = 0; i < n; i++) { glag.setEntry(i, bMatrix.getEntry(knew, i)); } for (int k = 0; k < npt; k++) { double tmp = ZERO; for (int j = 0; j < n; j++) { tmp += interpolationPoints.getEntry(k, j) * trustRegionCenterOffset.getEntry(j); } tmp *= hcol.getEntry(k); for (int i = 0; i < n; i++) { glag.setEntry(i, glag.getEntry(i) + tmp * interpolationPoints.getEntry(k, i)); } } // Search for a large denominator along the straight lines through XOPT // and another interpolation point. SLBD and SUBD will be lower and upper // bounds on the step along each of these lines in turn. PREDSQ will be // set to the square of the predicted denominator for each line. PRESAV // will be set to the largest admissible value of PREDSQ that occurs. double presav = ZERO; double step = Double.NaN; int ksav = 0; int ibdsav = 0; double stpsav = 0; for (int k = 0; k < npt; k++) { if (k == trustRegionCenterInterpolationPointIndex) { continue; } double dderiv = ZERO; double distsq = ZERO; for (int i = 0; i < n; i++) { final double tmp = interpolationPoints.getEntry(k, i) - trustRegionCenterOffset.getEntry(i); dderiv += glag.getEntry(i) * tmp; distsq += tmp * tmp; } double subd = adelt / Math.sqrt(distsq); double slbd = -subd; int ilbd = 0; int iubd = 0; final double sumin = Math.min(ONE, subd); // Revise SLBD and SUBD if necessary because of the bounds in SL and SU. for (int i = 0; i < n; i++) { final double tmp = interpolationPoints.getEntry(k, i) - trustRegionCenterOffset.getEntry(i); if (tmp > ZERO) { if (slbd * tmp < lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) { slbd = (lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) / tmp; ilbd = -i - 1; } if (subd * tmp > upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) { // Computing MAX subd = Math.max(sumin, (upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) / tmp); iubd = i + 1; } } else if (tmp < ZERO) { if (slbd * tmp > upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) { slbd = (upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) / tmp; ilbd = i + 1; } if (subd * tmp < lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) { // Computing MAX subd = Math.max(sumin, (lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) / tmp); iubd = -i - 1; } } } // Seek a large modulus of the KNEW-th Lagrange function when the index // of the other interpolation point on the line through XOPT is KNEW. step = slbd; int isbd = ilbd; double vlag = Double.NaN; if (k == knew) { final double diff = dderiv - ONE; vlag = slbd * (dderiv - slbd * diff); final double d1 = subd * (dderiv - subd * diff); if (Math.abs(d1) > Math.abs(vlag)) { step = subd; vlag = d1; isbd = iubd; } final double d2 = HALF * dderiv; final double d3 = d2 - diff * slbd; final double d4 = d2 - diff * subd; if (d3 * d4 < ZERO) { final double d5 = d2 * d2 / diff; if (Math.abs(d5) > Math.abs(vlag)) { step = d2 / diff; vlag = d5; isbd = 0; } } // Search along each of the other lines through XOPT and another point. } else { vlag = slbd * (ONE - slbd); final double tmp = subd * (ONE - subd); if (Math.abs(tmp) > Math.abs(vlag)) { step = subd; vlag = tmp; isbd = iubd; } if (subd > HALF) { if (Math.abs(vlag) < ONE_OVER_FOUR) { step = HALF; vlag = ONE_OVER_FOUR; isbd = 0; } } vlag *= dderiv; } // Calculate PREDSQ for the current line search and maintain PRESAV. final double tmp = step * (ONE - step) * distsq; final double predsq = vlag * vlag * (vlag * vlag + ha * tmp * tmp); if (predsq > presav) { presav = predsq; ksav = k; stpsav = step; ibdsav = isbd; } } // Construct XNEW in a way that satisfies the bound constraints exactly. for (int i = 0; i < n; i++) { final double tmp = trustRegionCenterOffset.getEntry(i) + stpsav * (interpolationPoints.getEntry(ksav, i) - trustRegionCenterOffset.getEntry(i)); newPoint.setEntry(i, Math.max(lowerDifference.getEntry(i), Math.min(upperDifference.getEntry(i), tmp))); } if (ibdsav < 0) { newPoint.setEntry(-ibdsav - 1, lowerDifference.getEntry(-ibdsav - 1)); } if (ibdsav > 0) { newPoint.setEntry(ibdsav - 1, upperDifference.getEntry(ibdsav - 1)); } // Prepare for the iterative method that assembles the constrained Cauchy // step in W. The sum of squares of the fixed components of W is formed in // WFIXSQ, and the free components of W are set to BIGSTP. final double bigstp = adelt + adelt; int iflag = 0; double cauchy = Double.NaN; double csave = ZERO; while (true) { double wfixsq = ZERO; double ggfree = ZERO; for (int i = 0; i < n; i++) { final double glagValue = glag.getEntry(i); work1.setEntry(i, ZERO); if (Math.min(trustRegionCenterOffset.getEntry(i) - lowerDifference.getEntry(i), glagValue) > ZERO || Math.max(trustRegionCenterOffset.getEntry(i) - upperDifference.getEntry(i), glagValue) < ZERO) { work1.setEntry(i, bigstp); // Computing 2nd power ggfree += glagValue * glagValue; } } if (ggfree == ZERO) { return new double[] { alpha, ZERO }; } // Investigate whether more components of W can be fixed. final double tmp1 = adelt * adelt - wfixsq; if (tmp1 > ZERO) { step = Math.sqrt(tmp1 / ggfree); ggfree = ZERO; for (int i = 0; i < n; i++) { if (work1.getEntry(i) == bigstp) { final double tmp2 = trustRegionCenterOffset.getEntry(i) - step * glag.getEntry(i); if (tmp2 <= lowerDifference.getEntry(i)) { work1.setEntry(i, lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)); // Computing 2nd power final double d1 = work1.getEntry(i); wfixsq += d1 * d1; } else if (tmp2 >= upperDifference.getEntry(i)) { work1.setEntry(i, upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)); // Computing 2nd power final double d1 = work1.getEntry(i); wfixsq += d1 * d1; } else { // Computing 2nd power final double d1 = glag.getEntry(i); ggfree += d1 * d1; } } } } // Set the remaining free components of W and all components of XALT, // except that W may be scaled later. double gw = ZERO; for (int i = 0; i < n; i++) { final double glagValue = glag.getEntry(i); if (work1.getEntry(i) == bigstp) { work1.setEntry(i, -step * glagValue); final double min = Math.min(upperDifference.getEntry(i), trustRegionCenterOffset.getEntry(i) + work1.getEntry(i)); alternativeNewPoint.setEntry(i, Math.max(lowerDifference.getEntry(i), min)); } else if (work1.getEntry(i) == ZERO) { alternativeNewPoint.setEntry(i, trustRegionCenterOffset.getEntry(i)); } else if (glagValue > ZERO) { alternativeNewPoint.setEntry(i, lowerDifference.getEntry(i)); } else { alternativeNewPoint.setEntry(i, upperDifference.getEntry(i)); } gw += glagValue * work1.getEntry(i); } // Set CURV to the curvature of the KNEW-th Lagrange function along W. // Scale W by a factor less than one if that can reduce the modulus of // the Lagrange function at XOPT+W. Set CAUCHY to the final value of // the square of this function. double curv = ZERO; for (int k = 0; k < npt; k++) { double tmp = ZERO; for (int j = 0; j < n; j++) { tmp += interpolationPoints.getEntry(k, j) * work1.getEntry(j); } curv += hcol.getEntry(k) * tmp * tmp; } if (iflag == 1) { curv = -curv; } if (curv > -gw && curv < -gw * (ONE + Math.sqrt(TWO))) { final double scale = -gw / curv; for (int i = 0; i < n; i++) { final double tmp = trustRegionCenterOffset.getEntry(i) + scale * work1.getEntry(i); alternativeNewPoint.setEntry(i, Math.max(lowerDifference.getEntry(i), Math.min(upperDifference.getEntry(i), tmp))); } // Computing 2nd power final double d1 = HALF * gw * scale; cauchy = d1 * d1; } else { // Computing 2nd power final double d1 = gw + HALF * curv; cauchy = d1 * d1; } // If IFLAG is zero, then XALT is calculated as before after reversing // the sign of GLAG. Thus two XALT vectors become available. The one that // is chosen is the one that gives the larger value of CAUCHY. if (iflag == 0) { for (int i = 0; i < n; i++) { glag.setEntry(i, -glag.getEntry(i)); work2.setEntry(i, alternativeNewPoint.getEntry(i)); } csave = cauchy; iflag = 1; } else { break; } } if (csave > cauchy) { for (int i = 0; i < n; i++) { alternativeNewPoint.setEntry(i, work2.getEntry(i)); } cauchy = csave; } return new double[] { alpha, cauchy }; }
From source file:com.joptimizer.optimizers.LPPrimalDualMethodNetlibTest.java
private void checkSolution(LPNetlibProblem problem, LPOptimizationRequest or, LPOptimizationResponse response) throws Exception { double expectedvalue = problem.optimalValue; log.debug("expectedvalue : " + expectedvalue); double[] sol = response.getSolution(); RealVector cVector = new ArrayRealVector(or.getC().toArray()); RealVector solVector = new ArrayRealVector(sol); double value = cVector.dotProduct(solVector); log.debug("sol : " + ArrayUtils.toString(sol)); log.debug("value : " + value); //check constraints assertEquals(or.getLb().size(), sol.length); assertEquals(or.getUb().size(), sol.length); RealVector x = MatrixUtils.createRealVector(sol); //x >= lb/*from w w w.j a v a 2 s . com*/ double maxLbmx = -Double.MAX_VALUE; for (int i = 0; i < or.getLb().size(); i++) { double di = Double.isNaN(or.getLb().getQuick(i)) ? -Double.MAX_VALUE : or.getLb().getQuick(i); maxLbmx = Math.max(maxLbmx, di - x.getEntry(i)); assertTrue(di <= x.getEntry(i) + or.getTolerance()); } log.debug("max(lb - x): " + maxLbmx); //x <= ub double maxXmub = -Double.MAX_VALUE; for (int i = 0; i < or.getUb().size(); i++) { double di = Double.isNaN(or.getUb().getQuick(i)) ? Double.MAX_VALUE : or.getUb().getQuick(i); maxXmub = Math.max(maxXmub, x.getEntry(i) - di); assertTrue(di + or.getTolerance() >= x.getEntry(i)); } log.debug("max(x - ub): " + maxXmub); //G.x <h if (or.getG() != null && or.getG().rows() > 0) { RealMatrix GMatrix = MatrixUtils.createRealMatrix(or.getG().toArray()); RealVector hvector = MatrixUtils.createRealVector(or.getH().toArray()); RealVector Gxh = GMatrix.operate(x).subtract(hvector); double maxGxh = -Double.MAX_VALUE; for (int i = 0; i < Gxh.getDimension(); i++) { maxGxh = Math.max(maxGxh, Gxh.getEntry(i)); assertTrue(Gxh.getEntry(i) - or.getTolerance() <= 0); } log.debug("max(G.x - h): " + maxGxh); } //A.x = b if (or.getA() != null && or.getA().rows() > 0) { RealMatrix AMatrix = MatrixUtils.createRealMatrix(or.getA().toArray()); RealVector bvector = MatrixUtils.createRealVector(or.getB().toArray()); RealVector Axb = AMatrix.operate(x).subtract(bvector); double norm = Axb.getNorm(); log.debug("||A.x -b||: " + norm); assertEquals(0., norm, or.getToleranceFeas()); } double percDelta = Math.abs((expectedvalue - value) / expectedvalue); log.debug("percDelta: " + percDelta); //assertEquals(0., percDelta, or.getTolerance()); //assertEquals(expectedvalue, value, or.getTolerance()); assertTrue(value < expectedvalue + or.getTolerance());//can even beat other optimizers! the rebel yell... }