PaiusCatalin9577

GlobalMinima (v1)

Oct 11th, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.60 KB | None | 0 0
  1. #include<iostream>
  2. #include<cmath>
  3. #include<cstdlib>
  4. #include<ctime>
  5.  
  6. #define pi 3.14
  7. using namespace std;
  8.  
  9. class Function {
  10. protected:
  11.     unsigned int n;
  12.     unsigned int m;
  13.     unsigned int SwitchOption;
  14.  
  15.     long double ComputeRandomValue(long double lower_bound, long double upper_bound) {
  16.         srand(static_cast <unsigned> (time(0)));
  17.         long double random = lower_bound + static_cast <long double> (rand()) / (static_cast <long double> (RAND_MAX / (upper_bound - lower_bound)));
  18.         return random;
  19.     }
  20.  
  21.     long double SphereFunction(unsigned int n, long double lower_bound, long double upper_bound){
  22.         long double FunctionValue = 0;
  23.         long double Temp;
  24.         for (unsigned int i = 1; i <= n; ++i) {
  25.             Temp = this->ComputeRandomValue(lower_bound, upper_bound);
  26.             FunctionValue += pow(Temp, 2);
  27.         }
  28.         return FunctionValue;
  29.     }
  30.  
  31.     long double RastriginFunction(unsigned int n, long double lower_bound, long double upper_bound){
  32.         long double FunctionValue;
  33.         long double Temp;
  34.         for (unsigned int i = 1; i <= n; ++i) {
  35.             Temp = this->ComputeRandomValue(lower_bound, upper_bound);
  36.             Temp += pow(Temp, 2) - 10.0 * cos(2 * pi*Temp);
  37.         }
  38.         FunctionValue = 10.0*n + Temp;
  39.         return FunctionValue;
  40.     }
  41.  
  42.     long double MichalewicsFunction(unsigned int n, unsigned int m, long double lower_bound, long double upper_bound){
  43.         long double Temp;
  44.         long double FunctionResult = 0;
  45.         for (unsigned int i = 1; i <= n; ++i) {
  46.             Temp = this->ComputeRandomValue(lower_bound, upper_bound);
  47.             FunctionResult += sin(Temp)*pow(sin(i*pow(Temp, 2) / pi), 2 * m);
  48.         }
  49.         return (-1)*FunctionResult;
  50.     }
  51.  
  52.     long double HumpFunction(long double lower_bound, long double upper_bound){
  53.         long double x1 = this->ComputeRandomValue(lower_bound, upper_bound);
  54.         long double x2 = this->ComputeRandomValue(lower_bound, upper_bound);
  55.         long double t1, t2, t3;
  56.         t1 = 4 - 2.1*pow(x1, 2) + (pow(x1, 4) / 3) *pow(x1, 2);
  57.         t2 = (-4 + 4 * pow(x2, 2))*pow(x2, 2);
  58.         t3 = x1*x2;
  59.         return (t1 + t2 + t3);
  60.     }
  61.  
  62. public:
  63.     void SetN(unsigned int n){
  64.         this->n = n;
  65.     }
  66.  
  67.     void SetM(unsigned int m){
  68.         this->m = m;
  69.     }
  70.  
  71.     void SetSwitchOption(unsigned int number) {
  72.         if (number>0 && number < 5) {
  73.             this->SwitchOption = number;
  74.         }
  75.         else {
  76.             this->SwitchOption = rand() % 4 + 1;
  77.         }
  78.     }
  79. };
  80.  
  81. class GlobalMinima : public Function {
  82. private:
  83.     long double GlobalMin = INT_MAX;
  84.     long double LOW_B;
  85.     long double UPP_B;
  86. public:
  87.  
  88.     void ComputeBounds(long double lower_bound, long double upper_bound) {
  89.         for (int i = 0; i < 6; ++i) { upper_bound = (lower_bound + upper_bound) / 2; }
  90.         this->LOW_B = lower_bound;
  91.         this->UPP_B = upper_bound;
  92.     }
  93.  
  94.     void ComputeRandomGlobalMin() {
  95.         switch (this->SwitchOption) {
  96.         case 1: { // Sphere function
  97.                     long double localMin=INT_MAX;
  98.                     for (int j = 0; j < 100; ++j) {
  99.                         long double LWB = this->LOW_B;
  100.                         long double UPB = this->UPP_B;
  101.                         long double FunctionValue;
  102.                         for (unsigned int i = 0; i < 64; i++) {
  103.                             FunctionValue = this->SphereFunction(this->n, LWB, UPB);
  104.                             if (FunctionValue < localMin) {
  105.                                 localMin = FunctionValue;
  106.                             }
  107.                             long double temp = UPB;
  108.                             UPB = UPB + (UPB - LWB);
  109.                             LWB = temp;
  110.                         }
  111.                         if (localMin < this->GlobalMin) {
  112.                             this->GlobalMin = localMin;
  113.                         }
  114.                     }
  115.                     break;
  116.         }
  117.         case 2: { // Rastrigin function
  118.                     long double localMin=INT_MAX;
  119.                     for (int j = 0; j < 100; ++j) {
  120.                         long double LWB = this->LOW_B;
  121.                         long double UPB = this->UPP_B;
  122.                         long double FunctionValue;
  123.                         for (unsigned int i = 0; i < 64; i++) {
  124.                             FunctionValue = this->RastriginFunction(this->n, LWB, UPB);
  125.                             if (FunctionValue < localMin) {
  126.                                 localMin = FunctionValue;
  127.                             }
  128.                             long double temp = UPB;
  129.                             UPB = UPB + (UPB - LWB);
  130.                             LWB = temp;
  131.                         }
  132.                         if (localMin < this->GlobalMin) {
  133.                             this->GlobalMin = localMin;
  134.                         }
  135.                     }
  136.                     break;
  137.         }
  138.         case 3: { // Michalewics function
  139.                     long double localMin = INT_MAX;
  140.                     for (int j = 0; j < 100; ++j) {
  141.                         long double LWB = this->LOW_B;
  142.                         long double UPB = this->UPP_B;
  143.                         long double FunctionValue;
  144.                         for (unsigned int i = 0; i < 64; i++) {
  145.                             FunctionValue = this->MichalewicsFunction(this->n, this->m, LWB, UPB);
  146.                             if (FunctionValue < localMin) {
  147.                                 localMin = FunctionValue;
  148.                             }
  149.                             long double temp = UPB;
  150.                             UPB = UPB + (UPB - LWB);
  151.                             LWB = temp;
  152.                         }
  153.                         if (localMin < this->GlobalMin) {
  154.                             this->GlobalMin = localMin;
  155.                         }
  156.                     }
  157.                     break;
  158.         }
  159.         case 4: { // Camila's function
  160.                     long double localMin = INT_MAX;
  161.                     for (int j = 0; j < 100; ++j) {
  162.                         long double LWB = this->LOW_B;
  163.                         long double UPB = this->UPP_B;
  164.                         long double FunctionValue;
  165.                         for (unsigned int i = 0; i < 64; i++) {
  166.                             FunctionValue = this->HumpFunction(LWB, UPB);
  167.                             if (FunctionValue < localMin) {
  168.                                 localMin = FunctionValue;
  169.                             }
  170.                             long double temp = UPB;
  171.                             UPB = UPB + (UPB - LWB);
  172.                             LWB = temp;
  173.                         }
  174.                         if (localMin < this->GlobalMin) {
  175.                             this->GlobalMin = localMin;
  176.                         }
  177.                     }
  178.                     break;
  179.         }
  180.         default: break;
  181.         }
  182.     }
  183.  
  184.     long double GetGlobalMinima(){
  185.         return this->GlobalMin;
  186.     }
  187. };
  188.  
  189. void main() {
  190.     GlobalMinima MinimulGlobalAlUneiFunctii;
  191.     MinimulGlobalAlUneiFunctii.SetN(2);
  192.     MinimulGlobalAlUneiFunctii.SetM(10);
  193.     MinimulGlobalAlUneiFunctii.ComputeBounds(-5.12, 5.12);
  194.     MinimulGlobalAlUneiFunctii.SetSwitchOption(2);
  195.     MinimulGlobalAlUneiFunctii.ComputeRandomGlobalMin();
  196.     cout << "\nMinimul global al functiei alese este: " << MinimulGlobalAlUneiFunctii.GetGlobalMinima() << "\n";
  197.  
  198. }
Add Comment
Please, Sign In to add comment