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 unoModalFunction(double x) {
- return -2 * sqrt(x) * sin(0.5 * x);
- }
- double multiModalFunction(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;
- for (int k = 0; k < 2; k++) {
- if (k == 0)
- cout << "\n\nFor Unomodal Function" << endl;
- else
- cout << "\n\nFor Multimodal Function" << endl;
- printTable(P);
- for (size_t i = 0; i < N.size(); i++) {
- double z = 0;
- for (unsigned int n = 0; n < N[i]; n++) {
- double x = rand() * 3 % ((b - a) * 10000) / 10000. + a;
- double y;
- if (k == 0)
- y = unoModalFunction(x);
- else
- y = multiModalFunction(x);
- if (n == 0)
- z = y;
- if (z > y)
- 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