Метод Пауэлла

Автор работы: Пользователь скрыл имя, 30 Ноября 2013 в 23:10, курсовая работа

Краткое описание

Цель данной курсовой работы:
- проанализировать и обработать теоретические и практические данные по методу Пауэлла;
- провести сравнительный анализ с другими методами;
- разработка программы, реализующая данный метод.
Постановка задачи
Изучить частный случай метода сопряженных направлений – метод Пауэлла.
Изучить алгоритмы реализации поиска минимума функции f(x) .
Реализовать пользовательский интерфейс.

Содержание

ВВЕДЕНИЕ…………………………………………………………………….…….4
ПОСТАНОВКА ЗАДАЧИ………………………………………….…………….…5
1 Метод Пауэлла и сопряженные направления 6
1.1 Обоснование применения сопряженных направлений в алгоритмах оптимизации. 6
1.2 Метод Пауэлла. 9
1.3 Стратегия поиска 11
1.4 Блок схема алгоритма метода сопряженных направлений 12
1.5 Пример поиска минимума функции методом Пауэлла 15
2 Описание программной части. Выбор среды программирования 16
3 Руководство пользователя 17
4 Описание программы 20
ВЫВОДЫ……………………………………….…………………………………..23
СПИСОК ИСПОЛЬЗУЕМОЙ ЛИТЕРАТУРЫ……………

Прикрепленные файлы: 1 файл

Метод Пауэлла.docx

— 2.48 Мб (Скачать документ)

        private double Function(double x1, double x2)

        {

            double xsquare = double.Parse(XSquare.Text);

            double xdouble = double.Parse(Xdouble.Text); ;

            double xone = double.Parse(X1.Text);

            double xtwo = double.Parse(X2.Text);

            double c = double.Parse(C.Text);

            return xsquare*Math.Pow(x1, 3) +

              xdouble*  Math.Pow(x2, 2) +

                xone * x1 +

                xtwo * x2 +

                c;

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            if (checkBox1.Checked)

            {

                x[0] = 8;

                x[1] = 9;

                E = 0.1;

                d1[0] = 1;

                d1[1] = 0;

                d2[0] = 0;

                d2[1] = 1;

                d0[0] = d2[0];

                d0[1] = d2[1];

                textX0.Text = x[0].ToString();

                textX1.Text = x[1].ToString();

                textE.Text = E.ToString();

                textD1x0.Text = d1[0].ToString();

                textD1x1.Text = d1[1].ToString();

                textD2x0.Text = d2[0].ToString();

                textD2x1.Text = d2[1].ToString();

            }

            else

            {

                x[0] = Convert.ToDouble(textX0.Text);

                x[1] = Convert.ToDouble(textX1.Text);

                E = Convert.ToDouble(textE.Text);

                d1[0] = Convert.ToDouble(textD1x0.Text);

                d1[1] = Convert.ToDouble(textD1x1.Text);

                d2[0] = Convert.ToDouble(textD2x0.Text);

                d2[1] = Convert.ToDouble(textD2x1.Text);

                d0[0] = d2[0];

                d0[1] = d2[1];

            }

            richTextBox1.Text = "";

            double t = 0;

            i = 0;

            y[0] = x[0];

            y[1] = x[1];

            y0[0] = y[0];

            y0[1] = y[1];

            k = 0;

            j = 0;

            if (E >= 0.1)

            {

                if (j == 0)

                    richTextBox1.Text += "\t\t\tИтерация " + Convert.ToString(j + 1) + "\n\n\tШаг 1. \nНачальная точка х = (" + x[0].ToString() + " ; " + x[1].ToString() + "), ε = " + E.ToString() + "\nd1 = (" + d1[0].ToString() + ";" + d1[1].ToString() + ") d2 = (" + d2[0].ToString() + ";" + d2[1].ToString() + ") d0 = dn\ni = " + i.ToString() + " k=" + k.ToString() + " y = x = (" + x[0].ToString() + ";" + x[1].ToString() + ")\n";

            step_2:

                if (j != 0)

                    richTextBox1.Text += "\n\t\t\tИтерация " + Convert.ToString(j + 1) + "\n";

                if (checkBox1.Checked)

                {

                    switch (j)

                    {

                        case 0: t = -8; break;

                        case 1: t = -7; break;

                        case 2: t = 0; break;

                        case 3: t = 0; break;

                        case 4: t = 0; break;

                    }

                    if (i == 0)

                    {

                        y[0] = y[0] + t * d0[0];

                        y[1] = y[1] + t * d0[1];

                        y1[0] = y[0];

                        y1[1] = y[1];

                    }

                    else

                        if (i == 2)

                        {

                            y[0] = y[0] + t * d2[0];

                            y[1] = y[1] + t * d2[1];

                        }

                        else

                        {

                            y[0] = y[0] + t * d1[0];

                            y[1] = y[1] + t * d1[1];

                        }

                }

                else

                {

                    if (i == 0)

                    {

                        t = extremum(d0);

                        y[0] = y[0] + t * d0[0];

                        y[1] = y[1] + t * d0[1];

                    }

 

                    if (i == 2)

                    {

                        t = extremum(d2);

                        y[0] = y[0] + t * d2[0];

                        y[1] = y[1] + t * d2[1];

                    }

                    else

                    {

                        t = extremum(d1);

                        y[0] = y[0] + t * d1[0];

                        y[1] = y[1] + t * d1[1];

                        y1[0] = y[0];

                        y1[1] = y[1];

                    }

                }

                j++;

                richTextBox1.Text += "\n\tШаг 2.\n y = (" + y[0].ToString() + " ; " + y[1].ToString() + ")\tt" + i.ToString() + " = " + t.ToString() + "\n";

                //Шаг 3

                richTextBox1.Text += "\n\tШаг 3.\n";

                if (i < n - 1)

                {

                    i++;

                    richTextBox1.Text += "Т.к. i=" + Convert.ToString(i - 1) + "< n-1 = 1, положим i = i+1 = " + Convert.ToString(i + 1);

                    goto step_2;

                }

                else

                    if (i == n - 1)

                    {

                        richTextBox1.Text += "y2=(" + y[0].ToString() + ";" + y[1].ToString() + ")\ty0=(" + y0[0].ToString() + ";" + y0[1].ToString() + ")";

                        if (y[0] == y0[0] && y[1] == y0[1])

                        {

                            //то поиск завершить

                            richTextBox1.Text += "\ni = " + i.ToString() + "\ni=n-1\ny2=y0\nПоиск завершен х* = (" + y[0].ToString() + ";" + y[1].ToString() + ")\n\n\tВсего итераций " + j.ToString();

                        }

                        else

                        {

                            richTextBox1.Text += "\ni = " + i.ToString() + "\ni=n-1\ny2 != y0\n Положим i = i+1 = 2 и перейдем к шагу 2\n";

                            i++;

                            goto step_2;

                        }

                    }

                    else

                    {

                        if (i == n)

                        {

                            richTextBox1.Text += "\ni = " + i.ToString() + "\ti = n";

                            richTextBox1.Text += "\ny3 = (" + y[0].ToString() + ";" + y[1].ToString() + ")\ty1 = (" + y1[0].ToString() + ";" + y1[1].ToString() + ")";

                            if (y[0] == y1[0] && y[1] == y1[1])

                            {

                                //то поиск завершить

                                richTextBox1.Text += "y3 = y1\nПоиск завершен х* = y3 = (" + y[0].ToString() + ";" + y[1].ToString() + ")\n\n\tВсего итераций " + j.ToString();

                            }

                            else

                            {

                                richTextBox1.Text += "\ny3 !=y1\nПерейдем к шагу 4 для построения сопряженного направления";

                                //step_4:

                                richTextBox1.Text += "\n\n\tШаг 4. \n";

                                x_st[0] = x[0];

                                x_st[1] = x[1];

                                x[0] = y[0];

                                x[1] = y[1];

                                richTextBox1.Text += "k = " + k.ToString() + " Положим x_k+1 = y = (" + x[0].ToString() + ";" + x[1].ToString() + ") и проверим условие окончания\n";

 

                                if (Math.Sqrt(Math.Pow((x_st[0] - x[0]), 2) + Math.Pow((x_st[1] - x[1]), 2)) < E)

                                {

                                    //то поиск завершить

                                    richTextBox1.Text += "||x_k+1 - x_k|| < ε\n||(" + x[0].ToString() + ";" + x[1].ToString() + ") - (" + x_st[0].ToString() + ";" + x_st[1].ToString() + ")|| = " + Math.Sqrt(Math.Pow((x_st[0] - x[0]), 2) + Math.Pow((x_st[1] - x[1]), 2)) + " < " + E.ToString();

                                    richTextBox1.Text += "\nПоиск завершен х* = x_k+1 = (" + x[0].ToString() + ";" + x[1].ToString() + ")\n\n\tВсего итераций " + j.ToString();

                                }

                                else

                                {

                                    richTextBox1.Text += "||x_k+1 - x_k|| >= ε\n||(" + x[0].ToString() + ";" + x[1].ToString() + ") - (" + x_st[0].ToString() + ";" + x_st[1].ToString() + ")|| = " + Math.Sqrt(Math.Pow((x_st[0] - x[0]), 2) + Math.Pow((x_st[1] - x[1]), 2)) + " >= " + E.ToString();

                                    d2_n[0] = y[0] - y1[0];

                                    d2_n[1] = y[1] - y1[1];

                                    d0_n[0] = d2_n[0];

                                    d0_n[1] = d2_n[1];

                                    d1_n[0] = d2[0];

                                    d1_n[1] = d2[1];

                                    richTextBox1.Text += "\nПоложим новое направление _d0 = _dn = y3-y1 = (" + y[0].ToString() + ";" + y[1].ToString() + ") - (" + y1[0].ToString() + ";" + y1[1].ToString() + ") = (" + d2_n[0].ToString() + ";" + d2_n[1].ToString() + ")\n";

                                    richTextBox1.Text += "\nИсключим старое направление _d1 = d2 = (" + d1_n[0].ToString() + ";" + d1_n[1].ToString() + ")";

                                    Matrica[0, 0] = d1_n[0];

                                    Matrica[0, 1] = d2_n[0];

                                    Matrica[1, 0] = d1_n[1];

                                    Matrica[1, 1] = d2_n[1];

                                    if (Rang_matrici(Matrica) == n)

                                    {

                                        richTextBox1.Text += "Т.к. rang(" + d1_n[0].ToString() + ", " + d1_n[1].ToString() + " ; " + d2_n[0].ToString() + ", " + d2_n[1].ToString() + ")=" + n.ToString() + " новая система направлений линейно независима\n";

                                        d0[0] = d0_n[0];

                                        d0[1] = d0_n[1];

                                        d1[0] = d1_n[0];

                                        d1[1] = d1_n[1];

                                        d2[0] = d2_n[0];

                                        d2[1] = d2_n[1];

                                        k++;

                                        i = 0;

                                        y[0] = x[0];

                                        y[1] = x[1];

                                        y0[0] = y[0];

                                        y0[1] = y[1];

                                        richTextBox1.Text += "Положим \n\td1 = _d1 = (" + d1_n[0].ToString() + ";" + d1_n[1].ToString() + ")\n\t d2 = _d2 = (" + d2_n[0].ToString() + ";" + d2_n[1].ToString() + ")\n\tk=" + k.ToString() + " i=" + i.ToString();

                                        richTextBox1.Text += "\ty0=(" + y[0].ToString() + ";" + y[1].ToString() + ")\n";

                                        goto step_2;

                                    }

                                    if (Rang_matrici(Matrica) < n)

                                    {

                                        richTextBox1.Text += "Т.к. rang(" + d1_n[0].ToString() + ", " + d1_n[1].ToString() + " ; " + d2_n[0].ToString() + ", " + d2_n[1].ToString() + ")=" + Rang_matrici(Matrica) + " < " + n.ToString() + " новая система направлений линейно зависима\n";

                                        y[0] = x[0];

                                        y[1] = x[1];

                                        y0[0] = y[0];

                                        y0[1] = y[1];

                                        k++;

                                        i = 0;

                                        richTextBox1.Text += "Положим: k=" + k.ToString() + " i=" + i.ToString() + " y0 = (" + y[0].ToString() + ";" + y[1].ToString() + ") и перейдем к шагу 2\n";

                                        goto step_2;

                                    }

                                }

                            }

                        }

                    }

            }

            else

                MessageBox.Show("Введите е >=0,1", "Просьба!");

        }

 

        private double extremum(double[] u) // u[] это d[]

        {

            double a = -2 * y[0];

            double b = 2 * y[0];

            double eps = (b - a) / 50;

            double l = (b - a) / 10;

            int g = 0; //счетчик

            double s; //y

            double w; //z

        step_3: s = (a + b - eps) / 2;

            w = (a + b + eps) / 2;

            if (Function_for_search_extremum(y, u, s) <= Function_for_search_extremum(y, u, w))

            {

                b = w;

            }

            else

            {

                a = s;

            }

            if (Math.Abs(b - a) <= l)

                return (a + b) / 2;

            else

            {

                g++;

                goto step_3;

            }

 

 

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            textE.Clear();

            textX0.Clear();

            textX1.Clear();

 

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

            MenuFileSaveAs();

 

        }

 

        private void MenuFileSaveAs()

        {

            saveFileDialog1.Filter = "Text files|*.txt";

            if (saveFileDialog1.ShowDialog() == DialogResult.OK && saveFileDialog1.FileName.Length > 0)

            {

                richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.UnicodePlainText);

            }

        }

 

        private void textX0_KeyPress(object sender, KeyPressEventArgs e)

        {

            if (e.KeyChar != 8 && (e.KeyChar < 48 || e.KeyChar > 57))

                e.Handled = true;

            if (e.KeyChar == 44)

                e.Handled = false;

 

        }

 

        private void textX1_KeyPress(object sender, KeyPressEventArgs e)

        {

            if (e.KeyChar != 8 && (e.KeyChar < 48 || e.KeyChar > 57))

                e.Handled = true;

            if (e.KeyChar == 44)

                e.Handled = false;

 

        }

 

        private void textE_KeyPress(object sender, KeyPressEventArgs e)

        {

            if (e.KeyChar != 8 && (e.KeyChar < 48 || e.KeyChar > 57))

                e.Handled = true;

            if (e.KeyChar == 44)

                e.Handled = false;

 

        }

 

        private void textD1x0_KeyPress(object sender, KeyPressEventArgs e)

        {

            if (e.KeyChar != 8 && (e.KeyChar < 48 || e.KeyChar > 57))

                e.Handled = true;

            if (e.KeyChar == 44)

                e.Handled = false;

 

        }

 

        private void textD1x1_KeyPress(object sender, KeyPressEventArgs e)

        {

            if (e.KeyChar != 8 && (e.KeyChar < 48 || e.KeyChar > 57))

                e.Handled = true;

Информация о работе Метод Пауэлла