List of usage examples for org.apache.commons.math3.linear RealVector set
public void set(double value)
From source file:eu.crisis_economics.abm.markets.clearing.heterogeneous.BoundedQuadraticEstimationClearingAlgorithm.java
@Override public double applyToNetwork(final MixedClearingNetwork network) { Preconditions.checkNotNull(network); final int dimension = network.getNumberOfEdges(); final ResidualCostFunction aggregateCostFunction = super.getResidualScalarCostFunction(network); final RealVector start = new ArrayRealVector(network.getNumberOfEdges()); start.set(1.0); // Initial rate guess. final BOBYQAOptimizer optimizer = new BOBYQAOptimizer(2 * dimension + 1, 1.2, 1.e-8); final PointValuePair result = optimizer.optimize(new MaxEval(maximumEvaluations), new ObjectiveFunction(aggregateCostFunction), GoalType.MINIMIZE, new SimpleBounds(new double[dimension], ArrayUtil.ones(dimension)), new InitialGuess(start.toArray())); final double residualCost = result.getValue(); System.out.println("Network cleared: residual cost: " + residualCost + "."); return residualCost; }
From source file:eu.crisis_economics.abm.markets.clearing.heterogeneous.NelderMeadClearingAlgorithm.java
@Override public double applyToNetwork(final MixedClearingNetwork network) { Preconditions.checkNotNull(network); final SimplexOptimizer optimizer = new SimplexOptimizer(relErrorTarget, absErrorTarget); final ResidualCostFunction aggregateCostFunction = super.getResidualScalarCostFunction(network); final RealVector start = new ArrayRealVector(network.getNumberOfEdges()); for (int i = 0; i < network.getNumberOfEdges(); ++i) start.setEntry(i, network.getEdges().get(i).getMaximumRateAdmissibleByBothParties()); start.set(1.); final PointValuePair result = optimizer.optimize(new MaxEval(maximumEvaluations), new ObjectiveFunction(aggregateCostFunction), GoalType.MINIMIZE, new InitialGuess(start.toArray()), new NelderMeadSimplex(network.getNumberOfEdges())); final double residualCost = result.getValue(); System.out.println("Network cleared: residual cost: " + residualCost + "."); return residualCost; }
From source file:org.grouplens.samantha.modeler.space.VariableSpace.java
default void initializeVector(RealVector vec, double initial, boolean randomize, boolean normalize) { if (randomize) { RandomInitializer randInit = new RandomInitializer(); randInit.randInitVector(vec, normalize); } else {/* w w w. j a va 2 s. c om*/ if (initial != 0.0) { vec.set(initial); } } }
From source file:org.grouplens.samantha.modeler.svdfeature.SVDFeature.java
private double predict(SVDFeatureInstance ins, StochasticOracle outOrc, RealVector outUfactSum, RealVector outIfactSum) {/*from www.j a v a 2 s . c o m*/ double pred = 0.0; for (int i = 0; i < ins.gfeas.size(); i++) { int ind = ins.gfeas.get(i).getIndex(); double val = ins.gfeas.get(i).getValue(); if (outOrc != null) { outOrc.addScalarOracle(SVDFeatureKey.BIASES.get(), ind, val); } pred += getScalarVarByNameIndex(SVDFeatureKey.BIASES.get(), ind) * val; } outUfactSum.set(0.0); for (int i = 0; i < ins.ufeas.size(); i++) { int index = ins.ufeas.get(i).getIndex(); outUfactSum.combineToSelf(1.0, ins.ufeas.get(i).getValue(), getVectorVarByNameIndex(SVDFeatureKey.FACTORS.get(), index)); } outIfactSum.set(0.0); for (int i = 0; i < ins.ifeas.size(); i++) { int index = ins.ifeas.get(i).getIndex(); outIfactSum.combineToSelf(1.0, ins.ifeas.get(i).getValue(), getVectorVarByNameIndex(SVDFeatureKey.FACTORS.get(), index)); } pred += outUfactSum.dotProduct(outIfactSum); return pred; }
From source file:org.lenskit.mf.funksvd.FunkSVDModelBuilder.java
@Override public FunkSVDModel get() { int userCount = snapshot.getUserIds().size(); RealMatrix userFeatures = MatrixUtils.createRealMatrix(userCount, featureCount); int itemCount = snapshot.getItemIds().size(); RealMatrix itemFeatures = MatrixUtils.createRealMatrix(itemCount, featureCount); logger.debug("Learning rate is {}", rule.getLearningRate()); logger.debug("Regularization term is {}", rule.getTrainingRegularization()); logger.info("Building SVD with {} features for {} ratings", featureCount, snapshot.getRatings().size()); TrainingEstimator estimates = rule.makeEstimator(snapshot); List<FeatureInfo> featureInfo = new ArrayList<>(featureCount); // Use scratch vectors for each feature for better cache locality // Per-feature vectors are strided in the output matrices RealVector uvec = MatrixUtils.createRealVector(new double[userCount]); RealVector ivec = MatrixUtils.createRealVector(new double[itemCount]); for (int f = 0; f < featureCount; f++) { logger.debug("Training feature {}", f); StopWatch timer = new StopWatch(); timer.start();//from w w w . j av a 2s . co m uvec.set(initialValue); ivec.set(initialValue); FeatureInfo.Builder fib = new FeatureInfo.Builder(f); trainFeature(f, estimates, uvec, ivec, fib); summarizeFeature(uvec, ivec, fib); featureInfo.add(fib.build()); // Update each rating's cached value to accommodate the feature values. estimates.update(uvec, ivec); // And store the data into the matrix userFeatures.setColumnVector(f, uvec); assert Math.abs(userFeatures.getColumnVector(f).getL1Norm() - uvec.getL1Norm()) < 1.0e-4 : "user column sum matches"; itemFeatures.setColumnVector(f, ivec); assert Math.abs(itemFeatures.getColumnVector(f).getL1Norm() - ivec.getL1Norm()) < 1.0e-4 : "item column sum matches"; timer.stop(); logger.info("Finished feature {} in {}", f, timer); } // Wrap the user/item matrices because we won't use or modify them again return new FunkSVDModel(userFeatures, itemFeatures, snapshot.userIndex(), snapshot.itemIndex(), featureInfo); }
From source file:org.lenskit.mf.funksvd.FunkSVDModelProvider.java
@Override public FunkSVDModel get() { int userCount = snapshot.getUserIds().size(); RealMatrix userFeatures = MatrixUtils.createRealMatrix(userCount, featureCount); int itemCount = snapshot.getItemIds().size(); RealMatrix itemFeatures = MatrixUtils.createRealMatrix(itemCount, featureCount); logger.debug("Learning rate is {}", rule.getLearningRate()); logger.debug("Regularization term is {}", rule.getTrainingRegularization()); logger.info("Building SVD with {} features for {} ratings", featureCount, snapshot.getRatings().size()); TrainingEstimator estimates = rule.makeEstimator(snapshot); List<FeatureInfo> featureInfo = new ArrayList<>(featureCount); // Use scratch vectors for each feature for better cache locality // Per-feature vectors are strided in the output matrices RealVector uvec = MatrixUtils.createRealVector(new double[userCount]); RealVector ivec = MatrixUtils.createRealVector(new double[itemCount]); for (int f = 0; f < featureCount; f++) { logger.debug("Training feature {}", f); StopWatch timer = new StopWatch(); timer.start();/*from w ww. j a va2 s . c o m*/ uvec.set(initialValue); ivec.set(initialValue); FeatureInfo.Builder fib = new FeatureInfo.Builder(f); double rmse = trainFeature(f, estimates, uvec, ivec, fib); summarizeFeature(uvec, ivec, fib); featureInfo.add(fib.build()); // Update each rating's cached value to accommodate the feature values. estimates.update(uvec, ivec); // And store the data into the matrix userFeatures.setColumnVector(f, uvec); assert Math.abs(userFeatures.getColumnVector(f).getL1Norm() - uvec.getL1Norm()) < 1.0e-4 : "user column sum matches"; itemFeatures.setColumnVector(f, ivec); assert Math.abs(itemFeatures.getColumnVector(f).getL1Norm() - ivec.getL1Norm()) < 1.0e-4 : "item column sum matches"; timer.stop(); logger.info("Finished feature {} in {} (RMSE={})", f, timer, rmse); } // Wrap the user/item matrices because we won't use or modify them again return new FunkSVDModel(userFeatures, itemFeatures, snapshot.userIndex(), snapshot.itemIndex(), featureInfo); }