private Bitmap MakeLevels(List <double> fx, List <double> x1, List <double> x2, double botBorder, double upBorder, double offset, int width, int height, int size, double deltaX) { var bitmap = new Bitmap(width, height); var graphics = Graphics.FromImage(bitmap); var painter = new PicPainter(graphics, width, height, picOffset); painter.ClearGraphics(); //First step painter.DrawCoordinateLines(botBorder, upBorder, offset, size, 0.5d); var list = new List <double>(); //Second step var res = new Dictionary <double, List <int> >(); for (var i = 0; i < fx.Count; i++) { if (!res.Keys.Contains(fx[i])) { res.Add(fx[i], new List <int> { i }); } else { res[fx[i]].Add(i); } } foreach (var fun in res) { if (fun.Value.Count > 1) { var alreadyDrawn = new List <int>(); for (var i = 0; i < fun.Value.Count - 1; i++) { var minLength = double.PositiveInfinity; var index = -1; for (var j = 0; j < fun.Value.Count; j++) { if (i == j) { continue; } var _i = fun.Value[i]; var _j = fun.Value[j]; var firstPart = x1[_i] - x1[_j]; var secondPart = x2[_i] - x2[_j]; var length = Math.Sqrt(firstPart * firstPart + secondPart * secondPart); if (minLength > length & !alreadyDrawn.Contains(j)) { index = j; minLength = length; } } if (index == -1) { break; } var iCoord = fun.Value[i]; var jCoord = fun.Value[index]; painter.DrawLine(x1[iCoord], x2[iCoord], x1[jCoord], x2[jCoord], botBorder, upBorder, offset, Pens.Red); alreadyDrawn.Add(index); } } } return(bitmap); }
private void HookeJeevesMethodWithPenalty(string expression) { var image = new Bitmap(levels); var picPainter = new PicPainter(Graphics.FromImage(image), pictureBox.Width, pictureBox.Height, picOffset); if (!Double.TryParse(addInfoTextBox.Text, out var deltaX) || !Double.TryParse(botBorderTextBox.Text, out var botBorder) || !Double.TryParse(upBorderTextBox.Text, out var upBorder) || !Double.TryParse(hjX1TextBox.Text, out var x1) || !Double.TryParse(hjX2TextBox.Text, out var x2) || !Double.TryParse(rTextBox.Text, out var r)) { MessageBox.Show("oops"); return; } var horizontalVector = 0; var verticalVector = 0; var size = CalcSize(botBorder, upBorder, deltaX); size++; var offset = CalcOffset(botBorder, upBorder) + picOffset; object[,] limits; try { limits = CreateLimits(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } var fx = CalcWithPenalty(x1, x2, r, expression, limits); while (deltaX > 10E-4) { double newFx = double.MinValue; #region Research horizontalVector = 0; verticalVector = 0; //Horizontal double minusHorizontalFx = CalcWithPenalty(x1, x2 - deltaX, r, expression, limits); double plusHorizontalFx = CalcWithPenalty(x1, x2 + deltaX, r, expression, limits); if (minusHorizontalFx < fx || plusHorizontalFx < fx) { horizontalVector = minusHorizontalFx <= plusHorizontalFx ? -1 : 1; } fx = CalcWithPenalty(x1, x2 + deltaX * horizontalVector, r, expression, limits); picPainter.DrawLine(x1, x2, x1, x2 + deltaX * horizontalVector, botBorder, upBorder, offset, Pens.Black); x2 += deltaX * horizontalVector; //Vertical double minusVerticalFx = CalcWithPenalty(x1 - deltaX, x2, r, expression, limits); double plusVerticalFx = CalcWithPenalty(x1 + deltaX, x2, r, expression, limits); if (minusVerticalFx < fx || plusVerticalFx < fx) { verticalVector = minusVerticalFx <= plusVerticalFx ? -1 : 1; } fx = CalcFunction(expression, x1 + deltaX * verticalVector, x2); picPainter.DrawLine(x1, x2, x1 + deltaX * verticalVector, x2, botBorder, upBorder, offset, Pens.Black); x1 += deltaX * verticalVector; #endregion // if (horizontalVector == 0 && verticalVector == 0) { deltaX /= 10; continue; } #region Pattern maching while (true) { newFx = CalcWithPenalty(x1 + deltaX * verticalVector, x2 + deltaX * horizontalVector, r, expression, limits); if (newFx < fx) { picPainter.DrawLine(x1, x2, x1 + deltaX * verticalVector, x2 + deltaX * horizontalVector, botBorder, upBorder, offset, Pens.Black); x2 += deltaX * horizontalVector; x1 += deltaX * verticalVector; fx = newFx; } else { break; } } #endregion } pictureBox.Image = image; fxTextBox.Text = fx.ToString(); x1TextBox.Text = x1.ToString(); x2TextBox.Text = x2.ToString(); }
private void BruteMethod(string expression) { var image = new Bitmap(levels); var picPainter = new PicPainter(Graphics.FromImage(image), pictureBox.Width, pictureBox.Height, picOffset); var minFx = Double.MaxValue; var minX1 = -1d; var minX2 = -1d; if (!Double.TryParse(addInfoTextBox.Text, out var deltaX) || !Double.TryParse(botBorderTextBox.Text, out var botBorder) || !Double.TryParse(upBorderTextBox.Text, out var upBorder)) { MessageBox.Show("oops"); return; } var size = CalcSize(botBorder, upBorder, deltaX); size++; object[,] limits; try { limits = CreateLimits(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } resultGridView.ColumnCount = size + 1; resultGridView.RowCount = size + 1; for (var i = 0; i < size + 1; i++) { resultGridView.Columns[i].Width = 40; } CreateNumbering(size, deltaX, botBorder); var offset = CalcOffset(botBorder, upBorder) + picOffset; bool isOk; double fx; var x1 = botBorder; var x2 = botBorder; var allFx = new List <double>(); var allX1 = new List <double>(); var allX2 = new List <double>(); var minI = -1; var minJ = -1; for (var i = 0; i < size; i++) { for (var j = 0; j < size; j++) { try { isOk = CalcLimits(limits, x1, x2); fx = Math.Round(CalcFunction(expression, x1, x2), 2); } catch (Exception ex) { MessageBox.Show(ex.Message + " " + ex.StackTrace + " Ошибка при вычислении", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (isOk) { picPainter.DrawPoint(x1, x2, botBorder, upBorder, offset); allFx.Add(fx); allX1.Add(x1); allX2.Add(x2); if (minFx > fx) { minFx = fx; minX1 = x1; minX2 = x2; minI = i; minJ = j; } resultGridView.Rows[size - i - 1].Cells[j + 1].Style.BackColor = Color.White; resultGridView.Rows[size - i - 1].Cells[j + 1].Value = fx; } else { resultGridView.Rows[size - i - 1].Cells[j + 1].Value = fx; resultGridView.Rows[size - i - 1].Cells[j + 1].Style.BackColor = Color.Red; } x2 += deltaX; } x1 += deltaX; x2 = botBorder; } resultGridView.Rows[size - minI - 1].Cells[minJ + 1].Style.BackColor = Color.Green; picPainter.DrawPoint(minX1, minX2, botBorder, upBorder, offset, true); fxTextBox.Text = Math.Round(minFx, 2).ToString(); x1TextBox.Text = Math.Round(minX1, 2).ToString(); x2TextBox.Text = Math.Round(minX2, 2).ToString(); for (var i = 0; i < allFx.Count; i++) { allFx[i] = Math.Round(allFx[i], 2); } pictureBox.Image = image; }
private void MonteCarloMethod(string expression) { var image = new Bitmap(levels); var picPainter = new PicPainter(Graphics.FromImage(image), pictureBox.Width, pictureBox.Height, picOffset); var fxList = new List <double>(); var fx = Double.MaxValue; var x1 = -1d; var x2 = -1d; var allFx = new List <double>(); var allX1 = new List <double>(); var allX2 = new List <double>(); if (!int.TryParse(addInfoTextBox.Text, out var stepsNum) || !Double.TryParse(botBorderTextBox.Text, out var botBorder) || !Double.TryParse(upBorderTextBox.Text, out var upBorder)) { MessageBox.Show("oops"); return; } var offset = CalcOffset(botBorder, upBorder) + picOffset; object[,] limits; try { limits = CreateLimits(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } var rnd = new Random(); var pointCount = 0; for (var i = 0; i < stepsNum; i++) { var _x1 = rnd.NextDouble() * (upBorder - botBorder) + botBorder; var _x2 = rnd.NextDouble() * (upBorder - botBorder) + botBorder; try { var isOk = CalcLimits(limits, _x1, _x2); if (isOk) { var tempFx = CalcFunction(expression, _x1, _x2); picPainter.DrawPoint(_x1, _x2, botBorder, upBorder, offset); allFx.Add(tempFx); allX1.Add(_x1); allX2.Add(_x2); if (tempFx < fx) { fx = tempFx; x1 = _x1; x2 = _x2; } pointCount++; } } catch { MessageBox.Show("Ошибка при вычислении", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } picPainter.DrawPoint(x1, x2, botBorder, upBorder, offset, true); for (var i = 0; i < allFx.Count; i++) { allFx[i] = Math.Round(allFx[i], 1); } fxTextBox.Text = fx.ToString(); x1TextBox.Text = x1.ToString(); x2TextBox.Text = x2.ToString(); addResultTextBox.Text = pointCount.ToString(); pictureBox.Image = image; }