-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathae_tp2_e4.py
More file actions
72 lines (51 loc) · 2.57 KB
/
Copy pathae_tp2_e4.py
File metadata and controls
72 lines (51 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#######################################################################
# CEIA - 16Co2024 - Algoritmos Evolutivos - TP2 - Ejercicio 4
# Gustavo J. Rivas (a1620) | Myrna L. Degano (a1618)
#######################################################################
# Sistema de 2 ecuaciones con 2 incógnitas resuelto con PSO.
#######################################################################
from pyswarm import pso
# Obtener parámetros de ejecución
def get_params():
num_p = input("Número de partículas (DEFAULT: 20): ").strip()
num_p = int(num_p) if num_p else 20
num_i = input("Número de iteraciones (DEFAULT: 50): ").strip()
num_i = int(num_i) if num_i else 50
c1 = input("Coeficiente de aceleración - Componente cognitivo (DEFAULT: 1.5): ").strip()
c1 = float(c1) if c1 else 1.5
c2 = input("Coeficiente de aceleración - Componente social (DEFAULT: 1.5): ").strip()
c2 = float(c2) if c2 else 1.5
w = input("Coeficiente de inercia (DEFAULT: 0.5): ").strip()
w = float(w) if w else 0.5
l = input("Límite inferior para las variables x e y (DEFAULT: -100): ").strip()
l = float(l) if l else -100.0
u = input("Límite superior para las variables x e y (DEFAULT: +100): ").strip()
u = float(u) if u else 100.0
return num_p, num_i, c1, c2, w, l, u
# Función objetivo (Suma de cuadrados de las ecuaciones -> Error a minimizar)
def f_obj(x1x2):
x1, x2 = x1x2
# Sistema de ecuaciones
# 3*x1 + 2*x2 - 9 = 0 => f1 (x1, x2) = 3*x1 + 2*x2 - 9
# x1 - 5*x2 - 4 = 0 => f1 (x1, x2) = x1 - 5*x2 - 4
# Función Objetivo a minimizar: f1^2 + f2^2
return (3*x1 + 2*x2 - 9)**2 + (x1 - 5*x2 - 4)**2
#######################################################################
# Desarrollo del algoritmo
#######################################################################
print("\nINGRESE LOS PARÁMETROS PARA LA EJECUCIÓN DEL ALGORITMO (O <ENTER> PARA TOMAR LOS VALORES POR DEFAULT)\n")
# Obtener parámetros de ejecución
# Cantidad de partículas, máximo de iteraciones
# Coeficientes de aceleración e incercia (c1, c2, w)
# Límites del espacio (inferior lb, superior ub)
particles, iterations, c1, c2, w, lb, ub = get_params()
# Ejecutar PSO en modo Debug
best_pos, best_val = pso(
f_obj, # Función objetivo
[lb, lb], # Límites inferiores
[ub, ub], # Límites superiores
swarmsize=particles, # Tamaño del enjambre
maxiter=iterations, # Número máximo de iteraciones
debug=True # Modo debug
)
print(f"\nLos valores aproximados encontrados para resolver el sistema de ecuaciones son:\n x1, x2 = {best_pos}")