|
4 | 4 | "cell_type": "markdown",
|
5 | 5 | "metadata": {},
|
6 | 6 | "source": [
|
7 |
| - "# SCIPで最適化問題を解く\n", |
| 7 | + "# Solving Optimization Problems with SCIP\n", |
8 | 8 | "\n",
|
9 |
| - "`jijmodeling` の使い方を理解するために、このページではナップサック問題を解いてみましょう。ただし、`jijmodeling` は数理モデルを記述するためのツールであるため、単独では最適化問題を解くことはできません。なので、数理最適化ソルバー[SCIP](https://www.scipopt.org/)と組み合わせて解くこととします。\n", |
| 9 | + "To understand how to use `jijmodeling`, let's solve the knapsack problem on this page. However, since `jijmodeling` is a tool for describing mathematical models, it cannot solve optimization problems on its own. Therefore, we will solve it in combination with the mathematical optimization solver [SCIP](https://www.scipopt.org/).\n", |
10 | 10 | "\n",
|
11 |
| - "`jijmodeling` とSCIPを組み合わせて使うには、 `ommx-pyscipopt-adapter` ([GitHub](https://github.com/Jij-Inc/ommx/tree/main/python/ommx-pyscipopt-adapter), [PyPI](https://pypi.org/project/ommx-pyscipopt-adapter/)) というPythonパッケージをインストールする必要があります。以下のコマンドでインストールしてください。\n", |
| 11 | + "To use `jijmodeling` with SCIP, you need to install a Python package called `ommx-pyscipopt-adapter` ([GitHub](https://github.com/Jij-Inc/ommx/tree/main/python/ommx-pyscipopt-adapter), [PyPI](https://pypi.org/project/ommx-pyscipopt-adapter/)). Please install it with the following command:\n", |
12 | 12 | "\n",
|
13 | 13 | "```bash\n",
|
14 | 14 | "pip install ommx-pyscipopt-adapter\n",
|
|
19 | 19 | "cell_type": "markdown",
|
20 | 20 | "metadata": {},
|
21 | 21 | "source": [
|
22 |
| - "## 問題設定\n", |
| 22 | + "## Problem Setting\n", |
23 | 23 | "\n",
|
24 |
| - "ナップサック問題は以下のように数理モデルとして定式化することができます:\n", |
| 24 | + "The knapsack problem can be formulated as a mathematical model as follows:\n", |
25 | 25 | "\n",
|
26 | 26 | "$$\n",
|
27 | 27 | "\\begin{align*}\n",
|
|
32 | 32 | "$$\n",
|
33 | 33 | "\n",
|
34 | 34 | ":::{hint}\n",
|
35 |
| - "ナップサック問題の定式化について詳しく知りたい場合は [こちら](https://www.documentation.jijzept.com/docs/tutorial/knapsack/) を参照してください。\n", |
| 35 | + "For more details on the formulation of the knapsack problem, please refer to [here](https://www.documentation.jijzept.com/docs/tutorial/knapsack/).\n", |
36 | 36 | ":::\n",
|
37 | 37 | "\n",
|
38 |
| - "この数理モデルにあるそれぞれのパラメーターの意味は以下の通りです:\n", |
| 38 | + "The meaning of each parameter in this mathematical model is as follows:\n", |
39 | 39 | "\n",
|
40 |
| - "| パラメーター | 説明 |\n", |
| 40 | + "| Parameter | Description |\n", |
41 | 41 | "| --- | --- |\n",
|
42 |
| - "| $N$ |\tアイテムの総数 |\n", |
43 |
| - "| $v_{i}$ | アイテム $i$ の価値 |\n", |
44 |
| - "| $w_{i}$ | アイテム $i$ の重さ |\n", |
45 |
| - "| $W$ | ナップサックの耐荷重 |\n", |
| 42 | + "| $N$ |\tTotal number of items |\n", |
| 43 | + "| $v_{i}$ | Value of item $i$ |\n", |
| 44 | + "| $w_{i}$ | Weight of item $i$ |\n", |
| 45 | + "| $W$ | Weight capacity of the knapsack |\n", |
46 | 46 | "\n",
|
47 |
| - "今回の説明では、上記の数理モデルのパラメーター $v_{i}, w_{i}, W$ に、次の値を入力して得られる[インスタンス](what_is_instance)を解くことを考えます:\n", |
| 47 | + "In this explanation, we will solve an [instance](what_is_instance) obtained by inputting the following values into the parameters $v_{i}, w_{i}, W$ of the above mathematical model:\n", |
48 | 48 | "\n",
|
49 |
| - "| パラメーター | 値 |\n", |
| 49 | + "| Parameter | Value |\n", |
50 | 50 | "| --- | --- |\n",
|
51 | 51 | "| $v_{i}$ | `[10, 13, 18, 31, 7, 15]` |\n",
|
52 | 52 | "| $w_{i}$ | `[11, 15, 20, 35, 10, 33]` |\n",
|
53 | 53 | "| $W$ | `47` |\n",
|
54 | 54 | "\n",
|
55 | 55 | "(what_is_instance)=\n",
|
56 |
| - ":::{admonition} インスタンスとは\n", |
57 |
| - "`jijmodeling` では、数理モデルのパラメータに具体的な値を入れたものを”インスタンス”と呼んでいます。\n", |
| 56 | + ":::{admonition} What is an instance?\n", |
| 57 | + "In `jijmodeling`, an instance is a mathematical model with specific values assigned to its parameters.\n", |
58 | 58 | ":::"
|
59 | 59 | ]
|
60 | 60 | },
|
61 | 61 | {
|
62 | 62 | "cell_type": "markdown",
|
63 | 63 | "metadata": {},
|
64 | 64 | "source": [
|
65 |
| - "## インスタンスの生成手順\n", |
| 65 | + "## Procedure for Generating an Instance\n", |
66 | 66 | "\n",
|
67 |
| - "`jijmodeling` を使うと、ソルバーに入力するためのインスタンスを次の3ステップで生成できます:\n", |
| 67 | + "Using `jijmodeling`, you can generate an instance to input into the solver in the following three steps:\n", |
68 | 68 | "\n",
|
69 |
| - "1. `jijmodeling` でナップサック問題を定式化する\n", |
70 |
| - "2. `Interpreter` オブジェクトにインスタンスデータを登録する\n", |
71 |
| - "3. `Interpreter` オブジェクトを使って数理モデルをインスタンスに変換する\n", |
| 69 | + "1. Formulate the knapsack problem with `jijmodeling`\n", |
| 70 | + "2. Register instance data in the `Interpreter` object\n", |
| 71 | + "3. Convert the mathematical model to an instance using the `Interpreter` object\n", |
72 | 72 | "\n",
|
73 | 73 | ""
|
74 | 74 | ]
|
|
77 | 77 | "cell_type": "markdown",
|
78 | 78 | "metadata": {},
|
79 | 79 | "source": [
|
80 |
| - "## Step1. JijModelingでナップサック問題を定式化する\n", |
| 80 | + "## Step1. Formulate the Knapsack Problem with JijModeling\n", |
81 | 81 | "\n",
|
82 |
| - "`jijmodeling` を使用してナップサック問題を定式化すると、以下のPythonコードになります:" |
| 82 | + "The following Python code formulates the knapsack problem using `jijmodeling`:" |
83 | 83 | ]
|
84 | 84 | },
|
85 | 85 | {
|
86 | 86 | "cell_type": "code",
|
87 |
| - "execution_count": 13, |
| 87 | + "execution_count": 1, |
88 | 88 | "metadata": {},
|
89 | 89 | "outputs": [
|
90 | 90 | {
|
91 | 91 | "data": {
|
92 | 92 | "text/latex": [
|
93 |
| - "$$\\begin{array}{cccc}\\text{Problem:} & \\text{problem} & & \\\\& & \\max \\quad \\displaystyle \\sum_{i = 0}^{N - 1} v_{i} \\cdot x_{i} & \\\\\\text{{s.t.}} & & & \\\\ & \\text{重量制限} & \\displaystyle \\sum_{i = 0}^{N - 1} w_{i} \\cdot x_{i} \\leq W & \\\\\\text{{where}} & & & \\\\& x & 1\\text{-dim binary variable}\\\\\\end{array}$$" |
| 93 | + "$$\\begin{array}{cccc}\\text{Problem:} & \\text{problem} & & \\\\& & \\max \\quad \\displaystyle \\sum_{i = 0}^{N - 1} v_{i} \\cdot x_{i} & \\\\\\text{{s.t.}} & & & \\\\ & \\text{Weight Constraint} & \\displaystyle \\sum_{i = 0}^{N - 1} w_{i} \\cdot x_{i} \\leq W & \\\\\\text{{where}} & & & \\\\& x & 1\\text{-dim binary variable}\\\\\\end{array}$$" |
94 | 94 | ],
|
95 | 95 | "text/plain": [
|
96 |
| - "<jijmodeling.Problem at 0x1234887d0>" |
| 96 | + "<jijmodeling.Problem at 0x12567f6f0>" |
97 | 97 | ]
|
98 | 98 | },
|
99 |
| - "execution_count": 13, |
| 99 | + "execution_count": 1, |
100 | 100 | "metadata": {},
|
101 | 101 | "output_type": "execute_result"
|
102 | 102 | }
|
103 | 103 | ],
|
104 | 104 | "source": [
|
105 | 105 | "import jijmodeling as jm\n",
|
106 | 106 | "\n",
|
107 |
| - "# アイテムの価値\n", |
| 107 | + "# Value of items\n", |
108 | 108 | "v = jm.Placeholder(\"v\", ndim=1)\n",
|
109 |
| - "# アイテムの重さ\n", |
| 109 | + "# Weight of items\n", |
110 | 110 | "w = jm.Placeholder(\"w\", ndim=1)\n",
|
111 |
| - "# ナップサックの耐荷重\n", |
| 111 | + "# Weight capacity of the knapsack\n", |
112 | 112 | "W = jm.Placeholder(\"W\")\n",
|
113 |
| - "# アイテムの総数\n", |
| 113 | + "# Total number of items\n", |
114 | 114 | "N = v.len_at(0, latex=\"N\")\n",
|
115 |
| - "# アイテムiをナップサックに入れる場合は1, 入れない場合は0を取る決定変数\n", |
| 115 | + "# Decision variable: 1 if item i is in the knapsack, 0 otherwise\n", |
116 | 116 | "x = jm.BinaryVar(\"x\", shape=(N,)) \n",
|
117 |
| - "# アイテムに割り当てられた番号を走る添え字\n", |
| 117 | + "# Index running over the assigned numbers of items\n", |
118 | 118 | "i = jm.Element(\"i\", belong_to=(0, N))\n",
|
119 | 119 | "\n",
|
120 | 120 | "problem = jm.Problem(\"problem\", sense=jm.ProblemSense.MAXIMIZE)\n",
|
121 |
| - "# 目的関数\n", |
| 121 | + "# Objective function\n", |
122 | 122 | "problem += jm.sum(i, v[i] * x[i])\n",
|
123 |
| - "# 制約条件: ナップサックの耐荷重を超えない\n", |
124 |
| - "problem += jm.Constraint(\"重量制限\", jm.sum(i, w[i] * x[i]) <= W)\n", |
| 123 | + "# Constraint: Do not exceed the weight capacity of the knapsack\n", |
| 124 | + "problem += jm.Constraint(\"Weight Constraint\", jm.sum(i, w[i] * x[i]) <= W)\n", |
125 | 125 | "problem"
|
126 | 126 | ]
|
127 | 127 | },
|
|
130 | 130 | "metadata": {},
|
131 | 131 | "source": [
|
132 | 132 | ":::{hint}\n",
|
133 |
| - "`jijmodeling` での定式化の方法については詳しく知りたい場合は[こちら](../tutorials/expressions.ipynb)を参照してください。\n", |
| 133 | + "For more details on how to formulate with `jijmodeling`, please refer to [here](../tutorials/expressions.ipynb).\n", |
134 | 134 | ":::"
|
135 | 135 | ]
|
136 | 136 | },
|
137 | 137 | {
|
138 | 138 | "cell_type": "markdown",
|
139 | 139 | "metadata": {},
|
140 | 140 | "source": [
|
141 |
| - "## Step2. `Interpreter` オブジェクトにインスタンスデータを登録する\n", |
| 141 | + "## Step2. Register Instance Data in the `Interpreter` Object\n", |
142 | 142 | "\n",
|
143 |
| - "Step1で定式化した数理モデルの `Placeholder` に入力するインスタンスデータを用意し、 `Interpreter` オブジェクトに登録します。\n", |
| 143 | + "Prepare the instance data to be input into the `Placeholder` of the mathematical model formulated in Step1, and register it in the `Interpreter` object.\n", |
144 | 144 | "\n",
|
145 |
| - "`Interpreter` クラスのコンストラクタの引数に、以下のキーと値を持つ辞書を渡すことでインスタンスデータを登録できます:\n", |
| 145 | + "You can register instance data by passing a dictionary with the following keys and values to the constructor of the `Interpreter` class:\n", |
146 | 146 | "\n",
|
147 |
| - "- キー:`Placeholder` オブジェクトの `name` プロパティに設定した文字列\n", |
148 |
| - "- 値:入力するデータ" |
| 147 | + "- Key: String set in the `name` property of the `Placeholder` object\n", |
| 148 | + "- Value: Data to be input" |
149 | 149 | ]
|
150 | 150 | },
|
151 | 151 | {
|
152 | 152 | "cell_type": "code",
|
153 |
| - "execution_count": 15, |
| 153 | + "execution_count": 2, |
154 | 154 | "metadata": {},
|
155 | 155 | "outputs": [],
|
156 | 156 | "source": [
|
157 | 157 | "instance_data = {\n",
|
158 |
| - " \"v\": [10, 13, 18, 31, 7, 15], # アイテムの価値のデータ\n", |
159 |
| - " \"w\": [11, 15, 20, 35, 10, 33], # アイテムの重さのデータ\n", |
160 |
| - " \"W\": 47, # ナップサックの耐荷重のデータ\n", |
| 158 | + " \"v\": [10, 13, 18, 31, 7, 15], # Data of item values\n", |
| 159 | + " \"w\": [11, 15, 20, 35, 10, 33], # Data of item weights\n", |
| 160 | + " \"W\": 47, # Data of the knapsack's weight capacity\n", |
161 | 161 | "}\n",
|
162 | 162 | "interpreter = jm.Interpreter(instance_data)"
|
163 | 163 | ]
|
|
166 | 166 | "cell_type": "markdown",
|
167 | 167 | "metadata": {},
|
168 | 168 | "source": [
|
169 |
| - "## Step3. `Interpreter` オブジェクトを使って数理モデルをインスタンスに変換する\n", |
| 169 | + "## Step3. Convert the Mathematical Model to an Instance Using the `Interpreter` Object\n", |
170 | 170 | "\n",
|
171 |
| - "数理モデルをインスタンスに変換するには、`Interpreter.eval_problem` メソッドを使用します。インスタンスデータが登録された `Interpreter` オブジェクトの `eval_problem` メソッドに `Problem` オブジェクトを渡すと、その `Problem` オブジェクトが持つ `Placeholder` にインスタンスデータを入力され、インスタンスに変換されます:" |
| 171 | + "To convert the mathematical model to an instance, use the `Interpreter.eval_problem` method. By passing the `Problem` object to the `eval_problem` method of the `Interpreter` object with registered instance data, the `Placeholder` in the `Problem` object will be filled with the instance data and converted to an instance:" |
172 | 172 | ]
|
173 | 173 | },
|
174 | 174 | {
|
|
185 | 185 | "metadata": {},
|
186 | 186 | "source": [
|
187 | 187 | ":::{hint}\n",
|
188 |
| - "`Interpreter.eval_problem` の返却値は `ommx.v1.Instance` オブジェクトです。OMMXについて詳しく知りたい場合は[こちら](https://github.com/Jij-Inc/ommx)を参照してください。\n", |
| 188 | + "The return value of `Interpreter.eval_problem` is an `ommx.v1.Instance` object. For more details about OMMX, please refer to [here](https://github.com/Jij-Inc/ommx).\n", |
189 | 189 | ":::"
|
190 | 190 | ]
|
191 | 191 | },
|
192 | 192 | {
|
193 | 193 | "cell_type": "markdown",
|
194 | 194 | "metadata": {},
|
195 | 195 | "source": [
|
196 |
| - "## 最適化問題を解く\n", |
| 196 | + "## Solving the Optimization Problem\n", |
197 | 197 | "\n",
|
198 |
| - "では、Step3で得られたインスタンスを最適化ソルバーSCIPで解いてみましょう。以下のPythonコードで目的関数の最適値を得ることができます:" |
| 198 | + "Now, let's solve the instance obtained in Step3 with the optimization solver SCIP. The following Python code can be used to obtain the optimal value of the objective function:" |
199 | 199 | ]
|
200 | 200 | },
|
201 | 201 | {
|
202 | 202 | "cell_type": "code",
|
203 |
| - "execution_count": null, |
| 203 | + "execution_count": 4, |
204 | 204 | "metadata": {},
|
205 | 205 | "outputs": [
|
206 | 206 | {
|
207 | 207 | "name": "stdout",
|
208 | 208 | "output_type": "stream",
|
209 | 209 | "text": [
|
210 |
| - "目的関数の最適値: 41.0\n" |
| 210 | + "Optimal value of the objective function: 41.0\n" |
211 | 211 | ]
|
212 | 212 | }
|
213 | 213 | ],
|
214 | 214 | "source": [
|
215 | 215 | "from ommx_pyscipopt_adapter import instance_to_model, model_to_solution\n",
|
216 | 216 | "\n",
|
217 |
| - "# インスタンスをSCIPの読み取れる形に変換する\n", |
| 217 | + "# Convert the instance to a format readable by SCIP\n", |
218 | 218 | "model = instance_to_model(instance)\n",
|
219 |
| - "# SCIPでインスタンスを解く\n", |
| 219 | + "# Solve the instance with SCIP\n", |
220 | 220 | "model.optimize()\n",
|
221 |
| - "# SCIPで得られた結果を取得する\n", |
| 221 | + "# Retrieve the results obtained by SCIP\n", |
222 | 222 | "solution = model_to_solution(model, instance)\n",
|
223 | 223 | "\n",
|
224 |
| - "print(f\"目的関数の最適値: {solution.objective}\")" |
| 224 | + "print(f\"Optimal value of the objective function: {solution.objective}\")" |
225 | 225 | ]
|
226 | 226 | },
|
227 | 227 | {
|
228 | 228 | "cell_type": "markdown",
|
229 | 229 | "metadata": {},
|
230 | 230 | "source": [
|
231 |
| - "また、`solution` の `decision_variables` プロパティを使うことで `pandas.DataFrame` オブジェクトとして決定変数の状態を表示できます:" |
| 231 | + "In addition, you can display the state of the decision variables as a `pandas.DataFrame` object using the `decision_variables` property of `solution`:" |
232 | 232 | ]
|
233 | 233 | },
|
234 | 234 | {
|
235 | 235 | "cell_type": "code",
|
236 |
| - "execution_count": 18, |
| 236 | + "execution_count": 5, |
237 | 237 | "metadata": {},
|
238 | 238 | "outputs": [
|
239 | 239 | {
|
|
331 | 331 | "5 x [5] 0.0"
|
332 | 332 | ]
|
333 | 333 | },
|
334 |
| - "execution_count": 18, |
| 334 | + "execution_count": 5, |
335 | 335 | "metadata": {},
|
336 | 336 | "output_type": "execute_result"
|
337 | 337 | }
|
|
366 | 366 | "name": "python",
|
367 | 367 | "nbconvert_exporter": "python",
|
368 | 368 | "pygments_lexer": "ipython3",
|
369 |
| - "version": "3.9.16" |
| 369 | + "version": "3.11.4" |
370 | 370 | }
|
371 | 371 | },
|
372 | 372 | "nbformat": 4,
|
|
0 commit comments