public float RunSiriusModel(string calibrationRoundID, string dataDirectory, string workDirectory, string projectDirectory, string genericAlgorithmsDirectory, ArrayList observationArray, ArrayList stdArray, int[] kParameterSiteIndex, int amplificationFactor, double CV, ArrayList observationDateArray, ArrayList observationSeperator) { #region Input variables //Assign the random values generated above to each of the parameters. int observationNumber = observationArray.Count; //Get the number of observations. string outputFileName = "ModelOutput"; string outputFileExtention = ".txt"; string wholeOutputFileName = workDirectory + genericAlgorithmsDirectory + outputFileName + outputFileExtention;//Assign the path of the text file to save the model outputs concerned. File.WriteAllText(wholeOutputFileName, ""); //Create a txt file to save model outputs concerned and clean up the output contents of last model run before a new run. #endregion #region Run the model AutomaticModelRun.ModelRun( TheArray, calibrationRoundID, dataDirectory, workDirectory, projectDirectory, wholeOutputFileName, kParameterSiteIndex, observationDateArray, observationSeperator ); //Call the AutomaticModelRun statis class to run the model. #endregion #region Read model outputs List <double> modelOutputs = new List <double>(); StreamReader SR = File.OpenText(wholeOutputFileName); string fileLineContent = null; modelOutputs.Clear(); while ((fileLineContent = SR.ReadLine()) != null) { modelOutputs.Add(Convert.ToDouble(fileLineContent)); } SR.Close(); //Read the model outputs saved during the model run and save them in a list. #endregion #region Fitness calculation // fitness for the closest predictions to observations. predictionArray.Clear(); for (int i = 0; i < observationNumber; i++) { predictionArray.Add(modelOutputs[i]); } //Prediction of anthesis dates and FLN of the hight nitrogen treatment at Clermont-Ferrand in 2007 and 2008 in order. //Declare an array to save the predictions concerned, e.g. anthesis date, final leaf number, LAI, etc. float[] fitnessValues = new float[observationNumber]; float CurrentFitness = 1; for (int i = 0; i < observationNumber; i++) { double observationValue = Convert.ToDouble(observationArray[i]); double stdValue = Convert.ToDouble(stdArray[i]); double predictionValue = Convert.ToDouble(predictionArray[i]); //Console.WriteLine(amplificationFactor); if (observationValue != -999 & predictionValue != -999) { //fitnessValues[i] = (float)(Math.Exp(-Math.Abs((float)Math.Pow((observationValue - predictionValue), 2) / (2 * (float)Math.Pow((stdValue* amplificationFactor), 2))))); fitnessValues[i] = (float)(Math.Exp(-Math.Abs((float)Math.Pow((observationValue - predictionValue), 2) / (2 * (float)Math.Pow((observationValue * CV), 2))))) * amplificationFactor; //The CV was modified to a variable for different sub-models since the fitness values are hard to calculate for many observation points. } else { fitnessValues[i] = 1; } //Console.WriteLine("***"); //Console.WriteLine(fitnessValues[i]); CurrentFitness = CurrentFitness * fitnessValues[i]; } Console.WriteLine("The fitness value is {0}", Math.Round(CurrentFitness, 15)); return(CurrentFitness); #endregion }
static void Main(string[] args) { Console.WriteLine("Global sensitivity analysis is starting, please wait...");//The procedure of GSA is starting. #region Working directory of the model. //int numberOfRandomLines = 860; //int numberOfParameters = 85; //For 85 parameters. int numberOfRandomLines = 770; int numberOfParameters = 76;//For 76 parameters. string projectDirectory = @"C:\Postdoc Research\INRA\SiriusQuality1.5\Data\v1.5-Project\"; string projectFileName = @"v1.5-SensitivityAnalysis.sqpro";//The name of the run file of sensitivity analysis. string projectPath = projectDirectory + projectFileName; //Console.WriteLine(projectPath); string GSADirectory = @"C:\Postdoc Research\INRA\SiriusQuality1.5\Data\INRA-BBSRC 16cv project\Sensitivity Analysis\";//GSA=global sensitivity analysis string inputDirectory = @"Morris\"; //string RandomInputFileName = @"All Parameter-Morris-860.txt"; string RandomInputFileName = @"76 Parameter-Morris.txt"; //It was changed to 76 parameters now. string RandomInputFilePath = GSADirectory + inputDirectory + RandomInputFileName; //Console.WriteLine(RandomInputFileName); string outputDirectory = @"Morris\"; string outputFileName = "ModelOutput_Morris_"; string[] treatmentNames = new string[] { "AV-HighN", "AV-LowN", "CF-HighN", "CF-LowN", "RR-HighN", "RR-LowN" }; string outputFileExtention = ".txt"; string[] wholeOutputFileNames = new string[treatmentNames.Length]; for (int i = 0; i < wholeOutputFileNames.Length; i++) { string wholeOutputFileName = GSADirectory + outputDirectory + outputFileName + treatmentNames[i] + outputFileExtention; //Assign the path of the text file to save the temporary model outputs concerned. File.WriteAllText(wholeOutputFileName, ""); //Clear the temp ouput file that contains model outputs concerned. wholeOutputFileNames[i] = wholeOutputFileName; //Console.WriteLine(wholeOutputFileNames[i]); } #endregion ReadTextFile readTextFile = new ReadTextFile(); string[,] randoms = readTextFile.ReadText(RandomInputFilePath, numberOfRandomLines, numberOfParameters); //Read the input file of random parameter sets. int usedRandomLines = randoms.GetLength(0); //The integer to show how many model runs will be conducted in current simulation. //int usedRandomLines = 200; for (int i = 0; i < usedRandomLines; i++) { #region 1. Load random parameter set. List <double> inputParameterValues = new List <double>(); for (int j = 0; j < randoms.GetLength(1); j++) { double temParameterValue = Convert.ToDouble(randoms[i, j]); inputParameterValues.Add(temParameterValue); } #endregion #region 2. Run the model. AutomaticModelRun.ModelRun(inputParameterValues, GSADirectory, projectPath, wholeOutputFileNames); inputParameterValues.Clear();//Run the project. #endregion Console.WriteLine("Parameter set {0} is finished, please wait...", i + 1); } Console.WriteLine("Global Sensitivity Analysis is finished!"); //Console.ReadLine(); }