Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.12 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "stdafx.h"
  3. #include "iostream"
  4. #include "complex"
  5. #include "math.h"
  6.  
  7. int k = 0;
  8. using namespace std;
  9. const double epsi = 0.00005;
  10. const int degree = 5;
  11. int iteration = 0;
  12. int counting = 0;
  13. complex<double> root[degree];
  14. 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)};
  15. 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) };
  16. 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) };
  17.  
  18. complex<double> f(complex<double> x, complex<double> kf[degree + 1])
  19. {
  20. complex<double> ans = 0;
  21. for (int i = 0; i < degree + 1; i++) {
  22. ans = ans + kf[i] * pow(x, i);
  23. }
  24. return ans;
  25. }
  26.  
  27. complex<double> grad(complex<double> z, complex<double> kf[degree + 1], complex<double> kfDx[degree + 1]) {
  28. complex<double> Pz = f(z,kf);
  29. complex<double> Pzconj = conj(Pz);
  30. complex<double> PzDx = f(z,kfDx);
  31. complex<double> gr = Pzconj * PzDx;
  32. return (conj(gr));
  33. }
  34.  
  35. void new_kfDx(complex<double> kf[degree + 1]) {
  36. for (int j = 0; j < degree; j++) {
  37. kfDx[j] = kf[j+1] * double(j+1);
  38. }
  39. }
  40.  
  41. void Gorner(complex<double> x, complex<double> kf[degree + 1]) {
  42. for (int i = degree - 1; i >= 0; i--) {
  43. kf[i] = kf[i] + x * kf[i + 1];
  44. }
  45. for (int i = 0; i < degree; i++) {
  46. kf[i] = kf[i + 1];
  47. }
  48. kf[degree] = 0;
  49. }
  50.  
  51. complex<double> SpuskCoorinat_2(complex<double> mins, complex<double> kf[degree + 1]) { // покоординатный спуск
  52. int iter = 0;
  53. double h = 1;
  54. complex<double> x, x1;
  55. FILE *ps;
  56. ps = fopen("D:\\optimization\\KS.txt", "at"); //Дописывает информацию к концу текстового файла.
  57. 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());// записываем в файл промежуточные значения
  58. do
  59. {
  60. do
  61. {
  62. x1 = mins;
  63. x = mins + complex<double>(h, 0);
  64. if (abs(f(mins, kf)) > abs(f(x, kf))) {
  65. mins = mins + complex<double>(h, 0);
  66. iter++;
  67. 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());// записываем в файл промежуточные значения
  68. }
  69. else {
  70. x = mins - complex<double>(h, 0);
  71.  
  72. if (abs(f(mins, kf)) > abs(f(x, kf))) {
  73. mins = mins - complex<double>(h, 0);
  74. iter++;
  75. 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());// записываем в файл промежуточные значения
  76. }
  77. }
  78. } while (x1 != mins);
  79. do
  80. {
  81. x1 = mins;
  82. x = mins + complex<double>(0, h);
  83.  
  84. if (abs(f(mins, kf)) > abs(f(x, kf))) {
  85. mins = mins + complex<double>(0, h);
  86. iter++;
  87. 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());// записываем в файл промежуточные значения
  88. }
  89. else {
  90. x = mins - complex<double>(0, h);
  91. if (abs(f(mins, kf)) > abs(f(x, kf))) {
  92. mins = mins - complex<double>(0, h);
  93. iter++;
  94. 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());// записываем в файл промежуточные значения
  95. }
  96. }
  97. } while (x1 != mins);
  98. h /= 10;
  99. } while (abs(f(mins, kf)) > epsi);
  100.  
  101. iteration = iteration + iter;
  102. fprintf(ps, "-----------------------------------------------------------------------\n");
  103. fclose(ps);
  104. return mins;
  105. }
  106.  
  107. complex<double> Gradient_Drop_Step(complex<double> z, complex<double> kf[degree + 1]) {
  108. FILE *ps;
  109. ps = fopen("D:\\optimization\\GDS.txt", "at"); //Дописывает информацию к концу текстового файла.
  110. new_kfDx(kf);
  111. int iter = 0;
  112. int count = 0;
  113. complex<double> x1;
  114. complex<double> x = z;
  115. double a = 0.1;
  116. complex<double> gr;
  117. 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());// записываем в файл промежуточные значения
  118. do {
  119. gr = grad(x, kf, kfDx);
  120. x1 = x - a * gr;
  121. count = count + 6;
  122. if (abs(f(x1,kf)) - abs(f(x,kf)) > -a * epsi*abs(pow(gr, 2))) {
  123. a = a * 0.1;
  124. }
  125. else {
  126. x = x1;
  127. iter++;
  128. 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());// записываем в файл промежуточные значения
  129. }
  130. } while (abs(grad(x1,kf,kfDx)) > epsi);
  131. iteration = iteration + iter;
  132. counting = counting + count;
  133. fprintf(ps, "-----------------------------------------------------------------------------------------------\n");
  134. fclose(ps);
  135. return x1;
  136. }
  137.  
  138. complex<double> Gradient_Const_Step(complex<double> z, complex<double> kf[degree + 1]) {
  139. FILE *ps;
  140. ps = fopen("D:\\optimization\\GCS.txt", "at"); //Дописывает информацию к концу текстового файла.
  141. complex<double> x1;
  142. complex<double> x = z;
  143. int iter = 0;
  144. new_kfDx(kf);
  145. 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());// записываем в файл промежуточные значения
  146. double a = 0.0001;
  147. do {
  148. x1 = x - a * grad(x, kf, kfDx);
  149. x = x1;
  150. iter++;
  151. 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());// записываем в файл промежуточные значения
  152. } while (abs(grad(x1, kf, kfDx)) > epsi);
  153. iteration = iteration + iter;
  154. fprintf(ps, "-----------------------------------------------------------------------------------------------\n");
  155. fclose(ps);
  156. return x1;
  157. }
  158.  
  159. complex<double> Gradient_Set_Step(complex<double> z, complex<double> kf[degree + 1]) {
  160. FILE *ps;
  161. ps = fopen("D:\\optimization\\GSS.txt", "at"); //Дописывает информацию к концу текстового файла.
  162. new_kfDx(kf);
  163. int iter = 0;
  164. complex<double> x1;
  165. complex<double> x = z;
  166. 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());// записываем в файл промежуточные значения
  167. double k = 10000;
  168. do {
  169. x1 = x - (1/k) * grad(x, kf, kfDx);
  170. x = x1;
  171. k++;
  172. iter++;
  173. 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());// записываем в файл промежуточные значения
  174. } while (abs(grad(x1, kf, kfDx)) > epsi);
  175. iteration = iteration + iter;
  176. fprintf(ps, "-----------------------------------------------------------------------------------------------\n");
  177. fclose(ps);
  178. return x1;
  179. }
  180.  
  181. void Poisk_korney_KS(complex<double> kf[degree + 1]) {
  182. FILE *ps;
  183. ps = fopen("D:\\optimization\\KS.txt", "w");//создаем текстовый файл в //который будут записываться выходные данные
  184. fprintf(ps, "Координатный спуск\n");
  185. fprintf(ps, "------------------------------\n");
  186. fprintf(ps, " Итер | Y | x |\n"); //«шапка» таблицы
  187. fprintf(ps, "------------------------------\n");
  188. complex<double> x;
  189. fclose(ps);
  190. for (int i = 0; i < degree; i++) {
  191. x = SpuskCoorinat_2(0,kf);
  192. x = SpuskCoorinat_2(x,first_kf);
  193. root[i] = x;
  194. ps = fopen("D:\\optimization\\KS.txt", "at");
  195. 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());
  196. fprintf(ps, "-----------------------------------------------------------------------\n");
  197. fclose(ps);
  198. Gorner(x,kf);
  199. }
  200. ps = fopen("D:\\optimization\\KS.txt", "at");
  201. fprintf(ps, "Итераций:%4d \n", iteration);
  202. for (int i = 0; i < degree; i++) {
  203. 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());
  204. }
  205. fprintf(ps, "\nКоличество вычисления целевой функции:%4d\n", iteration*4);
  206. iteration = 0;
  207. counting = 0;
  208. fclose(ps);
  209. for (int i = 0; i < degree + 1; i++) {
  210. kf[i] = first_kf[i];
  211. }
  212. }
  213.  
  214. void Poisk_korney_GDS(complex<double> kf[degree + 1]) {
  215. FILE *ps;
  216. ps = fopen("D:\\optimization\\GDS.txt", "w");//создаем текстовый файл в //который будут записываться выходные данные
  217. fprintf(ps, "Градиентный спуск с дробением шага\n");
  218. fprintf(ps, "-------------------------------------------\n");
  219. fprintf(ps, " Итер | Y | x | grad |\n"); //«шапка» таблицы
  220. fprintf(ps, "-------------------------------------------\n");
  221. complex<double> x;
  222. fclose(ps);
  223. for (int i = 0; i < degree; i++) {
  224. x = Gradient_Drop_Step(0,kf);
  225. x = Gradient_Drop_Step(x, first_kf);
  226. root[i] = x;
  227. ps = fopen("D:\\optimization\\GDS.txt", "at");
  228. 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());
  229. fprintf(ps, "-----------------------------------------------------------------------------------------------\n");
  230. fclose(ps);
  231. Gorner(x, kf);
  232. }
  233. ps = fopen("D:\\optimization\\GDS.txt", "at");
  234. fprintf(ps, "Итераций:%4d \n", iteration);
  235. for (int i = 0; i < degree; i++) {
  236. 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());
  237. }
  238. fprintf(ps, "\nКоличество вычисления целевой функции:%4d\n", counting);
  239. for (int i = 0; i < degree + 1; i++) {
  240. kf[i] = first_kf[i];
  241. }
  242. iteration = 0;
  243. counting = 0;
  244. fclose(ps);
  245. }
  246.  
  247. void Poisk_korney_GCS(complex<double> kf[degree + 1]) {
  248. FILE *ps;
  249. ps = fopen("D:\\optimization\\GCS.txt", "w");//создаем текстовый файл в //который будут записываться выходные данные
  250. fprintf(ps, "Градиентный спуск с постоянным шагом\n");
  251. fprintf(ps, "-------------------------------------------\n");
  252. fprintf(ps, " Итер | Y | x | grad |\n"); //«шапка» таблицы
  253. fprintf(ps, "-------------------------------------------\n");
  254. fclose(ps);
  255. complex<double> x;
  256. for (int i = 0; i < degree; i++) {
  257. x = Gradient_Const_Step(0, kf);
  258. x = Gradient_Const_Step(x, first_kf);
  259. root[i] = x;
  260. ps = fopen("D:\\optimization\\GCS.txt", "at");
  261. 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());
  262. fprintf(ps, "-----------------------------------------------------------------------------------------------\n");
  263. fclose(ps);
  264. Gorner(x, kf);
  265. }
  266. ps = fopen("D:\\optimization\\GCS.txt", "at");
  267. fprintf(ps, "Итераций:%4d \n", iteration);
  268. for (int i = 0; i < degree; i++) {
  269. 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());
  270. }
  271. fprintf(ps, "\nКоличество вычисления целевой функции:%4d\n", iteration*4);
  272. for (int i = 0; i < degree + 1; i++) {
  273. kf[i] = first_kf[i];
  274. }
  275. iteration = 0;
  276. counting = 0;
  277. fclose(ps);
  278. }
  279.  
  280. void Poisk_korney_GSS(complex<double> kf[degree + 1]) {
  281. FILE *ps;
  282. ps = fopen("D:\\optimization\\GSS.txt", "w");//создаем текстовый файл в //который будут записываться выходные данные
  283. fprintf(ps, "Градиентный спуск с заранее заданным шагом\n");
  284. fprintf(ps, "-------------------------------------------\n");
  285. fprintf(ps, " Итер | Y | x | grad |\n"); //«шапка» таблицы
  286. fprintf(ps, "-------------------------------------------\n");
  287. complex<double> x;
  288. fclose(ps);
  289. for (int i = 0; i < degree; i++) {
  290. x = Gradient_Set_Step(0, kf);
  291. x = Gradient_Set_Step(x, first_kf);
  292. root[i] = x;
  293. ps = fopen("D:\\optimization\\GSS.txt", "at");
  294. 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());
  295. fprintf(ps, "-----------------------------------------------------------------------------------------------\n");
  296. fclose(ps);
  297. Gorner(x, kf);
  298. }
  299. ps = fopen("D:\\optimization\\GSS.txt", "at");
  300. fprintf(ps, "Итераций:%4d \n", iteration);
  301. for (int i = 0; i < degree; i++) {
  302. 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());
  303. }
  304. fprintf(ps, "\nКоличество вычисления целевой функции:%4d\n", iteration*4);
  305. for (int i = 0; i < degree + 1; i++) {
  306. kf[i] = first_kf[i];
  307. }
  308. iteration = 0;
  309. counting = 0;
  310. fclose(ps);
  311. }
  312.  
  313. int main() {
  314. Poisk_korney_KS(kf);
  315. Poisk_korney_GSS(kf);
  316. Poisk_korney_GDS(kf);
  317. Poisk_korney_GCS(kf);
  318. system("pause");
  319. return 0;
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement