QuadraticMinimizer始终返回非感性答案

时间:2015-07-31 22:06:22

标签: scala-breeze

我在使用QuadraticMinimizer时遇到问题 - 每次执行时我都会遇到非感性错误。我看过scaladoc和代码,我只是不确定我做错了什么。

我有以下代码:

    val lBounds:DenseVector[Double] = DenseVector(Array(Double.NegativeInfinity, 5.0, 0.1, 50.0, 50.0))
  val uBounds:DenseVector[Double] = DenseVector(Array(Double.PositiveInfinity, 500.0, 100.0, 99000.0, 99000.0))

  val inputMatrix:DenseMatrix[Double] = breeze.linalg.csvread(file=new java.io.File(getClass.getResource("/input/zip06latlong.csv").toURI), skipLines=1)

  val y = inputMatrix(::, 0).toDenseVector
  val X = inputMatrix(::, Seq(1,2,3,4,5)).copy.toDenseMatrix
  val T = X.toDenseMatrix.t

  val gram = (T * X)
  val b:Transpose[DenseVector[Double]] = y.t * X

  println(gram)
  println(b.inner)
  breeze.linalg.csvwrite(new java.io.File("grammatrix.csv"), gram)
  val minimizer:QuadraticMinimizer = new QuadraticMinimizer(rank(gram), ProjectBox(lBounds,uBounds))


  val coeffs =  minimizer.minimize(gram, b.inner)
  println(coeffs)

这就是克的样子:

    279.0      628207.0       1461245.0        1024.0     729.5      
628207.0   1.569309427E9  3.414471724E9    2449533.0  1755536.5  
1461245.0  3.414471724E9  1.2324155401E10  5511816.0  3846583.0  
1024.0     2449533.0      5511816.0        3980.0     2786.0     
729.5      1755536.5      3846583.0        2786.0     2092.75

并且b矢量是:

2.917264069193999E8, 7.294450468242601E11, 1.585917338779061E12, 1.131888709844E9, 8.260072757806E8

当我执行代码时,我得到了:

DenseVector(-12171.118011368422, -424.79971124882286, -9.565028484748783, 49.3827769217138, 49.536905925364195)

低于指定的下限。

1 个答案:

答案 0 :(得分:0)

你能否加入H(克矩阵)和y(线性项)以便我可以测试出来?初始化模式也适用于高级用户(例如我为其编写解算器的Spark ALS),但您可以使用如下的简单模式(我使用了您的边界):

val lb = DenseVector(Array(Double.NegativeInfinity, 5.0, 0.1, 50.0, 50.0))
val ub = DenseVector(Array(Double.PositiveInfinity, 500.0, 100.0, 99000.0, 99000.0))

val n = 5

val ata = new DenseMatrix[Double](5, 5,
  Array(4.377, -3.531, -1.306, -0.139, 3.418,
    -3.531, 4.344, 0.934, 0.305, -2.140,
    -1.306, 0.934, 2.644, -0.203, -0.170,
    -0.139, 0.305, -0.203, 5.883, 1.428,
    3.418, -2.140, -0.170, 1.428, 4.684))

val atb = DenseVector(-1.632, 2.115, 1.094, -1.025, -0.636)

val qpSolverBounds = new QuadraticMinimizer(n, ProjectBox(lb, ub))
val result = qpSolverBounds.minimize(ata, atb)
println(s"Bounds test $result")

我得到的结果为DenseVector(-33.02749751949581,5.0,0.1,50.0,50.0),看起来并不敏感。

请不要更改默认的maxIter,因为此控制是针对不需要绝对收敛的流量给出的。让解算器选择maxIter。