Skip to content

Commit e7e83b1

Browse files
authored
Update OpenJij Quickstart to use OMMX Adapter (#42)
1 parent 6e725fd commit e7e83b1

File tree

15 files changed

+286
-1087
lines changed

15 files changed

+286
-1087
lines changed

docs/en/introduction.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"\n",
1818
"`jijmodeling` is a tool for describing mathematical models using Python code, not for evaluating or solving mathematical models. This specification focuses on the algebraic structure of mathematical models, independent of instance data, allowing for logical consideration, verification, and faster modification of mathematical models. It also functions as a schema for generating the input format expected by solvers.\n",
1919
"\n",
20-
"To solve the mathematical models described with `jijmodeling` using a solver, you need to combine them with actual instance data and convert them into the input format for a specific solver using other tools such as the [JijZept service](https://www.jijzept.com) or the freely available [jijmodeling-transpiler](https://www.documentation.jijzept.com/docs/jijmodelingtranspiler/)."
20+
"To solve the mathematical models described with `jijmodeling` using a solver, you need to combine them with actual instance data and convert them into the input format for a specific solver using other tools such as the [JijZept service](https://www.jijzept.com)."
2121
]
2222
},
2323
{
@@ -43,7 +43,7 @@
4343
{
4444
"data": {
4545
"text/plain": [
46-
"'1.10.0'"
46+
"'1.12.4'"
4747
]
4848
},
4949
"execution_count": 1,
@@ -68,7 +68,7 @@
6868
],
6969
"metadata": {
7070
"kernelspec": {
71-
"display_name": ".venv",
71+
"display_name": "Python 3 (ipykernel)",
7272
"language": "python",
7373
"name": "python3"
7474
},
@@ -82,9 +82,9 @@
8282
"name": "python",
8383
"nbconvert_exporter": "python",
8484
"pygments_lexer": "ipython3",
85-
"version": "3.11.4"
85+
"version": "3.9.18"
8686
}
8787
},
8888
"nbformat": 4,
89-
"nbformat_minor": 2
89+
"nbformat_minor": 4
9090
}

docs/en/quickstart/openjij.ipynb

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
"\n",
99
"Let's look at a basic example to understand how to use `jijmodeling`. Below, we will explain the steps to create a simple mathematical model, transform it, and run it with a solver. The first two sections are sufficient with just `jijmodeling`, but we recommend using [Jupyter Notebook](https://jupyter.org) to easily check LaTeX output.\n",
1010
"\n",
11-
"In the third section, we will use [JijModelingTranspiler](https://www.documentation.jijzept.com/docs/jijmodelingtranspiler/) and [OpenJij](https://openjij.github.io/OpenJij/index.html). JijModelingTranspiler and OpenJij can be installed using `pip`.\n",
11+
"In the third section, we will use [`ommx-openjij-adapter`](https://pypi.org/project/ommx-openjij-adapter/) to solve our model through [OpenJij](https://openjij.github.io/OpenJij/index.html). `ommx-openjij-adapter` can be installed through `pip`:\n",
1212
"\n",
1313
"```bash\n",
14-
"pip install jijmodeling-transpiler openjij\n",
14+
"pip install ommx-openjij-adapter\n",
1515
"```"
1616
]
1717
},
@@ -60,7 +60,7 @@
6060
"$$\\begin{array}{cccc}\\text{Problem:} & \\text{my problem} & & \\\\& & \\min \\quad \\displaystyle n \\cdot \\left(a + b + c\\right) & \\\\\\text{{s.t.}} & & & \\\\ & \\text{c1} & \\displaystyle 2 \\cdot \\left(b + c\\right) \\leq 75 & \\\\ & \\text{c2} & \\displaystyle a + b \\leq 40 & \\\\\\text{{where}} & & & \\\\& a & 0\\text{-dim integer variable}\\\\ & & \\text{lower bound: }5 & \\\\ & & \\text{upper bound: }20 & \\\\& b & 0\\text{-dim integer variable}\\\\ & & \\text{lower bound: }1 & \\\\ & & \\text{upper bound: }20 & \\\\& c & 0\\text{-dim integer variable}\\\\ & & \\text{lower bound: }20 & \\\\ & & \\text{upper bound: }30 & \\\\\\end{array}$$"
6161
],
6262
"text/plain": [
63-
"<jijmodeling.Problem at 0x145e1b300>"
63+
"<jijmodeling.Problem at 0x5badb53ce740>"
6464
]
6565
},
6666
"execution_count": 2,
@@ -139,7 +139,7 @@
139139
"$$\\begin{array}{cccc}\\text{Problem:} & \\text{knapsack} & & \\\\& & \\max \\quad \\displaystyle \\sum_{i = 0}^{N - 1} v_{i} \\cdot x_{i} & \\\\\\text{{s.t.}} & & & \\\\ & \\text{weight limit} & \\displaystyle \\sum_{i = 0}^{N - 1} w_{i} \\cdot x_{i} \\leq W & \\\\\\text{{where}} & & & \\\\& x & 1\\text{-dim binary variable}\\\\\\end{array}$$"
140140
],
141141
"text/plain": [
142-
"<jijmodeling.Problem at 0x157139210>"
142+
"<jijmodeling.Problem at 0x5badb53e17f0>"
143143
]
144144
},
145145
"execution_count": 4,
@@ -172,7 +172,7 @@
172172
"$$\\begin{array}{cccc}\\text{Problem:} & \\text{knapsack} & & \\\\& & \\max \\quad \\displaystyle \\sum_{i = 0}^{N - 1} v_{i} \\cdot x_{i} & \\\\\\text{{s.t.}} & & & \\\\ & \\text{weight limit} & \\displaystyle \\sum_{i = 0}^{N - 1} w_{i} \\cdot x_{i} \\leq W & \\\\\\text{{where}} & & & \\\\& x & 1\\text{-dim binary variable}\\\\\\end{array}$$"
173173
],
174174
"text/plain": [
175-
"<jijmodeling.Problem at 0x145e24520>"
175+
"<jijmodeling.Problem at 0x5badb53ffc20>"
176176
]
177177
},
178178
"execution_count": 5,
@@ -206,11 +206,12 @@
206206
"source": [
207207
"## Solving the Model\n",
208208
"\n",
209-
"With the above code, we were able to build a model, but `jijmodeling` alone doesn't do much besides LaTeX output. You need to combine it with instance data to generate input for the solver. As an example, let's see how to transform the mathematical model built with `jijmodeling` into a QUBO using the freely available [JijModelingTranspiler](https://www.documentation.jijzept.com/docs/jijmodelingtranspiler/) and solve the model using [OpenJij](https://openjij.github.io/OpenJij/index.html). Note that tools provided by [JijZept](https://www.documentation.jijzept.com/docs/jijzept/) also accept mathematical models built with `jijmodeling` as input.\n",
209+
"With the above code, we were able to build a model, but that model alone doesn't do much besides LaTeX output. You need to combine it with instance data to generate input for the solver. First, we define the instance data to be input into the `Placeholder`s in our model, and register that data in the `Interpreter` object.\n",
210210
"\n",
211-
"This section aims to provide a simple demonstration, so we won't explain JijModelingTranspiler or OpenJij in detail. Please refer to their respective documentation for more information.\n",
211+
"You can register instance data by passing a dictionary with the following keys and values to the constructor of the `Interpreter` class:\n",
212212
"\n",
213-
"Below, we use JijModelingTranspiler to transform the mathematical model built with `jijmodeling` and instance data into a QUBO."
213+
"- Key: String set in the `name` property of the `Placeholder` object\n",
214+
"- Value: Data to be input"
214215
]
215216
},
216217
{
@@ -219,66 +220,80 @@
219220
"metadata": {},
220221
"outputs": [],
221222
"source": [
222-
"from jijmodeling_transpiler.core import compile_model\n",
223-
"from jijmodeling_transpiler.core.pubo import transpile_to_pubo\n",
224-
"\n",
225-
"data = {\n",
226-
" \"W\": 100,\n",
227-
" \"v\": [100, 90, 80, 70, 60, 50, 40, 30],\n",
228-
" \"w\": [1, 5, 10, 20, 30, 40, 50, 60, 70]\n",
223+
"instance_data = {\n",
224+
" \"W\": 100, # Data of the knapsack's weight capacity\n",
225+
" \"v\": [100, 90, 80, 70, 60, 50, 40, 30], # Data of item values\n",
226+
" \"w\": [1, 5, 10, 20, 30, 40, 50, 60, 70] # Data of item weights\n",
229227
"}\n",
230-
"\n",
231-
"compiled = compile_model(problem, data)\n",
232-
"qubo, _ = transpile_to_pubo(compiled).get_qubo_dict()"
228+
"interpreter = jm.Interpreter(instance_data)"
233229
]
234230
},
235231
{
236232
"cell_type": "markdown",
237233
"metadata": {},
238234
"source": [
239-
"Now we have obtained a QUBO that can be accepted as input by OpenJij. Writing out QUBOs manually is prone to errors, but using `jijmodeling` and `jijmodeling_transpiler` eliminates such errors and makes verification easier. Let's actually input the QUBO into `openjij` and solve the problem."
235+
"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`s in the `Problem` object will be filled with the instance data and converted to an OMMX instance:"
240236
]
241237
},
242238
{
243239
"cell_type": "code",
244240
"execution_count": 7,
245241
"metadata": {},
242+
"outputs": [],
243+
"source": [
244+
"instance = interpreter.eval_problem(problem)"
245+
]
246+
},
247+
{
248+
"cell_type": "markdown",
249+
"metadata": {},
250+
"source": [
251+
":::{hint}\n",
252+
"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",
253+
":::"
254+
]
255+
},
256+
{
257+
"cell_type": "markdown",
258+
"metadata": {},
259+
"source": [
260+
"Now we have an OMMX instance which we can use as input to the OpenJij sampler. Let’s use `ommx-openjij-adapter` to actually sample the problem:"
261+
]
262+
},
263+
{
264+
"cell_type": "code",
265+
"execution_count": 17,
266+
"metadata": {},
246267
"outputs": [
247268
{
248-
"data": {
249-
"text/plain": [
250-
"Sample(sample={0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 0, 7: 0}, energy=-5.501111111111113, num_occurrences=1)"
251-
]
252-
},
253-
"execution_count": 7,
254-
"metadata": {},
255-
"output_type": "execute_result"
269+
"name": "stdout",
270+
"output_type": "stream",
271+
"text": [
272+
"Optimal value of the objective function: 369.234375\n"
273+
]
256274
}
257275
],
258276
"source": [
259-
"import openjij as oj\n",
277+
"from ommx_openjij_adapter import OMMXOpenJijSAAdapter\n",
260278
"\n",
261-
"sampler = oj.SASampler()\n",
262-
"response = sampler.sample_qubo(qubo, num_reads=1)\n",
263-
"response.first"
279+
"# Solve through SCIP and retrieve results as an ommx.v1.Solution\n",
280+
"samples = OMMXOpenJijSAAdapter.sample(instance, num_reads=1)\n",
281+
"\n",
282+
"print(f\"Optimal value of the objective function: {samples.best_feasible().objective}\")"
264283
]
265284
},
266285
{
267286
"cell_type": "markdown",
268287
"metadata": {},
269288
"source": [
270-
"The above code uses simulated annealing from `openjij`, and `num_reads=1` indicates sampling only once. By increasing the value of `num_reads`, you can sample multiple times and explore various results using the response object. However, for this problem, all samples reach the optimal solution, so we will sample once and look at the \"best\" solution found.\n",
271-
"\n",
272-
"```\n",
273-
"Sample(sample={0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 0, 7: 0}, energy=-5.501111111111113, num_occurrences=1)\n",
274-
"```"
289+
"The above code uses simulated annealing from `openjij`, and `num_reads=1` indicates sampling only once. By increasing the value of `num_reads`, you can sample multiple times and explore various results using the response object. However, for this problem, all samples reach the optimal solution, so we sample once and look at the \"best\" solution found."
275290
]
276291
},
277292
{
278293
"cell_type": "markdown",
279294
"metadata": {},
280295
"source": [
281-
"The above response object shows the values found by `openjij` for each decision variable. You can do much more to better process and visualize the results using JijModelingTranspiler and OpenJij, or reuse the same mathematical model for different purposes, but please refer to their respective documentation pages for more information."
296+
"The above samples object shows the values found by `openjij` for each decision variable. You can do much more to better process and visualize the results using the OMMX Adapter and OpenJij, or reuse the same mathematical model for different purposes, but please refer to their respective documentation pages for more information."
282297
]
283298
},
284299
{
@@ -289,13 +304,13 @@
289304
"\n",
290305
"- [Examples](https://www.documentation.jijzept.com/docs/category/learn)\n",
291306
"- [JijZept](https://www.documentation.jijzept.com/docs/jijzept/)\n",
292-
"- [JijModelingTranspiler](https://www.documentation.jijzept.com/docs/jijmodelingtranspiler/)"
307+
"- [OMMX](https://jij-inc.github.io/ommx/en/introduction.html)"
293308
]
294309
}
295310
],
296311
"metadata": {
297312
"kernelspec": {
298-
"display_name": ".venv",
313+
"display_name": "Python 3 (ipykernel)",
299314
"language": "python",
300315
"name": "python3"
301316
},
@@ -309,9 +324,9 @@
309324
"name": "python",
310325
"nbconvert_exporter": "python",
311326
"pygments_lexer": "ipython3",
312-
"version": "3.11.4"
327+
"version": "3.9.18"
313328
}
314329
},
315330
"nbformat": 4,
316-
"nbformat_minor": 2
331+
"nbformat_minor": 4
317332
}

docs/en/quickstart/scip.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
"source": [
141141
"## Step2. Register Instance Data in the `Interpreter` Object\n",
142142
"\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",
143+
"Prepare the instance data to be input into the `Placeholder`s of the mathematical model formulated in Step1, and register it in the `Interpreter` object.\n",
144144
"\n",
145145
"You can register instance data by passing a dictionary with the following keys and values to the constructor of the `Interpreter` class:\n",
146146
"\n",
@@ -349,7 +349,7 @@
349349
"metadata": {},
350350
"source": [
351351
":::{hint}\n",
352-
"`ommx_pyscipopt_adapter` の `model_to_solution` の返却値は `ommx.v1.Solution` オブジェクトです。OMMXについて詳しく知りたい場合は[こちら](https://github.com/Jij-Inc/ommx)を参照してください。\n",
352+
"`OMMXPySCIPOptAdapter.solve` returns an `ommx.v1.Solution` object. For more information on OMMX, see [here](https://github.com/Jij-Inc/ommx).\n",
353353
":::"
354354
]
355355
}

docs/en/references/tips.ipynb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"id": "4bb9ca8e-e701-4e76-863a-c39115b5b33e",
66
"metadata": {},
77
"source": [
8+
"# Tips\n",
9+
"\n",
810
"## Defining `Element` Along a Set\n",
911
"\n",
1012
"Some users may want to create an index $i \\in \\{0, 1, 2\\}$ using Python's set `Set` as follows:\n",
@@ -504,7 +506,7 @@
504506
"id": "4c3a952a-d1c4-47b7-98df-0d843c9bf6a7",
505507
"metadata": {},
506508
"source": [
507-
"It is recommended to construct instance data for use with `jijmodeling_transpiler` or JijZept as follows."
509+
"It is recommended to construct instance data for use with `jm.Interpreter` or JijZept as follows."
508510
]
509511
},
510512
{
@@ -533,7 +535,7 @@
533535
],
534536
"metadata": {
535537
"kernelspec": {
536-
"display_name": ".venv",
538+
"display_name": "Python 3 (ipykernel)",
537539
"language": "python",
538540
"name": "python3"
539541
},
@@ -547,7 +549,7 @@
547549
"name": "python",
548550
"nbconvert_exporter": "python",
549551
"pygments_lexer": "ipython3",
550-
"version": "3.9.16"
552+
"version": "3.9.18"
551553
}
552554
},
553555
"nbformat": 4,

0 commit comments

Comments
 (0)