Advertisement
Tetriss

Untitled

Feb 18th, 2020
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <math.h>
  6. #include <algorithm>
  7. #include <iomanip>
  8.  
  9. using namespace std;
  10.  
  11. struct Data
  12. {
  13. double h;
  14. double dist;
  15. vector<double> alpha;
  16. double w;
  17. double d;
  18. };
  19.  
  20.  
  21. double function(const double& x)
  22. {
  23. return sin(x) + 0.5;
  24. }
  25.  
  26. double random(double Min, double Max)
  27. {
  28. double f = (double)rand() / RAND_MAX;
  29. return Min + f * (Max - Min);
  30. }
  31.  
  32. double noise(const double& f, const double& a1, const double& a2)
  33. {
  34. return f + random(a1, a2);
  35. }
  36.  
  37. double xk(const int& k, const double& xmax, const double& xmin, const int& K)
  38. {
  39. return xmin + k * (xmax - xmin) / K;
  40. }
  41.  
  42. vector <double> function_noise(const vector<double>& x, const double& a1, const double& a2)
  43. {
  44. vector <double> func(101);
  45. for (auto i = 0; i <= 100; i++)
  46. {
  47. func[i] = noise(x[i], a1, a2);
  48. }
  49. return func;
  50. }
  51.  
  52. vector <double> root_mean_sqare(const vector<double>& foo, vector<double>& alpha, const int& r)
  53. {
  54. vector<double> rms(101);
  55. int M = (r - 1) / 2;
  56. for (auto i = 0; i <= 100; i++)
  57. {
  58. double sum = 1;
  59. for (auto j = i - M; j <= i + M; j++)
  60. {
  61. try
  62. {
  63. foo.at(j);
  64. //sum += foo[j] * foo[j] * alpha[j + M - i];
  65. sum *= pow(foo[j], alpha[j + M - i]);
  66. }
  67. catch (const out_of_range & s)
  68. {
  69. sum *= 1;
  70. }
  71. }
  72. rms[i] = sum;
  73. }
  74.  
  75. return rms;
  76. }
  77.  
  78. void set_alpha(vector<double>& alpha, const int& r)
  79. {
  80. alpha.clear();
  81. alpha.resize(r);
  82. alpha[(r - 1) / 2] = random(0, 1);
  83. for (auto i = (r - 1) / 2 - 1; i > 0; i--)
  84. {
  85. double sum1 = 0;
  86. for (auto i1 = i; i1 < r - i - 1; i1++)
  87. {
  88. sum1 += alpha[i1];
  89. }
  90. alpha[i] = 0.5 * random(0, 1 - sum1);
  91. sum1 = 0;
  92. }
  93. for (auto i = (r - 1) / 2 + 1; i < r - 1; i++)
  94. {
  95. alpha[i] = alpha[r - i - 1];
  96. }
  97. double sum2 = 0;
  98. for (auto i = 1; i < r - 1; i++)
  99. {
  100. sum2 += alpha[i];
  101. }
  102. alpha[0] = 0.5 * (1 - sum2);
  103. alpha[r - 1] = 0.5 * (1 - sum2);
  104. }
  105.  
  106. double prob(const double& P, const double& ep, const double& xmax, const double& xmin)
  107. {
  108. return (log(1 - P)) / (log(1 - (ep) / (xmax - xmin)));
  109. }
  110.  
  111. double dist(const double& w, const double& d)
  112. {
  113. return abs(w) + abs(d);
  114. }
  115.  
  116. double w(const vector<double>& f_filtred)
  117. {
  118. double w = 0;
  119. for (auto i = 1; i <= 100; i++)
  120. {
  121. w += abs(f_filtred[i] - f_filtred[i - 1]);
  122. }
  123. return w;
  124. }
  125.  
  126. double d(const vector<double>& f_filtred, const vector<double>& f_noise)
  127. {
  128. double d = 0;
  129. for (auto i = 0; i <= 100; i++)
  130. {
  131. d += abs(f_filtred[i] - f_noise[i]);
  132. }
  133. d /= 100;
  134. return d;
  135. }
  136.  
  137. double J(double h, double w, double d)
  138. {
  139. return h * w + (1 - h) * d;
  140. }
  141.  
  142. int main()
  143. {
  144. vector<Data> data;
  145. int r1 = 3, r2 = 5;
  146. vector <double> alpha, alphamin;
  147. double xmin = 0, xmax = M_PI;
  148. double a1 = -0.25, a2 = 0.25;
  149. int K = 100;
  150. int L = 10;
  151. double h = 0;
  152. double P = 0.95;
  153. double ep = 0.01;
  154. double W, Wmin;
  155. double D, Dmin;
  156. double Dist, Distmin = 9999999;
  157. double N = prob(P, ep, xmax, xmin);
  158. vector<double>Xk(101);
  159. for (int i = 0; i <= K; i++)
  160. {
  161. Xk[i] = xk(i, xmax, xmin, K);
  162. }
  163. vector<double> Fk(101);
  164. for (auto i = 0; i <= K; i++)
  165. {
  166. Fk[i] = function(Xk[i]);
  167. }
  168. cout << endl << "function" << endl;
  169. for (auto i : Fk) cout << i << endl;
  170. vector<double> f_noise = function_noise(Fk, a1, a2);
  171.  
  172. cout << "noise" << endl;
  173. for (auto i : f_noise) cout << i << endl;
  174. cout << endl;
  175. // r=3
  176.  
  177. vector<double> f_filtred;
  178. cout << " h dist alpha w d" << endl;
  179. for (auto l = 0; l <= 10; l++)
  180. {
  181. for (auto i = 0; i < 50; i++)
  182. {
  183. set_alpha(alpha, r1);
  184. f_filtred = root_mean_sqare(f_noise, alpha, r1);
  185. W = w(f_filtred);
  186. D = d(f_filtred, f_noise);
  187. Dist = dist(W, D);
  188.  
  189. if (Dist < Distmin)
  190. {
  191. Distmin = Dist;
  192. Wmin = W;
  193. Dmin = D;
  194. alphamin = alpha;
  195. }
  196. }
  197. h = (double)l / L;
  198. data.push_back({ h, Distmin, alphamin, Wmin, Dmin });
  199. cout << fixed << setprecision(1) << h << " " << setprecision(4) << Distmin << " [ ";
  200. for (auto i : alphamin) cout << i << " ";
  201. cout << "] " << Wmin << " " << Dmin << endl;
  202. Distmin = 900000;
  203. }
  204.  
  205. sort(data.begin(), data.end(), [](auto a, auto b) {return a.dist < b.dist; });
  206. cout << endl << " h* J w d" << endl;
  207. cout << setprecision(1) << data[0].h << " " << setprecision(4) << J(data[0].h, data[0].w, data[0].d) << " " << data[0].w << " " << data[0].d << endl;
  208. cout << endl << "filtred1" << endl;
  209. f_filtred = root_mean_sqare(f_noise, alphamin, r1);
  210. for (auto i : f_filtred) cout << i << endl;
  211. cout << endl;
  212.  
  213. // r = 5
  214. Distmin = 999999;
  215. data.clear();
  216. f_filtred.clear();
  217. alphamin.clear();
  218. Dmin = 0;
  219. Wmin = 0;
  220.  
  221. cout << endl << endl;
  222. cout << " h dist alpha w d" << endl;
  223. for (auto l = 0; l <= 10; l++)
  224. {
  225. for (auto i = 0; i < N; i++)
  226. {
  227. set_alpha(alpha, r2);
  228. f_filtred = root_mean_sqare(f_noise, alpha, r2);
  229. W = w(f_filtred);
  230. D = d(f_filtred, f_noise);
  231. Dist = dist(W, D);
  232.  
  233. if (Dist < Distmin)
  234. {
  235. Distmin = Dist;
  236. Wmin = W;
  237. Dmin = D;
  238. alphamin = alpha;
  239. }
  240. }
  241. h = (double)l / L;
  242. data.push_back({ h, Distmin, alphamin, Wmin, Dmin });
  243. cout << fixed << setprecision(1) << h << " " << setprecision(4) << Distmin << " [ ";
  244. for (auto i : alphamin) cout << i << " ";
  245. cout << "] " << Wmin << " " << Dmin << endl;
  246. }
  247. sort(data.begin(), data.end(), [](auto a, auto b) {return a.dist < b.dist; });
  248. cout << endl << " h* J w d" << endl;
  249. cout << setprecision(1) << data[0].h << " " << setprecision(4) << J(data[0].h, data[0].w, data[0].d) << " " << data[0].w << " " << data[0].d << endl;
  250. Distmin = 9000000;
  251. cout << endl << "filtred2" << endl;
  252. f_filtred = root_mean_sqare(f_noise, alphamin, r1);
  253. for (auto i : f_filtred) cout << i << endl;
  254. cout << endl;
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement