public TestData StartTest(double pk, double pm, int T, int n) { GeneticBaseOperations test = new GeneticBaseOperations(); test.aRange = -4; test.bRange = 12; test.Precision = 3; test.isElite = true; test.Pk = pk; test.Pm = pm; test.PopulationSize = n; test.GeneratePopulation(); test.HistoryHelper.Pk = test.Pk; test.HistoryHelper.Pm = test.Pm; test.HistoryHelper.N = test.PopulationSize; test.HistoryHelper.T = T; for (int i = 0; i < T; i++) { test.ExecuteAlgorithm(); test.Eval(); test.HistoryHelper.Generations.Add(test.GetCurrentPopulation()); } TestData td = new TestData { Pk = pk, PM = pm, N = n, T = T, MaxFx = test.HistoryHelper.AvgFx() }; test.Dispose(); return(td); }
private void button1_Click(object sender, EventArgs e) { GBO = new GeneticBaseOperations(); int T; if ( int.TryParse(textBox4.Text, out GBO.PopulationSize) && int.TryParse(textBox1.Text, out GBO.aRange) && int.TryParse(textBox2.Text, out GBO.bRange) && int.TryParse(textBox3.Text, out GBO.Precision) && double.TryParse(textBox6.Text, out GBO.Pk) && double.TryParse(textBox7.Text, out GBO.Pm) && int.TryParse(textBox5.Text, out T) ) { GBO.isElite = checkBox1.Checked; dataGridView1.Rows.Clear(); dataGridView2.Rows.Clear(); dataGridView3.Rows.Clear(); GBO.GeneratePopulation(); for (int i = 0; i < T; i++) { GBO.ExecuteAlgorithm(); GBO.HistoryHelper.Generations.Add(GBO.GetCurrentPopulation()); } GBO.Eval(); for (int i = 0; i < GBO.HistoryHelper.Generations.Count; i++) { DataGridViewRow label = new DataGridViewRow(); label.CreateCells(dataGridView1); label.Cells[0].Value = "Pokolenie"; label.Cells[1].Value = i; dataGridView1.Rows.Add(label); DataGridViewRow space1 = new DataGridViewRow(); space1.CreateCells(dataGridView1); space1.Cells[0].Value = "#"; space1.Cells[1].Value = "#"; space1.Cells[2].Value = "#"; space1.Cells[3].Value = "#"; dataGridView1.Rows.Add(space1); for (int j = 0; j < GBO.HistoryHelper.Generations[i].ListOfIndividuals.Count; j++) { DataGridViewRow row = new DataGridViewRow(); row.CreateCells(dataGridView1); row.Cells[0].Value = j; row.Cells[1].Value = GBO.HistoryHelper.Generations[i].ListOfIndividuals.ElementAt(j).IndvData.xReal; row.Cells[2].Value = GBO.HistoryHelper.Generations[i].ListOfIndividuals.ElementAt(j).IndvData.xBin; row.Cells[3].Value = GBO.HistoryHelper.Generations[i].ListOfIndividuals.ElementAt(j).IndvData.Fx; dataGridView1.Rows.Add(row); } DataGridViewRow space = new DataGridViewRow(); space.CreateCells(dataGridView1); space.Cells[0].Value = "#"; space.Cells[1].Value = "#"; space.Cells[2].Value = "#"; space.Cells[3].Value = "#"; dataGridView1.Rows.Add(space); } for (int i = 0; i < GBO.HistoryHelper.Generations.Last().ReturnPercent().Count; i++) { DataGridViewRow row = new DataGridViewRow(); row.CreateCells(dataGridView2); row.Cells[0].Value = i; row.Cells[1].Value = GBO.HistoryHelper.Generations.Last().ReturnPercent().ElementAt(i).Key; row.Cells[2].Value = GBO.HistoryHelper.Generations.Last().ReturnPercent().ElementAt(i).Value; dataGridView2.Rows.Add(row); } List <double> maxFx = new List <double>(); List <double> minFx = new List <double>(); List <double> avgFx = new List <double>(); for (int i = 0; i < GBO.HistoryHelper.Generations.Count; i++) { maxFx.Add(GBO.HistoryHelper.Generations[i].FindBestIndividual().IndvData.Fx); minFx.Add(GBO.HistoryHelper.Generations[i].FindWorstIndividual().IndvData.Fx); avgFx.Add(GBO.HistoryHelper.Generations[i].CalcAvgIndividual()); } //chart1.Series[0].Label = "Max F(x)"; chart1.Series[0].Color = Color.Blue; chart1.Series[0].Points.DataBindY(maxFx); //chart1.Series[1].Label = "Min F(x)"; chart1.Series[1].Color = Color.Red; chart1.Series[1].Points.DataBindY(minFx); //chart1.Series[2].Label = "Avg F(x)"; chart1.Series[2].Color = Color.Green; chart1.Series[2].Points.DataBindY(avgFx); chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; chart1.Series[1].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; chart1.Series[2].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; List <KeyValuePair <double, double> > extreme = new List <KeyValuePair <double, double> >(); extreme = GBO.ForceCalcSolution(); for (int i = 0; i < extreme.Count; i++) { DataGridViewRow row = new DataGridViewRow(); row.CreateCells(dataGridView3); row.Cells[0].Value = i; row.Cells[1].Value = extreme.ElementAt(i).Key; row.Cells[2].Value = extreme.ElementAt(i).Value; dataGridView3.Rows.Add(row); } if (checkBox2.Checked) { SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "Excel files (*.xls)|*.xls"; dlg.Title = "Export in Excel format"; dlg.CheckPathExists = true; dlg.ShowDialog(); if (dlg.FileName != "") { Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook xlWorkBook; Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); xlWorkSheet.Cells[1, 1] = "#"; xlWorkSheet.Cells[1, 2] = "XReal"; xlWorkSheet.Cells[1, 3] = "F(x)"; for (int i = 0; i < GBO.HistoryHelper.Generations.Count; i++) { for (int j = 1; j < GBO.HistoryHelper.Generations[i].ListOfIndividuals.Count - 1; j++) { xlWorkSheet.Cells[(GBO.HistoryHelper.Generations[i].ListOfIndividuals.Count * i + 1) + j, 1] = j; xlWorkSheet.Cells[(GBO.HistoryHelper.Generations[i].ListOfIndividuals.Count * i + 1) + j, 2] = GBO.HistoryHelper.Generations[i].ListOfIndividuals[j].IndvData.xReal; xlWorkSheet.Cells[(GBO.HistoryHelper.Generations[i].ListOfIndividuals.Count * i + 1) + j, 3] = GBO.HistoryHelper.Generations[i].ListOfIndividuals[j].IndvData.Fx; } } xlWorkBook.SaveAs(dlg.FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); Marshal.ReleaseComObject(xlWorkSheet); Marshal.ReleaseComObject(xlWorkBook); Marshal.ReleaseComObject(xlApp); } } } else { MessageBox.Show("Podano niepoprawne dane!", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Warning // for Warning //MessageBoxIcon.Error // for Error //MessageBoxIcon.Information // for Information //MessageBoxIcon.Question // for Question ); } }