Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 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 uniModelFunction(double x){
  10.     return -2*sqrt(x)*sin(0.5*x);
  11. }
  12.  
  13. double multiModelFunction(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.         }
  39.         else
  40.             q.push_back((i+1)/1000.*5);
  41.     }
  42.     cout << "Random search method" << endl << endl;
  43.     vector<unsigned int> N;
  44.     for(auto i:P){
  45.         for(auto j:q){
  46.             N.push_back(quantityOfPoints(i,j));
  47.         }
  48.     }
  49.     cout << str << endl;
  50.     printTable(P);
  51.     for(int i=0;i<q.size();i++){
  52.         cout << "|" << setw(6) << q[i] << " |";
  53.         for(int j=0;j<P.size();j++)
  54.             cout << setw(9) << N[i+j*column] << " |";
  55.         cout << endl;
  56.     }
  57.     cout << str << endl << endl;
  58.  
  59.     for(int k=0;k<2;k++){
  60.         if(k==0)
  61.             cout << "For Unimodal Function" << endl;
  62.         else
  63.             cout << "For Multimodal Function" << endl;
  64.         printTable(P);
  65.         for(int i=0;i<N.size();i++){
  66.             double z=0;
  67.             for(int j=0;j<N[i];j++){
  68.                 double x=rand()*3 % ((b-a)*10000) /10000. +a;
  69.                 double y=0;
  70.                 if(j==0)
  71.                     y=x;
  72.                 if(k==0){
  73.                     if(uniModelFunction(y)<uniModelFunction(x))
  74.                         y=x;
  75.                 }
  76.                 else {
  77.                     if (multiModelFunction(y) < multiModelFunction(x)) {
  78.                         y = x;
  79.                     }
  80.                 }
  81.                 z=y;
  82.             }
  83.             if(i%10==0){
  84.                 if(i!=0)
  85.                     cout << endl;
  86.                 cout << "|" << setw(6) << q[i/10] << " |";
  87.             }
  88.             cout << setw(9) << z << " |";
  89.         }
  90.         cout << endl << str;
  91.     }
  92.  
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement