/// <summary> /// Assumes an Experiment is loaded, this initiates an Experiment run. /// This can be very long-running. /// </summary> /// <param name="explanation"></param> /// <returns></returns> public bool RunModelExperiment(out string explanation) { explanation = ""; string marker = "Begin"; if (CurrentProject == null) { explanation = $"Cannot run plan. No Project is currently loaded"; return(false); } if (CurrentModel == null) { explanation = $"Cannot run plan. No Model is currently loaded"; return(false); } // Check for Experiment if (CurrentExperiment == null) { explanation = $"Model={CurrentModel.Name} has no Experiment selected."; return(false); } try { // Run the experiment. Events will be thrown when replications start and end, // so you would have to handle those elsewhere. marker = "Starting Experiment (Experiment.Run)"; CurrentExperiment.Run(); marker = "End"; return(true); } catch (Exception ex) { explanation = $"Project={CurrentProject.Name} Model={CurrentModel.Name} Marker={marker} Err={ex.Message} InnerEx={ex.InnerException}"; return(false); } }
/// <summary> /// Assumes an Experiment is loaded, this initiates an Experiment run. /// This can be very long-running. /// </summary> /// <param name="explanation"></param> /// <returns></returns> public bool RunModelExperiment(string resultFilepath, out string explanation) { explanation = ""; string marker = "Begin"; ExperimentResults experimentResults = new ExperimentResults(); if (CurrentProject == null) { explanation = $"Cannot run plan. No Project is currently loaded"; return(false); } if (CurrentModel == null) { explanation = $"Cannot run plan. No Model is currently loaded"; return(false); } // Check for Experiment if (CurrentExperiment == null) { explanation = $"Model={CurrentModel.Name} has no Experiment selected."; return(false); } try { CurrentExperiment.ReplicationEnded += (s, e) => { int repNumber = e.ReplicationNumber; double runTime = e.ActualRuntimeInSeconds; }; CurrentExperiment.ScenarioEnded += (s, e) => { LogIt($"ScenarioEnded: Results:"); foreach (var result in e.Results) { // Log the results to get a feel for what is being returned. LogIt($"ScenarioEnded: ObjType='{result.ObjectType}', ObjName='{result.ObjectName}', DS='{result.DataSource}', " + $"Cat='{result.StatisticCategory}', DataItem='{result.DataItem}', Stat='{result.StatisticCategory}', " + $"Avg='{result.Average:0.00}', Min='{result.Minimum:f0.00}', Max='{result.Maximum:f0.00}"); experimentResults.AddScenarioResults(e.Scenario, e.Results); } }; CurrentExperiment.RunCompleted += (s, e) => { LogIt($"RunCompleted:"); }; CurrentExperiment.RunProgressChanged += (s, e) => { LogIt($"ProgressChanged:"); string ss = e.ToString(); }; // Run the experiment. Events will be thrown when replications start and end, // so you would have to handle those elsewhere. marker = "Starting Experiment (Experiment.Run)"; CurrentExperiment.Reset(); CurrentExperiment.Run(); if (string.IsNullOrEmpty(resultFilepath)) { LogIt($"Info: No result Filepath specified. Results not written."); } else { string folder = Path.GetDirectoryName(resultFilepath); if (Directory.Exists(folder)) { StringBuilder sb = new StringBuilder(); // Build a tab-delimited file if (!experimentResults.OutputCsv(resultFilepath, out explanation)) { explanation = $"Cannot Write CSV results to={resultFilepath}"; return(false); } } } marker = "End"; return(true); } catch (Exception ex) { explanation = $"Project={CurrentProject.Name} Model={CurrentModel.Name} Marker={marker} Err={ex.Message} InnerEx={ex.InnerException}"; return(false); } }