Python scipy.optimize.leastsq to Java org.apache.commons.math3.fitting.leastsquares

时间:2017-12-14 18:12:44

标签: java math scipy least-squares

我尝试模仿用Python开发的algorithm,根据看到的Wifi台站位置计算地理位置,基于idea

该算法首先使用Numpy函数来计算观察到的纬度和经度的基本加权平均值。为了尽量减少可能的Wifi位置误差的影响,它还使用“ scipy.optimize.leastsq ”方法,以便以统计方式计算,如果可能的话,还可以使用更精确的位置。

我想在Java Android平台上实现相同的行为。

对于我成功依赖org.apache.commons.math3的所有其他计算。 因此,对于最小二乘问题,我逻辑上试图依赖https://commons.apache.org/proper/commons-math/userguide/leastsquares.html

如果我理解的话,我的问题是,Scipy为我管理雅各比函数定义的复杂性,而我糟糕的数学技能并不能让我正确定义LeastSquaresProblem的模型。我尝试了一些基于example的实验,这似乎与我需要的一样,但结果并不好,因为我不知道如何处理" jacobian&#34 ;部分。

正如有人为this帖子所做的那样,有人可以为我做同样的事情并尝试以简单的方式解释它吗?

有关Python部分如何工作的更多详细信息:

使用的“scipy.optimize.leastsq”语句是:

(lat, lon), cov_x, info, mesg, ier = 
scipy.optimize.leastsq(func, initial, args=data, full_output=True)

数据的位置:纬度/经度/年龄(以毫秒为单位)/信号强度,例如:data = numpy.array([(43.48932915, 1.66561772, 1000, -20), (43.48849093, 1.6648176, 2000, -10), (43.48818612, 1.66615113, 3000, -50)])

初始值是计算加权平均纬度/经度,在此示例中为:initial = 43.48864654, 1.66550075

功能

  def func(initial, data):
        return numpy.array([
            geographic distance((float(point[latitude]), float(point[longitude])), (initial[latitude], initial[longitude])).meters * min(math.sqrt(2000.0 / float(point[age])), 1.0) / math.pow(float(point[signal strength]), 2)

结果是:43.4885401095, 1.6648660983

我在Java中的实验,我已经取代了数据值并改变了方式" modelI"计算。我简化了信号强度和年龄值。但事实并非如此,结果表明,这还不够。

double modelI = calculateVincentyDistance(o.getY(), o.getX(), center.getY(), center.getX())* Math.min(Math.sqrt(2000.0/1000.0), 1.0) / Math.pow(-10, 2);

我也会尝试https://github.com/odinsbane/least-squares-in-java,但我不确定是否正确使用它,因为我不能掌握它的工作方式。

仅供参考,我使用Vincenty距离计算,例如可以用Haversine或Euclidean代替。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

代码不易移植,因为SciPy提供了更通用的Least-squares minimization接口,而Apache Commons Math提供了curve fitting。仍然可以将许多优化问题重新表述为曲线拟合。在Python代码中,您最小化

F(current_point) = Sum{ (distance(known_point[i], current_point) * weight[i])^2 } -> min

Java曲线拟合问题有点不同:

F(current_point) = Sum{ (target_value[i] - model[i](current_point))^2  } -> min

因此,可以通过将所有target_value指定为0并使model[i]计算从current_pointknown_point[i]的加权距离来创建等效拟合问题。

在一般情况下,这些问题没有使用公式的精确解决方案,并且使用一些数值优化方法。这里存在另一个不同之处:Java实现明确要求您为优化器提供计算正在优化的函数的导数的方法。如果没有提供Dfun,Python代码似乎使用某种差异区分。您可以手动或使用FiniteDifferencesDifferentiator在Java中执行此类操作,但对于简单公式,使用DerivativeStructure

可能更容易对它们进行显式编码
static class PositionInfo {
    public final double latitude;
    public final double longitude;
    public final int ageMs;
    public final int strength;

    public PositionInfo(double latitude, double longitude, int ageMs, int strength) {
        this.latitude = latitude;
        this.longitude = longitude;
        this.ageMs = ageMs;
        this.strength = strength;
    }

    public double getWeight() {
        return Math.min(1.0, Math.sqrt(2000.0 / ageMs)) / (strength * strength);
    }
}


static DerivativeStructure getWeightedEuclideanDistance(double tgtLat, double tgtLong, PositionInfo knownPos) {
    DerivativeStructure varLat = new DerivativeStructure(2, 1, 0, tgtLat); // latitude is 0-th variable of 2 for derivatives up to 1
    DerivativeStructure varLong = new DerivativeStructure(2, 1, 1, tgtLong); // longitude is 1-st variable of 2 for derivatives up to 1
    DerivativeStructure latDif = varLat.subtract(knownPos.latitude);
    DerivativeStructure longDif = varLong.subtract(knownPos.longitude);
    DerivativeStructure latDif2 = latDif.pow(2);
    DerivativeStructure longDif2 = longDif.pow(2);
    DerivativeStructure dist2 = latDif2.add(longDif2);
    DerivativeStructure dist = dist2.sqrt();
    return dist.multiply(knownPos.getWeight());
}

// as in https://en.wikipedia.org/wiki/Haversine_formula
static DerivativeStructure getWeightedHaversineDistance(double tgtLat, double tgtLong, PositionInfo knownPos) {
    DerivativeStructure varLat = new DerivativeStructure(2, 1, 0, tgtLat);
    DerivativeStructure varLong = new DerivativeStructure(2, 1, 1, tgtLong);
    DerivativeStructure varLatRad = varLat.toRadians();
    DerivativeStructure varLongRad = varLong.toRadians();
    DerivativeStructure latDifRad2 = varLat.subtract(knownPos.latitude).toRadians().divide(2);
    DerivativeStructure longDifRad2 = varLong.subtract(knownPos.longitude).toRadians().divide(2);
    DerivativeStructure sinLat2 = latDifRad2.sin().pow(2);
    DerivativeStructure sinLong2 = longDifRad2.sin().pow(2);
    DerivativeStructure summand2 = varLatRad.cos().multiply(varLongRad.cos()).multiply(sinLong2);
    DerivativeStructure sum = sinLat2.add(summand2);
    DerivativeStructure dist = sum.sqrt().asin();
    return dist.multiply(knownPos.getWeight());
}

使用这样的准备你可以这样做:

public static void main(String[] args) {

    // latitude/longitude/age in milliseconds/signal strength
    final PositionInfo[] data = new PositionInfo[]{
            new PositionInfo(43.48932915, 1.66561772, 1000, -20),
            new PositionInfo(43.48849093, 1.6648176, 2000, -10),
            new PositionInfo(43.48818612, 1.66615113, 3000, -50)
    };


    double[] target = new double[data.length];
    Arrays.fill(target, 0.0);

    double[] start = new double[2];

    for (PositionInfo row : data) {
        start[0] += row.latitude;
        start[1] += row.longitude;
    }
    start[0] /= data.length;
    start[1] /= data.length;

    MultivariateJacobianFunction distancesModel = new MultivariateJacobianFunction() {
        @Override
        public Pair<RealVector, RealMatrix> value(final RealVector point) {
            double tgtLat = point.getEntry(0);
            double tgtLong = point.getEntry(1);

            RealVector value = new ArrayRealVector(data.length);
            RealMatrix jacobian = new Array2DRowRealMatrix(data.length, 2);
            for (int i = 0; i < data.length; i++) {
                DerivativeStructure distance = getWeightedEuclideanDistance(tgtLat, tgtLong, data[i]);
                //DerivativeStructure distance = getWeightedHaversineDistance(tgtLat, tgtLong, data[i]);
                value.setEntry(i, distance.getValue());
                jacobian.setEntry(i, 0, distance.getPartialDerivative(1, 0));
                jacobian.setEntry(i, 1, distance.getPartialDerivative(0, 1));
            }

            return new Pair<RealVector, RealMatrix>(value, jacobian);
        }
    };


    LeastSquaresProblem problem = new LeastSquaresBuilder()
            .start(start)
            .model(distancesModel)
            .target(target)
            .lazyEvaluation(false)
            .maxEvaluations(1000)
            .maxIterations(1000)
            .build();

    LeastSquaresOptimizer optimizer = new LevenbergMarquardtOptimizer().
            withCostRelativeTolerance(1.0e-12).
            withParameterRelativeTolerance(1.0e-12);

    LeastSquaresOptimizer.Optimum optimum = optimizer.optimize(problem);
    RealVector point = optimum.getPoint();
    System.out.println("Start = " + Arrays.toString(start));
    System.out.println("Solve = " + point);
}

P.S。重量的逻辑对我来说似乎很可疑。在你提到的问题中,OP有一些半径估计,然后它是一个明显的权重。使用以 对数 dBm测量的信号强度的反平方对我来说似乎很奇怪。