public BasicData(MathExpr function, Criteria criteria, MathExpr[] restrictions, VarBounds bounds) { this.Function = function; this.Restrictions = restrictions; this.OptCriteria = criteria; this.Bounds = bounds; }
public GridBruteForce(BasicData input, double step) : base(input) { if (step <= 0) { throw new ArgumentException(); } this.StepSize = Math.Abs(step); this.e = 1 + MathExpr.GetPrecision(StepSize); }
private void SetChartArea(Optimize.BasicData.VarBounds bounds) { chart.Series.Clear(); chart.Series.Add("Null"); //chart.Series["Null"].Points.AddXY(0, 0); chart.ChartAreas[0].AxisX.Maximum = bounds.X1Upper; chart.ChartAreas[0].AxisX.Minimum = bounds.X1Lower; chart.ChartAreas[0].AxisY.Maximum = bounds.X2Upper; chart.ChartAreas[0].AxisY.Minimum = bounds.X2Lower; chart.ChartAreas[0].AxisX.Interval = MathExpr.Round(bounds.X1Interval / 20); chart.ChartAreas[0].AxisY.Interval = MathExpr.Round(bounds.X2Interval / 20); }
public static List <Series> Isolines(Optimize.BasicData input) { double x1step = input.Bounds.X1Interval / (double)x1stepcount; double x2step = input.Bounds.X2Interval / (double)x2stepcount; var list = new List <Point>(); var list_good = new List <Point>(); for (int i = 0; i < x1stepcount; i++) { double x1 = input.Bounds.X1Lower + x1step * i; for (int j = 0; j < x2stepcount; j++) { double x2 = input.Bounds.X2Lower + x2step * j; double y = input.Function.Calc(x1, x2); list.Add(new Point(x1, x2, y)); if (input.CheckRestrictions(x1, x2)) { list_good.Add(new Point(x1, x2, y)); } } } ; double min = list.AsParallel().Min(p => p.Y); double max = list.AsParallel().Max(p => p.Y); double step = (max - min) / steps; var ss = new List <Series>(); ss.Add(PlacePoints(list_good, $"Restrictions", Color.LightGreen, 1)); System.Threading.Tasks.Parallel.For(1, steps, (i) => { int ey = 1 + MathExpr.GetPrecision(min + i * step); double y = MathExpr.Round(min + i * step, ey); double delta_y = Math.Pow(step, -ey) / 1.5; var list2 = list.Where(p => p.Y > y - delta_y && p.Y < y + delta_y); var s = PlacePoints(list2, $"Iso{i}", SystemColors.Highlight, 1); int index = new Random().Next(s.Points.Count / 3, s.Points.Count / 3 * 2); if (index > 0) { s.Points[index].Label = y.ToString(); } ss.Add(s); }); return(ss); }
public override void RunOptimization() { Stopwatch stw = new Stopwatch(); stw.Start(); int x1Count = 1 + (int)(Data.Bounds.X1Interval / StepSize); int x2Count = 1 + (int)(Data.Bounds.X2Interval / StepSize); Iterations = (uint)(x1Count * x2Count); X1Array = new double[x1Count]; X2Array = new double[x2Count]; for (int x1Step = 0; x1Step < x1Count; x1Step++) { X1Array[x1Step] = MathExpr.Round(Data.Bounds.X1Lower + StepSize * x1Step, e); } for (int x2Step = 0; x2Step < x2Count; x2Step++) { X2Array[x2Step] = MathExpr.Round(Data.Bounds.X2Lower + StepSize * x2Step, e); } Matrix = new double[X1Array.Length, X2Array.Length]; int opt_i = 0; int opt_j = 0; for (int i = 0; i < Matrix.GetLength(0); i++) { for (int j = 0; j < Matrix.GetLength(1); j++) { Matrix[i, j] = Data.Function.Calc(X1Array[i], X2Array[j]); if (Data.CheckRestrictions(X1Array[i], X2Array[j])) { if ((Data.OptCriteria == BasicData.Criteria.Minimum && Matrix[i, j] < Matrix[opt_i, opt_j]) || (Data.OptCriteria == BasicData.Criteria.Maximum && Matrix[i, j] > Matrix[opt_i, opt_j])) { opt_i = i; opt_j = j; } } } } stw.Stop(); Duration = stw.Elapsed.TotalMilliseconds; Result = new Point(X1Array[opt_i], X2Array[opt_j], Matrix[opt_i, opt_j]); }
Optimize.BasicData PrepareValues() { Optimize.BasicData data = null; try { var func = new MathExpr(funcTextBox.Text); logstr += $"y={funcTextBox.Text}\u2192{criteriaComboBox.Text}"; var restr = new MathExpr[limitsGridView.Rows.Count]; for (int i = 0; i < limitsGridView.Rows.Count; i++) { string expr = limitsGridView[0, i].Value.ToString() + limitsGridView[1, i].Value.ToString() + limitsGridView[2, i].Value.ToString(); logstr += ", " + expr; restr[i] = new MathExpr(expr); } double x1min, x1max, x2min, x2max; x1min = Double.Parse(x1ltextBox.Text); x1max = Double.Parse(x1utextBox.Text); x2min = Double.Parse(x2ltextBox.Text); x2max = Double.Parse(x2utextBox.Text); Double.Parse(par1TextBox.Text); Double.Parse(textBox4.Text); Double.Parse(textBox3.Text); Double.Parse(textBox2.Text); uint.Parse(par2TextBox.Text); double par_eps = Double.Parse(textBox1.Text); var bounds = new Optimize.BasicData.VarBounds(x1min, x1max, x2min, x2max); logstr += " " + bounds; data = new Optimize.BasicData(func, crit, restr, bounds); } catch { MessageBox.Show("Не удалось обработать введённые данные. Процесс отменён."); logstr += " - Неудача!"; } return(data); }
public override void RunOptimization() { Stopwatch stw = new Stopwatch(); stw.Start(); CheckStartPoint(); Points = new List <Point>(); Points.Add(StartPoint); uint iter = 0; Point prevPoint = StartPoint; int e = 3 + MathExpr.GetPrecision(Epsylon); while (step1 > Epsylon || step2 > Epsylon) { iter++; bool x1_success = false; bool x2_success = false; double pr_x2 = MathExpr.Round(prevPoint.X2, e); double x1Upper = MathExpr.Round(prevPoint.X1 + step1, e); if (Data.CheckRestrictions(x1Upper, pr_x2)) { double y = Data.Function.Calc(x1Upper, pr_x2); if (IsBetter(y, prevPoint.Y)) { prevPoint = new Point(x1Upper, pr_x2, y); x1_success = true; } } double x1Lower = MathExpr.Round(prevPoint.X1 - step1, e); if (Data.CheckRestrictions(x1Lower, pr_x2)) { double y = Data.Function.Calc(x1Lower, pr_x2); if (IsBetter(y, prevPoint.Y)) { prevPoint = new Point(x1Lower, pr_x2, y); x1_success = true; } } if (!x1_success) { step1 /= div; double x2Upper = MathExpr.Round(prevPoint.X2 + step2, e); double pr_x1 = MathExpr.Round(prevPoint.X1, e); if (Data.CheckRestrictions(pr_x1, x2Upper)) { double y = Data.Function.Calc(pr_x1, x2Upper); if (IsBetter(y, prevPoint.Y)) { prevPoint = new Point(pr_x1, x2Upper, y); x2_success = true; } } double x2Lower = MathExpr.Round(prevPoint.X2 - step2, e); if (Data.CheckRestrictions(pr_x1, x2Lower)) { double y = Data.Function.Calc(prevPoint.X1, x2Lower); if (IsBetter(y, prevPoint.Y)) { prevPoint = new Point(pr_x1, x2Lower, y); x2_success = true; } } } Points.Add(prevPoint); if (x1_success || x2_success) { continue; } else { step1 /= div; step2 /= div; } } stw.Stop(); Duration = stw.Elapsed.TotalMilliseconds; Result = Points.Last(); Iterations = iter; }