private void button2_Click(object sender, EventArgs e) { BruteWorker.CancelAsync(); }
private void BruteWorker_DoWork(object sender, DoWorkEventArgs e) { string bruteExTemp = "", bruteInTemp = "", bruteCyPre = "", bruteVOP = "", bruteLoTor = "", bruteNOx = "", bruteCO = "", bruteHC = "", brutePM = "", bruteUCLan = "", bruteString = "", optimalSettings = ""; int gradeValue = 0, newGradeValue, progress = 0, Iterations = 0; //Extern temp for (int i = 0; i < 5; i++) { bruteExTemp = toBinary(2 ^ i); //Intern temp for (int j = 0; j < 5; j++) { bruteInTemp = bruteExTemp + toBinary(2 ^ j); //Cylinder Pressure for (int k = 0; k < 5; k++) { bruteCyPre = bruteExTemp + bruteInTemp + toBinary(2 ^ k); //Valve Opening Pressure for (int l = 0; l < 5; l++) { bruteVOP = bruteExTemp + bruteInTemp + bruteCyPre + toBinary(2 ^ l); //Load torque for (int m = 0; m < 5; m++) { bruteLoTor = bruteExTemp + bruteInTemp + bruteCyPre + bruteVOP + toBinary(2 ^ m); //NOx Emissions for (int n = 0; n < 5; n++) { bruteNOx = bruteExTemp + bruteInTemp + bruteCyPre + bruteVOP + bruteLoTor + toBinary(2 ^ n); //CO Emissions for (int o = 0; o < 5; o++) { bruteCO = bruteExTemp + bruteInTemp + bruteCyPre + bruteVOP + bruteLoTor + bruteNOx + toBinary(2 ^ o); //HC Emissions for (int p = 0; p < 5; p++) { bruteHC = bruteExTemp + bruteInTemp + bruteCyPre + bruteVOP + bruteLoTor + bruteNOx + bruteCO + toBinary(2 ^ p); //PM Emissions for (int q = 0; q < 5; q++) { brutePM = bruteExTemp + bruteInTemp + bruteCyPre + bruteVOP + bruteLoTor + bruteNOx + bruteCO + bruteHC + toBinary(2 ^ q); //UCLan Stuff for (int r = 0; r < 32; r++) { bruteUCLan = bruteExTemp + bruteInTemp + bruteCyPre + bruteVOP + bruteLoTor + bruteNOx + bruteCO + bruteHC + brutePM + toBinary(r); // for (int s = 0; s < 3; s++) { //temp if (s == 0) { //high bruteString = bruteExTemp + bruteInTemp + bruteCyPre + bruteVOP + bruteLoTor + bruteNOx + bruteCO + bruteHC + brutePM + bruteUCLan + "001"; } else if (s == 1) { //Middle bruteString = bruteExTemp + bruteInTemp + bruteCyPre + bruteVOP + bruteLoTor + bruteNOx + bruteCO + bruteHC + brutePM + bruteUCLan + "010"; } else { //low bruteString = bruteExTemp + bruteInTemp + bruteCyPre + bruteVOP + bruteLoTor + bruteNOx + bruteCO + bruteHC + brutePM + bruteUCLan + "100"; } //shows the progress in progress bar progress = (Iterations + 1 / 187500000); BruteWorker.ReportProgress(progress, BruteProgress); System.Threading.Thread.Sleep(1000); if (BruteWorker.CancellationPending) { e.Cancel = true; } FitFunc newtest = new FitFunc(); newGradeValue = newtest.evalFunc(bruteString); if (newGradeValue > gradeValue) { gradeValue = newGradeValue; optimalSettings = bruteString; } } } } } } } } } } } } //fin MessageBox.Show("Optimal Settings found: " + optimalSettings + Environment.NewLine + "With a grade of: " + gradeValue); Result_Box.Text = "Optimal Settings found: " + optimalSettings + Environment.NewLine + "With a grade of: " + gradeValue; }
private void bruteForce_Click(object sender, EventArgs e) { BruteWorker.RunWorkerAsync(e); }