private void descrBoundsBtn_Click(object sender, RoutedEventArgs e) { if (task.data == null) { Log.Text = "Данные не загружены"; return; } if (yBoundsData.Count == 0) { Log.Text = "Не выбран стандарт на марку"; return; } modelDescret = new DescrModel(task.x, task.y, Convert.ToDouble(alphaTextBox.Text), Convert.ToDouble(betaTextBox.Text)); modelDescret.SetBounds(xBoundsData, yBoundsData); //берем границы из табличек task.CorrectBoundsY(modelDescret); //если верхняя не определена - берем максимум по выборке //List<string[]> results; DescretOptTask descrOptimizeBackground = new DescretOptTask(task, modelDescret, Convert.ToInt32(numIntervalsTextBox.Text), Convert.ToInt32(degreeTextBox.Text), Convert.ToInt32(shiftTextBox.Text), Convert.ToDouble(epsTextBox.Text)); backgroundWorker.RunWorkerAsync(descrOptimizeBackground); Log.Text = "Начато построение дискретной модели . . . "; //передаем ссыль на модель, в функции все меняется getBoundsBtn.IsEnabled = false; descrBoundsBtn.IsEnabled = false; }
public double StartBoundsOptimize(int numIntervals, DescrModel model, int degree, int boundsShift, double eps, System.ComponentModel.BackgroundWorker backgroundWorker) { int[] intervals = new int[numIntervals]; for (int i = 0; i < numIntervals; i++) { intervals[i] = i; // берем все нулевые интервалы } int[][] vars = new int[x.Count][]; for (int i = 0; i < x.Count; i++) { vars[i] = intervals; } var cross = new CartesianProduct <int>(vars); IEnumerable <int[]> intervalVars = cross.Get(); double maxCriteria = -1000000; List <Conditions> bestBounds = new List <Conditions>(); foreach (int[] variant in intervalVars) //выбор начального подпространства { List <Conditions> bounds = model.IntervalsBounds(numIntervals, variant); double res = OneIntervalCriteria(bounds, model); if (res > maxCriteria) //здесь оцениваем интервал, просто сравниваем критерий { backgroundWorker.ReportProgress(1); maxCriteria = res; bestBounds.Clear(); for (int i = 0; i < bounds.Count; i++) { bestBounds.Add(new Conditions(bounds[i])); } } } //поехали двигать границы backgroundWorker.ReportProgress(2); DescrModel modelMaxBounds = GetPlavkaBounds(); bool gettingGlobalBetter = true; while (gettingGlobalBetter) { backgroundWorker.ReportProgress(3); gettingGlobalBetter = false; for (int i = 0; i < x.Count; i++) //для каждого параметра смотрим { bool gettingBetter = true; //пока изменяются границы хотя бы одного интервала - пересматриваем заново while (gettingBetter) //повторяем пока сдвиги границ дают результат { gettingBetter = false; //пока изменяются границы внутри интервала - пересматриваем заново //double size = bestBounds[i].upper - bestBounds[i].lower; List <Conditions> newBestBounds = new List <Conditions>(); for (int v = 0; v < 4; v++) { for (int j = 0; j < degree + 1; j++) { List <Conditions> newBounds = new List <Conditions>(); foreach (Conditions c in bestBounds) { newBounds.Add(new Conditions(c)); } if (v == 0) //увеличиваем интервал верхней границы { newBounds[i].upper += Math.Abs(modelMaxBounds.xBounds[i].upper - bestBounds[i].upper) / Math.Pow(boundsShift, i); } if (v == 1) //уменьшаем интервал { newBounds[i].upper -= (bestBounds[i].upper - bestBounds[i].lower) / Math.Pow(boundsShift, i + 1); //уменьшаем интервал } if (v == 2) //увеличиваем интервал нижней границы { newBounds[i].lower += Math.Abs(modelMaxBounds.xBounds[i].lower - bestBounds[i].lower) / Math.Pow(boundsShift, i); } if (v == 3) //уменьшаем интервал { newBounds[i].lower -= (bestBounds[i].upper - bestBounds[i].lower) / Math.Pow(boundsShift, i + 1); } double res = OneIntervalCriteria(newBounds, model); if (backgroundWorker.WorkerReportsProgress) { backgroundWorker.ReportProgress(1); } if (res > maxCriteria) //здесь оцениваем интервал, просто сравниваем критерий { backgroundWorker.ReportProgress(1); gettingBetter = true; if ((Math.Abs(res) - Math.Abs(maxCriteria)) > eps) { gettingGlobalBetter = true; } maxCriteria = res; newBestBounds.Clear(); for (int w = 0; w < newBounds.Count; w++) { newBestBounds.Add(new Conditions(newBounds[w])); //новые улучшенные границы } } } } if (gettingBetter) { bestBounds.Clear(); for (int w = 0; w < newBestBounds.Count; w++) { bestBounds.Add(new Conditions(newBestBounds[w])); } } } } } double beforeCriteria = OneIntervalCriteria(model.xBounds, model); model.criteria = maxCriteria; model.SetBounds(bestBounds); return(beforeCriteria); }