.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_opt_prob.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_opt_prob.py: Plotting optimization problem ============================= An example of how to use the :func:`niceplots.utils.plot_opt_prob` function to plot a constrained 2D optimization problem. This example plots the 2D Rosenbrock function with a quadratic equality constraint and 3 circular inequality constraints, which has an optimum at (1, 1). .. GENERATED FROM PYTHON SOURCE LINES 8-84 .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_plot_opt_prob_001.svg :alt: plot opt prob :srcset: /auto_examples/images/sphx_glr_plot_opt_prob_001.svg :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_opt_prob_002.svg :alt: plot opt prob :srcset: /auto_examples/images/sphx_glr_plot_opt_prob_002.svg :class: sphx-glr-multi-img .. code-block:: Python # ============================================================================== # Standard Python modules # ============================================================================== # ============================================================================== # External Python modules # ============================================================================== import matplotlib.pyplot as plt import niceplots # ============================================================================== # Extension modules # ============================================================================== plt.style.use(niceplots.get_style()) colors = niceplots.get_colors() def Rosenbrock(x, y): return (1 - x) ** 2 + 100 * (y - x**2) ** 2 def circleCon1(x, y): return x**2 + y**2 - 2 def circleCon2(x, y): return (x - 1.5) ** 2 + y**2 - 2 def circleCon3(x, y): return x**2 + (y - 1.5) ** 2 - 2 def eqCon(x, y): return x - y**2 # --- Some fake optimiser path --- optX = [0.8, 1.2, 1.015384614926278, 0.9999252638670815, 1.0] optY = [1.4, 1.3, 1.0626373624736707, 0.9999644713547426, 1.0] for conStyle in ["shaded", "hashed"]: fig, ax = plt.subplots(figsize=(8, 8)) ax.set_aspect("equal") niceplots.plot_opt_prob( Rosenbrock, xRange=[0, 1.5], yRange=[0, 1.5], ineqCon=[circleCon1, circleCon2, circleCon3], eqCon=eqCon, nPoints=31, optPoint=[1.0, 1.0], conStyle=conStyle, ax=ax, colors=None, cmap=None, levels=50, labelAxes=True, ) # --- Plot the optimiser's path to the optimum --- ax.plot( optX, optY, "-o", c=colors["Axis"], markeredgecolor="w", linewidth=2.0, markersize=8, clip_on=False, ) # Save figures niceplots.save_figs(fig, f"opt_prob-{conStyle}", ["png", "svg"]) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.481 seconds) .. _sphx_glr_download_auto_examples_plot_opt_prob.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_opt_prob.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_opt_prob.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_opt_prob.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_