예제 #1
0
        public void solveModel()
        {
            int n = 5;
            IElementalAccessMatrix A = new SparseRowMatrix(n, n, 5);
            IElementalAccessVector b = new DenseVector(n), x = new DenseVector(n);

            x.SetValue(0, 0.1);
            x.SetValue(1, 0.2);
            x.SetValue(2, 0.3);
            x.SetValue(3, 0.4);
            x.SetValue(4, 0.5);
            b = new DenseVector(x.Length);

            for (int it = 0; it < 100; it++)
            {
                model(b, x);
                grad(A, x);
                IElementalAccessVector dx     = new DenseVector(n);
                ILinearSolver          solver = new BiCGSolver();
                IPreconditioner        M      = new IdentityPreconditioner();
                DefaultLinearIteration iter   = new DefaultLinearIteration();
                iter.SetParameters(1e-10, 1e-50, 1e+5, 1000000);
                double[] ans = solve(A, b, dx, solver, M, iter);
                for (int i = 0; i < x.Length; i++)
                {
                    x.AddValue(i, -0.2 * dx.GetValue(i));
                }
                int iii = 1;
            }
        }
예제 #2
0
        //[Fact]
        private static void InvestigateNoiseStagnation()
        {
            double noiseWidth = 100;

            int order = 100;
            //var A = Matrix.CreateFromArray(MultiDiagonalMatrices.CreateSymmetric(order, new int[] { 0, 1, 5, 7, 12 }));
            var valueOfEachDiagonal = new Dictionary <int, double>();

            valueOfEachDiagonal[0] = 10.0;
            valueOfEachDiagonal[1] = 4.0;
            valueOfEachDiagonal[5] = 3.0;
            var A = Matrix.CreateFromArray(MultiDiagonalMatrices.CreateSymmetric(order, valueOfEachDiagonal));
            var M = new IdentityPreconditioner();
            //var M = new JacobiPreconditioner(A.GetDiagonalAsArray());

            // Method A: Regular PCG
            var pcgBuilder = new PcgAlgorithm.Builder();

            pcgBuilder.ResidualTolerance     = 1E-15;
            pcgBuilder.MaxIterationsProvider = new FixedMaxIterationsProvider(50);
            var pcg = pcgBuilder.Build();

            // Method B: Reorthogonalized PCG, but without keeping direction vectors from previous solutions.
            var pcgReorthoRestartBuilder = new ReorthogonalizedPcg.Builder();

            pcgReorthoRestartBuilder.ResidualTolerance     = 1E-15;
            pcgReorthoRestartBuilder.MaxIterationsProvider = new FixedMaxIterationsProvider(50);
            var pcgReorthoRestart = pcgReorthoRestartBuilder.Build();

            // Method C: Reorthogonalized PCG, where the second solution will reuse direction vectors from the first
            var pcgReorthoBuilder = new ReorthogonalizedPcg.Builder();

            pcgReorthoBuilder.ResidualTolerance     = 1E-15;
            pcgReorthoBuilder.MaxIterationsProvider = new FixedMaxIterationsProvider(50);
            var pcgReortho = pcgReorthoBuilder.Build();

            // Initial rhs
            Vector x0         = Vector.CreateWithValue(order, 1);
            Vector x0Expected = x0.Copy();
            Vector b0         = A * x0Expected;

            Vector xA = Vector.CreateZero(A.NumRows);
            IterativeStatistics statsA = pcg.Solve(A, M, b0, xA, true, () => Vector.CreateZero(order));

            Debug.WriteLine($"Initial run - method A: iterations = {statsA.NumIterationsRequired}");

            Vector xB = Vector.CreateZero(A.NumRows);
            IterativeStatistics statsB = pcgReorthoRestart.Solve(A, M, b0, xB, true, () => Vector.CreateZero(order));

            Debug.WriteLine($"Initial run - method B iterations = {statsB.NumIterationsRequired}");

            Vector xC = Vector.CreateZero(A.NumRows);
            IterativeStatistics statsC = pcgReortho.Solve(A, M, b0, xC, true, () => Vector.CreateZero(order));

            Debug.WriteLine($"Initial run - method C: iterations = {statsC.NumIterationsRequired}");

            // Perturbed rhs
            int    seed = 345;
            Vector dx   = Vector.CreateFromArray(RandomMatrices.CreateRandomVector(order, seed));

            Vector x1Expected = x0 + noiseWidth * dx;
            Vector b1         = A * x1Expected;

            xA     = Vector.CreateZero(A.NumRows);
            statsA = pcg.Solve(A, M, b1, xA, true, () => Vector.CreateZero(order));
            Debug.WriteLine($"2nd run, noise = {noiseWidth} - method A: iterations = {statsA.NumIterationsRequired}");

            xB = Vector.CreateZero(A.NumRows);
            pcgReorthoRestart.ReorthoCache.Clear();
            statsB = pcgReorthoRestart.Solve(A, M, b1, xB, true, () => Vector.CreateZero(order));
            Debug.WriteLine($"2nd run, noise = {noiseWidth} - method B iterations = {statsB.NumIterationsRequired}");

            xC     = Vector.CreateZero(A.NumRows);
            statsC = pcgReortho.Solve(A, M, b1, xC, true, () => Vector.CreateZero(order));
            Debug.WriteLine($"2nd run, noise = {noiseWidth} - method C: iterations = {statsC.NumIterationsRequired}");
        }
예제 #3
0
        //[Fact]
        private static void InvestigatePFetiDPCoarseProblem2D()
        {
            int order = PFetiDPCoarseProblem2D.Order;
            var A     = Matrix.CreateFromArray(PFetiDPCoarseProblem2D.MatrixScc);
            var M     = new IdentityPreconditioner();

            // Method A: Regular PCG
            var pcgBuilder = new PcgAlgorithm.Builder();

            pcgBuilder.ResidualTolerance     = 1E-20;
            pcgBuilder.MaxIterationsProvider = new FixedMaxIterationsProvider(50);
            var pcg = pcgBuilder.Build();

            // Method B: Reorthogonalized PCG, but without keeping direction vectors from previous solutions.
            var pcgReorthoRestartBuilder = new ReorthogonalizedPcg.Builder();

            pcgReorthoRestartBuilder.ResidualTolerance     = 1E-20;
            pcgReorthoRestartBuilder.MaxIterationsProvider = new FixedMaxIterationsProvider(50);
            var pcgReorthoRestart = pcgReorthoRestartBuilder.Build();

            // Method C: Reorthogonalized PCG, where the second solution will reuse direction vectors from the first
            var pcgReorthoBuilder = new ReorthogonalizedPcg.Builder();

            pcgReorthoBuilder.ResidualTolerance     = 1E-20;
            pcgReorthoBuilder.MaxIterationsProvider = new FixedMaxIterationsProvider(50);
            var pcgReortho = pcgReorthoBuilder.Build();

            // Initial rhs
            var b         = Vector.CreateFromArray(PFetiDPCoarseProblem2D.RhsVectors[0]);
            var xExpected = Vector.CreateFromArray(PFetiDPCoarseProblem2D.SolutionVectors[0]);

            Vector xA = Vector.CreateZero(A.NumRows);
            IterativeStatistics statsA = pcg.Solve(A, M, b, xA, true, () => Vector.CreateZero(order));

            Assert.True(xExpected.Equals(xA, 1E-10));
            Debug.WriteLine($"Initial run - method A: iterations = {statsA.NumIterationsRequired}");

            Vector xB = Vector.CreateZero(A.NumRows);
            IterativeStatistics statsB = pcgReorthoRestart.Solve(A, M, b, xB, true, () => Vector.CreateZero(order));

            Assert.True(xExpected.Equals(xB, 1E-10));
            Debug.WriteLine($"Initial run - method B iterations = {statsB.NumIterationsRequired}");

            Vector xC = Vector.CreateZero(A.NumRows);
            IterativeStatistics statsC = pcgReortho.Solve(A, M, b, xC, true, () => Vector.CreateZero(order));

            Assert.True(xExpected.Equals(xC, 1E-10));
            Debug.WriteLine($"Initial run - method C: iterations = {statsC.NumIterationsRequired}");

            // Next rhs
            b         = Vector.CreateFromArray(PFetiDPCoarseProblem2D.RhsVectors[1]);
            xExpected = Vector.CreateFromArray(PFetiDPCoarseProblem2D.SolutionVectors[1]);

            xA     = Vector.CreateZero(A.NumRows);
            statsA = pcg.Solve(A, M, b, xA, true, () => Vector.CreateZero(order));
            Assert.True(xExpected.Equals(xA, 1E-10));
            Debug.WriteLine($"Initial run - method A: iterations = {statsA.NumIterationsRequired}");

            xB = Vector.CreateZero(A.NumRows);
            pcgReorthoRestart.ReorthoCache.Clear();
            statsB = pcgReorthoRestart.Solve(A, M, b, xB, true, () => Vector.CreateZero(order));
            Assert.True(xExpected.Equals(xB, 1E-10));
            Debug.WriteLine($"Initial run - method B iterations = {statsB.NumIterationsRequired}");

            xC     = Vector.CreateZero(A.NumRows);
            statsC = pcgReortho.Solve(A, M, b, xC, true, () => Vector.CreateZero(order));
            Assert.True(xExpected.Equals(xC, 1E-10));
            Debug.WriteLine($"Initial run - method C: iterations = {statsC.NumIterationsRequired}");
        }