public void DoSimulation() { //0准备开始模拟 ResourceManager resourceManager = VariableMaintainer.CurrentResourceManager; FormLogisticCAWizard formLogisticCAWizard = VariableMaintainer.CurrentFormLogisticCAWizard; Random random = new Random(); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); //0.1获得数据 int rowCount = formLogisticCAWizard.CurrentStructRasterMetaData.RowCount; int columnCount = formLogisticCAWizard.CurrentStructRasterMetaData.ColumnCount; float[,] simulationStartImage = formLogisticCAWizard.SimulationStartImage; float[,] simulationEndImage = formLogisticCAWizard.SimulationEndImage; float[,] simulationImage = formLogisticCAWizard.SimulationImage; //0.2计算初始每种土地利用类型的单元数量 List <StructLanduseInfoAndCount> listLanduseInfoAndCount; //记录每种土地利用类型的单元数 listLanduseInfoAndCount = new List <StructLanduseInfoAndCount>(); StructLanduseInfoAndCount landuseInfoAndCount; foreach (StructLanduseInfo structLanduseInfo in formLogisticCAWizard.LandUseClassificationInfo.AllTypes) { landuseInfoAndCount = new StructLanduseInfoAndCount(); landuseInfoAndCount.structLanduseInfo = structLanduseInfo; landuseInfoAndCount.LanduseTypeCount = 0; listLanduseInfoAndCount.Add(landuseInfoAndCount); } for (int i = 0; i < rowCount; i++) { for (int j = 0; j < columnCount; j++) { CommonLibrary.GeneralOpertor.ChangeLandUseCount(simulationStartImage[i, j], -10000, listLanduseInfoAndCount); } } //0.3显示输出结果窗体和图表窗体 dockableWindowGraphy.GraphTitle = resourceManager.GetString("String40"); dockableWindowGraphy.XAxisTitle = resourceManager.GetString("String41"); dockableWindowGraphy.YAxisTitle = resourceManager.GetString("String42"); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String43")); Application.DoEvents(); //0.4绘制初始的图表 List <string> listPointPairListName = new List <string>(); List <string> notToDrawList = new List <string>(); notToDrawList.Add(resourceManager.GetString("String44")); dockableWindowGraphy.CreateGraph(listLanduseInfoAndCount, notToDrawList, out listPointPairListName); dockableWindowGraphy.RefreshGraph(); //1.计算Pg float[,] pg = new float[rowCount, columnCount]; for (int i = 0; i < rowCount; i++) { for (int j = 0; j < columnCount; j++) { if (simulationStartImage[i, j] == formLogisticCAWizard.LandUseClassificationInfo.NullValue.LanduseTypeValue) { pg[i, j] = formLogisticCAWizard.LandUseClassificationInfo.NullValue.LanduseTypeValue; } else { pg[i, j] += Convert.ToSingle(formLogisticCAWizard.Coefficents[0]); for (int k = 0; k < formLogisticCAWizard.ListVariableLayersName.Count; k++) { pg[i, j] += Convert.ToSingle(formLogisticCAWizard.Coefficents[k + 1]) * formLogisticCAWizard.VaribaleImages[k][i, j]; } } pg[i, j] = 1 / (1 + Convert.ToSingle(Math.Exp(-1 * pg[i, j]))); } } //GeneralOpertor.WriteDataFloat(formLogisticCAWizard.OutputFolder + @"\pg.txt", pg, rowCount, columnCount); int convertedCellCount = 0; //模拟中总共转换的元胞数量 List <float> neighbours; //Moore领域的元胞 float omega; //扩散系数 float conSuitable; //适应度函数 float maxPct; //每次迭代时最大的Pct值 float[,] pct; float[,] pst; float sumPtt = 0; //Ptt的总和 int randomRow, randomColumn; //Monte Carlo方法选取的随机行和随机列 int convertCountInOneIteration = formLogisticCAWizard.ConvertCount / formLogisticCAWizard.Iterations; //每次迭代应转换的数量 int convertCountOnce = 0; //每次迭代已经转换的数量 float oldValue, newValue; //每次转换前土地利用类型的新值和旧值 int[,] changeCells = new int[rowCount, columnCount]; //记录每次转换为城市的单元。 int iteration = 0; //迭代的次数 //2.开始进行转换 while (convertedCellCount < formLogisticCAWizard.ConvertCount) { if (convertedCellCount % formLogisticCAWizard.OutputImageInterval == 0 && convertedCellCount != 0) { changeCells = new int[rowCount, columnCount]; } convertCountOnce = 0; //2.1每次迭代先得到Pct和MaxPct maxPct = float.MinValue; pct = new float[rowCount, columnCount]; for (int i = 0; i < rowCount; i++) { for (int j = 0; j < columnCount; j++) { neighbours = CommonLibrary.GeneralOpertor.GetMooreNeighbors(i, j, simulationImage, rowCount, columnCount); omega = Convert.ToSingle(CommonLibrary.GeneralOpertor.GetUrbanCount(neighbours, formLogisticCAWizard.LandUseClassificationInfo.UrbanValues[0].LanduseTypeValue)) / (neighbours.Count - 1); if (formLogisticCAWizard.LandUseClassificationInfo.IsExistInConvertValue(simulationImage[i, j])) { conSuitable = 1f; } else { conSuitable = 0f; } pct[i, j] = pg[i, j] * conSuitable * omega; if (pct[i, j] > maxPct) { maxPct = pct[i, j]; } } } //CommonOperator.WriteData(Application.StartupPath + @"\\OutputData\pct.txt", pct, structAsciiImageFileMetaData); //2.2再得到Pst pst = new float[rowCount, columnCount]; sumPtt = 0; //2.2.1先得到ptt和Ptt的总和 for (int i = 0; i < rowCount; i++) { for (int j = 0; j < columnCount; j++) { pst[i, j] = pct[i, j] * Convert.ToSingle(Math.Exp(-1f * formLogisticCAWizard.Delta * (1f - pct[i, j] / maxPct))); sumPtt += pst[i, j]; } } for (int i = 0; i < rowCount; i++) { for (int j = 0; j < columnCount; j++) { pst[i, j] = convertCountInOneIteration * pst[i, j] / sumPtt; } } //CommonOperator.WriteData(Application.StartupPath + @"\\OutputData\pst.txt", pst, structAsciiImageFileMetaData); //2.3选择元胞与随机比较进行转换 //完成一次迭代 while (convertCountOnce < convertCountInOneIteration) { randomRow = random.Next(0, rowCount); randomColumn = random.Next(0, columnCount); if (pst[randomRow, randomColumn] > Convert.ToSingle(random.NextDouble())) { oldValue = simulationImage[randomRow, randomColumn]; newValue = formLogisticCAWizard.LandUseClassificationInfo.UrbanValues[0].LanduseTypeValue; simulationImage[randomRow, randomColumn] = newValue; CommonLibrary.GeneralOpertor.ChangeLandUseCount(newValue, oldValue, listLanduseInfoAndCount); convertCountOnce++; convertedCellCount++; changeCells[randomRow, randomColumn] = 1; } } iteration++; //2.4.刷新外部界面并输出中间结果数据 if (convertedCellCount == 1 || (iteration % formLogisticCAWizard.RefreshInterval == 0 && convertedCellCount != 0)) { //刷新图像 formLogisticCAWizard.SimulationImage = simulationImage; VariableMaintainer.IsNeedRefresh = true; //刷新图表窗体 string landuseTypeName = ""; for (int k = 0; k < listLanduseInfoAndCount.Count; k++) { for (int l = 0; l < listPointPairListName.Count; l++) { if (System.Globalization.CultureInfo.CurrentCulture.Parent.Name == "zh-CHS") { landuseTypeName = listLanduseInfoAndCount[k].structLanduseInfo.LanduseTypeChsName; } else if (System.Globalization.CultureInfo.CurrentCulture.Parent.Name == "zh-CHT") { landuseTypeName = listLanduseInfoAndCount[k].structLanduseInfo.LanduseTypeChtName; } else { landuseTypeName = listLanduseInfoAndCount[k].structLanduseInfo.LanduseTypeEnName; } if (landuseTypeName == listPointPairListName[l]) { dockableWindowGraphy.UpdateData(iteration, listLanduseInfoAndCount[k].LanduseTypeCount, l); } } } dockableWindowGraphy.RefreshGraph(); //刷新输出结果窗体 dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String45") + iteration.ToString() + resourceManager.GetString("String46")); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String47") + convertedCellCount.ToString()); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.ScrollTextbox(); Application.DoEvents(); } //输出中间结果 if (formLogisticCAWizard.IsOutput && (iteration % formLogisticCAWizard.OutputImageInterval == 0)) { GeneralOpertor.WriteDataFloat(formLogisticCAWizard.OutputFolder + @"\" + GeneralOpertor.GetNowString() + "_lr_iterate_" + iteration.ToString() + @".txt", simulationImage, rowCount, columnCount); } } //3.完成模拟,输出结果。 stopWatch.Stop(); VariableMaintainer.IsSimulationFinished = true; dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String48")); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String49") + GeneralOpertor.GetElapsedTimeString(stopWatch.Elapsed)); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText("\n"); //修改结果栅格的属性表 IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass(); IRasterWorkspace rasterWorkspace = workspaceFactory.OpenFromFile( VariableMaintainer.CurrentFormLogisticCAWizard.OutputFolder, 0) as IRasterWorkspace; IRasterDataset rasterDataset = rasterWorkspace.OpenRasterDataset(VariableMaintainer.CurrentFoucsMap.get_Layer(0).Name); IRasterDatasetEdit3 rasterDatasetEdit3 = rasterDataset as IRasterDatasetEdit3; rasterDatasetEdit3.BuildAttributeTable(); IRasterDataset3 rasterDataset3 = rasterDataset as IRasterDataset3; rasterDataset3.Refresh(); if (formLogisticCAWizard.SimulationEndImageName != "") { //GeneralOpertor.WriteDataFloat(formLogisticCAWizard.OutputFolder + @"\CA_LR_Reslut" + GeneralOpertor.GetNowString() + ".txt", // simulationImage, rowCount, columnCount); StructBinaryConfusionMatrix structConfusionMatrix = GeneralOpertor.GetBinaryAccuracy( simulationImage, simulationEndImage, rowCount, columnCount, formLogisticCAWizard.LandUseClassificationInfo); string accuracyString = GeneralOpertor.GetBinaryAccuracyReportString(structConfusionMatrix, convertedCellCount); dockableWindowOutput.AppendText(accuracyString); DataTable dtMatrixNumber = GeneralOpertor.GetMultiTypesMatrix( simulationImage, simulationEndImage, rowCount, columnCount, formLogisticCAWizard.LandUseClassificationInfo); double overallAccuracy = 0d; double kappa = 0d; GeneralOpertor.GetMultiTypesAccuracy(dtMatrixNumber, ref overallAccuracy, ref kappa, formLogisticCAWizard.LandUseClassificationInfo); FormConfusionMatrix formConfusionMatrix = new FormConfusionMatrix(); formConfusionMatrix.DataTableValues = dtMatrixNumber; DataTable dtMatrixPercent = dtMatrixNumber.Clone(); GeneralOpertor.CopyDataTableValues(dtMatrixNumber, dtMatrixPercent); formConfusionMatrix.DataTablePercents = dtMatrixPercent; formConfusionMatrix.DataGridViewConfusionMatrix.DataSource = dtMatrixNumber; formConfusionMatrix.LabelOverallAccuracy.Text = (overallAccuracy * 100).ToString("0.00") + " %"; formConfusionMatrix.LabelKappa.Text = kappa.ToString("0.000"); float[] fomValues = GeneralOpertor.GetBinaryFoMAccuracy(simulationStartImage, simulationEndImage, simulationImage, rowCount, columnCount, formLogisticCAWizard.LandUseClassificationInfo.UrbanValues[0].LanduseTypeValue); formConfusionMatrix.LabelFoMValues.Text = "A: " + fomValues[0] + "\nB: " + fomValues[1] + "\nC: " + fomValues[2] + "\nD: " + fomValues[3]; formConfusionMatrix.LabelFoM.Text = fomValues[4].ToString("0.000"); formConfusionMatrix.LabelPA.Text = fomValues[5].ToString("0.000"); formConfusionMatrix.LabelUA.Text = fomValues[6].ToString("0.000"); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String84")); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(GeneralOpertor.WriteCoufusionMatrix(dtMatrixPercent)); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String83")); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String85") + (overallAccuracy * 100).ToString("0.00") + " %"); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String86") + kappa.ToString("0.000")); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String87") + fomValues[4].ToString("0.000")); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String88") + fomValues[5].ToString("0.000")); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String89") + fomValues[6].ToString("0.000")); dockableWindowOutput.AppendText("\n"); formConfusionMatrix.Text = resourceManager.GetString("String105") + " - " + formLogisticCAWizard.SimulationLayerName; formConfusionMatrix.ShowDialog(); dockableWindowOutput.ScrollTextbox(); Application.DoEvents(); } dockableWindowOutput.AppendText("-------------------------------------------"); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.ScrollTextbox(); Application.DoEvents(); }
public void DoSimulation() { try { //0准备开始模拟 ResourceManager resourceManager = VariableMaintainer.CurrentResourceManager; FormANNCAWizard formANNCAWizard = VariableMaintainer.CurrentFormANNCAWizard; Random random = new Random(); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); //0.1获得数据 int rowCount = formANNCAWizard.CurrentStructRasterMetaData.RowCount; int columnCount = formANNCAWizard.CurrentStructRasterMetaData.ColumnCount; float[,] simulationStartImage = formANNCAWizard.SimulationStartImage; float[,] simulationEndImage = formANNCAWizard.SimulationEndImage; float[,] simulationImage = formANNCAWizard.SimulationImage; //0.2计算初始每种土地利用类型的单元数量 List <StructLanduseInfoAndCount> listLanduseInfoAndCount; //记录每种土地利用类型的单元数 listLanduseInfoAndCount = new List <StructLanduseInfoAndCount>(); StructLanduseInfoAndCount landuseInfoAndCount; foreach (StructLanduseInfo structLanduseInfo in formANNCAWizard.LandUseClassificationInfo.AllTypes) { landuseInfoAndCount = new StructLanduseInfoAndCount(); landuseInfoAndCount.structLanduseInfo = structLanduseInfo; landuseInfoAndCount.LanduseTypeCount = 0; listLanduseInfoAndCount.Add(landuseInfoAndCount); } for (int i = 0; i < rowCount; i++) { for (int j = 0; j < columnCount; j++) { CommonLibrary.GeneralOpertor.ChangeLandUseCount(simulationStartImage[i, j], -10000, listLanduseInfoAndCount); } } //0.3显示输出结果窗体和图表窗体 dockableWindowGraphy.GraphTitle = resourceManager.GetString("String40"); dockableWindowGraphy.XAxisTitle = resourceManager.GetString("String41"); dockableWindowGraphy.YAxisTitle = resourceManager.GetString("String42"); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String43")); Application.DoEvents(); //0.4绘制初始的图表 List <string> listPointPairListName = new List <string>(); List <string> notToDrawList = new List <string>(); notToDrawList.Add(resourceManager.GetString("String44")); dockableWindowGraphy.CreateGraph(listLanduseInfoAndCount, notToDrawList, out listPointPairListName); dockableWindowGraphy.RefreshGraph(); int convertedCellCount = 0; //模拟中总共转换的元胞数量 int randomRow, randomColumn; //Monte Carlo方法选取的随机行和随机列 int convertCountInOneIteration = formANNCAWizard.ConvertCount / formANNCAWizard.Iterations; //每次迭代应转换的数量 int convertCountOnce = 0; //每次迭代已经转换的数量 float oldValue, newValue; //每次转换前土地利用类型的新值和旧值 int iteration = 0; //迭代的次数 List <float> listLanduseValues = new List <float>(); //记录土地利用类型值的List for (int j = 0; j < formANNCAWizard.LandUseClassificationInfo.AllTypesCount; j++) { listLanduseValues.Add(formANNCAWizard.LandUseClassificationInfo.AllTypes[j].LanduseTypeValue); } int neiWindowSize = formANNCAWizard.NeighbourWindowSize; int inputNeuronsCount = formANNCAWizard.InputNeuronsCount; int outputNeuronsCount = formANNCAWizard.OutputNeuronsCount; //2.开始进行转换 while (convertedCellCount < formANNCAWizard.ConvertCount) { convertCountOnce = 0; //2.3选择元胞与随机比较进行转换 //完成一次迭代 while (convertCountOnce < convertCountInOneIteration) { //随机选择一个栅格进行计算 randomRow = random.Next(0, rowCount); randomColumn = random.Next(0, columnCount); //计算逻辑为: //这里需要首先获取当前元胞的输入值 //然后通过神经网络计算每种土地利用类型的概率,乘以随机量 //然后选出最高的概率 //再查看是否是同样的土地利用类型,如果不是则转变 //如果不是并且概率大于一个值,则进行转变(目前先不用) //如果是空值,则不进行计算 if (simulationImage[randomRow, randomColumn] == -9999f) { continue; } double[] tempInputsArray = new double[inputNeuronsCount]; double[] tempOutputsArray = new double[outputNeuronsCount]; //获取该栅格的空间变量值 for (int i = 0; i < formANNCAWizard.ListVaribaleImages.Count; i++) { tempInputsArray[i] = formANNCAWizard.ListVaribaleImages[i][randomRow, randomColumn]; } //获取该栅格的邻域值 NeighbourOperator neighbourOperator = new NeighbourOperator(); float[] counts = neighbourOperator.GetNeighbourCount(simulationImage, randomRow, randomColumn, neiWindowSize, rowCount, columnCount, formANNCAWizard.LandUseClassificationInfo.AllTypesCount, listLanduseValues); for (int z = 0; z < counts.Length; z++) { tempInputsArray[formANNCAWizard.ListVaribaleImages.Count + z] = counts[z] / (neiWindowSize * neiWindowSize - 1); } //获取该栅格的土地利用类型 tempInputsArray[inputNeuronsCount - 1] = simulationImage[randomRow, randomColumn]; //计算神经网络输出的概率值 double[] output = formANNCAWizard.ANN.ANNActivationNetwork.Compute(tempInputsArray); double maxOutput = 0; int maxIndex = -1; Random r = new Random(1); double gamma; double disturbance; double probability; double[] probabilityArray = new double[output.Length]; for (int k = 0; k < output.Length; k++) { gamma = random.NextDouble(); disturbance = 1 + Math.Pow((-Math.Log(gamma)), formANNCAWizard.Delta); probabilityArray[k] = disturbance * output[k]; if (maxOutput < probabilityArray[k]) { maxOutput = probabilityArray[k]; maxIndex = k; } } probability = maxOutput; //计算最高值的最终概率 //未使用邻域因素模拟较为散,所以加入邻域因素。但可能导致难以达到需要的转换数量,模拟时间过长。 double neiValue = 0f; for (int t = 0; t < listLanduseValues.Count; t++) { if (listLanduseValues[t] == formANNCAWizard.LandUseClassificationInfo.UrbanValues[0].LanduseTypeValue) { neiValue = Convert.ToDouble(counts[t]) / (neiWindowSize * neiWindowSize - 1); break; } } probability = probability * neiValue; //得到应转变为的土地利用类型值,以及当前土地利用类型值 newValue = formANNCAWizard.LandUseClassificationInfo.AllTypes[maxIndex].LanduseTypeValue; oldValue = simulationImage[randomRow, randomColumn]; //oldValue有时取到0值等不正确的值 bool isOldValueCorrect = false; for (int w = 0; w < formANNCAWizard.LandUseClassificationInfo.AllTypes.Count; w++) { if (oldValue == formANNCAWizard.LandUseClassificationInfo.AllTypes[w].LanduseTypeValue) { isOldValueCorrect = true; } } if (!isOldValueCorrect) { continue; } //根据转换矩阵进行判断是否可以转换 DataTable dtMatrix = formANNCAWizard.DTMatrix; int oldValueIndex = -1; for (int k = 0; k < formANNCAWizard.LandUseClassificationInfo.AllTypesCount; k++) { if (formANNCAWizard.LandUseClassificationInfo.AllTypes[k].LanduseTypeValue == oldValue) { oldValueIndex = k; break; } } string canConvert = dtMatrix.Rows[oldValueIndex][maxIndex].ToString(); if (canConvert == "0") { continue; } //与设定的阈值进行比较 double convertThreshold = formANNCAWizard.ConvertThreshold; if ((oldValue != newValue) && (probability >= convertThreshold)) { simulationImage[randomRow, randomColumn] = newValue; CommonLibrary.GeneralOpertor.ChangeLandUseCount(newValue, oldValue, listLanduseInfoAndCount); //以城市用地的转变量为总转变量的标准 for (int w = 0; w < formANNCAWizard.LandUseClassificationInfo.UrbanValues.Count; w++) { if (newValue == formANNCAWizard.LandUseClassificationInfo.UrbanValues[w].LanduseTypeValue) { convertCountOnce++; convertedCellCount++; } } //System.Diagnostics.Debug.WriteLine(convertedCellCount + " - Old: " + oldValue + " New: " + newValue); } } iteration++; System.Diagnostics.Debug.WriteLine("iteration: " + iteration); //2.4.刷新外部界面并输出中间结果数据 if (convertedCellCount == 1 || (iteration % formANNCAWizard.RefreshInterval == 0 && convertedCellCount != 0)) { //刷新图像 formANNCAWizard.SimulationImage = simulationImage; VariableMaintainer.IsNeedRefresh = true; //刷新图表窗体 string landuseTypeName = ""; for (int k = 0; k < listLanduseInfoAndCount.Count; k++) { for (int l = 0; l < listPointPairListName.Count; l++) { if (System.Globalization.CultureInfo.CurrentCulture.Parent.Name == "zh-CHS") { landuseTypeName = listLanduseInfoAndCount[k].structLanduseInfo.LanduseTypeChsName; } else if (System.Globalization.CultureInfo.CurrentCulture.Parent.Name == "zh-CHT") { landuseTypeName = listLanduseInfoAndCount[k].structLanduseInfo.LanduseTypeChtName; } else { landuseTypeName = listLanduseInfoAndCount[k].structLanduseInfo.LanduseTypeEnName; } if (landuseTypeName == listPointPairListName[l]) { dockableWindowGraphy.UpdateData(iteration, listLanduseInfoAndCount[k].LanduseTypeCount, l); } } } dockableWindowGraphy.RefreshGraph(); //刷新输出结果窗体 dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String45") + iteration.ToString() + resourceManager.GetString("String46")); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String47") + convertedCellCount.ToString()); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.ScrollTextbox(); Application.DoEvents(); } //输出中间结果 if (formANNCAWizard.IsOutput && (iteration % formANNCAWizard.OutputImageInterval == 0)) { GeneralOpertor.WriteDataFloat(formANNCAWizard.OutputFolder + @"\" + GeneralOpertor.GetNowString() + "_ann_iterate_" + iteration.ToString() + @".txt", simulationImage, rowCount, columnCount); } } //3.完成模拟,输出结果。 stopWatch.Stop(); VariableMaintainer.IsSimulationFinished = true; dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String48")); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String49") + GeneralOpertor.GetElapsedTimeString(stopWatch.Elapsed)); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText("\n"); //修改结果栅格的属性表 IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass(); IRasterWorkspace rasterWorkspace = workspaceFactory.OpenFromFile( VariableMaintainer.CurrentFormANNCAWizard.OutputFolder, 0) as IRasterWorkspace; IRasterDataset rasterDataset = rasterWorkspace.OpenRasterDataset(VariableMaintainer.CurrentFoucsMap.get_Layer(0).Name); IRasterDatasetEdit3 rasterDatasetEdit3 = rasterDataset as IRasterDatasetEdit3; rasterDatasetEdit3.BuildAttributeTable(); IRasterDataset3 rasterDataset3 = rasterDataset as IRasterDataset3; rasterDataset3.Refresh(); if (formANNCAWizard.SimulationEndImageName != "") { //GeneralOpertor.WriteDataFloat(formLogisticCAWizard.OutputFolder + @"\CA_ANN_Reslut" + GeneralOpertor.GetNowString() + ".txt", // simulationImage, rowCount,columnCount); DataTable dtMatrixNumber = GeneralOpertor.GetMultiTypesMatrix( simulationImage, simulationEndImage, rowCount, columnCount, formANNCAWizard.LandUseClassificationInfo); double overallAccuracy = 0d; double kappa = 0d; GeneralOpertor.GetMultiTypesAccuracy(dtMatrixNumber, ref overallAccuracy, ref kappa, formANNCAWizard.LandUseClassificationInfo); FormConfusionMatrix formConfusionMatrix = new FormConfusionMatrix(); formConfusionMatrix.DataTableValues = dtMatrixNumber; DataTable dtMatrixPercent = dtMatrixNumber.Clone(); GeneralOpertor.CopyDataTableValues(dtMatrixNumber, dtMatrixPercent); formConfusionMatrix.DataTablePercents = dtMatrixPercent; formConfusionMatrix.DataGridViewConfusionMatrix.DataSource = dtMatrixNumber; formConfusionMatrix.LabelOverallAccuracy.Text = (overallAccuracy * 100).ToString("0.00") + " %"; formConfusionMatrix.LabelKappa.Text = kappa.ToString("0.000"); float[] fomValues = GeneralOpertor.GetMultiFoMAccuracy(simulationStartImage, simulationEndImage, simulationImage, rowCount, columnCount); formConfusionMatrix.LabelFoMValues.Text = "A: " + fomValues[0] + "\nB: " + fomValues[1] + "\nC: " + fomValues[2] + "\nD: " + fomValues[3]; formConfusionMatrix.LabelFoM.Text = fomValues[4].ToString("0.000"); formConfusionMatrix.LabelPA.Text = fomValues[5].ToString("0.000"); formConfusionMatrix.LabelUA.Text = fomValues[6].ToString("0.000"); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String84")); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(GeneralOpertor.WriteCoufusionMatrix(dtMatrixPercent)); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String83")); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String85") + (overallAccuracy * 100).ToString("0.00") + " %"); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String86") + kappa.ToString("0.000")); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String87") + fomValues[4].ToString("0.000")); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String88") + fomValues[5].ToString("0.000")); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String89") + fomValues[6].ToString("0.000")); dockableWindowOutput.AppendText("\n"); formConfusionMatrix.Text = resourceManager.GetString("String105") + " - " + formANNCAWizard.SimulationLayerName; formConfusionMatrix.ShowDialog(); } dockableWindowOutput.AppendText("-------------------------------------------"); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.ScrollTextbox(); Application.DoEvents(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public void DoSimulation() { //0准备开始模拟 ResourceManager resourceManager = VariableMaintainer.CurrentResourceManager; FormDTCAWizard formDTCAWizard = VariableMaintainer.CurrentFormDTCAWizard; Random random = new Random(); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); //0.1获得数据 int rowCount = formDTCAWizard.CurrentStructRasterMetaData.RowCount; int columnCount = formDTCAWizard.CurrentStructRasterMetaData.ColumnCount; float[,] simulationStartImage = formDTCAWizard.SimulationStartImage; float[,] simulationEndImage = formDTCAWizard.SimulationEndImage; float[,] simulationImage = formDTCAWizard.SimulationImage; //0.2计算初始每种土地利用类型的单元数量 List <StructLanduseInfoAndCount> listLanduseInfoAndCount; //记录每种土地利用类型的单元数 listLanduseInfoAndCount = new List <StructLanduseInfoAndCount>(); StructLanduseInfoAndCount landuseInfoAndCount; foreach (StructLanduseInfo structLanduseInfo in formDTCAWizard.LandUseClassificationInfo.AllTypes) { landuseInfoAndCount = new StructLanduseInfoAndCount(); landuseInfoAndCount.structLanduseInfo = structLanduseInfo; landuseInfoAndCount.LanduseTypeCount = 0; listLanduseInfoAndCount.Add(landuseInfoAndCount); } for (int i = 0; i < rowCount; i++) { for (int j = 0; j < columnCount; j++) { CommonLibrary.GeneralOpertor.ChangeLandUseCount(simulationStartImage[i, j], -10000, listLanduseInfoAndCount); } } //0.3显示输出结果窗体和图表窗体 dockableWindowGraphy.GraphTitle = resourceManager.GetString("String40"); dockableWindowGraphy.XAxisTitle = resourceManager.GetString("String41"); dockableWindowGraphy.YAxisTitle = resourceManager.GetString("String42"); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String43")); Application.DoEvents(); //0.4绘制初始的图表 List <string> listPointPairListName = new List <string>(); List <string> notToDrawList = new List <string>(); notToDrawList.Add(resourceManager.GetString("String44")); dockableWindowGraphy.CreateGraph(listLanduseInfoAndCount, notToDrawList, out listPointPairListName); dockableWindowGraphy.RefreshGraph(); int convertedCellCount = 0; //模拟中总共转换的元胞数量 int randomRow, randomColumn; //Monte Carlo方法选取的随机行和随机列 int convertCountInOneIteration = formDTCAWizard.ConvertCount / formDTCAWizard.Iterations; //每次迭代应转换的数量 int convertCountOnce = 0; //每次迭代已经转换的数量 float oldValue, newValue; //每次转换前土地利用类型的新值和旧值 int iteration = 0; //迭代的次数 //float gamma = 1f / formDTCAWizard.Iterations; //float gamma = 0.4f; DecisionTree decisionTree = formDTCAWizard.CurrentDecisionTree; List <float> listUrbanValues = new List <float>(); for (int j = 0; j < formDTCAWizard.LandUseClassificationInfo.UrbanValues.Count; j++) { listUrbanValues.Add(formDTCAWizard.LandUseClassificationInfo.UrbanValues[j].LanduseTypeValue); } int neiWindowSize = formDTCAWizard.NeighbourWindowSize; try { //2.开始进行转换 while (convertedCellCount < formDTCAWizard.ConvertCount) { convertCountOnce = 0; while (convertCountOnce < convertCountInOneIteration) { //随机选择一个栅格进行计算 randomRow = random.Next(0, rowCount); randomColumn = random.Next(0, columnCount); //计算逻辑为: //这里需要首先获取当前元胞的输入值 //然后通过决策树计算是否发生转变 //然后看随机数是否小于等于预定值,则进行转变 //如果是空值,则不进行计算 if (simulationImage[randomRow, randomColumn] == -9999f) { continue; } //如果模拟影像该栅格是空值,也不进行计算 if (formDTCAWizard.ListVaribaleImages[formDTCAWizard.ListVaribaleImages.Count - 1][randomRow, randomColumn] == -9999f) { continue; } //如果已经是城市用地,则不计算 if (simulationImage[randomRow, randomColumn] == formDTCAWizard.LandUseClassificationInfo.UrbanValues[0].LanduseTypeValue) { continue; } double[] tempInputsArray = new double[formDTCAWizard.ListVaribaleImages.Count]; //获取该栅格的各空间变量值和其他输入变量 for (int i = 0; i < formDTCAWizard.ListVaribaleImages.Count; i++) { tempInputsArray[i] = formDTCAWizard.ListVaribaleImages[i][randomRow, randomColumn]; } //获取该栅格的邻域中城市用地数量 tempInputsArray[formDTCAWizard.ListVaribaleImages.Count - 2] = CommonLibrary.GeneralOpertor.GetNeighbors(randomRow, randomColumn, simulationImage, rowCount, columnCount, neiWindowSize, listUrbanValues); //获取该栅格当前的土地利用类型值 tempInputsArray[formDTCAWizard.ListVaribaleImages.Count - 1] = simulationImage[randomRow, randomColumn]; //计算决策树输出的转换值 int output = formDTCAWizard.CurrentDecisionTree.Compute(tempInputsArray); //如果计算为不转换,则不进行转换 if (output == 0) { continue; } //如果可以转换为城市用地 if (!formDTCAWizard.LandUseClassificationInfo.IsExistInConvertValue(simulationImage[randomRow, randomColumn])) { continue; } Random r = new Random(1); double convertThreshold = random.NextDouble(); //得到应转变为的土地利用类型值,以及当前土地利用类型值 newValue = formDTCAWizard.LandUseClassificationInfo.UrbanValues[0].LanduseTypeValue; oldValue = simulationImage[randomRow, randomColumn]; //未使用邻域因素模拟较为散,所以加入邻域因素 double neighbourValue = tempInputsArray[formDTCAWizard.ListVaribaleImages.Count - 2] / (neiWindowSize * neiWindowSize - 1); //double probability = convertThreshold * neighbourValue; //if ((oldValue != newValue) && (convertThreshold <= gamma)) //if ((oldValue != newValue) && (probability >= gamma)) //20170619添加限制层数据 // if ((VariableMaintainer.RestrictImage[randomRow, randomColumn] == 2) || (VariableMaintainer.RestrictImage[randomRow, randomColumn] == 1)) //if (VariableMaintainer.RestrictImage[randomRow, randomColumn] == 1) // continue; //引入随机因素,如果邻域因素大于随机数 if ((oldValue != newValue) && (neighbourValue > convertThreshold)) { simulationImage[randomRow, randomColumn] = newValue; CommonLibrary.GeneralOpertor.ChangeLandUseCount(newValue, oldValue, listLanduseInfoAndCount); convertCountOnce++; convertedCellCount++; //System.Diagnostics.Debug.WriteLine(convertedCellCount + " - Old: " + oldValue + " New: " + newValue); } } iteration++; //2.4.刷新外部界面并输出中间结果数据 if (convertedCellCount == 1 || (iteration % formDTCAWizard.RefreshInterval == 0 && convertedCellCount != 0)) { //刷新图像 formDTCAWizard.SimulationImage = simulationImage; VariableMaintainer.IsNeedRefresh = true; //刷新图表窗体 string landuseTypeName = ""; for (int k = 0; k < listLanduseInfoAndCount.Count; k++) { for (int l = 0; l < listPointPairListName.Count; l++) { if (System.Globalization.CultureInfo.CurrentCulture.Parent.Name == "zh-CHS") { landuseTypeName = listLanduseInfoAndCount[k].structLanduseInfo.LanduseTypeChsName; } else if (System.Globalization.CultureInfo.CurrentCulture.Parent.Name == "zh-CHT") { landuseTypeName = listLanduseInfoAndCount[k].structLanduseInfo.LanduseTypeChtName; } else { landuseTypeName = listLanduseInfoAndCount[k].structLanduseInfo.LanduseTypeEnName; } if (landuseTypeName == listPointPairListName[l]) { dockableWindowGraphy.UpdateData(iteration, listLanduseInfoAndCount[k].LanduseTypeCount, l); } } } dockableWindowGraphy.RefreshGraph(); //刷新输出结果窗体 dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String45") + iteration.ToString() + resourceManager.GetString("String46")); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String47") + convertedCellCount.ToString()); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.ScrollTextbox(); Application.DoEvents(); } //输出中间结果 if (formDTCAWizard.IsOutput && (iteration % formDTCAWizard.OutputImageInterval == 0)) { GeneralOpertor.WriteDataFloat(formDTCAWizard.OutputFolder + @"\" + GeneralOpertor.GetNowString() + "_ann_iterate_" + iteration.ToString() + @".txt", simulationImage, rowCount, columnCount); } } } catch (Exception ex) { MessageBox.Show("DTCA: " + ex.Message); } //3.完成模拟,输出结果。 stopWatch.Stop(); //isFinished = true; VariableMaintainer.IsSimulationFinished = true; dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String48")); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String49") + GeneralOpertor.GetElapsedTimeString(stopWatch.Elapsed)); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText("\n"); //修改结果栅格的属性表 IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass(); IRasterWorkspace rasterWorkspace = workspaceFactory.OpenFromFile( VariableMaintainer.CurrentFormDTCAWizard.OutputFolder, 0) as IRasterWorkspace; IRasterDataset rasterDataset = rasterWorkspace.OpenRasterDataset(VariableMaintainer.CurrentFoucsMap.get_Layer(0).Name); IRasterDatasetEdit3 rasterDatasetEdit3 = rasterDataset as IRasterDatasetEdit3; rasterDatasetEdit3.BuildAttributeTable(); IRasterDataset3 rasterDataset3 = rasterDataset as IRasterDataset3; rasterDataset3.Refresh(); if (formDTCAWizard.SimulationEndImageName != "") { //GeneralOpertor.WriteDataFloat(formLogisticCAWizard.OutputFolder + @"\CA_ANN_Reslut" + GeneralOpertor.GetNowString() + ".txt", // simulationImage, rowCount,columnCount); StructBinaryConfusionMatrix structConfusionMatrix = GeneralOpertor.GetBinaryAccuracy( simulationImage, simulationEndImage, rowCount, columnCount, formDTCAWizard.LandUseClassificationInfo); string accuracyString = GeneralOpertor.GetBinaryAccuracyReportString(structConfusionMatrix, convertedCellCount); dockableWindowOutput.AppendText(accuracyString); DataTable dtMatrixNumber = GeneralOpertor.GetMultiTypesMatrix( simulationImage, simulationEndImage, rowCount, columnCount, formDTCAWizard.LandUseClassificationInfo); double overallAccuracy = 0d; double kappa = 0d; GeneralOpertor.GetMultiTypesAccuracy(dtMatrixNumber, ref overallAccuracy, ref kappa, formDTCAWizard.LandUseClassificationInfo); FormConfusionMatrix formConfusionMatrix = new FormConfusionMatrix(); formConfusionMatrix.DataTableValues = dtMatrixNumber; DataTable dtMatrixPercent = dtMatrixNumber.Clone(); GeneralOpertor.CopyDataTableValues(dtMatrixNumber, dtMatrixPercent); formConfusionMatrix.DataTablePercents = dtMatrixPercent; formConfusionMatrix.DataGridViewConfusionMatrix.DataSource = dtMatrixNumber; formConfusionMatrix.LabelOverallAccuracy.Text = (overallAccuracy * 100).ToString("0.00") + " %"; formConfusionMatrix.LabelKappa.Text = kappa.ToString("0.000"); float[] fomValues = GeneralOpertor.GetBinaryFoMAccuracy(simulationStartImage, simulationEndImage, simulationImage, rowCount, columnCount, formDTCAWizard.LandUseClassificationInfo.UrbanValues[0].LanduseTypeValue); formConfusionMatrix.LabelFoMValues.Text = "A: " + fomValues[0] + "\nB: " + fomValues[1] + "\nC: " + fomValues[2] + "\nD: " + fomValues[3]; formConfusionMatrix.LabelFoM.Text = fomValues[4].ToString("0.000"); formConfusionMatrix.LabelPA.Text = fomValues[5].ToString("0.000"); formConfusionMatrix.LabelUA.Text = fomValues[6].ToString("0.000"); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String84")); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(GeneralOpertor.WriteCoufusionMatrix(dtMatrixPercent)); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String83")); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String85") + (overallAccuracy * 100).ToString("0.00") + " %"); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String86") + kappa.ToString("0.000")); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String87") + fomValues[4].ToString("0.000")); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String88") + fomValues[5].ToString("0.000")); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText(resourceManager.GetString("String89") + fomValues[6].ToString("0.000")); dockableWindowOutput.AppendText("\n"); formConfusionMatrix.Text = resourceManager.GetString("String105") + " - " + formDTCAWizard.SimulationLayerName; formConfusionMatrix.ShowDialog(); } dockableWindowOutput.AppendText("-------------------------------------------"); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.AppendText("\n"); dockableWindowOutput.ScrollTextbox(); Application.DoEvents(); }