private void ContinueButton2_Click(object sender, EventArgs e) { PopSizeBox.Enabled = false; GenNumberBox.Enabled = false; if (AdditionalRadioButton.Checked == true) { TimeBox.Enabled = false; ImprovementBox.Enabled = false; } CrossingNumeric.Enabled = false; MutProbNumeric.Enabled = false; ContinueButton2.Enabled = false; ContinueButton3.Enabled = false; ReturnButton1.Enabled = false; int pop_size = Int32.Parse(PopSizeBox.Text); int gens = Int32.Parse(GenNumberBox.Text); int time = 0; if (TimeBox.Text != "" || TimeBox.Text != "0") { time = Int32.Parse(TimeBox.Text); } int iterations = 0; if (ImprovementBox.Text != "" || ImprovementBox.Text != "0") { iterations = Int32.Parse(ImprovementBox.Text); } int tournament_size = Int32.Parse(TournamentBox.Text); double cross_prob = Convert.ToDouble(Math.Round(CrossingNumeric.Value, 0)) / 100; double mut_prob = Convert.ToDouble(Math.Round(MutProbNumeric.Value, 0)) / 100; genetic_algorithm = new GA(Instance.Copy(), pop_size, gens, time, iterations, tournament_size, cross_prob, mut_prob); progressBar1.Minimum = 0; progressBar1.Value = 0; progressBar1.Maximum = gens - 1; ProgressChart.Series["Funkcja celu"].Points.Clear(); genetic_algorithm.OnProgressUpdate += update_form; bw = new BackgroundWorker(); bw.WorkerReportsProgress = true; bw.DoWork += new DoWorkEventHandler( delegate(object o, DoWorkEventArgs args) { BackgroundWorker b = o as BackgroundWorker; GA ga = args.Argument as GA; ga.life_uh_finds_a_way(); } ); bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler( delegate(object o, RunWorkerCompletedEventArgs args) { MessageBox.Show("Skończone!!! NAJLEPSZE ROZWIĄZANIE: " + genetic_algorithm.Best.Fitness); ContinueButton3.Enabled = true; ReturnButton1.Enabled = true; PopSizeBox.Enabled = true; GenNumberBox.Enabled = true; if (AdditionalRadioButton.Checked == true) { TimeBox.Enabled = true; ImprovementBox.Enabled = true; } CrossingNumeric.Enabled = true; MutProbNumeric.Enabled = true; ContinueButton2.Enabled = true; pause = false; PauseButton.Text = "Pauza"; stop = false; }); bw.RunWorkerAsync(genetic_algorithm); PauseButton.Enabled = true; StopButton.Enabled = true; }
public void run_tests() { string current_dir = @"C:\Users\Jędrzej\Desktop\Studium_Naturae\Zaawansowane_Programowanie\ZP_GA\ZP_GA"; string file_path = current_dir + Path.DirectorySeparatorChar + "Testy" + Path.DirectorySeparatorChar + "testy.csv"; string header = "Fragments;Samples;Fill;Errors;Pop_Size;Gens;Tour_Size;CO;Mut;No;Fitness;Time[s]"; using (StreamWriter sw = new StreamWriter(file_path, true)) sw.WriteLine(header); //for (int m = 0; m < fragments.Count; m++) for (int ms = 0; ms < matrix_sizes.Count; ms++) //for (int n = 0; n < samples.Count; n++) { int current_fragments = matrix_sizes[ms].Item1; //fragments[m]; int current_samples = matrix_sizes[ms].Item2; //samples[n]; for (int f = 0; f < fills.Count; f++) { double current_fill = fills[f]; if (current_fill == 0.2 || current_fill == 0.4) { continue; } if (current_fill == 0.1 && current_fragments == 10 && current_samples == 10) // Bo 0.1 z 10 to 1 a to każda poprawna { continue; } List <int> errors = new List <int>(); int max_errors = (current_fragments * current_samples) / 5; float error_increment = max_errors / 5; float number_of_errors = 0; errors.Add(Convert.ToInt32(number_of_errors)); for (int inc = 0; inc < 5; inc++) { number_of_errors += error_increment; errors.Add(Convert.ToInt32(number_of_errors)); } for (int e = 0; e < errors.Count; e++) { for (int p = 0; p < pop_sizes.Count; p++) { if (pop_sizes[p] == 150 || pop_sizes[p] == 250 || pop_sizes[p] == 350 || pop_sizes[p] == 450) { continue; } if (pop_sizes[p] <= current_samples * 10) { int current_errors = errors[e]; int pop_size = pop_sizes[p]; List <int> tournament_sizes = new List <int> { Convert.ToInt32(pop_size * 0.1), Convert.ToInt32(pop_size * 0.3), Convert.ToInt32(pop_size * 0.5) }; for (int g = 0; g < gens.Count; g++) { if (gens[g] <= (current_fragments + current_samples) * 10) { for (int t = 0; t < tournament_sizes.Count; t++) { for (int cv = 0; cv < crossing_overs.Count; cv++) { for (int mut = 0; mut < mutations.Count; mut++) { int current_gens = gens[g]; int current_tournament = tournament_sizes[t]; double current_cv = crossing_overs[cv]; double current_mut = mutations[mut]; string line_template = "{0};{1};{2};{3};{4};{5};{6};{7};{8};"; string line = string.Format(line_template, current_fragments, current_samples, current_fill, current_errors, pop_size, current_gens, current_tournament, current_cv, current_mut); Console.WriteLine("\n\nTworzę plik!!! PARAMETRY:\nFragmenty:" + current_fragments + " Próbki: " + current_samples + " FILL: " + current_fill + " ERRORS: " + current_errors + " POP: " + pop_size + " GENS: " + current_gens + " TOURNAMENT: " + current_tournament + " CO: " + current_cv + " MUT:" + current_mut); for (int i = 0; i < 10; i++) { DataTable new_instance = Generator.Create(current_fragments, current_samples, current_fill, current_errors).Instance.Copy(); Stopwatch stopwatch = Stopwatch.StartNew(); GA ga = new GA(new_instance.Copy(), pop_size, current_gens, 0, 0, current_tournament, current_cv, current_mut); ga.life_uh_finds_a_way(); stopwatch.Stop(); var time = stopwatch.ElapsedMilliseconds; Console.WriteLine("INSTANCJA: " + i + "; WARTOŚĆ: " + ga.Best.Fitness + " CZAS W SEKUNDACH: " + time * 0.001); using (StreamWriter sw = new StreamWriter(file_path, true)) sw.WriteLine(line + i + ";" + ga.Best.Fitness + ";" + time * 0.001); } } } } } } } } } } } }