示例#1
0
文件: Morris.cs 项目: RobZys/ApsimX
        /// <summary>
        /// Get a list of parameter values that we are to run. Call R to do this.
        /// </summary>
        private void RunRPostSimulation(DataTable predictedValues, out DataTable eeDataRaw, out DataTable statsDataRaw)
        {
            string morrisParametersFileName = GetTempFileName("parameters", ".csv");
            string apsimVariableFileName    = GetTempFileName("apsimvariable", ".csv");
            string rFileName     = GetTempFileName("morrisscript", ".r");
            string eeFileName    = GetTempFileName("ee", ".csv");
            string statsFileName = GetTempFileName("stats", ".csv");

            // write predicted values file
            using (StreamWriter writer = new StreamWriter(apsimVariableFileName))
                DataTableUtilities.DataTableToText(predictedValues, 0, ",", true, writer);

            // Write parameters
            using (StreamWriter writer = new StreamWriter(morrisParametersFileName))
                DataTableUtilities.DataTableToText(ParameterValues, 0, ",", true, writer);

            string paramNames  = StringUtilities.Build(Parameters.Select(p => p.Name), ",", "\"", "\"");
            string lowerBounds = StringUtilities.Build(Parameters.Select(p => p.LowerBound), ",");
            string upperBounds = StringUtilities.Build(Parameters.Select(p => p.UpperBound), ",");
            string script      = GetMorrisRScript();

            script += string.Format
                          ("apsimMorris$X <- read.csv(\"{0}\")" + Environment.NewLine +
                          "values = read.csv(\"{1}\")" + Environment.NewLine +
                          "allEE <- data.frame()" + Environment.NewLine +
                          "allStats <- data.frame()" + Environment.NewLine +
                          "for (columnName in colnames(values))" + Environment.NewLine +
                          "{{" + Environment.NewLine +
                          " apsimMorris$y <- values[[columnName]]" + Environment.NewLine +
                          " tell(apsimMorris)" + Environment.NewLine +

                          " ee <- data.frame(apsimMorris$ee)" + Environment.NewLine +
                          " ee$variable <- columnName" + Environment.NewLine +
                          " ee$path <- c(1:{2})" + Environment.NewLine +
                          " allEE <- rbind(allEE, ee)" + Environment.NewLine +

                          " mu <- apply(apsimMorris$ee, 2, mean)" + Environment.NewLine +
                          " mustar <- apply(apsimMorris$ee, 2, function(x) mean(abs(x)))" + Environment.NewLine +
                          " sigma <- apply(apsimMorris$ee, 2, sd)" + Environment.NewLine +
                          " stats <- data.frame(mu, mustar, sigma)" + Environment.NewLine +
                          " stats$param <- params" + Environment.NewLine +
                          " stats$variable <- columnName" + Environment.NewLine +
                          " allStats <- rbind(allStats, stats)" + Environment.NewLine +

                          "}}" + Environment.NewLine +
                          "write.csv(allEE,\"{3}\", row.names=FALSE)" + Environment.NewLine +
                          "write.csv(allStats, \"{4}\", row.names=FALSE)" + Environment.NewLine,
                          morrisParametersFileName.Replace("\\", "/"),
                          apsimVariableFileName.Replace("\\", "/"),
                          NumPaths,
                          eeFileName.Replace("\\", "/"),
                          statsFileName.Replace("\\", "/"));
            File.WriteAllText(rFileName, script);

            // Run R
            R r = new R();

            r.RunToTable(rFileName);

            eeDataRaw    = ApsimTextFile.ToTable(eeFileName);
            statsDataRaw = ApsimTextFile.ToTable(statsFileName);
        }