Java 类org.apache.commons.math3.linear.DiagonalMatrix 实例源码

项目:armorvox-client    文件:VxmlClientGetVoiceprint.java   
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 = { 1.0, 1.0 };

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

    AbstractCurveFitter.TheoreticalValuesFunction model = new AbstractCurveFitter.TheoreticalValuesFunction(func, 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();
}
项目:CARMA    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testInconsistentSizes1() {
    try {
        LinearProblem problem
                = new LinearProblem(new double[][]{{1, 0},
                {0, 1}},
                new double[]{-1, 1});

        //TODO why is this part here? hasn't it been tested already?
        Optimum optimum = optimizer.optimize(problem.getBuilder().build());

        Assert.assertEquals(0, optimum.getRMS(), TOl);
        assertEquals(TOl, optimum.getPoint(), -1, 1);

        //TODO move to builder test
        optimizer.optimize(
                problem.getBuilder().weight(new DiagonalMatrix(new double[]{1})).build());

        fail(optimizer);
    } catch (DimensionMismatchException e) {
        //expected
    }
}
项目:CARMA    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testInconsistentSizes2() {
    try {
        LinearProblem problem
                = new LinearProblem(new double[][]{{1, 0}, {0, 1}},
                new double[]{-1, 1});

        Optimum optimum = optimizer.optimize(problem.getBuilder().build());

        Assert.assertEquals(0, optimum.getRMS(), TOl);
        assertEquals(TOl, optimum.getPoint(), -1, 1);

        //TODO move to builder test
        optimizer.optimize(
                problem.getBuilder()
                        .target(new double[]{1})
                        .weight(new DiagonalMatrix(new double[]{1}))
                        .build()
        );

        fail(optimizer);
    } catch (DimensionMismatchException e) {
        //expected
    }
}
项目:CARMA    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testCircleFittingBadInit() {
    CircleVectorial circle = new CircleVectorial();
    double[][] points = circlePoints;
    double[] weights = new double[points.length];
    final double[] start = {-12, -12};
    Arrays.fill(weights, 2);
    for (int i = 0; i < points.length; ++i) {
        circle.addPoint(points[i][0], points[i][1]);
    }

    Optimum optimum = optimizer.optimize(builder(circle).weight(new DiagonalMatrix(weights)).start(start).build());

    Vector2D center = new Vector2D(optimum.getPoint().getEntry(0), optimum.getPoint().getEntry(1));
    Assert.assertTrue(optimum.getEvaluations() < 25);
    Assert.assertEquals(0.043, optimum.getRMS(), 1e-3);
    Assert.assertEquals(0.292235, circle.getRadius(center), 1e-6);
    Assert.assertEquals(-0.151738, center.getX(), 1e-6);
    Assert.assertEquals(0.2075001, center.getY(), 1e-6);
}
项目:CARMA    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testCircleFittingGoodInit() {
    CircleVectorial circle = new CircleVectorial();
    double[][] points = circlePoints;
    double[] weights = new double[points.length];
    Arrays.fill(weights, 2);
    for (int i = 0; i < points.length; ++i) {
        circle.addPoint(points[i][0], points[i][1]);
    }
    final double[] start = {0, 0};

    Optimum optimum = optimizer.optimize(
            builder(circle).weight(new DiagonalMatrix(weights)).start(start).build());

    assertEquals(1e-6, optimum.getPoint(), -0.1517383071957963, 0.2074999736353867);
    Assert.assertEquals(0.04268731682389561, optimum.getRMS(), 1e-8);
}
项目:gatk-protected    文件:DecomposeSingularValuesIntegrationTest.java   
private void assertSVDValues(final File outputFileV, final File outputFileS, final File outputFileU) {
    try {
        final ReadCountCollection rcc = ReadCountCollectionUtils.parse(CONTROL_PCOV_FULL_FILE);
        final SVD svd = SVDFactory.createSVD(rcc.counts());
        final RealMatrix sDiag = new DiagonalMatrix(svd.getSingularValues());
        assertOutputFileValues(outputFileU, svd.getU());
        assertOutputFileValues(outputFileS, sDiag);
        assertOutputFileValues(outputFileV, svd.getV());
        assertUnitaryMatrix(svd.getV());
        assertUnitaryMatrix(svd.getU());
        Assert.assertTrue(MatrixUtils.isSymmetric(sDiag, 1e-32));

    } catch (final IOException ioe) {
        Assert.fail("Could not open test file: " + CONTROL_PCOV_FULL_FILE, ioe);
    }
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testInconsistentSizes1() {
    try {
        LinearProblem problem
                = new LinearProblem(new double[][]{{1, 0},
                {0, 1}},
                new double[]{-1, 1});

        //TODO why is this part here? hasn't it been tested already?
        Optimum optimum = optimizer.optimize(problem.getBuilder().build());

        Assert.assertEquals(0, optimum.getRMS(), TOl);
        assertEquals(TOl, optimum.getPoint(), -1, 1);

        //TODO move to builder test
        optimizer.optimize(
                problem.getBuilder().weight(new DiagonalMatrix(new double[]{1})).build());

        fail(optimizer);
    } catch (DimensionMismatchException e) {
        //expected
    }
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testCircleFittingBadInit() {
    CircleVectorial circle = new CircleVectorial();
    double[][] points = circlePoints;
    double[] weights = new double[points.length];
    final double[] start = {-12, -12};
    Arrays.fill(weights, 2);
    for (int i = 0; i < points.length; ++i) {
        circle.addPoint(points[i][0], points[i][1]);
    }

    Optimum optimum = optimizer.optimize(builder(circle).weight(new DiagonalMatrix(weights)).start(start).build());

    Vector2D center = new Vector2D(optimum.getPoint().getEntry(0), optimum.getPoint().getEntry(1));
    Assert.assertTrue(optimum.getEvaluations() < 25);
    Assert.assertEquals(0.043, optimum.getRMS(), 1e-3);
    Assert.assertEquals(0.292235, circle.getRadius(center), 1e-6);
    Assert.assertEquals(-0.151738, center.getX(), 1e-6);
    Assert.assertEquals(0.2075001, center.getY(), 1e-6);
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testCircleFittingGoodInit() {
    CircleVectorial circle = new CircleVectorial();
    double[][] points = circlePoints;
    double[] weights = new double[points.length];
    Arrays.fill(weights, 2);
    for (int i = 0; i < points.length; ++i) {
        circle.addPoint(points[i][0], points[i][1]);
    }
    final double[] start = {0, 0};

    Optimum optimum = optimizer.optimize(
            builder(circle).weight(new DiagonalMatrix(weights)).start(start).build());

    assertEquals(1e-6, optimum.getPoint(), -0.1517383071957963, 0.2074999736353867);
    Assert.assertEquals(0.04268731682389561, optimum.getRMS(), 1e-8);
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testCircleFittingGoodInit() {
    CircleVectorial circle = new CircleVectorial();
    double[][] points = circlePoints;
    double[] weights = new double[points.length];
    Arrays.fill(weights, 2);
    for (int i = 0; i < points.length; ++i) {
        circle.addPoint(points[i][0], points[i][1]);
    }
    final double[] start = {0, 0};

    Optimum optimum = optimizer.optimize(
            builder(circle).weight(new DiagonalMatrix(weights)).start(start).build());

    assertEquals(1e-6, optimum.getPoint(), -0.1517383071957963, 0.2074999736353867);
    Assert.assertEquals(0.04268731682389561, optimum.getRMS(), 1e-8);
}
项目:astor    文件:AbstractLeastSquaresOptimizerTest.java   
@Test
public void testComputeCost() throws IOException {
    final StatisticalReferenceDataset dataset
        = StatisticalReferenceDatasetFactory.createKirby2();
    final double[] a = dataset.getParameters();
    final double[] y = dataset.getData()[1];
    final double[] w = new double[y.length];
    Arrays.fill(w, 1d);

    StatisticalReferenceDataset.LeastSquaresProblem problem
        = dataset.getLeastSquaresProblem();

    final LevenbergMarquardtOptimizer optim = LevenbergMarquardtOptimizer.create()
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(y)
        .withWeight(new DiagonalMatrix(w))
        .withStartPoint(a);

    final double expected = dataset.getResidualSumOfSquares();
    final double cost = optim.computeCost(optim.computeResiduals(optim.getModel().value(optim.getStart())));
    final double actual = cost * cost;
    Assert.assertEquals(dataset.getName(), expected, actual, 1e-11 * expected);
}
项目:astor    文件:AbstractLeastSquaresOptimizerTest.java   
@Test
public void testComputeRMS() throws IOException {
    final StatisticalReferenceDataset dataset
        = StatisticalReferenceDatasetFactory.createKirby2();
    final double[] a = dataset.getParameters();
    final double[] y = dataset.getData()[1];
    final double[] w = new double[y.length];
    Arrays.fill(w, 1d);

    StatisticalReferenceDataset.LeastSquaresProblem problem
        = dataset.getLeastSquaresProblem();

    final LevenbergMarquardtOptimizer optim = LevenbergMarquardtOptimizer.create()
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(y)
        .withWeight(new DiagonalMatrix(w))
        .withStartPoint(a);

    final double expected = FastMath.sqrt(dataset.getResidualSumOfSquares() /
                                          dataset.getNumObservations());
    final double actual = optim.computeRMS(optim.getStart());
    Assert.assertEquals(dataset.getName(), expected, actual, 1e-11 * expected);
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testInconsistentSizes2() {
    try {
        LinearProblem problem
                = new LinearProblem(new double[][]{{1, 0}, {0, 1}},
                new double[]{-1, 1});

        Optimum optimum = optimizer.optimize(problem.getBuilder().build());

        Assert.assertEquals(0, optimum.getRMS(), TOl);
        assertEquals(TOl, optimum.getPoint(), -1, 1);

        //TODO move to builder test
        optimizer.optimize(
                problem.getBuilder()
                        .target(new double[]{1})
                        .weight(new DiagonalMatrix(new double[]{1}))
                        .build()
        );

        fail(optimizer);
    } catch (DimensionMismatchException e) {
        //expected
    }
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testTrivial() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 2 } },
                            new double[] { 3 });
    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1 }))
        .withStartPoint(new double[] { 0 });

    PointVectorValuePair optimum = optimizer.optimize();

    Assert.assertEquals(0, optimizer.computeRMS(optimum.getPoint()), 1e-10);
    Assert.assertEquals(1.5, optimum.getPoint()[0], 1e-10);
    Assert.assertEquals(3.0, optimum.getValue()[0], 1e-10);
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testQRColumnsPermutation() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 1, -1 }, { 0, 2 }, { 1, -2 } },
                            new double[] { 4, 6, 1 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 0, 0 });

    PointVectorValuePair optimum = optimizer.optimize();

    Assert.assertEquals(0, optimizer.computeRMS(optimum.getPoint()), 1e-10);
    Assert.assertEquals(7, optimum.getPoint()[0], 1e-10);
    Assert.assertEquals(3, optimum.getPoint()[1], 1e-10);
    Assert.assertEquals(4, optimum.getValue()[0], 1e-10);
    Assert.assertEquals(6, optimum.getValue()[1], 1e-10);
    Assert.assertEquals(1, optimum.getValue()[2], 1e-10);
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testNoDependency() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 2, 0, 0, 0, 0, 0 },
            { 0, 2, 0, 0, 0, 0 },
            { 0, 0, 2, 0, 0, 0 },
            { 0, 0, 0, 2, 0, 0 },
            { 0, 0, 0, 0, 2, 0 },
            { 0, 0, 0, 0, 0, 2 }
    }, new double[] { 0, 1.1, 2.2, 3.3, 4.4, 5.5 });
    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1, 1, 1, 1 }))
        .withStartPoint(new double[] { 0, 0, 0, 0, 0, 0 });

    double[] optimum = optimizer.optimize().getPoint();
    Assert.assertEquals(0, optimizer.computeRMS(optimum), 1e-10);
    for (int i = 0; i < problem.target.length; ++i) {
        Assert.assertEquals(0.55 * i, optimum[i], 1e-10);
    }
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testOneSet() {
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1,  0, 0 },
            { -1,  1, 0 },
            {  0, -1, 1 }
    }, new double[] { 1, 1, 1});

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 0, 0, 0 });

    double[] optimum = optimizer.optimize().getPoint();
    Assert.assertEquals(0, optimizer.computeRMS(optimum), 1e-10);
    Assert.assertEquals(1, optimum[0], 1e-10);
    Assert.assertEquals(2, optimum[1], 1e-10);
    Assert.assertEquals(3, optimum[2], 1e-10);
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test(expected=ConvergenceException.class)
public void testNonInvertible() throws Exception {
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 0, 0, 0 });

    optimizer.optimize();
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testMoreEstimatedParametersSimple() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 3, 2,  0, 0 },
            { 0, 1, -1, 1 },
            { 2, 0,  1, 0 }
    }, new double[] { 7, 3, 5 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 7, 6, 5, 4 });

    double[] optimum = optimizer.optimize().getPoint();
    Assert.assertEquals(0, optimizer.computeRMS(optimum), 1e-10);
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testRedundantEquations() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 1,  1 },
            { 1, -1 },
            { 1,  3 }
    }, new double[] { 3, 1, 5 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 1, 1 });

    double[] optimum = optimizer.optimize().getPoint();

    Assert.assertEquals(0, optimizer.computeRMS(optimum), 1e-10);
    Assert.assertEquals(2, optimum[0], 1e-10);
    Assert.assertEquals(1, optimum[1], 1e-10);
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testInconsistentEquations() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 1,  1 },
            { 1, -1 },
            { 1,  3 }
    }, new double[] { 3, 1, 4 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 1, 1 });

    double[] optimum = optimizer.optimize().getPoint();

    Assert.assertTrue(optimizer.computeRMS(optimum) > 0.1);
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test(expected=DimensionMismatchException.class)
public void testInconsistentSizes1() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 1, 0 },
                                             { 0, 1 } },
                            new double[] { -1, 1 });
    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1 }))
        .withStartPoint(new double[] { 0, 0 });

    double[] optimum = optimizer.optimize().getPoint();

    Assert.assertEquals(0, optimizer.computeRMS(optimum), 1e-10);
    Assert.assertEquals(-1, optimum[0], 1e-10);
    Assert.assertEquals(1, optimum[1], 1e-10);

    optimizer.withWeight(new DiagonalMatrix(new double[] { 1 })).optimize();
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test(expected=DimensionMismatchException.class)
public void testInconsistentSizes2() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 1, 0 }, { 0, 1 } },
                            new double[] { -1, 1 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1 }))
        .withStartPoint(new double[] { 0, 0 });

    double[] optimum = optimizer.optimize().getPoint();

    Assert.assertEquals(0, optimizer.computeRMS(optimum), 1e-10);
    Assert.assertEquals(-1, optimum[0], 1e-10);
    Assert.assertEquals(1, optimum[1], 1e-10);

    optimizer
        .withTarget(new double[] { 1 })
        .withWeight(new DiagonalMatrix(new double[] { 1 }))
        .optimize();
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testCircleFittingGoodInit() {
    CircleVectorial circle = new CircleVectorial();
    double[][] points = circlePoints;
    double[] target = new double[points.length];
    Arrays.fill(target, 0);
    double[] weights = new double[points.length];
    Arrays.fill(weights, 2);
    for (int i = 0; i < points.length; ++i) {
        circle.addPoint(points[i][0], points[i][1]);
    }
    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(circle.getModelFunction(),
                              circle.getModelFunctionJacobian())
        .withTarget(target)
        .withWeight(new DiagonalMatrix(weights))
        .withStartPoint(new double[] { 0, 0 });

    double[] optimum = optimizer.optimize().getPoint();

    Assert.assertEquals(-0.1517383071957963, optimum[0], 1e-6);
    Assert.assertEquals(0.2074999736353867,  optimum[1], 1e-6);
    Assert.assertEquals(0.04268731682389561, optimizer.computeRMS(optimum), 1e-8);
}
项目:astor    文件:AbstractLeastSquaresOptimizerTest.java   
@Test
public void testComputeCost() throws IOException {
    final StatisticalReferenceDataset dataset
        = StatisticalReferenceDatasetFactory.createKirby2();
    final double[] a = dataset.getParameters();
    final double[] y = dataset.getData()[1];
    final double[] w = new double[y.length];
    Arrays.fill(w, 1d);

    StatisticalReferenceDataset.LeastSquaresProblem problem
        = dataset.getLeastSquaresProblem();

    final LevenbergMarquardtOptimizer optim = LevenbergMarquardtOptimizer.create()
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(y)
        .withWeight(new DiagonalMatrix(w))
        .withStartPoint(a);

    final double expected = dataset.getResidualSumOfSquares();
    final double cost = optim.computeCost(optim.computeResiduals(optim.getModel().value(optim.getStart())));
    final double actual = cost * cost;
    Assert.assertEquals(dataset.getName(), expected, actual, 1e-11 * expected);
}
项目:astor    文件:GaussNewtonOptimizerTest.java   
@Test(expected=TooManyEvaluationsException.class)
public void testMaxEvaluations() throws Exception {
    CircleVectorial circle = new CircleVectorial();
    circle.addPoint( 30.0,  68.0);
    circle.addPoint( 50.0,  -6.0);
    circle.addPoint(110.0, -20.0);
    circle.addPoint( 35.0,  15.0);
    circle.addPoint( 45.0,  97.0);

    GaussNewtonOptimizer optimizer = createOptimizer()
        .withConvergenceChecker(new SimpleVectorValueChecker(1e-30, 1e-30))
        .withMaxIterations(Integer.MAX_VALUE)
        .withMaxEvaluations(100)
        .withModelAndJacobian(circle.getModelFunction(),
                              circle.getModelFunctionJacobian())
        .withTarget(new double[] { 0, 0, 0, 0, 0 })
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1, 1, 1 }))
        .withStartPoint(new double[] { 98.680, 47.345 });

    optimizer.optimize();
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testInconsistentSizes1() {
    try {
        LinearProblem problem
                = new LinearProblem(new double[][]{{1, 0},
                {0, 1}},
                new double[]{-1, 1});

        //TODO why is this part here? hasn't it been tested already?
        Optimum optimum = optimizer.optimize(problem.getBuilder().build());

        Assert.assertEquals(0, optimum.getRMS(), TOl);
        assertEquals(TOl, optimum.getPoint(), -1, 1);

        //TODO move to builder test
        optimizer.optimize(
                problem.getBuilder().weight(new DiagonalMatrix(new double[]{1})).build());

        fail(optimizer);
    } catch (DimensionMismatchException e) {
        //expected
    }
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testTrivial() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 2 } },
                            new double[] { 3 });
    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1 }))
        .withStartPoint(new double[] { 0 });

    PointVectorValuePair optimum = optimizer.optimize();

    Assert.assertEquals(0, optimizer.computeRMS(optimum.getPoint()), 1e-10);
    Assert.assertEquals(1.5, optimum.getPoint()[0], 1e-10);
    Assert.assertEquals(3.0, optimum.getValue()[0], 1e-10);
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testQRColumnsPermutation() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 1, -1 }, { 0, 2 }, { 1, -2 } },
                            new double[] { 4, 6, 1 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 0, 0 });

    PointVectorValuePair optimum = optimizer.optimize();

    Assert.assertEquals(0, optimizer.computeRMS(optimum.getPoint()), 1e-10);
    Assert.assertEquals(7, optimum.getPoint()[0], 1e-10);
    Assert.assertEquals(3, optimum.getPoint()[1], 1e-10);
    Assert.assertEquals(4, optimum.getValue()[0], 1e-10);
    Assert.assertEquals(6, optimum.getValue()[1], 1e-10);
    Assert.assertEquals(1, optimum.getValue()[2], 1e-10);
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testCircleFittingBadInit() {
    CircleVectorial circle = new CircleVectorial();
    double[][] points = circlePoints;
    double[] weights = new double[points.length];
    final double[] start = {-12, -12};
    Arrays.fill(weights, 2);
    for (int i = 0; i < points.length; ++i) {
        circle.addPoint(points[i][0], points[i][1]);
    }

    Optimum optimum = optimizer.optimize(builder(circle).weight(new DiagonalMatrix(weights)).start(start).build());

    Vector2D center = new Vector2D(optimum.getPoint().getEntry(0), optimum.getPoint().getEntry(1));
    Assert.assertTrue(optimum.getEvaluations() < 25);
    Assert.assertEquals(0.043, optimum.getRMS(), 1e-3);
    Assert.assertEquals(0.292235, circle.getRadius(center), 1e-6);
    Assert.assertEquals(-0.151738, center.getX(), 1e-6);
    Assert.assertEquals(0.2075001, center.getY(), 1e-6);
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testOneSet() {
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1,  0, 0 },
            { -1,  1, 0 },
            {  0, -1, 1 }
    }, new double[] { 1, 1, 1});

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 0, 0, 0 });

    double[] optimum = optimizer.optimize().getPoint();
    Assert.assertEquals(0, optimizer.computeRMS(optimum), 1e-10);
    Assert.assertEquals(1, optimum[0], 1e-10);
    Assert.assertEquals(2, optimum[1], 1e-10);
    Assert.assertEquals(3, optimum[2], 1e-10);
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test(expected=ConvergenceException.class)
public void testNonInvertible() throws Exception {
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 0, 0, 0 });

    optimizer.optimize();
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testInconsistentEquations() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 1,  1 },
            { 1, -1 },
            { 1,  3 }
    }, new double[] { 3, 1, 4 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 1, 1 });

    double[] optimum = optimizer.optimize().getPoint();

    Assert.assertTrue(optimizer.computeRMS(optimum) > 0.1);
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test(expected=DimensionMismatchException.class)
public void testInconsistentSizes1() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 1, 0 },
                                             { 0, 1 } },
                            new double[] { -1, 1 });
    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1 }))
        .withStartPoint(new double[] { 0, 0 });

    double[] optimum = optimizer.optimize().getPoint();

    Assert.assertEquals(0, optimizer.computeRMS(optimum), 1e-10);
    Assert.assertEquals(-1, optimum[0], 1e-10);
    Assert.assertEquals(1, optimum[1], 1e-10);

    optimizer.withWeight(new DiagonalMatrix(new double[] { 1 })).optimize();
}
项目:astor    文件:AbstractLeastSquaresOptimizerAbstractTest.java   
@Test
public void testCircleFittingGoodInit() {
    CircleVectorial circle = new CircleVectorial();
    double[][] points = circlePoints;
    double[] target = new double[points.length];
    Arrays.fill(target, 0);
    double[] weights = new double[points.length];
    Arrays.fill(weights, 2);
    for (int i = 0; i < points.length; ++i) {
        circle.addPoint(points[i][0], points[i][1]);
    }
    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(circle.getModelFunction(),
                              circle.getModelFunctionJacobian())
        .withTarget(target)
        .withWeight(new DiagonalMatrix(weights))
        .withStartPoint(new double[] { 0, 0 });

    double[] optimum = optimizer.optimize().getPoint();

    Assert.assertEquals(-0.1517383071957963, optimum[0], 1e-6);
    Assert.assertEquals(0.2074999736353867,  optimum[1], 1e-6);
    Assert.assertEquals(0.04268731682389561, optimizer.computeRMS(optimum), 1e-8);
}
项目:ojAlgo-extensions    文件:RealMatrixWrapper.java   
public static RealMatrixWrapper of(final RealMatrix delegate) {
    if (delegate instanceof Array2DRowRealMatrix) {
        return new Array2DRowWrapper((Array2DRowRealMatrix) delegate);
    } else if (delegate instanceof DiagonalMatrix) {
        return new DiagonalWrapper((DiagonalMatrix) delegate);
    } else {
        return new DefaultWrapper(delegate);
    }
}
项目:beacon-finder    文件:NonLinearLeastSquaresSolver.java   
public Optimum solve(double[] target, double[] weights, double[] initialPoint, boolean debugInfo) {
    if (debugInfo) {
        System.out.println("Max Number of Iterations : " + MAXNUMBEROFITERATIONS);
    }

    LeastSquaresProblem leastSquaresProblem = LeastSquaresFactory.create(
            // function to be optimized
            function,
            // target values at optimal point in least square equation
            // (x0+xi)^2 + (y0+yi)^2 + ri^2 = target[i]
            new ArrayRealVector(target, false), new ArrayRealVector(initialPoint, false), new DiagonalMatrix(weights), null, MAXNUMBEROFITERATIONS, MAXNUMBEROFITERATIONS);

    return leastSquaresOptimizer.optimize(leastSquaresProblem);
}
项目:SME    文件:PolynomialCurveFitter.java   
/** {@inheritDoc} */
@Override
protected LeastSquaresProblem getProblem(Collection<WeightedObservedPoint> observations) {
    // Prepare least-squares problem.
    final int len = observations.size();
    final double[] target  = new double[len];
    final double[] weights = new double[len];

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

    final AbstractCurveFitter.TheoreticalValuesFunction model =
            new AbstractCurveFitter.TheoreticalValuesFunction(FUNCTION, observations);

    if (initialGuess == null) {
        throw new MathInternalError();
    }

    // Return a new least squares problem set up to fit a polynomial curve to the
    // observed points.
    return new LeastSquaresBuilder().
            maxEvaluations(Integer.MAX_VALUE).
            maxIterations(maxIter).
            start(initialGuess).
            target(target).
            weight(new DiagonalMatrix(weights)).
            model(model.getModelFunction(), model.getModelFunctionJacobian()).
            build();

}
项目:SME    文件:GaussianCurveFitter.java   
/** {@inheritDoc} */
@Override
protected LeastSquaresProblem getProblem(Collection<WeightedObservedPoint> observations) {

    // Prepare least-squares problem.
    final int len = observations.size();
    final double[] target  = new double[len];
    final double[] weights = new double[len];

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

    final AbstractCurveFitter.TheoreticalValuesFunction model =
            new AbstractCurveFitter.TheoreticalValuesFunction(FUNCTION, observations);

    final double[] startPoint = initialGuess != null ?
        initialGuess :
        // Compute estimation.
        new ParameterGuesser(observations).guess();

    // Return a new least squares problem set up to fit a Gaussian curve to the
    // observed points.
    return new LeastSquaresBuilder().
            maxEvaluations(Integer.MAX_VALUE).
            maxIterations(maxIter).
            start(startPoint).
            target(target).
            weight(new DiagonalMatrix(weights)).
            model(model.getModelFunction(), model.getModelFunctionJacobian()).
            build();

}
项目:SME    文件:SimpleCurveFitter.java   
/** {@inheritDoc} */
@Override
protected LeastSquaresProblem getProblem(Collection<WeightedObservedPoint> observations) {
    // Prepare least-squares problem.
    final int len = observations.size();
    final double[] target  = new double[len];
    final double[] weights = new double[len];

    int count = 0;
    for (WeightedObservedPoint obs : observations) {
        target[count]  = obs.getY();
        weights[count] = obs.getWeight();
        ++count;
    }

    final AbstractCurveFitter.TheoreticalValuesFunction model
        = new AbstractCurveFitter.TheoreticalValuesFunction(function,
                                                            observations);

    // Create an optimizer for fitting the curve to the observed points.
    return new LeastSquaresBuilder().
            maxEvaluations(Integer.MAX_VALUE).
            maxIterations(maxIter).
            start(initialGuess).
            target(target).
            weight(new DiagonalMatrix(weights)).
            model(model.getModelFunction(), model.getModelFunctionJacobian()).
            build();
}