Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <iomanip>
- #include <string>
- using namespace std;
- double uniModelFunction(double x){
- return -2*sqrt(x)*sin(0.5*x);
- }
- double multiModelFunction(double x){
- return -2*sqrt(x)*sin(0.5*x)*sin(5*x);
- }
- unsigned int quantityOfPoints(double P,double q){
- return log(1-P)/log(1-q)+1;
- }
- string str= "-----------------------------------------------------------------------------------------------------------------------";
- void printTable(vector<double> P){
- cout << "| q\\P |";
- for(auto i:P){
- cout << setw(8) << i << " |";
- }
- cout << endl << str << endl;
- }
- int main(){
- int a=2,b=6,column=20,String=10;
- vector<double> P,q;
- for(int i=0;i<column;i++){
- if(i<String){
- P.push_back(i/100. +0.9);
- q.push_back((i+1)/1000.*5);
- }
- else
- q.push_back((i+1)/1000.*5);
- }
- cout << "Random search method" << endl << endl;
- vector<unsigned int> N;
- for(auto i:P){
- for(auto j:q){
- N.push_back(quantityOfPoints(i,j));
- }
- }
- cout << str << endl;
- printTable(P);
- for(int i=0;i<q.size();i++){
- cout << "|" << setw(6) << q[i] << " |";
- for(int j=0;j<P.size();j++)
- cout << setw(9) << N[i+j*column] << " |";
- cout << endl;
- }
- cout << str << endl << endl;
- for(int k=0;k<2;k++){
- if(k==0)
- cout << "For Unimodal Function" << endl;
- else
- cout << "For Multimodal Function" << endl;
- printTable(P);
- for(int i=0;i<N.size();i++){
- double z=0;
- for(int j=0;j<N[i];j++){
- double x=rand()*3 % ((b-a)*10000) /10000. +a;
- double y=0;
- if(j==0)
- y=x;
- if(k==0){
- if(uniModelFunction(y)<uniModelFunction(x))
- y=x;
- }
- else {
- if (multiModelFunction(y) < multiModelFunction(x)) {
- y = x;
- }
- }
- z=y;
- }
- if(i%10==0){
- if(i!=0)
- cout << endl;
- cout << "|" << setw(6) << q[i/10] << " |";
- }
- cout << setw(9) << z << " |";
- }
- cout << endl << str;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement