Exemplo n.º 1
0
        void t_Tick(object sender, EventArgs e)
        {
            try
            {
                if (VariableMaintainer.IsNeedRefresh)
                {
                    string       simulationLayerName  = VariableMaintainer.CurrentFoucsMap.get_Layer(0).Name;
                    IRasterLayer simulationImageLayer = new RasterLayerClass();
                    if (VariableMaintainer.CurrentModel == EnumCurrentModel.Simulation_CA_LogisticRegression)
                    {
                        simulationImageLayer.CreateFromFilePath(VariableMaintainer.CurrentFormLogisticCAWizard.OutputFolder + @"\" + simulationLayerName);
                        ArcGISOperator.WriteRaster(simulationImageLayer, VariableMaintainer.CurrentFormLogisticCAWizard.SimulationImage);
                    }
                    else if (VariableMaintainer.CurrentModel == EnumCurrentModel.Simulation_CA_ANN)
                    {
                        simulationImageLayer.CreateFromFilePath(VariableMaintainer.CurrentFormANNCAWizard.OutputFolder + @"\" + simulationLayerName);
                        ArcGISOperator.WriteRaster(simulationImageLayer, VariableMaintainer.CurrentFormANNCAWizard.SimulationImage);
                    }
                    else if (VariableMaintainer.CurrentModel == EnumCurrentModel.Simulation_CA_DT)
                    {
                        simulationImageLayer.CreateFromFilePath(VariableMaintainer.CurrentFormDTCAWizard.OutputFolder + @"\" + simulationLayerName);
                        ArcGISOperator.WriteRaster(simulationImageLayer, VariableMaintainer.CurrentFormDTCAWizard.SimulationImage);
                    }

                    IRasterLayer l       = ArcGISOperator.GetRasterLayerByName(simulationLayerName);
                    IRaster2     raster  = simulationImageLayer.Raster as IRaster2;
                    IPnt         fromPnt = new PntClass();
                    fromPnt.SetCoords(0, 0);
                    IPnt blockSize = new PntClass();
                    blockSize.SetCoords(simulationImageLayer.ColumnCount, simulationImageLayer.RowCount);
                    IPixelBlock pixelBlock = ((IRaster)raster).CreatePixelBlock(blockSize);
                    l.Raster.Read(fromPnt, pixelBlock);
                    ((IRasterEdit)l.Raster).Refresh();

                    IActiveView activeView = VariableMaintainer.CurrentFoucsMap as IActiveView;
                    activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
                    VariableMaintainer.IsNeedRefresh = false;
                }

                if (VariableMaintainer.IsSimulationFinished)
                {
                    VariableMaintainer.CurrentTimer.Stop();
                    VariableMaintainer.IsSimulationFinished = false;
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 更新信息素,同时更新最优状态
        /// </summary>
        /// <param name="Ants"></param>
        private void UpdatePheromoneData(Ant[] Ants)
        {
            for (int i = 0; i < structRasterMetaData.RowCount; i++)
            {
                for (int j = 0; j < structRasterMetaData.ColumnCount; j++)
                {
                    if (suitabilityData[i, j] != structRasterMetaData.NoDataValue)
                    {
                        //计算剩下的信息素
                        pheromoneData[i, j] = (float)(pheromoneData[i, j] * (1 - rho));
                        if (goalUtilityInMicroSearch > goalUtilityInAllIterations)
                        {
                            goalUtilityAntsStatusInAllIterations[i, j] = 0;
                        }
                    }
                }
            }

            double onePheromon = q * goalUtilityInMicroSearch;

            int conNN = 5;

            if (currentIteration > 200 && currentIteration <= 700)
            {
                conNN = 5;                                                      /*Beita=3;*/
            }
            if (currentIteration > 700)
            {
                conNN = 3;                           /*Beita=3;*/
            }

            for (int i = 0; i < Ants.Length; i++)
            {
                int x = Ants[i].X;
                int y = Ants[i].Y;

                //-------------计算增加的信息素-----------------------------------------------
                pheromoneData[x, y] += (float)onePheromon;                                                                                 //更新信息素
                UpdateNeighbour(pheromoneData, conNN, onePheromon, structRasterMetaData.ColumnCount, structRasterMetaData.RowCount, y, x); //更新邻域信息素;
                //-------------------------------------------------------------------------

                //判断是否需要更新最优状态
                if (goalUtilityInMicroSearch > goalUtilityInAllIterations)
                {
                    goalUtilityAntsStatusInAllIterations[x, y] = 1;
                }
            }

            if (goalUtilityInMicroSearch > goalUtilityInAllIterations)
            {
                goalUtilityInAllIterations = goalUtilityInMicroSearch;
                isNeedUpdate             = true;
                needFinishIterationCount = 1;
                currentRefreshIteration += 1;
            }
            else
            {
                needFinishIterationCount += 1;
            }

            currentIteration += 1;
            dockableWindowOutput.AppendText("CurrentInteration=" + currentIteration + "     CurrentRefreshIteration=" + currentRefreshIteration + "     NeedUpdate=" + isNeedUpdate + "     NeedFinishIterationCount=" + needFinishIterationCount + "       GoalUtilityValueInAllIterations=" + goalUtilityInAllIterations + "\r\n");
            dockableWindowOutput.AppendText(("\n"));
            dockableWindowOutput.ScrollTextbox();

            if (currentIteration == 1)
            {
                dockableWindowGraphy.CreateGraph(currentIteration, goalUtilityInAllIterations, "");
                dockableWindowGraphy.RefreshGraph();
            }
            else
            {
                dockableWindowGraphy.UpdateData(currentIteration, goalUtilityInAllIterations, 0);
                dockableWindowGraphy.RefreshGraph();
            }

            float[,] currentOccpied = new float[structRasterMetaData.RowCount, structRasterMetaData.ColumnCount];
            for (int i = 0; i < structRasterMetaData.RowCount; i++)
            {
                for (int j = 0; j < structRasterMetaData.ColumnCount; j++)
                {
                    if (suitabilityData[i, j] == -9999f)
                    {
                        currentOccpied[i, j] = 255;
                    }
                    else
                    {
                        if (occupiedStatus[i, j] == 1)
                        {
                            currentOccpied[i, j] = Convert.ToSingle(occupiedStatus[i, j]);
                        }
                        else
                        {
                            currentOccpied[i, j] = 0;
                        }
                    }
                }
            }

            int mapRefreshInterval = VariableMaintainer.CurrentStructACOParameters.MapRefreshInterval;

            if (mapRefreshInterval == 0)
            {
                mapRefreshInterval = 5;
            }
            if (currentIteration % mapRefreshInterval == 0)
            {
                string       simulationLayerName  = VariableMaintainer.CurrentFoucsMap.get_Layer(0).Name;
                IRasterLayer simulationImageLayer = new RasterLayerClass();
                simulationImageLayer.CreateFromFilePath(VariableMaintainer.DefaultOutputFolder + @"\" + simulationLayerName);
                ArcGISOperator.WriteRaster(simulationImageLayer, currentOccpied);
                IRasterLayer l = ArcGISOperator.GetRasterLayerByName(simulationLayerName);
                ((IRasterEdit)l.Raster).Refresh();

                IActiveView activeView = VariableMaintainer.CurrentFoucsMap as IActiveView;
                activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
            }

            //保存每次迭代的目标值,如果需要保存
            if (isNeedSave == true)
            {
                goalUtilityArray[currentIteration + 1] = currentIteration + "," + currentRefreshIteration + "," + isNeedUpdate + "," + needFinishIterationCount + "," + goalUtilityInAllIterations;
                if (currentIteration == totalItearationCount)
                {
                    string outResult = outputPath + outGoalName;
                    //CommonOperator.writeGoalData(outResult, goalSave);
                }
            }

            //判断是否需要把结果保存成文件
            if (isNeedSave == true && needFinishIterationCount == 1)
            {
                outFileName = outPreFileName + currentRefreshIteration + "_s" + currentIteration + ".txt";
                String outResult = outputPath + outFileName;

                dockableWindowOutput.AppendText("Writing file:" + outResult + "\r\n");
                dockableWindowOutput.ScrollTextbox();

                //CommonOperator.WriteData(outResult, allOptStatus, rows, cols, xllcorner, yllcorner, cellsize, NODATA_value);

                dockableWindowOutput.AppendText("Writing file finished\r\n");
                dockableWindowOutput.ScrollTextbox();
            }
        }
Exemplo n.º 3
0
        public float[,] GetSuitabilityData(string expression)
        {
            string[] splitedExpressionArray;
            bool     isSplited = SplitExpression(expression, out splitedExpressionArray);

            if (isSplited == false)
            {
                MessageBox.Show("Suitibility Function Express Error", VariableMaintainer.CurrentResourceManager.GetString("String2"),
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return(null);
            }
            //根据获取的文件名字符串去读取相应的栅格文件
            List <float[, ]> listLayers = new List <float[, ]>();

            structRasterMetaData = new StructRasterMetaData();
            for (int i = 0; i < splitedExpressionArray.Length; i++)
            {
                if (i > 0 && i % 2 > 0)
                {
                    string layerName = splitedExpressionArray[i];
                    if (i == 1)
                    {
                        listLayers.Add(ArcGISOperator.ReadRasterAndGetMetaData(
                                           ArcGISOperator.GetRasterLayerByName(layerName), out structRasterMetaData));
                    }
                    else
                    {
                        listLayers.Add(ArcGISOperator.ReadRaster(ArcGISOperator.GetRasterLayerByName(layerName),
                                                                 structRasterMetaData.NoDataValue));
                    }
                }
            }
            if (listLayers.Count == 0)
            {
                MessageBox.Show("Please select layers for calculation!", VariableMaintainer.CurrentResourceManager.GetString("String2"),
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return(null);
            }
            //读取全部文件后进行计算,生成适宜性图层数据,并进行归一化
            float[,] suitabilityData = new float[structRasterMetaData.RowCount, structRasterMetaData.ColumnCount];
            float      min = 1, max = 0;
            Expression ex = new Expression("1+1");

            //MathParser mathParser = new MathParser();
            for (int i = 0; i < structRasterMetaData.RowCount; i++)
            {
                for (int j = 0; j < structRasterMetaData.ColumnCount; j++)
                {
                    bool isNullData = false;
                    for (int k = 0; k < listLayers.Count; k++)
                    {
                        if (listLayers[k][i, j] == structRasterMetaData.NoDataValue)
                        {
                            isNullData = true;
                            break;
                        }
                    }
                    if (isNullData)
                    {
                        suitabilityData[i, j] = structRasterMetaData.NoDataValue;
                    }
                    else
                    {
                        string calculateExpression = "";
                        for (int w = 0; w < splitedExpressionArray.Length; w++)
                        {
                            if (w > 0 && w % 2 == 1)
                            {
                                calculateExpression += listLayers[w / 2][i, j];
                            }
                            else
                            {
                                calculateExpression += splitedExpressionArray[w];
                            }
                        }
                        ex = new Expression(calculateExpression);
                        suitabilityData[i, j] = Convert.ToSingle(ex.Evaluate());
                        //suitabilityData[i, j] = Convert.ToSingle(mathParser.Parse(calculateExpression, false));
                        if (suitabilityData[i, j] < min)
                        {
                            min = suitabilityData[i, j];
                        }
                        if (suitabilityData[i, j] > max)
                        {
                            max = suitabilityData[i, j];
                        }
                    }
                }
            }
            float[,] suitabilityData2 = new float[structRasterMetaData.RowCount, structRasterMetaData.ColumnCount];
            for (int i = 0; i < structRasterMetaData.RowCount; i++)
            {
                for (int j = 0; j < structRasterMetaData.ColumnCount; j++)
                {
                    if (suitabilityData[i, j] == structRasterMetaData.NoDataValue)
                    {
                        suitabilityData2[i, j] = structRasterMetaData.NoDataValue;
                    }
                    else
                    {
                        suitabilityData2[i, j] = (suitabilityData[i, j] - min) / (max - min);
                    }
                }
            }

            //将其他图层关闭,以免影响蚁群优化的刷新效果
            for (int i = 0; i < VariableMaintainer.CurrentFoucsMap.LayerCount; i++)
            {
                if (VariableMaintainer.CurrentFoucsMap.get_Layer(i).Visible)
                {
                    VariableMaintainer.CurrentFoucsMap.get_Layer(i).Visible = false;
                }
            }
            //显示适宜性图层
            string dateTime   = GeneralOpertor.GetDataTimeFullString(DateTime.Now);
            string rasterName = "suitability" + dateTime + ".img";

            ArcMap.Application.StatusBar.set_Message(0, VariableMaintainer.CurrentResourceManager.GetString("String169"));
            VariableMaintainer.CurrentDockableWindowOutput.AppendText(VariableMaintainer.CurrentResourceManager.GetString("String169") + rasterName + ".....\n");
            VariableMaintainer.CurrentDockableWindowOutput.AppendText("\n");

            IRasterLayer templateImageLayer = ArcGISOperator.GetRasterLayerByName(
                splitedExpressionArray[1].ToString());
            IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass();
            IWorkspace        workspace        = workspaceFactory.OpenFromFile(VariableMaintainer.DefaultOutputFolder, 0);
            ISaveAs2          saveAs2          = (ISaveAs2)templateImageLayer.Raster;

            saveAs2.SaveAs(rasterName, workspace, "IMAGINE Image");
            IRasterLayer sutiabilityImageLayer = new RasterLayerClass();

            sutiabilityImageLayer.CreateFromFilePath(VariableMaintainer.DefaultOutputFolder + @"\" + rasterName);
            //sutiabilityImageLayer.Renderer = templateImageLayer.Renderer;
            ArcGISOperator.FoucsMap.AddLayer((ILayer)sutiabilityImageLayer);

            IRasterLayer simulationImageLayer = new RasterLayerClass();

            simulationImageLayer.CreateFromFilePath(VariableMaintainer.DefaultOutputFolder + @"\" + rasterName);
            ArcGISOperator.WriteRaster(simulationImageLayer, suitabilityData2);
            IRasterLayer l       = ArcGISOperator.GetRasterLayerByName(rasterName);
            IRaster2     raster  = simulationImageLayer.Raster as IRaster2;
            IPnt         fromPnt = new PntClass();

            fromPnt.SetCoords(0, 0);
            IPnt blockSize = new PntClass();

            blockSize.SetCoords(simulationImageLayer.ColumnCount, simulationImageLayer.RowCount);
            IPixelBlock pixelBlock = ((IRaster)raster).CreatePixelBlock(blockSize);

            l.Raster.Read(fromPnt, pixelBlock);
            ((IRasterEdit)l.Raster).Refresh();

            IActiveView activeView = VariableMaintainer.CurrentFoucsMap as IActiveView;

            activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
            return(suitabilityData2);
        }