Example #1
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);
        }
Example #2
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);
        }
Example #3
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);
        }
Example #4
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);
        }