Skip to content

Commit 44ecb35

Browse files
committed
Update quickstart openjij EN
1 parent 4878fc0 commit 44ecb35

File tree

2 files changed

+58
-43
lines changed

2 files changed

+58
-43
lines changed

docs/en/introduction.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 55 additions & 40 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
{
@@ -295,7 +310,7 @@
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
}

0 commit comments

Comments
 (0)