.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/optimization/dynamic_programming/plot_01_knapsack.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_api_gallery_optimization_dynamic_programming_plot_01_knapsack.py: Bellman's principle of optimality: the 0/1 knapsack ========================================================= Fills a table of optimal values for every prefix of the items and every capacity, then traces back the chosen items. The table is the dynamic-programming "value function". .. GENERATED FROM PYTHON SOURCE LINES 11-15 .. code-block:: Python import matplotlib.pyplot as plt from mathematicskit.optimization import knapsack .. GENERATED FROM PYTHON SOURCE LINES 16-18 Six items, capacity 15 ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 18-25 .. code-block:: Python values = [10, 13, 7, 8, 15, 4] weights = [5, 6, 3, 4, 7, 2] capacity = 15 result = knapsack(values, weights, capacity) print(f"best value {result.value:.0f} using items {result.items.tolist()} (total weight {result.weight})") .. rst-class:: sphx-glr-script-out .. code-block:: none best value 32 using items [0, 2, 4] (total weight 15) .. GENERATED FROM PYTHON SOURCE LINES 26-28 The value table V(i, c) ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 28-35 .. code-block:: Python fig, ax = plt.subplots(figsize=(9, 4)) im = ax.imshow(result.table, cmap="viridis", aspect="auto", origin="lower") fig.colorbar(im, ax=ax, label="best value") ax.set_xlabel("capacity c") ax.set_ylabel("items considered i") ax.set_title("Bellman's value table for the knapsack problem (1957)") .. image-sg:: /api/gallery/optimization/dynamic_programming/images/sphx_glr_plot_01_knapsack_001.png :alt: Bellman's value table for the knapsack problem (1957) :srcset: /api/gallery/optimization/dynamic_programming/images/sphx_glr_plot_01_knapsack_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, "Bellman's value table for the knapsack problem (1957)") .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.037 seconds) .. _sphx_glr_download_api_gallery_optimization_dynamic_programming_plot_01_knapsack.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_knapsack.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_knapsack.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_knapsack.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_