Advertisement
Guest User

tsis5_1

a guest
Jan 27th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.41 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include <iomanip>
  4.  
  5. #include <random>
  6.  
  7. #include <vector>
  8.  
  9. #include <cmath>
  10.  
  11. #include "fstream"
  12.  
  13. const double a = 0.25, P = 0.95, E = 0.01,
  14.  
  15. x_min = 0, x_max = M_PI;
  16.  
  17. const int K = 100, L = 10;
  18.  
  19. std::random_device Device;
  20.  
  21. std::mt19937 Mt(Device());
  22.  
  23. double Function(double x) {
  24.    
  25.     return sin(x) + 0.5;
  26.    
  27. }
  28.  
  29. std::vector<double> AlphaCount(int r, int M) {
  30.    
  31.     std::vector<double> Alpha(r);
  32.    
  33.     std::uniform_real_distribution<double> Rand(0, 1);
  34.    
  35.     Alpha[M] = Rand(Mt);
  36.    
  37.     for (int m = 1; m < M; m++) {
  38.        
  39.         double Sum = 0;
  40.        
  41.         for (int s = m + 1; s < r - m - 1; s++)
  42.            
  43.             Sum += Alpha[s];
  44.        
  45.         std::uniform_real_distribution<double> Rand2(0, 1 - Sum);
  46.        
  47.         Alpha[r - m - 1] = 0.5 * Rand2(Mt);
  48.        
  49.         Alpha[m] = Alpha[r - m - 1];
  50.        
  51.     }
  52.    
  53.     double Sum = 0;
  54.    
  55.     for (int s = 1; s < r - 1; s++)
  56.        
  57.         Sum += Alpha[s];
  58.    
  59.     Alpha[r - 1] = 0.5 * (1 - Sum);
  60.    
  61.     Alpha[0] = Alpha[r - 1];
  62.    
  63.     return Alpha;
  64.    
  65. }
  66.  
  67. double AverageCount(std::vector<double> &Y, std::vector<double> &Alpha, int k, int M) {
  68.    
  69.     double Return = 0;
  70.    
  71.     for (int j = k - M; j <= k + M; j++) {
  72.        
  73.         if (j < 1 || j > K + 1)
  74.            
  75.             continue;
  76.        
  77.         Return += Y.at(j - 1) * Alpha.at(j + M - k);
  78.        
  79.     }
  80.    
  81.     return Return;
  82.    
  83. }
  84.  
  85. std::vector<double> ResultCount(std::vector<double> &Y, std::vector<double> &Alpha, int M) {
  86.    
  87.     std::vector<double> Return;
  88.    
  89.     for (int k = 0; k <= K; k++) {
  90.        
  91.         double Temp = AverageCount(Y, Alpha, k, M);
  92.        
  93.         Return.emplace_back(Temp);
  94.        
  95.     }
  96.    
  97.     return Return;
  98.    
  99. }
  100.  
  101. double OmegaCount(std::vector<double> &Y) {
  102.    
  103.     double Sum = 0;
  104.    
  105.     for (int k = 1; k < K + 1; k++)
  106.        
  107.         Sum += std::abs(Y[k] - Y[k - 1]);
  108.    
  109.     return Sum;
  110.    
  111. }
  112.  
  113. double DeltaCount(std::vector<double> &Y, std::vector<double> &_Y) {
  114.    
  115.     double Sum = 0;
  116.    
  117.     for (int k = 0; k < K + 1; k++)
  118.        
  119.         Sum += std::abs(Y[k] - _Y[k]) / K;
  120.    
  121.     return Sum;
  122.    
  123. }
  124.  
  125. double JCount(double Lmd, double Omega, double Delta) {
  126.    
  127.     return (Lmd * Omega + (1 - Lmd) * Delta * 10000.) / 10000.;
  128.    
  129. }
  130.  
  131. double Dist(double Omega, double Delta) {
  132.     return std::max(std::abs(Omega), std::abs(Delta));
  133. }
  134.  
  135. void PrintLine(int r) {
  136.    
  137.     int Amount = 83;
  138.    
  139.     if (r == 5)
  140.        
  141.         Amount +=18;
  142.    
  143.     for (int i = 0; i < Amount; i++)
  144.        
  145.         std::cout << "-";
  146.    
  147.     std::cout << std::endl;
  148.    
  149. }
  150.  
  151. void Filter(int r) {
  152.    
  153.     std::vector<double> X, Y, _Y;
  154.    
  155.     for (int i = 0; i <= K; i++) {
  156.        
  157.         X.emplace_back(x_min + i * (x_max - x_min) / K);
  158.        
  159.         Y.emplace_back(Function(X[i]));
  160.        
  161.         std::uniform_real_distribution<double> Rand(-a, a);
  162.        
  163.         double Shift = Rand(Mt);
  164.        
  165.         _Y.emplace_back(Y[i] + Shift);
  166.        
  167.     }
  168.    
  169.     std::vector<double> Lmd;
  170.    
  171.     for (int i = 0; i <= L; i++) {
  172.        
  173.         Lmd.emplace_back(double(i) / L);
  174.        
  175.     }
  176.    
  177.     int N = log(1 - P) / log(1 - (E / x_max - x_min)),
  178.    
  179.     M = (r - 1) / 2;
  180.    
  181.     std::vector<std::vector<double>> Alpha;
  182.    
  183.     Alpha.emplace_back(AlphaCount(r, M));
  184.    
  185.     std::vector<double> Y_min = ResultCount(_Y, Alpha[0], M);
  186.    
  187.     size_t Alpha_min = 0,
  188.    
  189.     Lmd_min = 0;
  190.    
  191.     double Omega_min = OmegaCount(Y_min),
  192.    
  193.     Delta_min = DeltaCount(Y_min, _Y),
  194.    
  195.     J_min = JCount(Lmd[Lmd_min], Omega_min, Delta_min);
  196.    
  197.     std::vector<double> omegas, deltas, distances;
  198.    
  199.     std::vector<std::vector<double>> Alpha_optimal;
  200.    
  201.     for (size_t i = 0; i < Lmd.size(); i++) {
  202.        
  203.         for (int j = 0; j < N; j++) {
  204.            
  205.             Alpha.emplace_back(AlphaCount(r, M));
  206.            
  207.             std::vector<double> Y_current = ResultCount(_Y, Alpha[Alpha.size() - 1], M);
  208.            
  209.             double Omega = OmegaCount(Y_current),
  210.            
  211.             Delta = DeltaCount(Y_current, _Y);
  212.            
  213.             if (Dist(JCount(Lmd.at(i), Omega, Delta), 0) < Dist(J_min, 0)) {
  214.                
  215.                 Lmd_min = i;
  216.                
  217.                 Alpha_min = Alpha.size() - 1;
  218.                
  219.                 J_min = JCount(Lmd[i], Omega, Delta);
  220.                
  221.                 Omega_min = Omega;
  222.                
  223.                 Delta_min = Delta;
  224.                
  225.                 Y_min = Y_current;
  226.                
  227.             }
  228.            
  229.         }
  230.        
  231.         Alpha_optimal.emplace_back(Alpha[Alpha_min]);
  232.        
  233.         omegas.emplace_back(Omega_min);
  234.        
  235.         deltas.emplace_back(Delta_min);
  236.        
  237.         distances.emplace_back(Dist(J_min, 0));
  238.        
  239.     }
  240.    
  241.     std::cout << "\n r = " << r << "\n";
  242.    
  243.     PrintLine(r);
  244.    
  245.     std::cout << "|    Lmd     |      W     |      D     |  Distance  |";
  246.    
  247.     if (r == 5)
  248.        
  249.         std::cout << "                       a                       |\n";
  250.    
  251.     else
  252.        
  253.         std::cout << "               a             |\n";
  254.    
  255.     PrintLine(r);
  256.    
  257.     std::ofstream out;
  258.     out.open("../dots.txt");
  259.    
  260.    
  261.     for (int j = 0; j < 11; j++) {
  262.        out << "("<< omegas[j] << ";" << deltas[j] << ") , ";
  263.         std::cout << "| " << std::setprecision(4) << std::setw(10) << Lmd[j];
  264.        
  265.         std::cout << " | " << std::setprecision(6) << std::setw(10) << omegas[j];
  266.        
  267.         std::cout << " | " << std::setprecision(4) << std::setw(10) << deltas[j];
  268.        
  269.         std::cout << " | " << std::setprecision(4) << std::setw(10) << distances[j];
  270.        
  271.         std::cout << " | " ;
  272.        
  273.         for (const auto &A : Alpha_optimal[j])
  274.            
  275.             std::cout << std::setprecision(4) << std::setw(8) << A << " ";
  276.        
  277.         std::cout << " |\n";
  278.        
  279.     }
  280.    
  281.     PrintLine(r);
  282.    
  283.     std::cout << "Results:\n";
  284.    
  285.     std::cout << "J: " << J_min << std::endl << "Lmd: " << Lmd[Lmd_min] << std::endl
  286.    
  287.     << "W: " << Omega_min << std::endl << "D: " << Delta_min << std::endl;
  288.    
  289. }
  290.  
  291. int main() {
  292.    
  293.     int r = 3;
  294.    
  295.     Filter(r);
  296.    
  297.     r = 5;
  298.    
  299.     Filter(r);
  300.    
  301.     return 0;
  302.    
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement