Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <iomanip>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. double unoModalFunction(double x) {
  10.     return -2 * sqrt(x) * sin(0.5 * x);
  11. }
  12.  
  13. double multiModalFunction(double x) {
  14.     return -2 * sqrt(x) * sin(0.5 * x) * sin(5 * x);
  15. }
  16.  
  17. unsigned int quantityOfPoints(double P, double q) {
  18.     return log(1 - P) / log(1 - q) + 1;
  19. }
  20.  
  21. string str = "-----------------------------------------------------------------------------------------------------------------------";
  22.  
  23. void printTable(vector<double> P) {
  24.     cout << "|  q\\P  |";
  25.     for (auto i:P) {
  26.         cout << setw(8) << i << "  |";
  27.     }
  28.     cout << endl << str << endl;
  29. }
  30.  
  31. int main() {
  32.     int a = 2, b = 6, column = 20, String = 10;
  33.     vector<double> P, q;
  34.     for (int i = 0; i < column; i++) {
  35.         if (i < String) {
  36.             P.push_back(i / 100. + 0.9);
  37.             q.push_back((i + 1) / 1000. * 5);
  38.         } else
  39.             q.push_back((i + 1) / 1000. * 5);
  40.     }
  41.     cout << "Random search method" << endl << endl;
  42.     vector<unsigned int> N;
  43.     for (auto i:P) {
  44.         for (auto j:q) {
  45.             N.push_back(quantityOfPoints(i, j));
  46.         }
  47.     }
  48.     cout << str << endl;
  49.     printTable(P);
  50.     for (int i = 0; i < q.size(); i++) {
  51.         cout << "|" << setw(6) << q[i] << " |";
  52.         for (int j = 0; j < P.size(); j++)
  53.             cout << setw(9) << N[i + j * column] << " |";
  54.         cout << endl;
  55.     }
  56.     cout << str;
  57.  
  58.     for (int k = 0; k < 2; k++) {
  59.         if (k == 0)
  60.             cout << "\n\nFor Unomodal Function" << endl;
  61.         else
  62.             cout << "\n\nFor Multimodal Function" << endl;
  63.  
  64.         printTable(P);
  65.         for (size_t i = 0; i < N.size(); i++) {
  66.             double z = 0;
  67.             for (unsigned int n = 0; n < N[i]; n++) {
  68.                 double x = rand() * 3 % ((b - a) * 10000) / 10000. + a;
  69.                 double y;
  70.                 if (k == 0)
  71.                     y = unoModalFunction(x);
  72.                 else
  73.                     y = multiModalFunction(x);
  74.  
  75.                 if (n == 0)
  76.                     z = y;
  77.  
  78.                 if (z > y)
  79.                     z = y;
  80.             }
  81.  
  82.             if (i % 10 == 0) {
  83.                 if (i != 0)
  84.                     cout << endl;
  85.                 cout << "|" << setw(6) << q[i / 10] << " |";
  86.             }
  87.             cout << setw(9) << z << " |";
  88.         }
  89.         cout << endl << str;
  90.     }
  91.  
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement