首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向Apache Math3拟合中添加参数约束

向Apache Math3拟合中添加参数约束
EN

Stack Overflow用户
提问于 2015-07-10 17:22:34
回答 1查看 406关注 0票数 2

我正在使用Apache开发一个合适的应用程序。我已经成功地创建了ParametricUnivariateFunction

代码语言:javascript
复制
public class MyFunc implements ParametricUnivariateFunction {
@Override
public double value(double x, double... Parameters) {
    double m = parameters[0], k = parameters[1], b = parameters[2];
    return m * k * b * Math.exp(-k * x) * Math.pow(1 - Math.exp(-k * x), b - 1);
}
@Override
public double[] gradient(double x, double... Parameters) {
    final double m = parameters[0];
    final double k = parameters[1];
    final double b = parameters[2];
    return new double[]{
        b * k * Math.exp(-k * x) * Math.pow(1 - Math.exp(-k * x), b - 1),
        (b - 1) * b * k * m * x * Math.exp(-2 * k * x) * Math.pow(1 - Math.exp(-k * x), b - 2) + b * m * Math.exp(-k * x) * Math.pow(1 - Math.exp(-k * x), b - 1) - b * k * m * x * Math.exp(-k * x) * Math.pow(1 - Math.exp(-k * x), b - 1),
        k * m * Math.exp(-k * x) * Math.pow(1 - Math.exp(-k * x), b - 1) + b * k * m * Math.exp(-k * x) * Math.pow(1 - Math.exp(-k * x), b - 1) * Math.log(1 - Math.exp(-k * x))
    };
}

}

和AbstractCurveFitter

代码语言:javascript
复制
public class MyFuncFitter extends AbstractCurveFitter {

@Override
protected LeastSquaresProblem getProblem(Collection<WeightedObservedPoint> points) {
    final int len = points.size();
    final double[] target = new double[len];
    final double[] weights = new double[len];
    final double[] initialGuess = {50, 1.0, 1.0};

    int i = 0;
    for (WeightedObservedPoint point : points) {
        target[i] = point.getY();
        weights[i] = point.getWeight();
        i += 1;
    }

    final AbstractCurveFitter.TheoreticalValuesFunction model = new AbstractCurveFitter.TheoreticalValuesFunction(new MyFunc(), points);

    return new LeastSquaresBuilder().
            maxEvaluations(Integer.MAX_VALUE).
            maxIterations(Integer.MAX_VALUE).
            start(initialGuess).
            target(target).
            weight(new DiagonalMatrix(weights)).
            model(model.getModelFunction(), model.getModelFunctionJacobian()).build();
}

}

我主要是用它们

代码语言:javascript
复制
public static void main(String[] args) {

    MyFuncFitter fitter = new MyFuncFitter();
    ArrayList<WeightedObservedPoint> points = new ArrayList<>();

    points.add(new WeightedObservedPoint(1.0, 0.25, 3.801713179));
    ///...
    points.add(new WeightedObservedPoint(1.0, 4, 10.46561902));

    final double coeffs[] = fitter.fit(points);
    System.out.println(Arrays.toString(coeffs));
}

这个很好用!

现在,我必须向参数添加约束(特别是m<=100,k>=0 e b>=1)。

如何将这些约束添加到上面的系统中?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-25 14:22:23

我找到了一个解决方案:使用Java优化模型

代码语言:javascript
复制
OptimizationProblem op = new OptimizationProblem();
...
op.addDecisionVariable("m", false, new int[]{1, 1});
...
op.addConstraint("m<=100");//<- the constraints
...
op.setInitialSolution("m", 50);//optional
...
op.setObjectiveFunction("minimize", str);//where str is the string representing the function to minimize
...
System.loadLibrary("Ipopt38");
op.solve("ipopt");
...
if (!op.solutionIsOptimal()) {
        return null;
}

features[0] = op.getPrimalSolution("m").toValue();
...
features[3] = op.getOptimalCost();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31346767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档