diff --git a/MILP-demo.ipynb b/MILP-demo.ipynb index 737edd5..d9bbbe8 100644 --- a/MILP-demo.ipynb +++ b/MILP-demo.ipynb @@ -70,7 +70,9 @@ } ], "source": [ - "c = -np.array([1, 2, 2])\n", + "## constant value in objective\n", + "\n", + "c = -np.array([1, 2, 2]) \n", "A = np.array([[-1, 1, 1], [3, 2, -1], [1, 1, 1], [2, 3, 3]])\n", "b_u = np.array([1, 12, 10, np.inf])\n", "b_l = np.array([-np.inf, -np.inf, -np.inf, 12])\n", @@ -87,7 +89,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We see the MILP solver tells us the object function is maximized when $x_1 = 6, x_2 = 0, x_3 = 7$.\n", + "We see the MILP solver tells us the object function is maximized when $x_1 = 5, x_2 = 0, x_3 = 5$.\n", "\n", "As the next step, let's create a corresponding `QuadraticProgram` class in `qiskit_optimization`." ] @@ -125,7 +127,7 @@ ], "source": [ "## Consider the possibility of using sparse matrix(to_dict)\n", - "\n", + "## 1e20=inf\n", "qp = QuadraticProgram('example-1')\n", "qp.integer_var(name='x1')\n", "qp.integer_var(name='x2')\n", @@ -202,6 +204,9 @@ "\n", "\n", "## Check objective is linear\n", + "from types import NoneType\n", + "\n", + "\n", "if qp.objective.quadratic.to_dict()!={}:\n", " raise QiskitError('Obejective function is not linear!')\n", "\n", @@ -213,7 +218,7 @@ "sense = qp.objective.sense.value\n", "\n", "## cost function for milp solver\n", - "c_qp = qp.objective.linear.to_array() * sense\n", + "c_qp = qp.objective.linear.to_array() * sense \n", "\n", "## constraints for milp solver\n", "for i, constraint in enumerate(qp.linear_constraints):\n", @@ -226,19 +231,50 @@ " bl_qp = np.array([constraint_value if constraint_sense==1 else -np.inf])\n", " bu_qp = np.array([constraint_value if constraint_sense==0 else np.inf])\n", " else:\n", - " A_qp = np.vstack((A_qp,constraint_array))\n", + " A_qp = np.vstack((A_qp,constraint_array)) ## Not efficient. Use list directly.\n", " bl_qp = np.append(bl_qp, [constraint_value if constraint_sense==1 else -np.inf])\n", " bu_qp = np.append(bu_qp, [constraint_value if constraint_sense==0 else np.inf])\n", "\n", "constraints = LinearConstraint(A_qp, bl_qp, bu_qp)\n", "\n", "## integrity for milp solver\n", + "## Use Enum and VarType to compare\n", "integrality = np.array([ 1 if variable.vartype.value==2 else 0 for variable in qp.variables]) ## check on other vartype!\n", "\n", "raw_res = milp(c=c_qp, constraints=constraints, integrality=integrality)\n", "\n", "print(raw_res)\n", - " " + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "## Sparse converter based on to_dict" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "ename": "AttributeError", + "evalue": "'QuadraticProgram' object has no attribute 'constraints'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn [37], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mqp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconstraints\u001b[49m\n", + "\u001b[0;31mAttributeError\u001b[0m: 'QuadraticProgram' object has no attribute 'constraints'" + ] + } + ], + "source": [ + "qp.linear_constraints.sense" ] }, {