/// <summary> /// Calculate factors that we need to run. Put combinations into allCombinations /// </summary> private void CalculateFactors() { if (allCombinations.Count == 0) { if (ParameterValues == null) { ParameterValues = RunRToGetParameterValues(); } if (ParameterValues == null || ParameterValues.Rows.Count == 0) { throw new Exception("The morris function in R returned null"); } int simulationNumber = 1; foreach (DataRow parameterRow in ParameterValues.Rows) { var factors = new List <CompositeFactor>(); foreach (Parameter param in Parameters) { object value = Convert.ToDouble(parameterRow[param.Name]); CompositeFactor f = new CompositeFactor(param.Name, param.Path, value); factors.Add(f); } string newSimulationName = Name + "Simulation" + simulationNumber; allCombinations.Add(factors); simulationNumber++; } } }
/// <summary> /// Attach the view /// </summary> /// <param name="model">The model</param> /// <param name="view">The view to attach</param> /// <param name="explorerPresenter">The presenter</param> public void Attach(object model, object view, ExplorerPresenter explorerPresenter) { this.factor = model as CompositeFactor; this.factorView = view as IEditorView; this.presenter = explorerPresenter; intellisense = new IntellisensePresenter(factorView as ViewBase); this.factorView.Lines = factor.Specifications.ToArray(); this.factorView.TextHasChangedByUser += this.OnTextHasChangedByUser; this.factorView.ContextItemsNeeded += this.OnContextItemsNeeded; this.presenter.CommandHistory.ModelChanged += this.OnModelChanged; intellisense.ItemSelected += OnIntellisenseItemSelected; }
/// <summary> /// Calculate factors that we need to run. Put combinations into allCombinations /// </summary> private void CalculateFactors() { if (allCombinations.Count == 0) { if (ParameterValues == null || ParameterValues.Rows.Count == 0) { ParameterValues = RunRToGetParameterValues(); } if (ParameterValues == null || ParameterValues.Rows.Count == 0) { throw new Exception("The morris function in R returned null"); } if (ParameterValues.Rows.Count != NumPaths * (Parameters.Count + 1)) { StringBuilder msg = new StringBuilder(); msg.AppendLine("Morris error: Number of parameter values from R is not equal to num paths * (N + 1)."); msg.AppendLine($"Number of parameters from R = {ParameterValues.Rows.Count}"); msg.AppendLine($"NumPaths={NumPaths}"); msg.AppendLine($"Parameters.Count={Parameters.Count}"); msg.AppendLine($"ParameterValues as returned from R:"); using (StringWriter writer = new StringWriter(msg)) DataTableUtilities.DataTableToText(ParameterValues, 0, ",", true, writer); Console.WriteLine(msg.ToString()); } int simulationNumber = 1; foreach (DataRow parameterRow in ParameterValues.Rows) { var factors = new List <CompositeFactor>(); foreach (Parameter param in Parameters) { object value = Convert.ToDouble(parameterRow[param.Name], CultureInfo.InvariantCulture); CompositeFactor f = new CompositeFactor(param.Name, param.Path, value); factors.Add(f); } string newSimulationName = Name + "Simulation" + simulationNumber; allCombinations.Add(factors); simulationNumber++; } } }
/// <summary> /// Calculate factors that we need to run. Put combinations into allCombinations /// </summary> private void CalculateFactors() { if (allCombinations.Count == 0) { if (ParameterValues == null || ParameterValues.Rows.Count == 0) { ParameterValues = RunRToGetParameterValues(); } if (ParameterValues == null || ParameterValues.Rows.Count == 0) { throw new Exception("The morris function in R returned null"); } int n = NumPaths * (Parameters.Count + 1); // Sometimes R will return an incorrect number of parameter values, usually // this happens when jump is too high. In this situation, we retry up to 10 times. for (int numTries = 1; numTries < 10 && ParameterValues.Rows.Count != n; numTries++) { StringBuilder msg = new StringBuilder(); msg.AppendLine("Morris error: Number of parameter values from R is not equal to num paths * (N + 1)."); msg.AppendLine($"Number of parameters from R = {ParameterValues.Rows.Count}"); msg.AppendLine($"NumPaths={NumPaths}"); msg.AppendLine($"Parameters.Count={Parameters.Count}"); msg.AppendLine($"Trying again..."); Console.WriteLine(msg.ToString()); ParameterValues = RunRToGetParameterValues(); } if (ParameterValues.Rows.Count != n) { // We've tried and failed 10 times to generate the right number of parameter values. // Time to give up and throw a fatal. StringBuilder msg = new StringBuilder(); msg.AppendLine("Morris error: Number of parameter values from R is not equal to num paths * (N + 1)."); msg.AppendLine($"Number of parameters from R = {ParameterValues.Rows.Count}"); msg.AppendLine($"NumPaths={NumPaths}"); msg.AppendLine($"Parameters.Count={Parameters.Count}"); msg.AppendLine($"ParameterValues as returned from R:"); using (StringWriter writer = new StringWriter(msg)) DataTableUtilities.DataTableToText(ParameterValues, 0, ",", true, writer); throw new Exception(msg.ToString()); } int simulationNumber = 1; foreach (DataRow parameterRow in ParameterValues.Rows) { var factors = new List <CompositeFactor>(); foreach (Parameter param in Parameters) { object value = Convert.ToDouble(parameterRow[param.Name], CultureInfo.InvariantCulture); CompositeFactor f = new CompositeFactor(param.Name, param.Path, value); factors.Add(f); } string newSimulationName = Name + "Simulation" + simulationNumber; allCombinations.Add(factors); simulationNumber++; } } }
/// <summary> /// Multiplies a collection of factors. /// </summary> /// <param name="factors">The collection of factors to multiply.</param> /// <returns>The multiplied factor.</returns> public static Factor Multiply(params Factor[] factors) { var factor = new CompositeFactor(factors); return(factor.Factors.Count == 1 ? factor.Factors[0] : factor); }
/// <summary> /// Calculate factors that we need to run. Put combinations into allCombinations /// </summary> private void CalculateFactors() { if (allCombinations.Count == 0) { if (ParameterValues == null || ParameterValues.Rows.Count == 0) { // Write a script to get random numbers from R. string script = string.Format ($".libPaths(c('{R.PackagesDirectory}', .libPaths()))" + Environment.NewLine + $"library('boot', lib.loc = '{R.PackagesDirectory}')" + Environment.NewLine + $"library('sensitivity', lib.loc = '{R.PackagesDirectory}')" + Environment.NewLine + "n <- {0}" + Environment.NewLine + "nparams <- {1}" + Environment.NewLine + "X1 <- data.frame(matrix(nr = n, nc = nparams))" + Environment.NewLine + "X2 <- data.frame(matrix(nr = n, nc = nparams))" + Environment.NewLine , NumPaths, Parameters.Count); for (int i = 0; i < Parameters.Count; i++) { script += string.Format("X1[, {0}] <- {1}+runif(n)*{2}" + Environment.NewLine + "X2[, {0}] <- {1}+runif(n)*{2}" + Environment.NewLine , i + 1, Parameters[i].LowerBound, Parameters[i].UpperBound - Parameters[i].LowerBound); } string sobolx1FileName = GetTempFileName("sobolx1", ".csv"); string sobolx2FileName = GetTempFileName("sobolx2", ".csv"); script += string.Format("write.table(X1, \"{0}\",sep=\",\",row.names=FALSE)" + Environment.NewLine + "write.table(X2, \"{1}\",sep=\",\",row.names=FALSE)" + Environment.NewLine + "sa <- sobolSalt(model = NULL, X1, X2, scheme=\"A\", nboot = 100)" + Environment.NewLine + "write.csv(sa$X,row.names=FALSE)" + Environment.NewLine, sobolx1FileName.Replace("\\", "/"), sobolx2FileName.Replace("\\", "/")); // Run the script ParameterValues = RunR(script); // Read in the 2 data frames (X1, X2) that R wrote. if (!File.Exists(sobolx1FileName)) { string rFileName = GetTempFileName("sobolscript", ".r"); if (!File.Exists(rFileName)) { throw new Exception("Cannot find file: " + rFileName); } string message = "Cannot find : " + sobolx1FileName + Environment.NewLine + "Script:" + Environment.NewLine + File.ReadAllText(rFileName); throw new Exception(message); } X1 = ApsimTextFile.ToTable(sobolx1FileName); X2 = ApsimTextFile.ToTable(sobolx2FileName); } int simulationNumber = 1; foreach (DataRow parameterRow in ParameterValues.Rows) { var factors = new List <CompositeFactor>(); for (int p = 0; p < Parameters.Count; p++) { object value = Convert.ToDouble(parameterRow[p], CultureInfo.InvariantCulture); var f = new CompositeFactor(Parameters[p].Name, Parameters[p].Path, value); factors.Add(f); } string newSimulationName = Name + "Simulation" + simulationNumber; allCombinations.Add(factors); simulationNumber++; } } }