/// <summary> /// Compute the probability that an ant uses exactly the specified steps, and /// display the probability on the screen. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Compute_Prob_Click(object sender, EventArgs e) { // Calculate the probability of an ant walking to the diagonal, using // exact the number of steps specified by user. try { // Try to parse string for steps to int. int steps = int.Parse(ProbOfStep.Text); ProbOfStepRes.Text = StatisticsCal.probOfStep(steps).ToString(); } catch (FormatException) { // If fail to parse, do nothing and wait for a valid input. } }
/// <summary> /// When the Simulate button is clicked, the program makes the calculation /// and display the mean and standard deviation on the screen. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Simulate_Click(object sender, EventArgs e) { // Clean up the history from previous calculation GlobalData.frequencyThread.Clear(); GlobalData.frequency.Clear(); try { // Try to parse the string for num of threads to int. GlobalData.threads = int.Parse(numOfThreads.Text); if (GlobalData.threads <= 0) { throw new FormatException(); } // Use the manual parallel instead of using Task Parallel Library. GlobalData.useTPL = false; } catch (FormatException) { // If the number of threads are not provided, or provided // in a wrong format, than use Task Parallel Library. GlobalData.useTPL = true; } try { // Try to parse the string for num of ants into int. GlobalData.numAnts = int.Parse(numOfAnts.Text); if (GlobalData.numAnts <= 0) { throw new FormatException(); } // If there are more threads than ants, reduce the number of the // threads to the number of ants. GlobalData.threads = Math.Min(GlobalData.threads, GlobalData.numAnts); // Choose which simulation method to use. if (GlobalData.useTPL) { SimulationTPL.Simuluation(); } else { SimulationMan.Simulation(); } // Calculate the mean and standard deviation. GlobalData.mean = StatisticsCal.findMean(); GlobalData.deviation = StatisticsCal.findDeviation(); // Display the mean and variable on the screen. StepMean.Text = GlobalData.mean.ToString(); StepVar.Text = GlobalData.deviation.ToString(); } catch (FormatException) { // If fail to parse or wrong input, clear history and wait for a valid input. this.chart1.Series.Clear(); StepMean.Text = ""; StepVar.Text = ""; } }