Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include "stdafx.h"
- #include "iostream"
- #include "complex"
- #include "math.h"
- int k = 0;
- using namespace std;
- const double epsi = 0.00005;
- const int degree = 5;
- int iteration = 0;
- int counting = 0;
- complex<double> root[degree];
- complex<double> first_kf[degree + 1] = { complex<double>(0, 1), complex<double>(14, 0), complex<double>(1, 0), complex<double>(18, 0), complex<double>(12, 0), complex<double>(6, 0)};
- complex<double> kf[degree + 1] = { complex<double>(0, 1), complex<double>(14, 0), complex<double>(1, 0), complex<double>(18, 0), complex<double>(12, 0), complex<double>(6, 0) };
- complex<double> kfDx[degree + 1];// = { complex<double>(14, 0), complex<double>(2, 0), complex<double>(54 , 0), complex<double>(48, 0), complex<double>(30, 0), complex<double>(78, 0), complex<double>(112, 0), complex<double>(24, 0), complex<double>(9, 0) };
- complex<double> f(complex<double> x, complex<double> kf[degree + 1])
- {
- complex<double> ans = 0;
- for (int i = 0; i < degree + 1; i++) {
- ans = ans + kf[i] * pow(x, i);
- }
- return ans;
- }
- complex<double> grad(complex<double> z, complex<double> kf[degree + 1], complex<double> kfDx[degree + 1]) {
- complex<double> Pz = f(z,kf);
- complex<double> Pzconj = conj(Pz);
- complex<double> PzDx = f(z,kfDx);
- complex<double> gr = Pzconj * PzDx;
- return (conj(gr));
- }
- void new_kfDx(complex<double> kf[degree + 1]) {
- for (int j = 0; j < degree; j++) {
- kfDx[j] = kf[j+1] * double(j+1);
- }
- }
- void Gorner(complex<double> x, complex<double> kf[degree + 1]) {
- for (int i = degree - 1; i >= 0; i--) {
- kf[i] = kf[i] + x * kf[i + 1];
- }
- for (int i = 0; i < degree; i++) {
- kf[i] = kf[i + 1];
- }
- kf[degree] = 0;
- }
- complex<double> SpuskCoorinat_2(complex<double> mins, complex<double> kf[degree + 1]) { // покоординатный спуск
- int iter = 0;
- double h = 1;
- complex<double> x, x1;
- FILE *ps;
- ps = fopen("D:\\optimization\\KS.txt", "at"); //Дописывает информацию к концу текстового файла.
- fprintf(ps, "%4d | (%6.10f, %6.10f) | (%6.10f, %6.10f) |\n", iter, f(mins, kf).real(), f(mins, kf).imag(), mins.real(), mins.imag());// записываем в файл промежуточные значения
- do
- {
- do
- {
- x1 = mins;
- x = mins + complex<double>(h, 0);
- if (abs(f(mins, kf)) > abs(f(x, kf))) {
- mins = mins + complex<double>(h, 0);
- iter++;
- fprintf(ps, "%4d | (%6.10f, %6.10f) | (%6.10f, %6.10f) |\n", iter, f(mins, kf).real(), f(mins, kf).imag(), mins.real(), mins.imag());// записываем в файл промежуточные значения
- }
- else {
- x = mins - complex<double>(h, 0);
- if (abs(f(mins, kf)) > abs(f(x, kf))) {
- mins = mins - complex<double>(h, 0);
- iter++;
- fprintf(ps, "%4d | (%6.10f, %6.10f) | (%6.10f, %6.10f) |\n", iter, f(mins, kf).real(), f(mins, kf).imag(), mins.real(), mins.imag());// записываем в файл промежуточные значения
- }
- }
- } while (x1 != mins);
- do
- {
- x1 = mins;
- x = mins + complex<double>(0, h);
- if (abs(f(mins, kf)) > abs(f(x, kf))) {
- mins = mins + complex<double>(0, h);
- iter++;
- fprintf(ps, "%4d | (%6.10f, %6.10f) | (%6.10f, %6.10f) |\n", iter, f(mins, kf).real(), f(mins, kf).imag(), mins.real(), mins.imag());// записываем в файл промежуточные значения
- }
- else {
- x = mins - complex<double>(0, h);
- if (abs(f(mins, kf)) > abs(f(x, kf))) {
- mins = mins - complex<double>(0, h);
- iter++;
- fprintf(ps, "%4d | (%6.10f, %6.10f) | (%6.10f, %6.10f) |\n", iter, f(mins, kf).real(), f(mins, kf).imag(), mins.real(), mins.imag());// записываем в файл промежуточные значения
- }
- }
- } while (x1 != mins);
- h /= 10;
- } while (abs(f(mins, kf)) > epsi);
- iteration = iteration + iter;
- fprintf(ps, "-----------------------------------------------------------------------\n");
- fclose(ps);
- return mins;
- }
- complex<double> Gradient_Drop_Step(complex<double> z, complex<double> kf[degree + 1]) {
- FILE *ps;
- ps = fopen("D:\\optimization\\GDS.txt", "at"); //Дописывает информацию к концу текстового файла.
- new_kfDx(kf);
- int iter = 0;
- int count = 0;
- complex<double> x1;
- complex<double> x = z;
- double a = 0.1;
- complex<double> gr;
- fprintf(ps, "%4d | (%6.10f, %6.10f) | (%6.10f, %6.10f) | (%6.10f, %6.10f) |\n", iter, f(x, kf).real(), f(x, kf).imag(), x.real(), x.imag(), grad(x, kf, kfDx).real(), grad(x, kf, kfDx).imag());// записываем в файл промежуточные значения
- do {
- gr = grad(x, kf, kfDx);
- x1 = x - a * gr;
- count = count + 6;
- if (abs(f(x1,kf)) - abs(f(x,kf)) > -a * epsi*abs(pow(gr, 2))) {
- a = a * 0.1;
- }
- else {
- x = x1;
- iter++;
- fprintf(ps, "%4d | (%6.10f, %6.10f) | (%6.10f, %6.10f) | (%6.10f, %6.10f) |\n", iter, f(x1, kf).real(), f(x1, kf).imag(), x1.real(), x1.imag(), grad(x1, kf, kfDx).real(), grad(x1, kf, kfDx).imag());// записываем в файл промежуточные значения
- }
- } while (abs(grad(x1,kf,kfDx)) > epsi);
- iteration = iteration + iter;
- counting = counting + count;
- fprintf(ps, "-----------------------------------------------------------------------------------------------\n");
- fclose(ps);
- return x1;
- }
- complex<double> Gradient_Const_Step(complex<double> z, complex<double> kf[degree + 1]) {
- FILE *ps;
- ps = fopen("D:\\optimization\\GCS.txt", "at"); //Дописывает информацию к концу текстового файла.
- complex<double> x1;
- complex<double> x = z;
- int iter = 0;
- new_kfDx(kf);
- fprintf(ps, "%4d | (%6.10f, %6.10f) | (%6.10f, %6.10f) | (%6.10f, %6.10f) |\n", iter, f(x, kf).real(), f(x, kf).imag(), x.real(), x.imag(), grad(x, kf, kfDx).real(), grad(x, kf, kfDx).imag());// записываем в файл промежуточные значения
- double a = 0.0001;
- do {
- x1 = x - a * grad(x, kf, kfDx);
- x = x1;
- iter++;
- fprintf(ps, "%4d | (%6.10f, %6.10f) | (%6.10f, %6.10f) | (%6.10f, %6.10f) |\n", iter, f(x, kf).real(), f(x, kf).imag(), x.real(), x.imag(), grad(x, kf, kfDx).real(), grad(x, kf, kfDx).imag());// записываем в файл промежуточные значения
- } while (abs(grad(x1, kf, kfDx)) > epsi);
- iteration = iteration + iter;
- fprintf(ps, "-----------------------------------------------------------------------------------------------\n");
- fclose(ps);
- return x1;
- }
- complex<double> Gradient_Set_Step(complex<double> z, complex<double> kf[degree + 1]) {
- FILE *ps;
- ps = fopen("D:\\optimization\\GSS.txt", "at"); //Дописывает информацию к концу текстового файла.
- new_kfDx(kf);
- int iter = 0;
- complex<double> x1;
- complex<double> x = z;
- fprintf(ps, "%4d | (%6.10f, %6.10f) | (%6.10f, %6.10f) | (%6.10f, %6.10f) |\n", iter, f(x, kf).real(), f(x, kf).imag(), x.real(), x.imag(), grad(x, kf, kfDx).real(), grad(x, kf, kfDx).imag());// записываем в файл промежуточные значения
- double k = 10000;
- do {
- x1 = x - (1/k) * grad(x, kf, kfDx);
- x = x1;
- k++;
- iter++;
- fprintf(ps, "%4d | (%6.10f, %6.10f) | (%6.10f, %6.10f) | (%6.10f, %6.10f) |\n", iter, f(x, kf).real(), f(x, kf).imag(), x.real(), x.imag(), grad(x, kf, kfDx).real(), grad(x, kf, kfDx).imag());// записываем в файл промежуточные значения
- } while (abs(grad(x1, kf, kfDx)) > epsi);
- iteration = iteration + iter;
- fprintf(ps, "-----------------------------------------------------------------------------------------------\n");
- fclose(ps);
- return x1;
- }
- void Poisk_korney_KS(complex<double> kf[degree + 1]) {
- FILE *ps;
- ps = fopen("D:\\optimization\\KS.txt", "w");//создаем текстовый файл в //который будут записываться выходные данные
- fprintf(ps, "Координатный спуск\n");
- fprintf(ps, "------------------------------\n");
- fprintf(ps, " Итер | Y | x |\n"); //«шапка» таблицы
- fprintf(ps, "------------------------------\n");
- complex<double> x;
- fclose(ps);
- for (int i = 0; i < degree; i++) {
- x = SpuskCoorinat_2(0,kf);
- x = SpuskCoorinat_2(x,first_kf);
- root[i] = x;
- ps = fopen("D:\\optimization\\KS.txt", "at");
- fprintf(ps, "Корень: x = (%6.10f,%6.10f) y = (%6.10f,%6.10f)\n", root[i].real(), root[i].imag(), f(root[i], first_kf).real(), f(root[i], first_kf).imag());
- fprintf(ps, "-----------------------------------------------------------------------\n");
- fclose(ps);
- Gorner(x,kf);
- }
- ps = fopen("D:\\optimization\\KS.txt", "at");
- fprintf(ps, "Итераций:%4d \n", iteration);
- for (int i = 0; i < degree; i++) {
- fprintf(ps, "Корень: x = (%6.10f,%6.10f) y = (%6.10f,%6.10f)\n", root[i].real(), root[i].imag(), f(root[i], first_kf).real(), f(root[i], first_kf).imag());
- }
- fprintf(ps, "\nКоличество вычисления целевой функции:%4d\n", iteration*4);
- iteration = 0;
- counting = 0;
- fclose(ps);
- for (int i = 0; i < degree + 1; i++) {
- kf[i] = first_kf[i];
- }
- }
- void Poisk_korney_GDS(complex<double> kf[degree + 1]) {
- FILE *ps;
- ps = fopen("D:\\optimization\\GDS.txt", "w");//создаем текстовый файл в //который будут записываться выходные данные
- fprintf(ps, "Градиентный спуск с дробением шага\n");
- fprintf(ps, "-------------------------------------------\n");
- fprintf(ps, " Итер | Y | x | grad |\n"); //«шапка» таблицы
- fprintf(ps, "-------------------------------------------\n");
- complex<double> x;
- fclose(ps);
- for (int i = 0; i < degree; i++) {
- x = Gradient_Drop_Step(0,kf);
- x = Gradient_Drop_Step(x, first_kf);
- root[i] = x;
- ps = fopen("D:\\optimization\\GDS.txt", "at");
- fprintf(ps, "Корень: x = (%6.10f,%6.10f) y = (%6.10f,%6.10f)\n", root[i].real(), root[i].imag(), f(root[i], first_kf).real(), f(root[i], first_kf).imag());
- fprintf(ps, "-----------------------------------------------------------------------------------------------\n");
- fclose(ps);
- Gorner(x, kf);
- }
- ps = fopen("D:\\optimization\\GDS.txt", "at");
- fprintf(ps, "Итераций:%4d \n", iteration);
- for (int i = 0; i < degree; i++) {
- fprintf(ps, "Корень: x = (%6.10f,%6.10f) y = (%6.10f,%6.10f)\n", root[i].real(),root[i].imag(),f(root[i],first_kf).real(), f(root[i], first_kf).imag());
- }
- fprintf(ps, "\nКоличество вычисления целевой функции:%4d\n", counting);
- for (int i = 0; i < degree + 1; i++) {
- kf[i] = first_kf[i];
- }
- iteration = 0;
- counting = 0;
- fclose(ps);
- }
- void Poisk_korney_GCS(complex<double> kf[degree + 1]) {
- FILE *ps;
- ps = fopen("D:\\optimization\\GCS.txt", "w");//создаем текстовый файл в //который будут записываться выходные данные
- fprintf(ps, "Градиентный спуск с постоянным шагом\n");
- fprintf(ps, "-------------------------------------------\n");
- fprintf(ps, " Итер | Y | x | grad |\n"); //«шапка» таблицы
- fprintf(ps, "-------------------------------------------\n");
- fclose(ps);
- complex<double> x;
- for (int i = 0; i < degree; i++) {
- x = Gradient_Const_Step(0, kf);
- x = Gradient_Const_Step(x, first_kf);
- root[i] = x;
- ps = fopen("D:\\optimization\\GCS.txt", "at");
- fprintf(ps, "Корень: x = (%6.10f,%6.10f) y = (%6.10f,%6.10f)\n", root[i].real(), root[i].imag(), f(root[i], first_kf).real(), f(root[i], first_kf).imag());
- fprintf(ps, "-----------------------------------------------------------------------------------------------\n");
- fclose(ps);
- Gorner(x, kf);
- }
- ps = fopen("D:\\optimization\\GCS.txt", "at");
- fprintf(ps, "Итераций:%4d \n", iteration);
- for (int i = 0; i < degree; i++) {
- fprintf(ps, "Корень: x = (%6.10f,%6.10f) y = (%6.10f,%6.10f)\n", root[i].real(), root[i].imag(), f(root[i], first_kf).real(), f(root[i], first_kf).imag());
- }
- fprintf(ps, "\nКоличество вычисления целевой функции:%4d\n", iteration*4);
- for (int i = 0; i < degree + 1; i++) {
- kf[i] = first_kf[i];
- }
- iteration = 0;
- counting = 0;
- fclose(ps);
- }
- void Poisk_korney_GSS(complex<double> kf[degree + 1]) {
- FILE *ps;
- ps = fopen("D:\\optimization\\GSS.txt", "w");//создаем текстовый файл в //который будут записываться выходные данные
- fprintf(ps, "Градиентный спуск с заранее заданным шагом\n");
- fprintf(ps, "-------------------------------------------\n");
- fprintf(ps, " Итер | Y | x | grad |\n"); //«шапка» таблицы
- fprintf(ps, "-------------------------------------------\n");
- complex<double> x;
- fclose(ps);
- for (int i = 0; i < degree; i++) {
- x = Gradient_Set_Step(0, kf);
- x = Gradient_Set_Step(x, first_kf);
- root[i] = x;
- ps = fopen("D:\\optimization\\GSS.txt", "at");
- fprintf(ps, "Корень: x = (%6.10f,%6.10f) y = (%6.10f,%6.10f)\n", root[i].real(), root[i].imag(), f(root[i], first_kf).real(), f(root[i], first_kf).imag());
- fprintf(ps, "-----------------------------------------------------------------------------------------------\n");
- fclose(ps);
- Gorner(x, kf);
- }
- ps = fopen("D:\\optimization\\GSS.txt", "at");
- fprintf(ps, "Итераций:%4d \n", iteration);
- for (int i = 0; i < degree; i++) {
- fprintf(ps, "Корень: x = (%6.10f,%6.10f) y = (%6.10f,%6.10f)\n", root[i].real(), root[i].imag(), f(root[i], first_kf).real(), f(root[i], first_kf).imag());
- }
- fprintf(ps, "\nКоличество вычисления целевой функции:%4d\n", iteration*4);
- for (int i = 0; i < degree + 1; i++) {
- kf[i] = first_kf[i];
- }
- iteration = 0;
- counting = 0;
- fclose(ps);
- }
- int main() {
- Poisk_korney_KS(kf);
- Poisk_korney_GSS(kf);
- Poisk_korney_GDS(kf);
- Poisk_korney_GCS(kf);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement