Пример #1
0
        public void KappaTestConstructorTest2()
        {
            // Example from: M. Reichenheim (2004). Confidence intervals for the
            // kappa statistic. The Stata Journal (2004) 4, Number 4, pp. 421–428.
            // http://www.stata-journal.com/sjpdf.html?articlenum=st0076

            int[,] matrix = // (pg 599)
            {
                { 48,  12 },
                { 16, 160 },
            };

            GeneralConfusionMatrix a = new GeneralConfusionMatrix(matrix);

            // Reichenheim's paper shows:
            // Agreement | Expected Agreement | Kappa  | Std. Error |   Z
            //   88.14%  |       61.25%       | 0.6938 |   0.0650   | 10.67

            Assert.AreEqual(88.14, a.OverallAgreement * 100, 1e-2);
            Assert.AreEqual(61.25, a.ChanceAgreement * 100, 1e-2);
            Assert.AreEqual(0.6938, a.Kappa, 1e-4);
            Assert.AreEqual(0.0650, a.StandardErrorUnderNull, 1e-4);

            KappaTest target = new KappaTest(a);

            Assert.AreEqual(OneSampleHypothesis.ValueIsGreaterThanHypothesis, target.Hypothesis);

            Assert.AreEqual(10.67, target.Statistic, 1e-3);
            Assert.AreEqual(7.0849733798130419E-27, target.PValue);
        }
Пример #2
0
        public void KappaTestConstructorTest4()
        {
            // Example from Statistical Methods for Rates and Proportions

            // Checked against http://graphpad.com/quickcalcs/Kappa2.cfm     (OK)
            // Checked against Statistical Methods for Rates and Proportions (OK)
            // Checked against http://vassarstats.net/kappa.html           (FAIL)

            int[,] matrix = // (pg 599)
            {
                { 75, 1,  4 },
                {  5, 4,  1 },
                {  0, 0, 10 },
            };

            GeneralConfusionMatrix a = new GeneralConfusionMatrix(matrix);

            Assert.AreEqual(100, a.Samples);

            Assert.AreEqual(80, a.RowTotals[0]);
            Assert.AreEqual(10, a.RowTotals[1]);
            Assert.AreEqual(10, a.RowTotals[2]);

            Assert.AreEqual(80, a.ColumnTotals[0]);
            Assert.AreEqual(5, a.ColumnTotals[1]);
            Assert.AreEqual(15, a.ColumnTotals[2]);

            double[,] proportions = // (pg 599)
            {
                { 0.75, 0.01, 0.04 },
                { 0.05, 0.04, 0.01 },
                { 0.00, 0.00, 0.10 },
            };

            Assert.IsTrue(proportions.IsEqual(a.ProportionMatrix));


            double expectedVar = a.Variance;

            // Test against non-null hypothesis
            KappaTest target = new KappaTest(a, hypothesizedKappa: 0.8);

            // Fleiss   reports 0.68  (page 606)
            // Graphpad reports 0.676
            Assert.AreEqual(0.68, target.EstimatedValue, 0.01);
            Assert.AreEqual(a.Kappa, target.EstimatedValue);
            Assert.IsFalse(double.IsNaN(target.EstimatedValue));

            // Fleiss   reports 0.087 (page 607)
            // Graphpad reports 0.088
            Assert.AreEqual(0.087, target.StandardError, 0.001);
            Assert.IsFalse(double.IsNaN(target.StandardError));

            Assert.AreEqual(-1.38, target.Statistic, 0.029);
            Assert.AreEqual(0.1589, target.PValue, 0.0001);

            Assert.IsFalse(target.Significant);
        }
Пример #3
0
        public void KappaTestConstructorTest5()
        {
            // Example from Statistical Methods for Rates and Proportions
            // for kappa variance under the null hypothesis

            // Checked against Statistical Methods for Rates and Proportions (OK)


            int[,] matrix = // (pg 599)
            {
                { 75, 1,  4 },
                {  5, 4,  1 },
                {  0, 0, 10 },
            };

            GeneralConfusionMatrix a = new GeneralConfusionMatrix(matrix);

            Assert.AreEqual(100, a.Samples);

            Assert.AreEqual(80, a.RowTotals[0]);
            Assert.AreEqual(10, a.RowTotals[1]);
            Assert.AreEqual(10, a.RowTotals[2]);

            Assert.AreEqual(80, a.ColumnTotals[0]);
            Assert.AreEqual(5, a.ColumnTotals[1]);
            Assert.AreEqual(15, a.ColumnTotals[2]);

            double[,] proportions = // (pg 599)
            {
                { 0.75, 0.01, 0.04 },
                { 0.05, 0.04, 0.01 },
                { 0.00, 0.00, 0.10 },
            };

            Assert.IsTrue(proportions.IsEqual(a.ProportionMatrix));

            // Test under null hypothesis
            KappaTest target = new KappaTest(a, hypothesizedKappa: 0,
                                             alternate: OneSampleHypothesis.ValueIsGreaterThanHypothesis);

            Assert.AreEqual(0.68, target.EstimatedValue, 0.01); // pg 605
            Assert.AreEqual(a.Kappa, target.EstimatedValue);
            Assert.IsFalse(double.IsNaN(target.EstimatedValue));

            Assert.AreEqual(0.076, target.StandardError, 0.001);
            Assert.IsFalse(double.IsNaN(target.StandardError));

            Assert.AreEqual(8.95, target.Statistic, 0.08);

            Assert.IsTrue(target.Significant);
        }
Пример #4
0
        public void KappaTestConstructorTest()
        {
            // Example from http://vassarstats.net/kappa.html

            // Checked against http://graphpad.com/quickcalcs/Kappa2.cfm     (OK)

            int[,] matrix =
            {
                { 44,  5, 1 },
                {  7, 20, 3 },
                {  9,  5, 6 },
            };

            GeneralConfusionMatrix a = new GeneralConfusionMatrix(matrix);

            Assert.AreEqual(a.RowTotals[0], 50);
            Assert.AreEqual(a.RowTotals[1], 30);
            Assert.AreEqual(a.RowTotals[2], 20);

            Assert.AreEqual(a.ColumnTotals[0], 60);
            Assert.AreEqual(a.ColumnTotals[1], 30);
            Assert.AreEqual(a.ColumnTotals[2], 10);


            Assert.AreEqual(0.4915, a.Kappa, 1e-4);
            Assert.IsFalse(double.IsNaN(a.Kappa));

            double var  = a.Variance;
            double var0 = a.VarianceUnderNull;
            double varD = Accord.Statistics.Testing.KappaTest.DeltaMethodKappaVariance(a);

            double se  = System.Math.Sqrt(var);
            double se0 = System.Math.Sqrt(var0);
            double seD = System.Math.Sqrt(varD);

            Assert.AreEqual(0.072, a.StandardError, 0.0005);

            // Create a test of the null hypothesis (actual k = 0)
            KappaTest target = new KappaTest(a, hypothesizedKappa: 0);

            // Std. Error is computed differently under the null hypothesis:
            Assert.AreEqual(0.073509316753225237, target.StandardError, 1e-5);
            Assert.IsFalse(double.IsNaN(target.StandardError));
        }
Пример #5
0
        public void KappaTestConstructorTest3()
        {
            int[,] matrix =
            {
                { 377, 79,  0, 0 },
                {   2, 72,  0, 0 },
                {  33,  5, 60, 0 },
                {   3, 20,  0, 8 },
            };

            GeneralConfusionMatrix a = new GeneralConfusionMatrix(matrix);

            Assert.AreEqual(0.7845, a.OverralAgreement, 1e-4);
            Assert.IsFalse(double.IsNaN(a.OverralAgreement));

            Assert.AreEqual(0.47986, a.ChanceAgreement, 1e-5);
            Assert.IsFalse(double.IsNaN(a.ChanceAgreement));

            Assert.AreEqual(0.586, a.Kappa, 1e-3);
            Assert.IsFalse(double.IsNaN(a.Kappa));

            KappaTest target = new KappaTest(a);


            Assert.AreEqual(0.586, target.Kappa, 1e-3);
            Assert.AreEqual(a.Kappa, target.Kappa);
            Assert.IsFalse(double.IsNaN(target.Kappa));

            Assert.AreEqual(0.00087457, target.Variance, 1e-5);
            Assert.IsFalse(double.IsNaN(target.Variance));

            Assert.AreEqual(19.806, target.Statistic, 0.1);
            Assert.IsFalse(double.IsNaN(target.Statistic));


            Assert.AreEqual(0.644, target.Confidence.Max, 1e-3);
            Assert.AreEqual(0.528, target.Confidence.Min, 1e-3);

            Assert.AreEqual(0.0, target.PValue, 1e-6);
            Assert.IsFalse(double.IsNaN(target.PValue));

            Assert.IsTrue(target.Significant);
        }
Пример #6
0
        public void KappaTestConstructorTest2()
        {
            int[,] matrix =
            {
                { 317,  23,  0, 0 },
                {  61, 120,  0, 0 },
                {   2,   4, 60, 0 },
                {  35,  29,  0, 8 },
            };

            GeneralConfusionMatrix a = new GeneralConfusionMatrix(matrix);

            Assert.AreEqual(0.7663, a.OverralAgreement, 1e-4);
            Assert.IsFalse(double.IsNaN(a.OverralAgreement));

            Assert.AreEqual(0.4087, a.ChanceAgreement, 1e-5);
            Assert.IsFalse(double.IsNaN(a.ChanceAgreement));

            KappaTest target = new KappaTest(a);

            Assert.AreEqual(0.605, target.Kappa, 1e-3);
            Assert.AreEqual(a.Kappa, target.Kappa);
            Assert.IsFalse(double.IsNaN(target.Kappa));

            Assert.AreEqual(0.00073735, target.Variance, 1e-5);
            Assert.IsFalse(double.IsNaN(target.Variance));



            Assert.AreEqual(22.272, target.Statistic, 0.15);
            Assert.IsFalse(double.IsNaN(target.Statistic));


            Assert.AreEqual(0.658, target.Confidence.Max, 1e-3);
            Assert.AreEqual(0.552, target.Confidence.Min, 1e-3);

            Assert.AreEqual(0.0, target.PValue, 1e-6);
            Assert.IsFalse(double.IsNaN(target.PValue));

            Assert.IsTrue(target.Significant);
        }
Пример #7
0
        public void KappaTestConstructorTest()
        {
            int[,] matrix =
            {
                { 44,  5, 1 },
                {  7, 20, 3 },
                {  9,  5, 6 },
            };

            GeneralConfusionMatrix a = new GeneralConfusionMatrix(matrix);

            Assert.AreEqual(0.4915, a.Kappa, 1e-4);
            Assert.IsFalse(double.IsNaN(a.Kappa));

            KappaTest target = new KappaTest(a);

            Assert.AreEqual(0.0777, target.StandardError, 1e-2);
            Assert.IsFalse(double.IsNaN(target.StandardError));

            Assert.AreEqual(0.3393, target.Confidence.Min, 1e-2);
            Assert.AreEqual(0.6437, target.Confidence.Max, 1e-2);
        }
Пример #8
0
        public void KappaTestConstructorTest1()
        {
            // Example from Ientilucci, Emmett (2006). "On Using and Computing the Kappa Statistic".
            // Available on: http://www.cis.rit.edu/~ejipci/Reports/On_Using_and_Computing_the_Kappa_Statistic.pdf

            // This paper uses the delta method approximation
            // for computing the kappa variance.

            int[,] matrix1 =
            {
                { 317,  23,  0, 0 },
                {  61, 120,  0, 0 },
                {   2,   4, 60, 0 },
                {  35,  29,  0, 8 },
            };

            int[,] matrix2 =
            {
                { 377, 79,  0, 0 },
                {   2, 72,  0, 0 },
                {  33,  5, 60, 0 },
                {   3, 20,  0, 8 },
            };

            GeneralConfusionMatrix a = new GeneralConfusionMatrix(matrix1);
            GeneralConfusionMatrix b = new GeneralConfusionMatrix(matrix2);


            Assert.AreEqual(0.7663, a.OverallAgreement, 1e-4);
            Assert.AreEqual(0.7845, b.OverallAgreement, 1e-4);

            Assert.AreEqual(0.4087, a.ChanceAgreement, 1e-4);
            Assert.AreEqual(0.47986, b.ChanceAgreement, 1e-4);

            double kA = a.Kappa;
            double kB = b.Kappa;

            double varA = KappaTest.DeltaMethodKappaVariance(a);
            double varB = KappaTest.DeltaMethodKappaVariance(b);

            TwoMatrixKappaTest target = new TwoMatrixKappaTest(kA, varA, kB, varB);

            Assert.AreEqual(TwoSampleHypothesis.ValuesAreDifferent, target.Hypothesis);
            Assert.AreEqual(DistributionTail.TwoTail, target.Tail);

            // Compare Kappas
            Assert.AreEqual(0.605, target.EstimatedValue1, 1e-3);
            Assert.IsFalse(double.IsNaN(a.Kappa));

            Assert.AreEqual(0.586, target.EstimatedValue2, 1e-3);
            Assert.IsFalse(double.IsNaN(b.Kappa));


            // Compare variances:
            Assert.AreEqual(0.00073735, target.Variance1, 1e-7);
            Assert.IsFalse(double.IsNaN(a.Variance));

            Assert.AreEqual(0.00087457, target.Variance2, 1e-7);
            Assert.IsFalse(double.IsNaN(b.Variance));


            Assert.AreEqual(0.475, target.Statistic, 1e-3);
            Assert.IsFalse(double.IsNaN(target.Statistic));

            Assert.IsFalse(target.Significant);
        }
Пример #9
0
        public void KappaTestConstructorTest2()
        {
            // Example from Congalton

            int[,] matrix1 = // pg 108
            {
                { 65,  4, 22, 24 },
                {  6, 81,  5,  8 },
                {  0, 11, 85, 19 },
                {  4,  7,  3, 90 },
            };

            GeneralConfusionMatrix a = new GeneralConfusionMatrix(matrix1);

            Assert.AreEqual(115, a.RowTotals[0]);
            Assert.AreEqual(100, a.RowTotals[1]);
            Assert.AreEqual(115, a.RowTotals[2]);
            Assert.AreEqual(104, a.RowTotals[3]);

            Assert.AreEqual(75, a.ColumnTotals[0]);
            Assert.AreEqual(103, a.ColumnTotals[1]);
            Assert.AreEqual(115, a.ColumnTotals[2]);
            Assert.AreEqual(141, a.ColumnTotals[3]);



            int[,] matrix2 = // pg 109
            {
                { 45,  4, 12, 24 },
                {  6, 91,  5,  8 },
                {  0,  8, 55,  9 },
                {  4,  7,  3, 55 },
            };


            GeneralConfusionMatrix b = new GeneralConfusionMatrix(matrix2);

            Assert.AreEqual(85, b.RowTotals[0]);
            Assert.AreEqual(110, b.RowTotals[1]);
            Assert.AreEqual(72, b.RowTotals[2]);
            Assert.AreEqual(69, b.RowTotals[3]);

            Assert.AreEqual(55, b.ColumnTotals[0]);
            Assert.AreEqual(110, b.ColumnTotals[1]);
            Assert.AreEqual(75, b.ColumnTotals[2]);
            Assert.AreEqual(96, b.ColumnTotals[3]);

            // Check overall accuracy
            Assert.AreEqual(0.74, a.OverallAgreement, 0.005);
            Assert.AreEqual(0.73, b.OverallAgreement, 0.005);


            double kA = a.Kappa;
            double kB = b.Kappa;

            double varA = KappaTest.DeltaMethodKappaVariance(a);
            double varB = KappaTest.DeltaMethodKappaVariance(b);

            // Create the test
            TwoMatrixKappaTest target = new TwoMatrixKappaTest(kA, varA, kB, varB);

            Assert.AreEqual(TwoSampleHypothesis.ValuesAreDifferent, target.Hypothesis);
            Assert.AreEqual(DistributionTail.TwoTail, target.Tail);

            // Compare Kappas (pg 109)
            Assert.AreEqual(0.65, target.EstimatedValue1, 0.05);
            Assert.IsFalse(double.IsNaN(a.Kappa));

            Assert.AreEqual(0.64, target.EstimatedValue2, 0.05);
            Assert.IsFalse(double.IsNaN(b.Kappa));


            // Compare variances:
            Assert.AreEqual(0.0007778, target.Variance1, 1e-7);
            Assert.IsFalse(double.IsNaN(a.Variance));

            Assert.AreEqual(0.0010233, target.Variance2, 1e-7);
            Assert.IsFalse(double.IsNaN(b.Variance));


            Assert.AreEqual(0.3087, target.Statistic, 1e-5);
            Assert.IsFalse(double.IsNaN(target.Statistic));

            Assert.IsFalse(target.Significant);
        }
Пример #10
0
        /// <summary>
        /// 模拟
        /// </summary>
        public void Simulate()
        {
            this.ChartCountOfTypes = this.landInfo.NumOfLandUseTypes;
            this.ChartCellCountArr = new int[this.ChartCountOfTypes];
            this.InitialChart();

            this.BeginCityCnt = this.GetCityCnt();
            int nowCityCnt = this.BeginCityCnt;

            // 计算lg值
            this.lgBuffer = GetLgBuffer();
            int times    = 0;
            int numOfAll = 0;

            while (times < this.timeOfSimulate)
            {
                if (nowCityCnt > this.EndCityCnt)
                {
                    break;
                }

                // 清空图表存储数组
                this.ClearChartCellCountArr();
                times++;
                int numOfChange = 0;
                #region parallel for
                Parallel.For(0, height * width, pos =>
                {
                    int row = pos / width;
                    int col = pos % width;

                    middleBuffer[pos] = beginBuffer[pos];
                    if (this.landInfo.IsExistInConvertableInfos(this.beginBuffer[pos]))
                    {
                        double globalValue = this.lgBuffer[pos];
                        double localValue  = GetNeighbourAffect(row, col, this.sizeOfNeighbour);
                        double randomValue = Math.Pow(-Math.Log(ThreadLocalRandom.NextDouble()), this.alpha);
                        if (globalValue * localValue * randomValue > threshold)
                        {
                            this.middleBuffer[pos] = this.landInfo.UrbanInfos[0].LandUseTypeValue;
                            numOfChange++;
                            numOfAll++;
                            nowCityCnt++;
                        }
                        // 修改图表计数数组
                    }
                    AddChartCellCountArr(middleBuffer[pos]);
                });
                #endregion
                #region sequential
                //    for (int row = 0; row < height; row++)
                //{
                //    for (int col = 0; col < width; col++)
                //    {
                //        int pos = row * width + col;
                //        this.middleBuffer[pos] = this.beginBuffer[pos];
                //        if (this.landInfo.IsExistInConvertableInfos(this.beginBuffer[pos]))
                //        {
                //            double globalValue = this.lgBuffer[pos];
                //            double localValue = GetNeighbourAffect(row, col, this.sizeOfNeighbour);
                //            double randomValue = Math.Pow(-Math.Log(rnd.NextDouble()), this.alpha);
                //            if (globalValue * localValue * randomValue > threshold)
                //            {
                //                this.middleBuffer[pos] = this.landInfo.UrbanInfos[0].LandUseTypeValue;
                //                numOfChange++;
                //                numOfAll++;
                //                nowCityCnt++;
                //            }
                //            // 修改图表计数数组

                //        }
                //        AddChartCellCountArr(middleBuffer[pos]);

                //    }
                //}
                #endregion
                updateConsoleEvent("-------");
                updateConsoleEvent("当前城市数目: " + nowCityCnt);
                updateConsoleEvent("这次增长了" + numOfChange + "个元胞");
                updateConsoleEvent("-------");
                this.updateImageEvent(this.middleBuffer, this.width, this.height, this.landInfo, times);
                this.updateChartEvent(this.ChartCellCountArr, times++, this.landInfo);
                // 将middle buffer的值复制会begin buffer
                for (int pos = 0; pos < width * height; pos++)
                {
                    this.beginBuffer[pos] = this.middleBuffer[pos];
                }
            }
            this.updateConsoleEvent("模拟结束");
            this.updateConsoleEvent("总共增长了" + numOfAll + "个元胞");

            var kappa    = new KappaTest(this.endBuffer, this.beginBuffer, width, height, landInfo);
            var kappaVal = kappa.GetVal();
            this.updateConsoleEvent("kappa值 : " + kappaVal);
            this.simulateEndEvent(this);
        }
Пример #11
0
        public void Simulate(object obj)
        {
            int nowCityCnt = this.BeginCityCnt;
            int times      = (int)obj;

            // 开始模拟
            int cnt = 0;

            double[] middleBuffer = new double[width * height];
            while (cnt < times)
            {
                int addedCity = 0;
                if (nowCityCnt > this.EndCityCnt)
                {
                    break;
                }

                // 清空图表计数数组
                this.ClearChartCellCountArr();
                #region parallel for
                Parallel.For(0, height * width, pos =>
                {
                    int row = pos / width;
                    int col = pos % width;

                    middleBuffer[pos] = beginBuffer[pos];
                    double type       = beginBuffer[pos];
                    // 只考虑可以转化为城市的栅格
                    if (this.landInfo.IsExistInConvertableInfos(type))
                    {
                        List <double> input = (from buffer in driveBufferList
                                               select buffer[pos]).ToList <double>();
                        input.Add(GetNeighbourAffect(beginBuffer, width, height, row, col, 3));
                        double[] inputArr = input.ToArray <double>();
                        int newType       = func(inputArr);
                        // 如果转化为城市
                        if (newType == 1 && ThreadLocalRandom.NextDouble() < 0.1)
                        {
                            middleBuffer[pos] = this.landInfo.UrbanInfos[0].LandUseTypeValue;
                            addedCity++;
                            nowCityCnt++;
                        }
                    }
                    // 修改图表计数数组
                    AddChartCellCountArr(middleBuffer[pos]);
                });
                #endregion

                #region sequential for
                //for (int row = 0; row < height; row++)
                //{
                //    for (int col = 0; col < width; col++)
                //    {
                //        int pos = row * width + col;
                //        middleBuffer[pos] = beginBuffer[pos];
                //        double type = beginBuffer[pos];
                //        // 只考虑可以转化为城市的栅格
                //        if (this.landInfo.IsExistInConvertableInfos(type))
                //        {
                //            List<double> input = (from buffer in driveBufferList
                //                                  select buffer[pos]).ToList<double>();
                //            input.Add(GetNeighbourAffect(beginBuffer, width, height, row, col, 3));
                //            double[] inputArr = input.ToArray<double>();
                //            int newType = func(inputArr);
                //            // 如果转化为城市
                //            if (newType == 1 && rnd.NextDouble() < 0.1)
                //            {
                //                middleBuffer[pos] = this.landInfo.UrbanInfos[0].LandUseTypeValue;
                //                addedCity++;
                //                nowCityCnt++;
                //            }
                //        }
                //        // 修改图表计数数组
                //        AddChartCellCountArr(middleBuffer[pos]);
                //    }
                //}
                #endregion

                // 将结果复制会outputBuffer
                for (int pos = 0; pos < width * height; pos++)
                {
                    beginBuffer[pos] = middleBuffer[pos];
                }
                updateConsoleEvent("-------");
                updateConsoleEvent("当前城市数目:" + nowCityCnt);
                updateChartEvent(ChartCellCountArr, cnt + 1, landInfo);
                updateImageEvent(beginBuffer, width, height, this.landInfo, cnt); // 通知主线程绘图
                updateConsoleEvent("新增: " + addedCity);                           // 通知主线程输出控制台消息
                cnt++;
            }
            updateConsoleEvent("模拟结束");
            var kappa    = new KappaTest(this.endBuffer, this.beginBuffer, width, height, landInfo);
            var kappaVal = kappa.GetVal();
            this.updateConsoleEvent("kappa值 : " + kappaVal);
        }
Пример #12
0
        /// <summary>
        /// 模拟
        /// </summary>
        public void Simulate(int times)
        {
            updateConsoleEvent("--------- 模拟开始");
            updateConsoleEvent("初始城市数目:" + this.BeginCityCnt);
            updateConsoleEvent("目标城市数目:" + this.EndCityCnt);
            updateConsoleEvent("---------");

            Random rnd = new Random();
            int    cnt = 0;
            int    numOfChangesOneTime = 0;

            double[] middleBuffer = new double[width * height];

            int nowCityCnt = this.BeginCityCnt;

            while (cnt < times)
            {
                if (nowCityCnt > this.EndCityCnt)
                {
                    break;
                }
                // 清空图表存储数组
                this.ClearChartCellCountArr();

                #region parallel for
                Parallel.For(0, height * width, pos =>
                {
                    int row = pos / width;
                    int col = pos % width;

                    middleBuffer[pos] = beginBuffer[pos];

                    // null value 跳过
                    if (beginBuffer[pos] < 0 || Math.Abs(beginBuffer[pos] - this.landInfo.NullInfo.LandUseTypeValue) < Double.Epsilon)
                    {
                        return;
                    }
                    var input = getOneInput(new Cell()
                    {
                        row = row, col = col
                    });
                    double[] answer = this.network.Compute(input);
                    int actual;
                    double max = answer.Max(out actual);

                    //double ra = (double)(1 + Math.Pow(-Math.Log(ThreadLocalRandom.NextDouble(), Math.E), this.alpha)); // -------todo 这个随机数太大了??
                    double ra = ThreadLocalRandom.NextDouble();
                    if (max * ra > this.threshold)
                    {
                        // 转换控制
                        int srcIdx = 0;
                        for (int i = 0; i < this.landInfo.NumOfLandUseTypes; i++)
                        {
                            if (middleBuffer[pos] == this.landInfo.AllTypes[i].LandUseTypeValue)
                            {
                                srcIdx = i;
                            }
                        }
                        if (this.TransformControlMatrix[srcIdx, actual] == 0)
                        {
                            // 不转换
                            AddChartCellCountArr(middleBuffer[pos]);
                            return;
                        }

                        middleBuffer[pos] = landInfo.AllTypes[actual].LandUseTypeValue;

                        if (landInfo.AllTypes[actual].LandUseTypeValue != beginBuffer[pos])
                        {
                            if (actual == this.landInfo.UrbanIndex)
                            {
                                nowCityCnt++;
                            }
                            numOfChangesOneTime++;
                        }
                    }
                    // 修改图表计数数组
                    AddChartCellCountArr(middleBuffer[pos]);
                });
                #endregion
                #region sequential for
                //for (int row = 0; row < height; row++)
                //{

                //    for (int col = 0; col < width; col++)
                //    {
                //        int pos = row * width + col;
                //        middleBuffer[pos] = beginBuffer[pos];
                //        // null value 跳过
                //        if (beginBuffer[pos] < 0 || Math.Abs(beginBuffer[pos] - this.landInfo.NullInfo.LandUseTypeValue) <Double.Epsilon)
                //        {

                //            continue;
                //        }
                //        var input = getOneInput(new Cell() { row = row, col = col });
                //        double[] answer = this.network.Compute(input);
                //        int actual;
                //        double max = answer.Max(out actual);

                //        double ra = (double)(1 + Math.Pow(-Math.Log(rnd.NextDouble(), Math.E), this.alpha)); -------todo 这个随机数太大了??
                //        //double ra = rnd.NextDouble();
                //        if (max * ra > this.threshold)
                //        {
                //            // 转换控制
                //            int srcIdx = 0;
                //            for (int i = 0; i < this.landInfo.NumOfLandUseTypes; i++)
                //            {
                //                if (middleBuffer[pos] == this.landInfo.AllTypes[i].LandUseTypeValue)
                //                {
                //                    srcIdx = i;
                //                }
                //            }
                //            if (this.TransformControlMatrix[srcIdx, actual] == 0)
                //            {
                //                // 不转换
                //                AddChartCellCountArr(middleBuffer[pos]);
                //                continue;
                //            }

                //            middleBuffer[pos] = landInfo.AllTypes[actual].LandUseTypeValue;

                //            if (landInfo.AllTypes[actual].LandUseTypeValue != beginBuffer[pos])
                //            {
                //                if (actual == this.landInfo.UrbanIndex)
                //                {
                //                    nowCityCnt++;
                //                }
                //                numOfChangesOneTime++;
                //            }

                //        }
                //        // 修改图表计数数组
                //        AddChartCellCountArr(middleBuffer[pos]);
                //    }
                //}
                #endregion

                this.beginBuffer = middleBuffer;
                updateConsoleEvent("------");
                updateConsoleEvent("当前城市数目:" + nowCityCnt);
                updateImageEvent(this.beginBuffer, width, height, this.landInfo, cnt);  // draw
                updateChartEvent(this.ChartCellCountArr, cnt + 1, landInfo);
                middleBuffer = new double[width * height];
                updateConsoleEvent("转化了:" + numOfChangesOneTime);
                updateConsoleEvent("------");
                numOfChangesOneTime = 0;
                cnt++;
            }
            this.updateConsoleEvent("---------------模拟结束------------");
            var kappa    = new KappaTest(this.endBuffer, this.beginBuffer, width, height, landInfo);
            var kappaVal = kappa.GetVal();
            this.updateConsoleEvent("kappa值 : " + kappaVal);
        }
Пример #13
0
        /// <summary>
        /// 模拟
        /// </summary>
        public void Simulate(int times)
        {
            int nowCityCnt          = this.nowCityCount;
            int cnt                 = 0;
            int numOfChangesOneTime = 0;

            double[] middleBuffer = new double[width * height];
            while (cnt < times)
            {
                if (this.nowCityCount > this.targetCityCnt)
                {
                    break;
                }

                // 清空图表存储数组
                this.ClearChartCellCountArr();



                #region parallel for
                Parallel.For(0, height * width, pos =>
                {
                    int row = pos / width;
                    int col = pos % width;

                    middleBuffer[pos] = beginBuffer[pos];

                    if (!IsValid(pos)) // 无效值 跳过
                    {
                        return;
                    }

                    if (middleBuffer[pos] == this.landInfo.WaterInfo.LandUseTypeValue) // 水体跳过
                    {
                        AddChartCellCountArr(middleBuffer[pos]);
                        return;
                    }
                    if (this.landInfo.IsExistInUrbanInfos(middleBuffer[pos])) // 城市跳过
                    {
                        AddChartCellCountArr(middleBuffer[pos]);
                        return;
                    }

                    var input = getOneInput(new Cell()
                    {
                        row = row, col = col
                    });

                    double[] propabilities = new double[this.landInfo.AllTypes.Count];
                    alglib.dfprocess(this.Forest, input, ref propabilities);
                    for (int i = 0; i < propabilities.Length; i++)
                    {
                        double ra        = (double)(1 + Math.Pow(-Math.Log(ThreadLocalRandom.NextDouble(), Math.E), this.alpha));
                        double neighbour = GetNeighbourAffect(row, col, this.landInfo.AllTypes[i].LandUseTypeValue, this.sizeOfNeighbour);
                        propabilities[i] = propabilities[i] * ra * neighbour;
                    }

                    int idx = getIdxFromPropArr(propabilities);

                    if (idx == this.landInfo.UrbanIndex)
                    {
                        // Interlocked.Add(ref nowCityCnt, 1);
                        // this.nowCityCount++;
                        nowCityCnt++;
                    }

                    if (this.landInfo.AllTypes[idx].LandUseTypeValue == this.landInfo.WaterInfo.LandUseTypeValue) // target 不能为水体
                    {
                        AddChartCellCountArr(middleBuffer[pos]);
                        return;
                    }



                    // 转换控制
                    int srcIdx = 0;
                    for (int i = 0; i < this.landInfo.NumOfLandUseTypes; i++)
                    {
                        if (middleBuffer[pos] == this.landInfo.AllTypes[i].LandUseTypeValue)
                        {
                            srcIdx = i;
                        }
                    }
                    if (this.transformControlMatrix[srcIdx, idx] == 0)
                    {
                        // 不转换
                        AddChartCellCountArr(middleBuffer[pos]);
                        return;
                    }

                    middleBuffer[pos] = this.landInfo.AllTypes[idx].LandUseTypeValue;
                    if (middleBuffer[pos] != this.beginBuffer[pos])
                    {
                        numOfChangesOneTime++;
                    }
                    // 修改图表计数数组
                    AddChartCellCountArr(middleBuffer[pos]);
                });
                #endregion



                this.nowCityCount = nowCityCnt;
                this.updateConsoleEvent("---当前城市数目: " + this.nowCityCount);
                this.beginBuffer = middleBuffer;
                if (updateImageEvent != null)
                {
                    this.updateImageEvent(this.beginBuffer, this.width, this.height, this.landInfo, cnt);
                }
                this.updateChartEvent(this.ChartCellCountArr, cnt + 1, landInfo);

                middleBuffer = new double[width * height];
                if (updateConsoleEvent != null)
                {
                    this.updateConsoleEvent("转化了:" + numOfChangesOneTime);
                }

                numOfChangesOneTime = 0;
                cnt++;
            }
            if (updateConsoleEvent != null)
            {
                this.updateConsoleEvent("模拟结束");
            }
            var kappa    = new KappaTest(this.endBuffer, this.beginBuffer, width, height, landInfo);
            var kappaVal = kappa.GetVal();
            this.updateConsoleEvent("kappa值 : " + kappaVal);
        }