{ "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", " | name | \n", "subscripts | \n", "description | \n", "
---|---|---|---|
id | \n", "\n", " | \n", " | \n", " |
6 | \n", "Profit | \n", "[0] | \n", "<NA> | \n", "
7 | \n", "Profit | \n", "[1] | \n", "<NA> | \n", "
8 | \n", "Profit | \n", "[2] | \n", "<NA> | \n", "
9 | \n", "Profit | \n", "[3] | \n", "<NA> | \n", "
10 | \n", "Profit | \n", "[4] | \n", "<NA> | \n", "
11 | \n", "Profit | \n", "[5] | \n", "<NA> | \n", "
12 | \n", "Weight | \n", "[0] | \n", "<NA> | \n", "
13 | \n", "Weight | \n", "[1] | \n", "<NA> | \n", "
14 | \n", "Weight | \n", "[2] | \n", "<NA> | \n", "
15 | \n", "Weight | \n", "[3] | \n", "<NA> | \n", "
16 | \n", "Weight | \n", "[4] | \n", "<NA> | \n", "
17 | \n", "Weight | \n", "[5] | \n", "<NA> | \n", "
18 | \n", "Capacity | \n", "[] | \n", "<NA> | \n", "