{ "cells": [ { "cell_type": "markdown", "id": "a07e739f", "metadata": {}, "source": [ "# Knapsack Problem\n", "\n", "In the knapsack problem, we are given the set of items $N = \\{ 0, ..., n-1 \\}$ with weights $w_i$ and profits $p_i$ for $i \\in N$ and a knapsack with capacity $c$.\n", "We want to maximize the total profit of the items in the knapsack.\n", "\n", "## DP Formulation\n", "\n", "Let $V(r, i)$ be the maximum profit of selecting items to pack from $\\{ i, ..., n - 1 \\}$ into a knapsack with capacity $r$.\n", "The DP formulation is as follows:\n", "\n", "$$\n", "\\begin{align}\n", " \\text{compute } & V(c, 0) \\\\\n", " & V(r, i) = \\begin{cases}\n", " \\max\\{ p_i + V(r - w_i, i + 1), V(r, i + 1) \\} & \\text{if } i < n \\land r \\geq w_i \\\\\n", " V(r, i + 1) & \\text{if } i < n \\land r < w_i \\\\\n", " 0 & \\text{otherwise.}\n", " \\end{cases}\n", "\\end{align}\n", "$$" ] }, { "cell_type": "markdown", "id": "319326c4", "metadata": {}, "source": [ "## Install DIDPPy" ] }, { "cell_type": "code", "execution_count": 1, "id": "86a40ebc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: didppy in /home/kuro/tidel/code/didp-rs/didppy/.venv/lib/python3.10/site-packages (0.7.1)\r\n" ] } ], "source": [ "!pip install didppy" ] }, { "cell_type": "markdown", "id": "1348cd06", "metadata": {}, "source": [ "## Data" ] }, { "cell_type": "code", "execution_count": 2, "id": "bb78b2a6", "metadata": {}, "outputs": [], "source": [ "n = 4\n", "weights = [10, 20, 30, 40]\n", "profits = [5, 25, 35, 50]\n", "capacity = 50" ] }, { "cell_type": "markdown", "id": "3dd70db8", "metadata": {}, "source": [ "## Modeling" ] }, { "cell_type": "code", "execution_count": 3, "id": "4f1905cb", "metadata": {}, "outputs": [], "source": [ "import didppy as dp\n", "\n", "model = dp.Model(maximize=True, float_cost=False)\n", "\n", "item = model.add_object_type(number=n)\n", "r = model.add_int_var(target=capacity)\n", "i = model.add_element_var(object_type=item, target=0)\n", "\n", "w = model.add_int_table(weights)\n", "p = model.add_int_table(profits)\n", "\n", "pack = dp.Transition(\n", " name=\"pack\",\n", " cost=p[i] + dp.IntExpr.state_cost(),\n", " effects=[(r, r - w[i]), (i, i + 1)],\n", " preconditions=[i < n, r >= w[i]],\n", ")\n", "model.add_transition(pack)\n", "\n", "ignore = dp.Transition(\n", " name=\"ignore\",\n", " cost=dp.IntExpr.state_cost(),\n", " effects=[(i, i + 1)],\n", " preconditions=[i < n],\n", ")\n", "model.add_transition(ignore)\n", "\n", "model.add_base_case([i == n])" ] }, { "cell_type": "markdown", "id": "2608cba4", "metadata": {}, "source": [ "## Solving with ForwardRecursion" ] }, { "cell_type": "code", "execution_count": 4, "id": "5f5f3122", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Solver: ForwardRecursion from DIDPPy v0.7.1\n", "pack 1\n", "pack 2\n", "profit: 60\n" ] } ], "source": [ "solver = dp.ForwardRecursion(model)\n", "solution = solver.search()\n", "\n", "for i, t in enumerate(solution.transitions):\n", " if t.name == \"pack\":\n", " print(\"pack {}\".format(i))\n", "\n", "print(\"profit: {}\".format(solution.cost))" ] }, { "cell_type": "markdown", "id": "563920fc", "metadata": {}, "source": [ "## Solving with CABS" ] }, { "cell_type": "code", "execution_count": 5, "id": "5118404b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pack 1\n", "pack 2\n", "\n", "Profit: 60\n" ] } ], "source": [ "solver = dp.CABS(model, quiet=True)\n", "solution = solver.search()\n", "\n", "for i, t in enumerate(solution.transitions):\n", " if t.name == \"pack\":\n", " print(\"pack {}\".format(i))\n", "\n", "print(\"\")\n", "print(\"Profit: {}\".format(solution.cost))" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": ".venv" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" } }, "nbformat": 4, "nbformat_minor": 5 }