MCPcopy Create free account
hub / github.com/anyoptimization/pymoo / plot_problem_surface

Function plot_problem_surface

pymoo/visualization/util.py:254–321  ·  view source on GitHub ↗
(
    problem,
    n_samples,
    plot_type="wireframe",
    cmap="summer",
    show=True,
    return_figure=False,
)

Source from the content-addressed store, hash-verified

252
253
254def 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:

Calls 3

evaluateMethod · 0.80
plot_surfaceMethod · 0.80
showMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…