visualizacion_pareto_avanzada.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from mpl_toolkits.mplot3d import Axes3D
  4. import warnings
  5. warnings.filterwarnings('ignore')
  6. # Leer el archivo manualmente
  7. data = []
  8. with open('resultados1.txt', 'r', encoding='utf-8') as f:
  9. header = f.readline().strip().split('\t')
  10. for line in f:
  11. values = line.strip().split()
  12. if len(values) >= 15:
  13. data.append(values)
  14. # Convertir datos
  15. def convert_value(x):
  16. try:
  17. return float(x.replace(',', '.').strip())
  18. except:
  19. return np.nan
  20. tf = np.array([convert_value(row[2]) for row in data])
  21. tw = np.array([convert_value(row[3]) for row in data])
  22. flecha = np.array([convert_value(row[8]) for row in data])
  23. # Filtrar NaN
  24. valid = ~(np.isnan(tf) | np.isnan(tw) | np.isnan(flecha))
  25. tf = tf[valid]
  26. tw = tw[valid]
  27. flecha = flecha[valid]
  28. # Crear figura con 4 subplots
  29. fig = plt.figure(figsize=(18, 14))
  30. # Colores basados en la deflexión
  31. colors = plt.cm.RdYlGn_r((np.abs(flecha) - np.abs(flecha).min()) / (np.abs(flecha).max() - np.abs(flecha).min()))
  32. # 1. Vista 3D principal
  33. ax1 = fig.add_subplot(2, 2, 1, projection='3d')
  34. scatter1 = ax1.scatter(tf, tw, flecha, c=np.abs(flecha), cmap='RdYlGn_r',
  35. s=100, alpha=0.6, edgecolors='black', linewidth=0.5)
  36. ax1.set_xlabel('tf (ala)', fontweight='bold')
  37. ax1.set_ylabel('tw (alma)', fontweight='bold')
  38. ax1.set_zlabel('Flecha', fontweight='bold')
  39. ax1.set_title('Vista 3D Principal - Coloreado por |Deflexión|', fontweight='bold', fontsize=11)
  40. ax1.view_init(elev=20, azim=45)
  41. cbar1 = plt.colorbar(scatter1, ax=ax1, pad=0.1, shrink=0.8)
  42. cbar1.set_label('|Deflexión|', fontweight='bold')
  43. # 2. Vista 3D desde otro ángulo
  44. ax2 = fig.add_subplot(2, 2, 2, projection='3d')
  45. scatter2 = ax2.scatter(tf, tw, flecha, c=np.abs(flecha), cmap='RdYlGn_r',
  46. s=100, alpha=0.6, edgecolors='black', linewidth=0.5)
  47. ax2.set_xlabel('tf (ala)', fontweight='bold')
  48. ax2.set_ylabel('tw (alma)', fontweight='bold')
  49. ax2.set_zlabel('Flecha', fontweight='bold')
  50. ax2.set_title('Vista 3D Alternativa - Ángulo 225°', fontweight='bold', fontsize=11)
  51. ax2.view_init(elev=10, azim=225)
  52. # 3. Proyección 2D: tf vs tw (coloreado por flecha)
  53. ax3 = fig.add_subplot(2, 2, 3)
  54. scatter3 = ax3.scatter(tf, tw, c=np.abs(flecha), cmap='RdYlGn_r',
  55. s=150, alpha=0.7, edgecolors='black', linewidth=0.5)
  56. ax3.set_xlabel('tf - Espesor ala (mm)', fontweight='bold', fontsize=11)
  57. ax3.set_ylabel('tw - Espesor alma (mm)', fontweight='bold', fontsize=11)
  58. ax3.set_title('Proyección 2D: tf vs tw', fontweight='bold', fontsize=11)
  59. ax3.grid(True, alpha=0.3)
  60. cbar3 = plt.colorbar(scatter3, ax=ax3)
  61. cbar3.set_label('|Deflexión| (mm)', fontweight='bold')
  62. # 4. Gráfico de contorno 3D: Deflexión vs tf y tw
  63. ax4 = fig.add_subplot(2, 2, 4)
  64. # Crear grid para contorno
  65. tf_unique = np.sort(np.unique(tf))
  66. tw_unique = np.sort(np.unique(tw))
  67. # Crear matriz de valores Z (flecha)
  68. Z = np.full((len(tw_unique), len(tf_unique)), np.nan)
  69. for i, t_f in enumerate(tf_unique):
  70. for j, t_w in enumerate(tw_unique):
  71. mask = (tf == t_f) & (tw == t_w)
  72. if np.any(mask):
  73. Z[j, i] = np.abs(flecha[mask][0])
  74. # Graficar contorno
  75. X_grid, Y_grid = np.meshgrid(tf_unique, tw_unique)
  76. contour = ax4.contourf(X_grid, Y_grid, Z, levels=20, cmap='RdYlGn_r')
  77. contour_lines = ax4.contour(X_grid, Y_grid, Z, levels=10, colors='black', alpha=0.3, linewidths=0.5)
  78. ax4.clabel(contour_lines, inline=True, fontsize=8)
  79. ax4.set_xlabel('tf - Espesor ala (mm)', fontweight='bold', fontsize=11)
  80. ax4.set_ylabel('tw - Espesor alma (mm)', fontweight='bold', fontsize=11)
  81. ax4.set_title('Mapa de Contorno: |Deflexión|', fontweight='bold', fontsize=11)
  82. cbar4 = plt.colorbar(contour, ax=ax4)
  83. cbar4.set_label('|Deflexión| (mm)', fontweight='bold')
  84. plt.suptitle('Análisis Completo de la Frontera de Pareto - Sección de Viga',
  85. fontsize=14, fontweight='bold', y=0.995)
  86. plt.tight_layout(rect=[0, 0, 1, 0.99])
  87. plt.savefig('analisis_completo_pareto.png', dpi=300, bbox_inches='tight')
  88. print("✓ Visualización completa guardada como 'analisis_completo_pareto.png'")
  89. # Crear gráfico de superficies de respuesta
  90. fig2 = plt.figure(figsize=(16, 12))
  91. # 1. Superficie 3D de deflexión
  92. ax5 = fig2.add_subplot(2, 2, 1, projection='3d')
  93. surf1 = ax5.plot_surface(X_grid, Y_grid, Z, cmap='RdYlGn_r', alpha=0.8,
  94. edgecolor='none', antialiased=True)
  95. ax5.scatter(tf, tw, np.abs(flecha), c='black', s=20, alpha=0.3)
  96. ax5.set_xlabel('tf (mm)', fontweight='bold')
  97. ax5.set_ylabel('tw (mm)', fontweight='bold')
  98. ax5.set_zlabel('|Deflexión| (mm)', fontweight='bold')
  99. ax5.set_title('Superficie de Respuesta: |Deflexión|', fontweight='bold', fontsize=11)
  100. ax5.view_init(elev=25, azim=45)
  101. cbar5 = plt.colorbar(surf1, ax=ax5, pad=0.1, shrink=0.8)
  102. cbar5.set_label('|Deflexión| (mm)', fontweight='bold')
  103. # 2. Superficie 3D desde otro ángulo
  104. ax6 = fig2.add_subplot(2, 2, 2, projection='3d')
  105. surf2 = ax6.plot_surface(X_grid, Y_grid, Z, cmap='RdYlGn_r', alpha=0.8,
  106. edgecolor='none', antialiased=True)
  107. ax6.set_xlabel('tf (mm)', fontweight='bold')
  108. ax6.set_ylabel('tw (mm)', fontweight='bold')
  109. ax6.set_zlabel('|Deflexión| (mm)', fontweight='bold')
  110. ax6.set_title('Superficie de Respuesta - Vista Lateral', fontweight='bold', fontsize=11)
  111. ax6.view_init(elev=10, azim=120)
  112. # 3. Gráfico de dispersión con líneas de iso-deflexión
  113. ax7 = fig2.add_subplot(2, 2, 3)
  114. scatter7 = ax7.scatter(tf, tw, s=200, c=np.abs(flecha), cmap='RdYlGn_r',
  115. alpha=0.7, edgecolors='black', linewidth=1)
  116. contour7 = ax7.contour(X_grid, Y_grid, Z, levels=15, colors='gray', alpha=0.4, linewidths=0.8)
  117. ax7.clabel(contour7, inline=True, fontsize=7)
  118. ax7.set_xlabel('tf - Espesor ala (mm)', fontweight='bold', fontsize=11)
  119. ax7.set_ylabel('tw - Espesor alma (mm)', fontweight='bold', fontsize=11)
  120. ax7.set_title('Puntos de Diseño con Líneas de Iso-Deflexión', fontweight='bold', fontsize=11)
  121. ax7.grid(True, alpha=0.3)
  122. cbar7 = plt.colorbar(scatter7, ax=ax7)
  123. cbar7.set_label('|Deflexión| (mm)', fontweight='bold')
  124. # 4. Gráfico de efectos principales
  125. ax8 = fig2.add_subplot(2, 2, 4)
  126. tf_effect = [np.abs(flecha[tf == t_f]).mean() for t_f in tf_unique]
  127. tw_effect = [np.abs(flecha[tw == t_w]).mean() for t_w in tw_unique]
  128. ax8_2 = ax8.twinx()
  129. ax8.plot(tf_unique, tf_effect, 'b-o', linewidth=2, markersize=6, label='Efecto de tf')
  130. ax8_2.plot(tw_unique, tw_effect, 'r-s', linewidth=2, markersize=6, label='Efecto de tw')
  131. ax8.set_xlabel('Valor del parámetro (mm)', fontweight='bold', fontsize=11)
  132. ax8.set_ylabel('Deflexión media (tf)', color='b', fontweight='bold')
  133. ax8_2.set_ylabel('Deflexión media (tw)', color='r', fontweight='bold')
  134. ax8.set_title('Gráficos de Efectos Principales', fontweight='bold', fontsize=11)
  135. ax8.grid(True, alpha=0.3)
  136. ax8.tick_params(axis='y', labelcolor='b')
  137. ax8_2.tick_params(axis='y', labelcolor='r')
  138. lines1, labels1 = ax8.get_legend_handles_labels()
  139. lines2, labels2 = ax8_2.get_legend_handles_labels()
  140. ax8.legend(lines1 + lines2, labels1 + labels2, loc='upper left')
  141. plt.suptitle('Superficies de Respuesta y Análisis de Sensibilidad',
  142. fontsize=14, fontweight='bold', y=0.995)
  143. plt.tight_layout(rect=[0, 0, 1, 0.99])
  144. plt.savefig('superficies_respuesta.png', dpi=300, bbox_inches='tight')
  145. print("✓ Superficies de respuesta guardadas como 'superficies_respuesta.png'")
  146. print("\n✓ Análisis completado exitosamente")
  147. print(f" - {len(tf)} puntos analizados")
  148. print(f" - Valores tf: {tf.min():.4f} a {tf.max():.4f} mm")
  149. print(f" - Valores tw: {tw.min():.4f} a {tw.max():.4f} mm")
  150. print(f" - Deflexión: {np.abs(flecha).min():.2f} a {np.abs(flecha).max():.2f} mm")
  151. plt.show()