|
本帖最后由 S$MULATE 于 2013-10-19 16:19 编辑
simio下一代系统仿真:中国技术服务商/总代理:http://www.xunhetech.com
专注于SIMIO教育和项目开发。
===============================================
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using SimioAPI;
namespace RunExperiments
{
public partial class RunExperiments : Form
{
private static double _expReturnValue = 0.0;
private static int _currentRun = 0;
public RunExperiments()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
var reps = Convert.ToInt32(textNumberOfReps.Text);
var runtime = Convert.ToInt32(textDaysPerRun.Text);
var getFile = new OpenFileDialog();
getFile.Filter = "Simio Project Files(*.spfx)|*.spfx";
if (getFile.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("Canceled by user.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
string[] warnings;
ISimioProject project = SimioProjectFactory.LoadProject(getFile.FileName, out warnings);
textProjectLoaded.Text = getFile.FileName;
IModel model = project.Models["Model"];
IExperiment experiment = model.Experiments["Experiment1"];
// Specify run times.
IRunSetup setup = experiment.RunSetup;
setup.StartingTime = new DateTime(2010, 10, 01);
setup.WarmupPeriod = TimeSpan.FromHours(0);
setup.EndingTime = experiment.RunSetup.StartingTime + TimeSpan.FromDays(runtime);
// Add event handler for ScenarioEnded
experiment.ScenarioEnded += new EventHandler<ScenarioEndedEventArgs>(experiment_ScenarioEnded);
experiment.RunCompleted += new EventHandler<RunCompletedEventArgs>(experiment_RunCompleted);
textResults.Text = "Start!!!" + "\r\n";
// For each number of runs
for (int i = 0; i < experiment.Scenarios.Count; i++)
{
experiment.Scenarios.ReplicationsRequired = reps;
}
textResults.Text += "Start run: " + DateTime.Now.ToString() + " \r\n";
// set table parameter value
model.Tables["Parameters"].Rows[0].Properties["Column1"].Value = _currentRun.ToString(System.Globalization.CultureInfo.InvariantCulture);
// Reset _expReturnValue
_expReturnValue = 0.0;
experiment.Scenarios[0].ReplicationsRequired = reps;
// Run Experiment
experiment.RunAsync();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void experiment_RunCompleted(object sender, RunCompletedEventArgs e)
{
textResults.Text += "End run: " + DateTime.Now.ToString() + " return value: " + _expReturnValue.ToString() + "\r\n";
IExperiment experiment = (IExperiment)sender;
experiment.Reset();
}
void experiment_ScenarioEnded(object sender, ScenarioEndedEventArgs e)
{
IExperiment experiment = (IExperiment)sender;
// get response value
foreach (IExperimentResponse response in experiment.Responses)
{
double responseValue = 0.0;
if (e.Scenario.GetResponseValue(response, ref responseValue))
{
_expReturnValue += responseValue;
textResults.Text += "End scenario(" + e.Scenario.Name + ") " + DateTime.Now.ToString() + " return value: " + responseValue.ToString() + "\r\n";
break;
}
}
}
}
}
===========================
Simio高级项目顾问 jeffzz111 QQ#: 77172358 |
|