Advertisement
AlejandroGY

Optimizacion

Dec 7th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <valarray>
  4. #include <chrono>
  5. #include <iomanip>
  6. #include <climits>
  7. using namespace std;
  8.  
  9. constexpr int iteraciones = 500;
  10. constexpr int tamano_poblacion = 1000;
  11. constexpr double factor_social = 1;
  12. constexpr double factor_congnitivo = 2;
  13. constexpr double factor_inercia = 0.5;
  14.  
  15. double funcion (std::vector<std::string>& componentes, std::valarray<double>& valores) {
  16.     std::vector<double> pila;
  17.         for (const auto& p : componentes)
  18.         {
  19.             if (p.size( ) == 1 && std::ispunct(p[0])) {
  20.                 auto temp = pila.back( );
  21.                 pila.pop_back( );
  22.                 if (p == "^") {
  23.                     pila.back( ) = std::pow(pila.back( ), temp);
  24.                 } else if (p == "+") {
  25.                     pila.back( ) += temp;
  26.                 } else if (p == "-") {
  27.                     pila.back( ) -= temp;
  28.                 } else if (p == "*") {
  29.                     pila.back( ) *= temp;
  30.                 } else if (p == "/") {
  31.                     pila.back( ) /= temp;
  32.                 }
  33.             } else if (p == "sin") {
  34.                 pila.back( ) = std::sin(pila.back( ));
  35.             } else if (p == "cos") {
  36.                 pila.back( ) = std::cos(pila.back( ));
  37.             } else if (p == "tan") {
  38.                 pila.back( ) = std::tan(pila.back( ));
  39.             } else if (p == "sqrt") {
  40.                 pila.back( ) = std::sqrt(pila.back( ));
  41.             }else if (p[0] == 'x') {
  42.                 pila.push_back(valores[std::atoi(p.data( ) + 1)]);
  43.             } else {
  44.                 pila.push_back(std::atof(p.data( )));
  45.         }
  46.     }
  47.  
  48.     //std::cout << pila.back( ) << "\n";
  49.     return pila.back( );
  50. }
  51.  
  52. int main() {
  53.  
  54.     ios_base::sync_with_stdio(0);
  55.     cin.tie(0);
  56.     cout.tie(0);
  57.  
  58.     int n, dimensiones;
  59.     cin >> n >> dimensiones;
  60.     vector<string> componentes(n);
  61.     for (int i = 0; i < n; ++i) {
  62.         cin >> componentes[i];
  63.     }
  64.  
  65.     valarray<double> x_actual[tamano_poblacion];
  66.     valarray<double> x_mejor[tamano_poblacion];
  67.     valarray<double> v_actual[tamano_poblacion];
  68.  
  69.     for (int i = 0; i < tamano_poblacion; ++i) {
  70.         x_actual[i].resize(dimensiones);
  71.         v_actual[i].resize(dimensiones);
  72.         x_mejor[i].resize(dimensiones);
  73.     }
  74.  
  75.     mt19937_64 gen(time(nullptr));
  76.     for (int T = 0; T < tamano_poblacion; ++T) {
  77.         for (int i = 0; i < dimensiones; ++i) {
  78.             double temp_x = uniform_int_distribution<int>( -50, 50 )(gen);
  79.             double temp_v = uniform_int_distribution<int>( -50, 50 )(gen);
  80.             x_actual[T][i] = temp_x;
  81.             x_mejor[T][i] = temp_x;
  82.             v_actual[T][i] = temp_v;
  83.         }
  84.     }
  85.  
  86.     /**/
  87.     double optimo_global = numeric_limits<double>::max();
  88.     valarray<double> g;
  89.     for (int it = 0; it < iteraciones; ++it) {
  90.         /**/
  91.         for (int j = 0; j < tamano_poblacion; ++j) {
  92.             double r1 = funcion(componentes, x_mejor[j]);
  93.             if (r1 < optimo_global) {
  94.                 optimo_global = r1;
  95.                 g = x_mejor[j];
  96.             }
  97.         }
  98.         /**/
  99.         double p1 = uniform_int_distribution<int>(0, 1)(gen);
  100.         double p2 = 1 - p1;
  101.         for (int T = 0; T < tamano_poblacion; ++T) {
  102.             v_actual[T] = (factor_inercia * v_actual[T]) + (p1 * factor_congnitivo * (x_mejor[T] - x_actual[T])) + (p2 * factor_social * (g - x_actual[T]));
  103.             valarray<double> temp = x_actual[T] + v_actual[T];
  104.             double r1 = funcion(componentes, temp);
  105.             double r2 = funcion(componentes, x_actual[T]);
  106.             if (r1 < r2) {
  107.                 x_mejor[T] = temp;
  108.             }
  109.             x_actual[T] = temp;
  110.         }
  111.     }
  112.     //cout << setprecision(3) << fixed;
  113.     cout << optimo_global << "\n";
  114.     for (int act : g) {
  115.         cout << act << " ";
  116.     }
  117.     cout << "\n";
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement