Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <random>
- #include <valarray>
- #include <chrono>
- #include <iomanip>
- #include <climits>
- using namespace std;
- constexpr int iteraciones = 500;
- constexpr int tamano_poblacion = 1000;
- constexpr double factor_social = 1;
- constexpr double factor_congnitivo = 2;
- constexpr double factor_inercia = 0.5;
- double funcion (std::vector<std::string>& componentes, std::valarray<double>& valores) {
- std::vector<double> pila;
- for (const auto& p : componentes)
- {
- if (p.size( ) == 1 && std::ispunct(p[0])) {
- auto temp = pila.back( );
- pila.pop_back( );
- if (p == "^") {
- pila.back( ) = std::pow(pila.back( ), temp);
- } else if (p == "+") {
- pila.back( ) += temp;
- } else if (p == "-") {
- pila.back( ) -= temp;
- } else if (p == "*") {
- pila.back( ) *= temp;
- } else if (p == "/") {
- pila.back( ) /= temp;
- }
- } else if (p == "sin") {
- pila.back( ) = std::sin(pila.back( ));
- } else if (p == "cos") {
- pila.back( ) = std::cos(pila.back( ));
- } else if (p == "tan") {
- pila.back( ) = std::tan(pila.back( ));
- } else if (p == "sqrt") {
- pila.back( ) = std::sqrt(pila.back( ));
- }else if (p[0] == 'x') {
- pila.push_back(valores[std::atoi(p.data( ) + 1)]);
- } else {
- pila.push_back(std::atof(p.data( )));
- }
- }
- //std::cout << pila.back( ) << "\n";
- return pila.back( );
- }
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int n, dimensiones;
- cin >> n >> dimensiones;
- vector<string> componentes(n);
- for (int i = 0; i < n; ++i) {
- cin >> componentes[i];
- }
- valarray<double> x_actual[tamano_poblacion];
- valarray<double> x_mejor[tamano_poblacion];
- valarray<double> v_actual[tamano_poblacion];
- for (int i = 0; i < tamano_poblacion; ++i) {
- x_actual[i].resize(dimensiones);
- v_actual[i].resize(dimensiones);
- x_mejor[i].resize(dimensiones);
- }
- mt19937_64 gen(time(nullptr));
- for (int T = 0; T < tamano_poblacion; ++T) {
- for (int i = 0; i < dimensiones; ++i) {
- double temp_x = uniform_int_distribution<int>( -50, 50 )(gen);
- double temp_v = uniform_int_distribution<int>( -50, 50 )(gen);
- x_actual[T][i] = temp_x;
- x_mejor[T][i] = temp_x;
- v_actual[T][i] = temp_v;
- }
- }
- /**/
- double optimo_global = numeric_limits<double>::max();
- valarray<double> g;
- for (int it = 0; it < iteraciones; ++it) {
- /**/
- for (int j = 0; j < tamano_poblacion; ++j) {
- double r1 = funcion(componentes, x_mejor[j]);
- if (r1 < optimo_global) {
- optimo_global = r1;
- g = x_mejor[j];
- }
- }
- /**/
- double p1 = uniform_int_distribution<int>(0, 1)(gen);
- double p2 = 1 - p1;
- for (int T = 0; T < tamano_poblacion; ++T) {
- v_actual[T] = (factor_inercia * v_actual[T]) + (p1 * factor_congnitivo * (x_mejor[T] - x_actual[T])) + (p2 * factor_social * (g - x_actual[T]));
- valarray<double> temp = x_actual[T] + v_actual[T];
- double r1 = funcion(componentes, temp);
- double r2 = funcion(componentes, x_actual[T]);
- if (r1 < r2) {
- x_mejor[T] = temp;
- }
- x_actual[T] = temp;
- }
- }
- //cout << setprecision(3) << fixed;
- cout << optimo_global << "\n";
- for (int act : g) {
- cout << act << " ";
- }
- cout << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement