docs/tutorial/ja/optimization/number_partition.ipynb
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 数分割問題\n",
"\n",
"こちらでは、[Lucas, 2014, \"Ising formulations of many NP problems\"](https://doi.org/10.3389/fphy.2014.00005)の 2.1. Number Partitioning を OpenJij と [JijModeling](https://www.ref.documentation.jijzept.com/jijmodeling/)、そして[JijModeling transpiler](https://www.ref.documentation.jijzept.com/jijmodeling-transpiler/) を用いて解く方法をご紹介します。\n",
"\n",
"## 概要: 数分割問題とは\n",
"\n",
"数分割問題は、与えられた数字の集合を足した合計値が等しくなるように2つの集合に分割する問題です。\n",
"ここで、簡単な例を考えてみましょう。\n",
"\n",
"例えば、$A=\\{1,2,3,4\\}$という数字の集合$A$があるとします。\n",
"この集合を合計値が等しくなるように分割するのは簡単で、$\\{1,4\\},\\{2,3\\}$とすれば、それぞれの集合の合計値が5になるということがわかります。\n",
"このように、集合のサイズが小さい場合には、比較的簡単に答えがもとまりますが、これが大きくなるとすぐには解けません。\n",
"そこで、このチュートリアルでは、この問題をアニーリングを使って解いてみましょう。 \n",
"まず初めに、この問題のハミルトニアンを考えます。\n",
"分割する集合を$A$とし、その要素を$a_i (i = \\{0,1,\\dots,N-1\\})$とします。\n",
"ここで$N$はこの集合の要素数です。\n",
"この集合$A$を二つの集合を$A_0$と$A_1$に分割するとします。\n",
"この時、$x_i$を$A$の$i$番目の要素が、集合$A_0$に含まれる時0、$A_1$に含まれる時1となる変数とします。\n",
"この変数$x_i$を用いると、$A_0$に入っている数の合計値は$\\sum_i a_i (1-x_i)$とかけ、$A_1$の$\\sum_i a_i x_i$となることがわかります。\n",
"この問題は、$A_0$と$A_1$に含まれている数の合計値が等しくなるという制約を満たす解を求める問題ですので、これを式にすると、\n",
"\n",
"$$\\sum_i a_i (1-x_i)=\\sum_i a_i x_i$$\n",
"\n",
"という制約条件を満たす$x_i$を求めよという問題であることがわかります。\n",
"これを式変形すると、$\\sum_i a_i (1-2x_i)=0$と書くことができ、さらに、Penalty法を用いて、この制約条件を2乗したものをハミルトニアンとすると、結局、数分割問題のハミルトニアンは、\n",
"\n",
"$$\n",
"H=\\left( \\sum_{i=0}^{N-1} a_i (1-2x_i)\\right)^2 \\tag{1}\n",
"$$\n",
"\n",
"となります。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## JijModelingによるモデル構築\n",
"\n",
"式(1)をJijModelingを用いて定式化していきます。"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import jijmodeling as jm\n",
"\n",
"problem = jm.Problem(\"Number Partition\")\n",
"a = jm.Placeholder(\"a\", ndim=1)\n",
"N = a.len_at(0, latex=\"N\")\n",
"x = jm.BinaryVar(\"x\", shape=(N, ))\n",
"i = jm.Element(\"i\", (0, N))\n",
"problem += jm.sum(i, a[i]*(1-2*x[i])) ** 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Jupyter Notebookで実装の確認を行いましょう。"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$$\\begin{array}{cccc}\\text{Problem:} & \\text{Number Partition} & & \\\\& & \\min \\quad \\displaystyle \\left(\\left(\\sum_{i = 0}^{N - 1} a_{i} \\cdot \\left(- 2 \\cdot x_{i} + 1\\right)\\right)^{2}\\right) & \\\\\\text{{where}} & & & \\\\& x & 1\\text{-dim binary variable}\\\\\\end{array}$$"
],
"text/plain": [
"<jijmodeling.Problem at 0x1870ca0>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"problem"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### インスタンスデータの作成\n",
"ここでは、1から40までの数字を分割する問題を考えましょう。\n",
"$N_{i}$から$N_{f}$まで連続する数を分割する問題(連続する数の合計数が偶数の時)では、分割の仕方はいろんなパターンがありますが分割された集合の合計値は\n",
"\n",
"$$\\mathrm{total\\ value} = \\frac{(N_{i} + N_{f})(N_{f} - N_{i} + 1)}{4}$$\n",
"\n",
"と計算することができます。\n",
"今の場合には、合計値は410となります。\n",
"実際にそれを確かめてみましょう。"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"inst_N = 40\n",
"instance_data = {\"a\": np.arange(1, inst_N+1)}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### JijModeling transpilerによるPyQUBOへの変換\n",
"\n",
"ここまで行われてきた実装は、全てJijModelingによるものでした。\n",
"これを[PyQUBO](https://pyqubo.readthedocs.io/en/latest/)に変換することで、OpenJijはもちろん、他のソルバーを用いた組合せ最適化計算を行うことが可能になります。"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import jijmodeling_transpiler as jmt\n",
"\n",
"# compile\n",
"compiled_model = jmt.core.compile_model(problem, instance_data, {})\n",
"# get qubo model\n",
"pubo_builder = jmt.core.pubo.transpile_to_pubo(compiled_model=compiled_model, relax_method=jmt.core.pubo.RelaxationMethod.AugmentedLagrangian)\n",
"qubo, const = pubo_builder.get_qubo_dict(multipliers={})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### OpenJijによる最適化計算の実行\n",
"OpenJijを用いて計算してみます。"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import openjij as oj\n",
"\n",
"sampler = oj.SASampler()\n",
"response = sampler.sample_qubo(qubo, num_reads=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### デコードと解の表示\n",
"\n",
"計算結果をデコードします。\n",
"ここでは、$A$の中で$A_1$に分類されたindexと$A_0$に分類されたindexを分けて、それらについて和をとっています。"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"class 1 : [ 2 8 9 12 15 18 19 21 22 23 24 26 30 31 35 36 39 40] , total value = 410\n",
"class 0 : [ 1 3 4 5 6 7 10 11 13 14 16 17 20 25 27 28 29 32 33 34 37 38] , total value = 410\n"
]
}
],
"source": [
"# decode a result to JijModeling sampleset\n",
"sampleset = jmt.core.pubo.decode_from_openjij(response, pubo_builder, compiled_model)\n",
"# get the indices of x == 1\n",
"class_1_indices = sampleset.record.solution[\"x\"][0][0][0]\n",
"class_0_indices = [i for i in range(0, inst_N) if i not in class_1_indices]\n",
"\n",
"class_1 = instance_data['a'][class_1_indices]\n",
"class_0 = instance_data['a'][class_0_indices]\n",
"\n",
"print(f\"class 1 : {class_1} , total value = {np.sum(class_1)}\")\n",
"print(f\"class 0 : {class_0} , total value = {np.sum(class_0)}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"我々の予想通り、それぞれの合計値410が得られていることがわかりました。"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.5"
},
"vscode": {
"interpreter": {
"hash": "5de874e2fc479b2d8c72d9b9d7199763e296392b542125e77f5cad711bb306ce"
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}