(
problem,
n_samples,
plot_type="wireframe",
cmap="summer",
show=True,
return_figure=False,
)
| 252 | |
| 253 | |
| 254 | def plot_problem_surface( |
| 255 | problem, |
| 256 | n_samples, |
| 257 | plot_type="wireframe", |
| 258 | cmap="summer", |
| 259 | show=True, |
| 260 | return_figure=False, |
| 261 | ): # noqa: ANN201 |
| 262 | try: |
| 263 | from pymoo.visualization.matplotlib import plt # noqa: F401 |
| 264 | except Exception as e: # noqa: E722 |
| 265 | raise Exception( |
| 266 | "Please install 'matplotlib' to use the plotting functionality." |
| 267 | ) from e |
| 268 | |
| 269 | fig = plt.figure() |
| 270 | |
| 271 | if problem.n_var == 1 and problem.n_obj == 1: |
| 272 | X = np.linspace(problem.xl[0], problem.xu[0], num=n_samples)[:, None] |
| 273 | Y = problem.evaluate(X, return_values_of=["F"]) |
| 274 | ax = plt.plot(X, Y) |
| 275 | |
| 276 | elif problem.n_var == 2 and problem.n_obj == 1: |
| 277 | X_range = np.linspace(problem.xl[0], problem.xu[0], num=n_samples) |
| 278 | Y_range = np.linspace(problem.xl[1], problem.xu[1], num=n_samples) |
| 279 | X, Y = np.meshgrid(X_range, Y_range) |
| 280 | |
| 281 | A = np.zeros((n_samples * n_samples, 2)) |
| 282 | counter = 0 |
| 283 | for i, x in enumerate(X_range): |
| 284 | for j, y in enumerate(Y_range): |
| 285 | A[counter, 0] = x |
| 286 | A[counter, 1] = y |
| 287 | counter += 1 |
| 288 | |
| 289 | F = np.reshape( |
| 290 | problem.evaluate(A, return_values_of=["F"]), (n_samples, n_samples) |
| 291 | ) |
| 292 | |
| 293 | # Plot the surface. |
| 294 | if plot_type == "wireframe": |
| 295 | ax = fig.add_subplot(111, projection="3d") |
| 296 | ax.plot_wireframe(X, Y, F) |
| 297 | elif plot_type == "contour": |
| 298 | CS = plt.contour(X, Y, F) |
| 299 | plt.clabel(CS, inline=1, fontsize=10) |
| 300 | elif plot_type == "wireframe+contour": |
| 301 | ax = fig.add_subplot(111, projection="3d") |
| 302 | ax.plot_surface(X, Y, F, cmap=cmap, rstride=1, cstride=1) |
| 303 | ax.contour(X, Y, F, 10, linestyles="solid", offset=-1) |
| 304 | ax.set_xlabel("$x_1$") |
| 305 | ax.set_ylabel("$x_2$") |
| 306 | ax.set_zlabel("$f(x)$") |
| 307 | ax.view_init(45, 45) |
| 308 | else: |
| 309 | raise Exception("Unknown plotting method.") |
| 310 | |
| 311 | else: |
no test coverage detected
searching dependent graphs…