Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <cmath>
- #include <ctime>
- #include <iomanip>
- //#define ECUATION_FOR_X2 x1 - (float)((T / T_max * (b - a))/2) + ((float)rand() / RAND_MAX) * ((float)T / T_max * (b - a))
- #define line "___________________________________"
- float uni_func(const float x)
- {
- return -1 * std::sqrt(x)*std::sin(x) + 2;
- }
- float multi_func(const float x)
- {
- return (-1 * std::sqrt(x)*std::sin(x) + 2) * sin(5 * x);
- }
- bool uni_CompareValue(float x1, float x2)
- {
- return uni_func(x1) > uni_func(x2);
- }
- bool multi_CompareValue(float x1, float x2)
- {
- return multi_func(x1) > multi_func(x2);
- }
- void TablePrint_Uni(int N, float T, float x, float f_value, bool accepted)
- {
- std::cout << "| ";
- if (N / 10 == 0)
- std::cout << " ";
- else if (N / 100 == 0)
- std::cout << " ";
- std::cout << N << " | " << T << " | " << x << " | " << f_value << " | ";
- if (accepted)
- std::cout << " Accepted |";
- else
- std::cout << " Rejected |";
- std::cout << std::endl;
- }
- void TablePrint(int N, float T, float x, float f_value, bool accepted)
- {
- std::cout << "| ";
- if (N / 10 == 0)
- std::cout << " ";
- else if (N / 100 == 0)
- std::cout << " ";
- std::cout << N << " | ";
- if (T / 10 < 1)
- std::cout << " ";
- else if (T / 100 < 1)
- std::cout << " ";
- else if (T / 1000 < 1)
- std::cout << " ";
- else if (T / 10000 < 1)
- std::cout << " ";
- std::cout << T << " | ";
- std::cout << x << " | ";
- if (f_value > 0)
- std::cout << " ";
- std::cout<< f_value << " | ";
- if (accepted)
- std::cout << " Accepted |";
- else
- std::cout << " Rejected |";
- std::cout << std::endl;
- }
- int main()
- {
- srand(clock());
- std::cout << std::fixed << std::setprecision(4);
- bool accepted;
- const float a = 1;
- const float b = 4;
- const float T_max = 10000;
- const float T_min = 0.1;
- float T = T_max;
- float diff=0;
- float x1=(float)rand() / RAND_MAX * (b-a) + a;
- float x2;
- //const float delta=0.1;
- int counter = 1;
- std::cout << std::endl;
- std::cout << " UNIMODAL FUNCTION" << std::endl;
- std::cout << line << std::endl << std::endl;
- std::cout << " Table 1: For unimodal function" << std::endl;
- std::cout << " ___________________________________________________________ " << std::endl;
- std::cout << "| Iteration | T | X1 | f(x1) | Acception |" << std::endl;
- std::cout << "|--------------|-------------|--------|---------|-----------|" << std::endl;
- while (T > T_min)
- {
- //x2 = (float)rand() / RAND_MAX * 2 * delta + x1 - delta;
- x2 = (float)rand() / RAND_MAX * (b - a) + a;
- diff = uni_func(x2) - uni_func(x1);
- if (diff>0)
- {
- accepted = false;
- if ((float)rand() / RAND_MAX < exp((float)-1*diff/T))
- {
- accepted = true;
- x1 = x2;
- }
- }
- else
- {
- accepted = true;
- x1 = x2;
- }
- TablePrint(counter, T, x1, uni_func(x1), accepted);
- counter++;
- T *= 0.95;
- }
- std::cout << "|______________|_____________|________|_________|___________|" << std::endl;
- std::cout << "Result for unimodal: " << uni_func(x1) << " in " << x1 << std::endl << std::endl;
- std::cout << std::endl << " MULTIMODAL FUNCTION" << std::endl;
- std::cout << line << std::endl;
- std::cout << std::endl << " Table 2 : For multimodal function" << std::endl;
- std::cout << " ___________________________________________________________" << std::endl;
- std::cout << "| Iteration | T | X1 | f(x1) | Acception |" << std::endl;
- std::cout << "|--------------|-------------|--------|---------|-----------|" << std::endl;
- x1= (float)rand() / RAND_MAX * (b - a) + a;
- T = T_max;
- counter = 0;
- diff=0;
- while (T > T_min)
- {
- x2 = (float)rand() / RAND_MAX * (b - a) + a;
- diff = multi_func(x2) - multi_func(x1);
- accepted = false;
- if (diff > 0)
- {
- if (((float)rand() / RAND_MAX) < exp((float)-1 * diff / T))
- {
- accepted = true;
- x1 = x2;
- }
- }
- else
- {
- accepted = true;
- x1 = x2;
- }
- TablePrint(counter, T, x1, multi_func(x1), accepted);
- counter++;
- T *= 0.95;
- }
- std::cout << "|______________|_____________|________|_________|___________|" << std::endl;
- std::cout << "Result for multimodal: " << multi_func(x1) << " in " << x1 << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement