List of usage examples for org.apache.commons.math3.linear RealVector setEntry
public abstract void setEntry(int index, double value) throws OutOfRangeException;
From source file:com.joptimizer.optimizers.LPStandardConverterTest.java
/** * Standardization of a problem on the form: * min(c) s.t.// w w w. j a va2 s. co m * 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:edu.stanford.cfuller.colocalization3d.correction.Correction.java
/** * Applies an existing correction to a single x-y position in the Image plane. * * @param x The x-position at which to apply the correction. * @param y The y-position at which to apply the correction. * @return A RealVector containing 3 elements-- the magnitude of the correction in the x, y, and z dimensions, in that order. *///from www. java 2 s .com public RealVector correctPosition(double x, double y) throws UnableToCorrectException { RealVector corrections = new ArrayRealVector(3, 0.0); RealVector distsToCentroids = this.getPositionsForCorrection().getColumnVector(0).mapSubtract(x) .mapToSelf(new Power(2)); distsToCentroids = distsToCentroids .add(this.getPositionsForCorrection().getColumnVector(1).mapSubtract(y).mapToSelf(new Power(2))); distsToCentroids.mapToSelf(new Sqrt()); RealVector distRatio = distsToCentroids.ebeDivide(this.getDistanceCutoffs()); RealVector distRatioBin = new ArrayRealVector(distRatio.getDimension(), 0.0); for (int i = 0; i < distRatio.getDimension(); i++) { if (distRatio.getEntry(i) <= 1) distRatioBin.setEntry(i, 1.0); } RealVector weights = distRatio.map(new Power(2.0)).mapMultiplyToSelf(-3).mapAddToSelf(1) .add(distRatio.map(new Power(3.0)).mapMultiplyToSelf(2)); weights = weights.ebeMultiply(distRatioBin); double sumWeights = 0; int countWeights = 0; for (int i = 0; i < weights.getDimension(); i++) { if (weights.getEntry(i) > 0) { sumWeights += weights.getEntry(i); countWeights++; } } if (countWeights == 0) { // this means there were no points in the correction dataset near the position being corrected. throw (new UnableToCorrectException( "Incomplete coverage in correction dataset at (x,y) = (" + x + ", " + y + ").")); } RealMatrix cX = new Array2DRowRealMatrix(countWeights, this.getCorrectionX().getColumnDimension()); RealMatrix cY = new Array2DRowRealMatrix(countWeights, this.getCorrectionX().getColumnDimension()); RealMatrix cZ = new Array2DRowRealMatrix(countWeights, this.getCorrectionX().getColumnDimension()); RealVector xVec = new ArrayRealVector(countWeights, 0.0); RealVector yVec = new ArrayRealVector(countWeights, 0.0); RealVector keptWeights = new ArrayRealVector(countWeights, 0.0); int keptCounter = 0; for (int i = 0; i < weights.getDimension(); i++) { if (weights.getEntry(i) > 0) { cX.setRowVector(keptCounter, this.getCorrectionX().getRowVector(i)); cY.setRowVector(keptCounter, this.getCorrectionY().getRowVector(i)); cZ.setRowVector(keptCounter, this.getCorrectionZ().getRowVector(i)); xVec.setEntry(keptCounter, x - this.getPositionsForCorrection().getEntry(i, 0)); yVec.setEntry(keptCounter, y - this.getPositionsForCorrection().getEntry(i, 1)); keptWeights.setEntry(keptCounter, weights.getEntry(i)); keptCounter++; } } double xCorr = 0; double yCorr = 0; double zCorr = 0; RealMatrix allCorrectionParameters = new Array2DRowRealMatrix(countWeights, numberOfCorrectionParameters); RealVector ones = new ArrayRealVector(countWeights, 1.0); allCorrectionParameters.setColumnVector(0, ones); allCorrectionParameters.setColumnVector(1, xVec); allCorrectionParameters.setColumnVector(2, yVec); allCorrectionParameters.setColumnVector(3, xVec.map(new Power(2))); allCorrectionParameters.setColumnVector(4, yVec.map(new Power(2))); allCorrectionParameters.setColumnVector(5, xVec.ebeMultiply(yVec)); for (int i = 0; i < countWeights; i++) { xCorr += allCorrectionParameters.getRowVector(i).dotProduct(cX.getRowVector(i)) * keptWeights.getEntry(i); yCorr += allCorrectionParameters.getRowVector(i).dotProduct(cY.getRowVector(i)) * keptWeights.getEntry(i); zCorr += allCorrectionParameters.getRowVector(i).dotProduct(cZ.getRowVector(i)) * keptWeights.getEntry(i); } xCorr /= sumWeights; yCorr /= sumWeights; zCorr /= sumWeights; corrections.setEntry(0, xCorr); corrections.setEntry(1, yCorr); corrections.setEntry(2, zCorr); return corrections; }
From source file:agent.perceptors.visiontree.Player.java
@Override protected synchronized void setField(int guess, int start, int end, char[] msg) { String[] str = new String(msg, start, (end - start)).split(" "); RealVector v = null; switch (guess) { case 1:// w ww .ja v a 2 s.com v = headLocation; break; case 2: v = rightLowerArmLocation; break; case 3: v = leftLowerArmLocation; break; case 4: v = rightFootLocation; break; case 5: v = leftFootLocation; break; } if (v == null) { throw new UnsupportedOperationException("Invalid guess!"); } double r = Double.parseDouble(str[1]); double a = Double.parseDouble(str[2]); double b = Double.parseDouble(str[3]); v.setEntry(0, r * sin(a) * cos(b)); v.setEntry(1, r * sin(a) * sin(b)); v.setEntry(2, r * cos(a)); // for (int i = 0; i < 3; i++) { // v.setEntry(i, Double.parseDouble(str[i+1])); // } }
From source file:com.datumbox.framework.core.machinelearning.featureselection.PCA.java
/** {@inheritDoc} */ @Override//from w w w .j a va 2 s. co m protected void _fit(Dataframe trainingData) { ModelParameters modelParameters = knowledgeBase.getModelParameters(); int n = trainingData.size(); int d = trainingData.xColumnSize(); //convert data into matrix Map<Object, Integer> featureIds = modelParameters.getFeatureIds(); DataframeMatrix matrixDataset = DataframeMatrix.newInstance(trainingData, false, null, featureIds); RealMatrix X = matrixDataset.getX(); //calculate means and subtract them from data RealVector meanValues = new OpenMapRealVector(d); for (Integer columnId : featureIds.values()) { double mean = 0.0; for (int row = 0; row < n; row++) { mean += X.getEntry(row, columnId); } mean /= n; for (int row = 0; row < n; row++) { X.addToEntry(row, columnId, -mean); } meanValues.setEntry(columnId, mean); } modelParameters.setMean(meanValues); //dxd matrix RealMatrix covarianceDD = (X.transpose().multiply(X)).scalarMultiply(1.0 / (n - 1.0)); EigenDecomposition decomposition = new EigenDecomposition(covarianceDD); RealVector eigenValues = new ArrayRealVector(decomposition.getRealEigenvalues(), false); RealMatrix components = decomposition.getV(); //Whiten Components W = U*L^0.5; To whiten them we multiply with L^0.5. if (knowledgeBase.getTrainingParameters().isWhitened()) { RealMatrix sqrtEigenValues = new DiagonalMatrix(d); for (int i = 0; i < d; i++) { sqrtEigenValues.setEntry(i, i, FastMath.sqrt(eigenValues.getEntry(i))); } components = components.multiply(sqrtEigenValues); } //the eigenvalues and their components are sorted by descending order no need to resort them Integer maxDimensions = knowledgeBase.getTrainingParameters().getMaxDimensions(); Double variancePercentageThreshold = knowledgeBase.getTrainingParameters().getVariancePercentageThreshold(); if (variancePercentageThreshold != null && variancePercentageThreshold <= 1) { double totalVariance = 0.0; for (int i = 0; i < d; i++) { totalVariance += eigenValues.getEntry(i); } double sum = 0.0; int varCounter = 0; for (int i = 0; i < d; i++) { sum += eigenValues.getEntry(i) / totalVariance; varCounter++; if (sum >= variancePercentageThreshold) { break; } } if (maxDimensions == null || maxDimensions > varCounter) { maxDimensions = varCounter; } } if (maxDimensions != null && maxDimensions < d) { //keep only the maximum selected eigenvalues eigenValues = eigenValues.getSubVector(0, maxDimensions); //keep only the maximum selected eigenvectors components = components.getSubMatrix(0, components.getRowDimension() - 1, 0, maxDimensions - 1); } modelParameters.setEigenValues(eigenValues); modelParameters.setComponents(components); }
From source file:edu.stanford.cfuller.colocalization3d.Colocalization3DMain.java
private RealVector getScalarDifferencesFromVectorDifferences(java.util.List<RealVector> vecDiffs) { RealVector scalarDiffs = new org.apache.commons.math3.linear.ArrayRealVector(vecDiffs.size(), 0.0); for (int i = 0; i < vecDiffs.size(); i++) { scalarDiffs.setEntry(i, vecDiffs.get(i).getNorm()); }/*from ww w.j av a 2 s . co m*/ return scalarDiffs; }
From source file:com.cloudera.oryx.kmeans.computation.local.Standardize.java
@Override public List<List<RealVector>> call() throws IOException { File[] inputFiles = inputDir.listFiles(IOUtils.NOT_HIDDEN); if (inputFiles == null || inputFiles.length == 0) { log.info("No input files in {}", inputDir); return null; }//from w w w . j a v a 2 s.c om Config config = ConfigUtils.getDefaultConfig(); InboundSettings inboundSettings = InboundSettings.create(config); int numFeatures = inboundSettings.getColumnNames().size(); NormalizeSettings settings = NormalizeSettings.create(config); Crossfold crossfold = new Crossfold(config.getInt("model.cross-folds")); RandomGenerator rand = crossfold.getRandomGenerator(); Collection<Integer> ignoredColumns = inboundSettings.getIgnoredColumns(); Collection<Integer> idColumns = inboundSettings.getIdColumns(); int expansion = -ignoredColumns.size() + summary.getNetLevels() - (!idColumns.isEmpty() && !ignoredColumns.contains(idColumns.iterator().next()) ? 1 : 0); boolean sparse = settings.getSparse() != null ? settings.getSparse() : expansion > 2 * (summary.getFieldCount() - ignoredColumns.size()); List<List<RealVector>> ret = new ArrayList<>(); for (int i = 0; i < crossfold.getNumFolds(); i++) { ret.add(new LinkedList<RealVector>()); } for (File inputFile : inputFiles) { log.info("Standardizing input from {}", inputFile.getName()); for (String line : new FileLineIterable(inputFile)) { if (line.isEmpty()) { continue; } String[] tokens = DelimitedDataUtils.decode(line); RealVector v = sparse ? Vectors.sparse(tokens.length + expansion) : Vectors.dense(tokens.length + expansion); int offset = 0; for (int i = 0; i < numFeatures; i++) { if (!inboundSettings.isIgnored(i)) { if (inboundSettings.isNumeric(i)) { SummaryStats ss = summary.getStats(i); Transform t = settings.getTransform(i); double raw = asDouble(tokens[i]); if (!Double.isNaN(raw)) { double n = t.apply(raw, ss) * settings.getScale(i); v.setEntry(offset, n); } offset++; } else if (inboundSettings.isCategorical(i)) { SummaryStats ss = summary.getStats(i); int index = ss.index(tokens[i]); if (index >= 0) { v.setEntry(offset + index, settings.getScale(i)); offset += ss.numLevels(); } else { log.warn("Unrecognized value for category {}: {}", i, tokens[i]); } } } } if (!inboundSettings.getIdColumns().isEmpty()) { // TODO: multiple ID columns v = new NamedRealVector(v, tokens[inboundSettings.getIdColumns().iterator().next()]); } // Assign the vector to a fold int fold = rand.nextInt(crossfold.getNumFolds()); ret.get(fold).add(v); } } return ret; }
From source file:edu.stanford.cfuller.colocalization3d.correction.PositionCorrector.java
/** * Applies an existing correction to the positions of a set of objects, using a specified reference * and correction channel.// w w w. j ava 2 s .c o m * * @param imageObjects A Vector containing all the ImageObjects to be corrected. * @param c The Correction object to be used, which could have been generated with determineCorrection, or loaded from disk. * @return A RealVector with one entry per ImageObject, containing the corrected scalar distance between the object in the reference channel and the channel being corrected. */ public RealVector applyCorrection(Correction c, java.util.List<ImageObject> imageObjects) { int referenceChannel = this.parameters.getIntValueForKey(REF_CH_PARAM); int channelToCorrect = this.parameters.getIntValueForKey(CORR_CH_PARAM); RealVector diffs = new ArrayRealVector(imageObjects.size(), 0.0); RealVector averageVectorDiffs = null; for (int i = 0; i < imageObjects.size(); i++) { diffs.setEntry(i, imageObjects.get(i).getScalarDifferenceBetweenChannels(referenceChannel, channelToCorrect, this.pixelToDistanceConversions)); if (i == 0) { averageVectorDiffs = imageObjects.get(i) .getVectorDifferenceBetweenChannels(referenceChannel, channelToCorrect).copy(); } else { averageVectorDiffs = averageVectorDiffs.add(imageObjects.get(i) .getVectorDifferenceBetweenChannels(referenceChannel, channelToCorrect).map(new Abs())); } } averageVectorDiffs.mapDivideToSelf(imageObjects.size()); averageVectorDiffs = averageVectorDiffs.ebeMultiply(this.pixelToDistanceConversions); java.util.logging.Logger.getLogger("edu.stanford.cfuller.colocalization3d") .info("mean separation uncorrected = " + diffs.getL1Norm() / diffs.getDimension()); java.util.logging.Logger.getLogger("edu.stanford.cfuller.colocalization3d") .info("mean separation components uncorrected = " + averageVectorDiffs.toString()); RealVector newDiffs = new ArrayRealVector(imageObjects.size(), 0.0); if (this.parameters.getBooleanValueForKey("correct_images")) { for (int i = 0; i < imageObjects.size(); i++) { try { newDiffs.setEntry(i, this.correctSingleObjectVectorDifference(c, imageObjects.get(i), referenceChannel, channelToCorrect).getNorm()); imageObjects.get(i).setCorrectionSuccessful(true); } catch (UnableToCorrectException e) { newDiffs.setEntry(i, -1.0 * Double.MAX_VALUE); imageObjects.get(i).setCorrectionSuccessful(false); } } java.util.logging.Logger.getLogger("edu.stanford.cfuller.colocalization3d") .info("mean separation corrected = " + newDiffs.getL1Norm() / newDiffs.getDimension()); } else { newDiffs = diffs; } return newDiffs; }
From source file:com.cloudera.oryx.kmeans.computation.local.Standarize.java
@Override public List<List<RealVector>> call() throws IOException { File[] inputFiles = inputDir.listFiles(IOUtils.CSV_COMPRESSED_FILTER); if (inputFiles == null || inputFiles.length == 0) { log.info("No .csv input files in {}", inputDir); return null; }//from w w w . j av a2s .c o m Config config = ConfigUtils.getDefaultConfig(); InboundSettings inboundSettings = InboundSettings.create(config); int numFeatures = inboundSettings.getColumnNames().size(); NormalizeSettings settings = NormalizeSettings.create(config); Crossfold crossfold = new Crossfold(config.getInt("model.cross-folds")); RandomGenerator rand = crossfold.getRandomGenerator(); Collection<Integer> ignoredColumns = inboundSettings.getIgnoredColumns(); Collection<Integer> idColumns = inboundSettings.getIdColumns(); int expansion = -ignoredColumns.size() + summary.getNetLevels() - (!idColumns.isEmpty() && !ignoredColumns.contains(idColumns.iterator().next()) ? 1 : 0); boolean sparse = settings.getSparse() != null ? settings.getSparse() : expansion > 2 * (summary.getFieldCount() - ignoredColumns.size()); List<List<RealVector>> ret = Lists.newArrayList(); for (int i = 0; i < crossfold.getNumFolds(); i++) { ret.add(Lists.<RealVector>newLinkedList()); } for (File inputFile : inputFiles) { log.info("Standardizing input from {}", inputFile.getName()); for (String line : new FileLineIterable(inputFile)) { if (line.isEmpty()) { continue; } String[] tokens = DelimitedDataUtils.decode(line); RealVector v = sparse ? Vectors.sparse(tokens.length + expansion) : Vectors.dense(tokens.length + expansion); int offset = 0; for (int i = 0; i < numFeatures; i++) { if (inboundSettings.isIgnored(i)) { // Do nothing } else if (inboundSettings.isNumeric(i)) { SummaryStats ss = summary.getStats(i); Transform t = settings.getTransform(i); double raw = asDouble(tokens[i]); if (!Double.isNaN(raw)) { double n = t.apply(raw, ss) * settings.getScale(i); v.setEntry(offset, n); } offset++; } else if (inboundSettings.isCategorical(i)) { SummaryStats ss = summary.getStats(i); int index = ss.index(tokens[i]); if (index >= 0) { v.setEntry(offset + index, settings.getScale(i)); offset += ss.numLevels(); } else { log.warn("Unrecognized value for category {}: {}", i, tokens[i]); } } } if (!inboundSettings.getIdColumns().isEmpty()) { // TODO: multiple ID columns v = new NamedRealVector(v, tokens[inboundSettings.getIdColumns().iterator().next()]); } // Assign the vector to a fold int fold = rand.nextInt(crossfold.getNumFolds()); ret.get(fold).add(v); } } return ret; }
From source file:com.cloudera.oryx.kmeans.computation.normalize.StandardizeFn.java
@Override public void process(Record record, Emitter<RealVector> emitter) { int len = record.getSpec().size() + expansion; RealVector v; if (record instanceof VectorRecord) { RealVector innerRecord = ((VectorRecord) record).getVector(); if (innerRecord instanceof ArrayRealVector) { v = Vectors.dense(len);// w w w.j a v a2 s. co m } else { v = Vectors.sparse(len); } } else if (sparse) { v = Vectors.sparse(len); } else { v = Vectors.dense(len); } increment("NORMALIZE", "INPUT_RECORDS"); int offset = 0; for (int i = 0; i < record.getSpec().size(); i++) { if (!idColumns.contains(i) && !ignoredColumns.contains(i)) { SummaryStats ss = summary.getStats(i); if (ss == null || ss.isEmpty()) { v.setEntry(offset, record.getAsDouble(i)); offset++; } else if (ss.isNumeric()) { Transform t = settings.getTransform(i); double raw = record.getAsDouble(i); if (Double.isNaN(raw)) { increment("NORMALIZE", "NaN record value"); return; } double n = t.apply(raw, ss) * settings.getScale(i); v.setEntry(offset, n); offset++; } else { int index = ss.index(record.getAsString(i)); if (index < 0) { increment("NORMALIZE", "Negative record index"); return; } v.setEntry(offset + index, settings.getScale(i)); offset += ss.numLevels(); } } } increment("NORMALIZE", "PRE_NAMING"); if (!idColumns.isEmpty()) { // TODO: arbitrary properties v = new NamedRealVector(v, record.getAsString(idColumns.iterator().next())); } emitter.emit(v); increment("NORMALIZE", "OUTPUT_VECTORS"); }
From source file:edu.stanford.cfuller.colocalization3d.correction.PositionCorrector.java
private RealVector correctSingleObjectVectorDifference(Correction c, ImageObject obj, int referenceChannel, int correctionChannel) throws UnableToCorrectException { RealVector corr = c.correctPosition(obj.getPositionForChannel(referenceChannel).getEntry(0), obj.getPositionForChannel(referenceChannel).getEntry(1)); boolean flip = this.parameters.getBooleanValueForKey("flip_channels_at_end"); if (flip)//from w ww.j a v a 2 s .c o m corr.mapMultiplyToSelf(-1.0); boolean invert_z = this.parameters.hasKeyAndTrue(INVERT_Z_PARAM); if (invert_z) corr.setEntry(2, -1.0 * corr.getEntry(2)); obj.applyCorrectionVectorToChannel(correctionChannel, corr); RealVector correctedVectorDiff = obj .getCorrectedVectorDifferenceBetweenChannels(referenceChannel, correctionChannel) .ebeMultiply(this.pixelToDistanceConversions); return correctedVectorDiff; }