本文整理汇总了Java中org.apache.commons.math3.analysis.MultivariateFunction类的典型用法代码示例。如果您正苦于以下问题:Java MultivariateFunction类的具体用法?Java MultivariateFunction怎么用?Java MultivariateFunction使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MultivariateFunction类属于org.apache.commons.math3.analysis包,在下文中一共展示了MultivariateFunction类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: evaluateNewSimplex
import org.apache.commons.math3.analysis.MultivariateFunction; //导入依赖的package包/类
/**
* Compute and evaluate a new simplex.
*
* @param evaluationFunction Evaluation function.
* @param original Original simplex (to be preserved).
* @param coeff Linear coefficient.
* @param comparator Comparator to use to sort simplex vertices from best
* to poorest.
* @return the best point in the transformed simplex.
* @throws org.apache.commons.math3.exception.TooManyEvaluationsException
* if the maximal number of evaluations is exceeded.
*/
private PointValuePair evaluateNewSimplex(final MultivariateFunction evaluationFunction,
final PointValuePair[] original,
final double coeff,
final Comparator<PointValuePair> comparator) {
final double[] xSmallest = original[0].getPointRef();
// Perform a linear transformation on all the simplex points,
// except the first one.
setPoint(0, original[0]);
final int dim = getDimension();
for (int i = 1; i < getSize(); i++) {
final double[] xOriginal = original[i].getPointRef();
final double[] xTransformed = new double[dim];
for (int j = 0; j < dim; j++) {
xTransformed[j] = xSmallest[j] + coeff * (xSmallest[j] - xOriginal[j]);
}
setPoint(i, new PointValuePair(xTransformed, Double.NaN, false));
}
// Evaluate the simplex.
evaluate(evaluationFunction, comparator);
return getPoint(0);
}
示例2: getCostFunction
import org.apache.commons.math3.analysis.MultivariateFunction; //导入依赖的package包/类
/**
* Defines that the Mean Squared Error has to be minimized
* in order to get optimized / good parameters for alpha, betta and gamma.
* It also defines the minimum and maximum values for the parameters to optimize.
* @param dataPoints the data points
* @param seasonLength the amount of data points per season
* @return a cost function {@link MultivariateFunctionMappingAdapter} which
* defines that the Mean Squared Error has to be minimized
* in order to get optimized / good parameters for alpha, betta and gamma
*/
private static MultivariateFunctionMappingAdapter getCostFunction(final double[] dataPoints, final int seasonLength) {
MultivariateFunction multivariateFunction = new MultivariateFunction() {
@Override
public double value(double[] point) {
double alpha = point[0];
double beta = point[1];
double gamma = point[2];
if (beta >= alpha) {
return Double.POSITIVE_INFINITY;
}
List<Double> predictedValues = TripleExponentialSmoothing.getSmoothedDataPointsWithPredictions(dataPoints, seasonLength, alpha, beta, gamma, 1);
predictedValues.remove(predictedValues.size()-1);
double meanSquaredError = getMeanSquaredError(dataPoints, predictedValues);
return meanSquaredError;
}
};
double[][] minMax = getMinMaxValues();
return new MultivariateFunctionMappingAdapter(multivariateFunction, minMax[0], minMax[1]);
}
示例3: getLikelihoodFunction
import org.apache.commons.math3.analysis.MultivariateFunction; //导入依赖的package包/类
public MultivariateFunction getLikelihoodFunction(final double[] xgrid, final double[] ygrid, final double[] imageValues) {
final MultivariateVectorFunction valueFunction = this.getValueFunction(xgrid, ygrid);
return new MultivariateFunction() {
@Override
public double value(double[] point) {
double[] expectedValues = valueFunction.value(point);
double logLikelihood = 0;
for(int i = 0; i < expectedValues.length; i++) {
double expectedValue = expectedValues[i];
double log = log(expectedValue);
if(log < -1e6) {
log = -1e6;
}
logLikelihood += imageValues[i] * log - expectedValue;
}
// IJ.log("likelihood:" + logLikelihood);
// IJ.log("point: " + Arrays.toString(point));
return logLikelihood;
}
};
}
示例4: getLikelihoodFunction
import org.apache.commons.math3.analysis.MultivariateFunction; //导入依赖的package包/类
public MultivariateFunction getLikelihoodFunction(double[] xgrid1, double[] ygrid1, final double[] values1, double[] xgrid2, double[] ygrid2, final double[] values2) {
final MultivariateVectorFunction valueFunction = getValueFunction(xgrid1, ygrid1, xgrid2, ygrid2);
return new MultivariateFunction() {
@Override
public double value(double[] point) {
double[] expectedValues = valueFunction.value(point);
double logLikelihood = 0;
int index = 0;
for(int i = 0; i < values1.length; i++, index++) {
logLikelihood += values1[i] * Math.max(-1e6, log(expectedValues[index])) - expectedValues[index];
}
for(int i = 0; i < values2.length; i++, index++) {
logLikelihood += values2[i] * Math.max(-1e6, log(expectedValues[index])) - expectedValues[index];
}
return logLikelihood;
}
};
}
示例5: testRosenbrock
import org.apache.commons.math3.analysis.MultivariateFunction; //导入依赖的package包/类
@Test
public void testRosenbrock() {
MultivariateFunction rosenbrock =
new MultivariateFunction() {
public double value(double[] x) {
++count;
double a = x[1] - x[0] * x[0];
double b = 1.0 - x[0];
return 100 * a * a + b * b;
}
};
count = 0;
SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3);
optimizer.setSimplex(new MultiDirectionalSimplex(new double[][] {
{ -1.2, 1.0 }, { 0.9, 1.2 } , { 3.5, -2.3 }
}));
PointValuePair optimum =
optimizer.optimize(100, rosenbrock, GoalType.MINIMIZE, new double[] { -1.2, 1 });
Assert.assertEquals(count, optimizer.getEvaluations());
Assert.assertTrue(optimizer.getEvaluations() > 50);
Assert.assertTrue(optimizer.getEvaluations() < 100);
Assert.assertTrue(optimum.getValue() > 1e-2);
}
示例6: testPowell
import org.apache.commons.math3.analysis.MultivariateFunction; //导入依赖的package包/类
@Test
public void testPowell() {
MultivariateFunction powell =
new MultivariateFunction() {
public double value(double[] x) {
++count;
double a = x[0] + 10 * x[1];
double b = x[2] - x[3];
double c = x[1] - 2 * x[2];
double d = x[0] - x[3];
return a * a + 5 * b * b + c * c * c * c + 10 * d * d * d * d;
}
};
count = 0;
SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3);
optimizer.setSimplex(new MultiDirectionalSimplex(4));
PointValuePair optimum =
optimizer.optimize(1000, powell, GoalType.MINIMIZE, new double[] { 3, -1, 0, 1 });
Assert.assertEquals(count, optimizer.getEvaluations());
Assert.assertTrue(optimizer.getEvaluations() > 800);
Assert.assertTrue(optimizer.getEvaluations() < 900);
Assert.assertTrue(optimum.getValue() > 1e-2);
}
示例7: testMath864
import org.apache.commons.math3.analysis.MultivariateFunction; //导入依赖的package包/类
@Test
public void testMath864() {
final CMAESOptimizer optimizer = new CMAESOptimizer();
final MultivariateFunction fitnessFunction = new MultivariateFunction() {
public double value(double[] parameters) {
final double target = 1;
final double error = target - parameters[0];
return error * error;
}
};
final double[] start = { 0 };
final double[] lower = { -1e6 };
final double[] upper = { 1.5 };
final double[] result = optimizer.optimize(10000, fitnessFunction, GoalType.MINIMIZE,
start, lower, upper).getPoint();
Assert.assertTrue("Out of bounds (" + result[0] + " > " + upper[0] + ")",
result[0] <= upper[0]);
}
示例8: doTest
import org.apache.commons.math3.analysis.MultivariateFunction; //导入依赖的package包/类
/**
* @param func Function to optimize.
* @param optimum Expected optimum.
* @param init Starting point.
* @param goal Minimization or maximization.
* @param fTol Tolerance (relative error on the objective function) for
* "Powell" algorithm.
* @param pointTol Tolerance for checking that the optimum is correct.
*/
private void doTest(MultivariateFunction func,
double[] optimum,
double[] init,
GoalType goal,
double fTol,
double pointTol) {
final MultivariateOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d));
final PointValuePair result = optim.optimize(1000, func, goal, init);
final double[] point = result.getPoint();
for (int i = 0, dim = optimum.length; i < dim; i++) {
Assert.assertEquals("found[" + i + "]=" + point[i] + " value=" + result.getValue(),
optimum[i], point[i], pointTol);
}
}
示例9: doTest
import org.apache.commons.math3.analysis.MultivariateFunction; //导入依赖的package包/类
/**
* @param func Function to optimize.
* @param startPoint Starting point.
* @param boundaries Upper / lower point limit.
* @param goal Minimization or maximization.
* @param fTol Tolerance relative error on the objective function.
* @param pointTol Tolerance for checking that the optimum is correct.
* @param maxEvaluations Maximum number of evaluations.
* @param expected Expected point / value.
*/
private void doTest(MultivariateFunction func,
double[] startPoint,
double[][] boundaries,
GoalType goal,
double fTol,
double pointTol,
int maxEvaluations,
PointValuePair expected) {
doTest(func,
startPoint,
boundaries,
goal,
fTol,
pointTol,
maxEvaluations,
0,
expected,
"");
}
示例10: doTest
import org.apache.commons.math3.analysis.MultivariateFunction; //导入依赖的package包/类
/**
* @param func Function to optimize.
* @param optimum Expected optimum.
* @param init Starting point.
* @param goal Minimization or maximization.
* @param fTol Tolerance (relative error on the objective function) for
* "Powell" algorithm.
* @param pointTol Tolerance for checking that the optimum is correct.
*/
private void doTest(MultivariateFunction func,
double[] optimum,
double[] init,
GoalType goal,
double fTol,
double pointTol) {
final PowellOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d));
final PointValuePair result = optim.optimize(new MaxEval(1000),
new ObjectiveFunction(func),
goal,
new InitialGuess(init));
final double[] point = result.getPoint();
for (int i = 0, dim = optimum.length; i < dim; i++) {
Assert.assertEquals("found[" + i + "]=" + point[i] + " value=" + result.getValue(),
optimum[i], point[i], pointTol);
}
}
示例11: generateLDPLObjectiveFunction
import org.apache.commons.math3.analysis.MultivariateFunction; //导入依赖的package包/类
MultivariateFunction generateLDPLObjectiveFunction(final GaussianProcessLDPLMeanModel model, final double[] initPoint){
MultivariateFunction negaLLfunc = new MultivariateFunction(){
@Override
public double value(double[] point){
double[] params = gpLDPL.getParams();
for(int i=0; i<initPoint.length; i++){
params[i] = point[i];
}
gpLDPL.setParams(params);
model.train(samples);
gpLDPL.updateByActiveBeaconList(activeBeaconList);
double logLikelihood = gpLDPL.looPredLogLikelihood();
double looMSE = gpLDPL.looMSE();
StringBuilder sb = new StringBuilder();
sb.append("optimizeForLDPL: n="+params[0]+",A="+params[1]+",fa="+params[2]+",fb="+params[3]);
sb.append(", LOOMSE="+looMSE);
sb.append(", logLikelihood="+(-logLikelihood));
System.out.println(sb.toString());
return -logLikelihood;
}
};
return negaLLfunc;
}
示例12: generateGPIsoScaleLOOMSE
import org.apache.commons.math3.analysis.MultivariateFunction; //导入依赖的package包/类
MultivariateFunction generateGPIsoScaleLOOMSE(final GaussianProcessLDPLMeanModel model){
MultivariateFunction negaLLfunc = new MultivariateFunction(){
@Override
public double value(double[] point){
double lengthx = Math.pow(10, point[0]);
double lambda = Math.pow(10, point[1]);
double stdev = model.stdev;
double lz = model.lengthes[2];
model.lengthes = new double[]{lengthx, lengthx, lz};
model.sigmaN = Math.sqrt(lambda)*stdev;
model.train(samples);
gpLDPL.updateByActiveBeaconList(activeBeaconList);
double looMSE = gpLDPL.looMSE();
StringBuilder sb = new StringBuilder();
sb.append("optimizeForGPLOOError: lx=ly="+lengthx+",lz="+lz+",lambda="+lambda+",looMSE="+looMSE);
System.out.println(sb.toString());
return looMSE;
}
};
return negaLLfunc;
}
示例13: LineSearchObjectiveFunction
import org.apache.commons.math3.analysis.MultivariateFunction; //导入依赖的package包/类
LineSearchObjectiveFunction(
final MultivariateFunction function,
final double[] startingPoint,
final double[] vectorDirection
) {
StateVerifier.checkNotNull(function, startingPoint, vectorDirection);
Preconditions.checkArgument(startingPoint.length > 0);
Preconditions.checkArgument(startingPoint.length == vectorDirection.length);
this.function = function;
this.normalizedDirection = new double[vectorDirection.length];
{ // Normalize the input vector.
double norm = 0.;
for(int i = 0; i< vectorDirection.length; ++i)
norm += vectorDirection[i] * vectorDirection[i];
norm = Math.sqrt(norm);
for(int i = 0; i< vectorDirection.length; ++i)
normalizedDirection[i] = vectorDirection[i] / norm;
}
this.startingPoint = Arrays.copyOf(startingPoint, startingPoint.length);
this.workspace = new double[startingPoint.length];
}
示例14: doLineSearch
import org.apache.commons.math3.analysis.MultivariateFunction; //导入依赖的package包/类
static public LineSearchResult doLineSearch(
final MultivariateFunction function,
final double[] startingPoint,
final double[] vectorDirection,
final double[] domainMaxima,
final double[] domainMinima
) {
final double
maximumDistanceToTravel = computeMaximumSearchDistanceFromDomainBounds(
startingPoint, vectorDirection, domainMaxima, domainMinima);
if(maximumDistanceToTravel == 0.)
return new LineSearchResult(
Arrays.copyOf(startingPoint, startingPoint.length), function.value(startingPoint));
return doLineSearch(
function, startingPoint, vectorDirection, maximumDistanceToTravel,
DEFAULT_MAXIMUM_EVALUATIONS,
DEFAULT_ERROR_TARGET_RELATIVE,
DEFAULT_ERROR_TARGET_ABSOLUTE
);
}
示例15: testOneDimensionalCosMinimization
import org.apache.commons.math3.analysis.MultivariateFunction; //导入依赖的package包/类
@Test
/**
* Test the Brent line search algorithm by minimizing a one-dimensional
* cosine function. The starting point of the minimization is the origin,
* and the maximum distance to travel in the positive x direction is 2*Pi.
* The expected solution is therefor the local minimum at Pi.
*/
public void testOneDimensionalCosMinimization() {
final MultivariateFunction meritFunction = new MultivariateFunction() {
@Override
public double value(final double[] x) {
return Math.cos(x[0]);
}
};
final double[] lineDirection = new double[] { 1.0 };
double distanceToTravel = Math.PI * 2.;
final LineSearchResult solution = BrentLineSearch.doLineSearch(
meritFunction, new double[] { 0.0 }, lineDirection, distanceToTravel);
Assert.assertTrue(Math.abs(solution.getEvaluationAtSolution() + 1.) < 1.e-10);
Assert.assertEquals(solution.getSolutionPoint()[0], Math.PI, 1.e-10);
}