{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# ommx.v1.ParametricInstance\n", "\n", "[`ommx.v1.ParametricInstance`](https://jij-inc.github.io/ommx/python/ommx/autoapi/ommx/v1/index.html#ommx.v1.ParametricInstance) is a class that represents mathematical models similar to [`ommx.v1.Instance`](https://jij-inc.github.io/ommx/python/ommx/autoapi/ommx/v1/index.html#ommx.v1.Instance). It also supports parameters (via [`ommx.v1.Parameter`](https://jij-inc.github.io/ommx/python/ommx/autoapi/ommx/v1/index.html#ommx.v1.Parameter)) in addition to decision variables. By assigning values to these parameters, you can create an `ommx.v1.Instance`. Because the resulting `ommx.v1.Instance` keeps the IDs of decision variables and constraints from `ommx.v1.ParametricInstance`, it is helpful when you need to handle a series of models where only some coefficients of the objective function or constraints change.\n", "\n", "Consider the following knapsack problem.\n", "\n", "$$\n", "\\begin{align*}\n", "\\text{maximize} \\quad & \\sum_{i=1}^{N} p_i x_i \\\\\n", "\\text{subject to} \\quad & \\sum_{i=1}^{N} w_i x_i \\leq W \\\\\n", "& x_i \\in \\{0, 1\\} \\quad (i=1, 2, \\ldots, N)\n", "\\end{align*}\n", "$$\n", "\n", "Here, $N$ is the number of items, $p_i$ is the value of item i, $w_i$ is the weight of item i, and $W$ is the knapsack's capacity. The variable $x_i$ is binary and indicates whether item i is included in the knapsack. In `ommx.v1.Instance`, fixed values were used for $p_i$ and $w_i$, but here they are treated as parameters." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from ommx.v1 import ParametricInstance, DecisionVariable, Parameter, Instance\n", "\n", "N = 6\n", "x = [DecisionVariable.binary(id=i, name=\"x\", subscripts=[i]) for i in range(N)]\n", "\n", "p = [Parameter.new(id=i+ N, name=\"Profit\", subscripts=[i]) for i in range(N)]\n", "w = [Parameter.new(id=i+2*N, name=\"Weight\", subscripts=[i]) for i in range(N)]\n", "W = Parameter.new(id= 3*N, name=\"Capacity\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`ommx.v1.Parameter` also has an ID and uses the same numbering as `ommx.v1.DecisionVariable`, so please ensure there are no duplicates. Like decision variables, parameters can have names and subscripts. They can also be used with operators such as `+` and `<=` to create `ommx.v1.Function` or `ommx.v1.Constraint` objects." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "objective = sum(p[i] * x[i] for i in range(N))\n", "constraint = sum(w[i] * x[i] for i in range(N)) <= W" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let’s combine these elements into an `ommx.v1.ParametricInstance` that represents the knapsack problem." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "parametric_instance = ParametricInstance.from_components(\n", " decision_variables=x,\n", " parameters=p + w + [W],\n", " objective=objective,\n", " constraints=[constraint],\n", " sense=Instance.MAXIMIZE,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Like `ommx.v1.Instance`, you can view the decision variables and constraints as DataFrames through the `decision_variables` and `constraints` properties. In addition, `ommx.v1.ParametricInstance` has a `parameters` property for viewing parameter information in a DataFrame." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
namesubscriptsdescription
id
6Profit[0]<NA>
7Profit[1]<NA>
8Profit[2]<NA>
9Profit[3]<NA>
10Profit[4]<NA>
11Profit[5]<NA>
12Weight[0]<NA>
13Weight[1]<NA>
14Weight[2]<NA>
15Weight[3]<NA>
16Weight[4]<NA>
17Weight[5]<NA>
18Capacity[]<NA>
\n", "
" ], "text/plain": [ " name subscripts description\n", "id \n", "6 Profit [0] \n", "7 Profit [1] \n", "8 Profit [2] \n", "9 Profit [3] \n", "10 Profit [4] \n", "11 Profit [5] \n", "12 Weight [0] \n", "13 Weight [1] \n", "14 Weight [2] \n", "15 Weight [3] \n", "16 Weight [4] \n", "17 Weight [5] \n", "18 Capacity [] " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "parametric_instance.parameters" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, let’s assign specific values to the parameters. Use `ParametricInstance.with_parameters`, which takes a dictionary mapping each `ommx.v1.Parameter` ID to its corresponding value." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "p_values = { x.id: value for x, value in zip(p, [10, 13, 18, 31, 7, 15]) }\n", "w_values = { x.id: value for x, value in zip(w, [11, 15, 20, 35, 10, 33]) }\n", "W_value = { W.id: 47 }\n", "\n", "instance = parametric_instance.with_parameters({**p_values, **w_values, **W_value})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "````{note}\n", "`ommx.v1.ParametricInstance` cannot handle parameters that change the number of decision variables or parameters (for example, a variable $N$). If you need this functionality, please use a more advanced modeler such as [JijModeling](https://jij-inc.github.io/JijModeling-Tutorials/ja/introduction.html).\n", "````" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "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.12.8" } }, "nbformat": 4, "nbformat_minor": 2 }