List of usage examples for org.apache.commons.math3.linear ArrayRealVector ArrayRealVector
public ArrayRealVector(ArrayRealVector v) throws NullArgumentException
From source file:com.joptimizer.optimizers.LPStandardConverterTest.java
/** * Standardization of a problem on the form: * min(c) s.t.//from w w w.ja v a 2 s . c om * G.x < h * A.x = b * lb <= x <= ub */ public void testCGhAbLbUb4() throws Exception { log.debug("testCGhAbLbUb4"); String problemId = "4"; 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 nOsSplittingVariables = 0; // for(int i=0; i<lb.length; i++){ // if(Double.compare(lb[i], 0.) != 0){ // nOsSplittingVariables++; // } // } //standard form conversion double unboundedLBValue = Double.NaN;//this is because in the file the unbounded lb are NaN values (and also the default value) double unboundedUBValue = Double.NaN;//this is because in the file the unbounded ub are NaN 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); //check consistency assertEquals(G.length, s); assertEquals(s + lpConverter.getOriginalN() + nOsSplittingVariables, 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 * 1.001); // 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.mycompany.myfirstindoorsapp.PagedActivity.java
public void positionUpdated(Coordinate userPosition, int accuracy) { /**/*from w w w . j a v a 2 s . c o m*/ * Is called each time the Indoors Library calculated a new position for the user * If Lat/Lon/Rotation of your building are set correctly you can calculate a * GeoCoordinate for your users current location in the building. */ // if first try, itialize list of zones if (connected == true && firstCall == false) // if first try { this.rotation = building.getRotation(); accel = new Accel(this, (SensorManager) getSystemService(Context.SENSOR_SERVICE), rotation); firstCall = true; } currentPosition = userPosition; if (kalman != null) { long time = SystemClock.uptimeMillis(); double[] coord = new double[2]; coord[0] = (double) userPosition.x; coord[1] = (double) userPosition.y; kalman.correct(new ArrayRealVector(coord)); double[] estimatedState; estimatedState = kalman.getStateEstimationVector().toArray(); data.addToFilter(time, new Coordinate((int) estimatedState[0], (int) estimatedState[1], FLOORLVL)); data.addToIndoors(time, userPosition); } }
From source file:com.joptimizer.optimizers.LPPrimalDualMethodTest.java
/** * Problem in the form/*w w w . j a va 2 s. c o m*/ * min(c.x) s.t. * G.x < h * A.x = b * * This is the same as testCGhAbLbUb2, but lb and ub are into G. * The presolved problem has a deterministic solution, that is, all the variables have a fixed value. */ public void testCGhAb3() throws Exception { log.debug("testCGhAb3"); String problemId = "3"; 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[] expectedSol = Utils.loadDoubleArrayFromFile("lp" + File.separator + "sol" + problemId + ".txt"); double expectedvalue = Utils .loadDoubleArrayFromFile("lp" + File.separator + "value" + problemId + ".txt")[0]; double minLb = 0; double maxUb = 1.0E15;//it is so high because of the very high values of the elements of h LPOptimizationRequest or = new LPOptimizationRequest(); or.setC(c); or.setG(G); or.setH(h); or.setA(A); or.setB(b); or.setCheckKKTSolutionAccuracy(true); //or.setToleranceKKT(1.e-5); // or.setToleranceFeas(5.E-5); // or.setTolerance(1.E-7); or.setDumpProblem(true); //or.setPresolvingDisabled(true); //optimization LPPrimalDualMethod opt = new LPPrimalDualMethod(minLb, maxUb); 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 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); RealVector Gxh = GMatrix.operate(x).subtract(hvector); for (int i = 0; i < Gxh.getDimension(); i++) { assertTrue(Gxh.getEntry(i) <= 0); } 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:edu.utexas.cs.tactex.servercustomers.factoredcustomer.DefaultCapacityOriginator.java
/** * NOTE: subtle conversion that caused a bug - we use predictions * starting next time-step, but forecastCapacities are assumed * to be from current - so to return data to broker we need to * offset the record by 1/*from w w w . ja va 2 s.c o m*/ */ protected ArrayRealVector convertEnergyProfileFromServerToBroker(CapacityProfile predictedEnergyProfile, int recordLength) throws Exception { //log.info("scaleEnergyProfile()"); //log.info("predictedEnergyProfile" + Arrays.toString(predictedEnergyProfile.values.toArray())); int profileLength = predictedEnergyProfile.NUM_TIMESLOTS; // verify divides boolean divides = (recordLength / profileLength * profileLength) == recordLength; if (!divides) { throw new Exception("How come profileLength doesn't divide recordLength"); } //log.info("recordLength=" + recordLength); ArrayRealVector result = new ArrayRealVector(recordLength); for (int i = 0; i < recordLength; ++i) { result.setEntry(i, predictedEnergyProfile.getCapacity((i + 1) % profileLength)); //log.info("appending " + predictedEnergyProfile.getCapacity( i % profileLength ) + " at " + i); } //log.info("result" + Arrays.toString(result.toArray())); return result; }
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 w w .j ava 2 s. c o m * A.x = b * lb <= x <= ub * * This is the presolved (with JOptimizer) pilot4 netlib problem. * @TODO: the strict conversion is net yet ready. */ public void xxxtestCAbLbUb5Strict() throws Exception { log.debug("testCAbLbUb5Strict"); String problemId = "5"; double[] c = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "standardization" + File.separator + "c" + 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 expectedTol = Utils.loadDoubleArrayFromFile( "lp" + File.separator + "standardization" + File.separator + "tolerance" + problemId + ".txt")[0]; 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 = nOfSlackVariables; //standard form conversion boolean strictlyStandardForm = true; LPStandardConverter lpConverter = new LPStandardConverter(strictlyStandardForm); lpConverter.toStandardForm(c, null, null, 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 AStandard = new Array2DRowRealMatrix(A); RealVector bStandard = new ArrayRealVector(b); double[] expectedStandardSol = lpConverter.getStandardComponents(expectedSol); RealVector expectedStandardSolVector = new ArrayRealVector(expectedStandardSol); for (int i = 0; i < expectedStandardSolVector.getDimension(); i++) { assertTrue(expectedStandardSolVector.getEntry(i) + 1.E-8 >= 0.); } RealVector Axmb = AStandard.operate(expectedStandardSolVector).subtract(bStandard); for (int i = 0; i < Axmb.getDimension(); i++) { assertEquals(0., Axmb.getEntry(i), expectedTol); } 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:gamlss.smoothing.PB.java
/** * Constructs the base matrix.// ww w. ja v a 2s .c o m * @param colValues - values of the certain column of * the smooth matrix which corresponds to the * currently fitting distribution parameter * @return - base matrix */ //bbase <- function(x, xl, xr, ndx, deg, quantiles=FALSE) private static BlockRealMatrix formX(final ArrayRealVector colValues) { //control$inter <- if (lx<99) 10 else control$inter # //this is to prevent singularities when length(x) is small if (colValues.getDimension() < 99) { Controls.INTER = 10; } //xl <- min(x) double xl = colValues.getMinValue(); //xr <- max(x) double xr = colValues.getMaxValue(); //xmax <- xr + 0.01 * (xr - xl) double xmax = xr + 0.01 * (xr - xl); //xmin <- xl - 0.01 * (xr - xl) double xmin = xl - 0.01 * (xr - xl); //dx <- (xr - xl) / ndx double dx = (xmax - xmin) / Controls.INTER; //if (quantiles) # if true use splineDesign if (Controls.QUANTILES) { //knots <- sort(c(seq(xl-deg*dx, xl, dx),quantile(x, //prob=seq(0, 1, length=ndx)), seq(xr, xr+deg*dx, dx))) ArrayRealVector kts = null; //B <- splineDesign(knots, x = x, outer.ok = TRUE, ord=deg+1) //return(B) return null; } else { //kts <- seq(xl - deg * dx, xr + deg * dx, by = dx) //ArrayRealVector kts = new ArrayRealVector( //ArithmeticSeries.getSeries(xl-deg*dx, xr+deg*dx, dx),false); rConnection.assingVar("min", new double[] { xmin - Controls.DEGREE * dx }); rConnection.assingVar("max", new double[] { xmax + Controls.DEGREE * dx }); rConnection.assingVar("step", new double[] { dx }); ArrayRealVector kts = new ArrayRealVector( rConnection.runEvalDoubles("knots <- seq(min, max, by = step)")); //P <- outer(x, kts, FUN = tpower, deg) BlockRealMatrix pM = MatrixFunctions.outertpowerPB(colValues, kts, Controls.DEGREE); //D <- diff(diag(dim(P)[2]), //diff = deg + 1) / (gamma(deg + 1) * dx ^ deg) BlockRealMatrix tempM = MatrixFunctions .diff(MatrixFunctions.buildIdentityMatrix(pM.getColumnDimension()), Controls.DEGREE + 1); double[][] tempArrArr = new double[tempM.getRowDimension()][tempM.getColumnDimension()]; for (int i = 0; i < tempArrArr.length; i++) { for (int j = 0; j < tempArrArr[i].length; j++) { tempArrArr[i][j] = tempM.getEntry(i, j) / ((FastMath.exp(Gamma.logGamma(Controls.DEGREE + 1))) * FastMath.pow(dx, Controls.DEGREE)); } } tempM = new BlockRealMatrix(tempArrArr); //B <- (-1) ^ (deg + 1) * P %*% t(D) return (BlockRealMatrix) pM.multiply(tempM.transpose()) .scalarMultiply(FastMath.pow(-1, (Controls.DEGREE + 1))); } }
From source file:com.mycompany.myfirstindoorsapp.PagedActivity.java
void onAccelChanged(double[] accelData) //store accelData { if (kalman != null) { long time = SystemClock.uptimeMillis(); kalman.predict(new ArrayRealVector(accelData)); double[] estimatedState; estimatedState = kalman.getStateEstimationVector().toArray(); data.addToFilter(time, new Coordinate((int) estimatedState[0], (int) estimatedState[1], FLOORLVL)); Log.d("velocityvelocity", "" + estimatedState[2] + " | " + estimatedState[3]); if (null != overlayKalman) { indoorsSurfaceFragment.removeOverlay(overlayKalman); overlayKalman = null;// w w w . j a v a 2s . c o m } overlayKalman = new RedSurfaceOverlay( new Coordinate((int) estimatedState[0], (int) estimatedState[1], FLOORLVL)); indoorsSurfaceFragment.addOverlay(overlayKalman); indoorsSurfaceFragment.updateSurface(); } }
From source file:com.joptimizer.optimizers.LPPrimalDualMethodTest.java
/** * Problem in the form/* w ww .ja v a 2s . c o m*/ * min(c.x) s.t. * G.x < h * The objective function of the presolved problem has a 0-gradient. */ public void testCGh4() throws Exception { log.debug("testCGh4"); String problemId = "4"; //the original problem: ok until precision 1.E-7 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[] expectedSol = Utils.loadDoubleArrayFromFile("lp" + File.separator + "sol" + problemId + ".txt"); double expectedValue = Utils .loadDoubleArrayFromFile("lp" + File.separator + "value" + problemId + ".txt")[0]; //double norm = MatrixUtils.createRealMatrix(A).operate(MatrixUtils.createRealVector(expectedSol)).subtract(MatrixUtils.createRealVector(b)).getNorm(); //assertTrue(norm < 1.e-10); LPOptimizationRequest or = new LPOptimizationRequest(); or.setC(c); or.setG(G); or.setH(h); or.setCheckKKTSolutionAccuracy(true); or.setToleranceKKT(1.E-7); or.setToleranceFeas(1.E-6); or.setTolerance(1.E-5); or.setDumpProblem(true); or.setRescalingDisabled(true);//this fails with false //optimization //LPPrimalDualMethodOLD opt = new LPPrimalDualMethodOLD(); 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 RealVector x = MatrixUtils.createRealVector(sol); RealMatrix GMatrix = MatrixUtils.createRealMatrix(G); RealVector hvector = MatrixUtils.createRealVector(h); RealVector Gxh = GMatrix.operate(x).subtract(hvector); for (int i = 0; i < Gxh.getDimension(); i++) { assertTrue(Gxh.getEntry(i) <= 0);//not strictly because some constraint has been treated as a bound } //check value assertEquals(expectedValue, value, or.getTolerance()); }
From source file:lirmm.inria.fr.math.BigSparseRealMatrixTest.java
@Test public void testGetRowVector() { RealMatrix m = new BigSparseRealMatrix(subTestData); RealVector mRow0 = new ArrayRealVector(subRow0[0]); RealVector mRow3 = new ArrayRealVector(subRow3[0]); Assert.assertEquals("Row0", mRow0, m.getRowVector(0)); Assert.assertEquals("Row3", mRow3, m.getRowVector(3)); try {/*from ww w . j a va 2 s . com*/ m.getRowVector(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getRowVector(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } }
From source file:lirmm.inria.fr.math.BigSparseRealMatrixTest.java
@Test public void testSetRowVector() { RealMatrix m = new BigSparseRealMatrix(subTestData); RealVector mRow3 = new ArrayRealVector(subRow3[0]); Assert.assertNotSame(mRow3, m.getRowMatrix(0)); m.setRowVector(0, mRow3);/* w w w . j av a 2s. c o m*/ Assert.assertEquals(mRow3, m.getRowVector(0)); try { m.setRowVector(-1, mRow3); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setRowVector(0, new ArrayRealVector(5)); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } }