{ "cells": [ { "cell_type": "markdown", "id": "95a492d4", "metadata": { "tags": [ "COP", "medium", "complex", "AllDifferent", "Intension", "BinPacking", "Precedence", "symmetry-breaking", "Element", "objSum" ] }, "source": [ "# Problem *Cardinality Constrained Multi-cycle Problem (CCMcP)*" ] }, { "cell_type": "markdown", "id": "ec926be8", "metadata": {}, "source": [ "The Cardinality Constrained Multi-cycle Problem (CCMcP) is a variation of the Kidney Exchange Problem (KEP).\n", "One can consider the CCMcP as an Asymmetric Travelling Salesman Problem (ATSP) with subtours (cycles) allowed (but of limited size $k$).\n", "Each arc has an associated weight.\n", "The arcs with negative weights cannot be part of subtours, and the objective is to maximize the sum of weights occurring along the arcs of the computed subtours (cycles).\n", "The problem is studied in \"On the kidney exchange problem: cardinality constrained cycle and chain problems\n", "on directed graphs: a survey of integer programming approaches\" by V. Mak-Hau, in Journal of Combinatorial\n", "Optimization 2017.\n" ] }, { "cell_type": "markdown", "id": "f898fcd6", "metadata": {}, "source": [ "To build a COP (Constraint Optimization Problem) model, we need first to import the library PyCSP$^3$:" ] }, { "cell_type": "code", "execution_count": 1, "id": "1500590b", "metadata": {}, "outputs": [], "source": [ "from pycsp3 import *" ] }, { "cell_type": "markdown", "id": "118b1507", "metadata": {}, "source": [ "Then, we need some data. " ] }, { "cell_type": "code", "execution_count": 2, "id": "55ecd7d9", "metadata": {}, "outputs": [], "source": [ "weights = cp_array([\n", " [0, -1, 4, 2, 5, 6],\n", " [-1, 0, -1, 8, -1, -1],\n", " [3, 8, 0, -1, 9, 3],\n", " [5, -1, -1, 0, 6, 5],\n", " [4, -1, 9, -1, 0, 2],\n", " [7, 8, 8, 2, -1, 0]\n", "])\n", "n, k = len(weights), 3 # n is the number of nodes, k is the length limit of cycles (here, 3)\n", "nMaxCycles = n//k + (1 if n%k != 0 else 0)" ] }, { "cell_type": "markdown", "id": "60245a31", "metadata": {}, "source": [ "Note that we need to call *cp_array()* here on the two-dimensional list of integers (to specialize its type), because of the expression we will write later when posting the objective. \n", "However, the good news is that when data come from a file, the transformation operated by *cp_array()* is automatically performed." ] }, { "cell_type": "markdown", "id": "b021c396", "metadata": {}, "source": [ "Ultimately, we need to know how cycles are formed (what is the successor node of each node?).\n", "Hence, we introduce an array $x$ of variables.\n", "We also need a way to identify the cycles (to which cyle belongs each node?).\n", "We introduce a second array $y$ of variables." ] }, { "cell_type": "code", "execution_count": 3, "id": "55dbebcb", "metadata": {}, "outputs": [], "source": [ "# x[i] is the successor node of node i (in the cycle where i belongs)\n", "x = VarArray(size=n, dom=range(n))\n", "\n", "# y[i] is the cycle (index) where the node i belongs\n", "y = VarArray(size=n, dom=range(nMaxCycles))" ] }, { "cell_type": "markdown", "id": "fb942a15", "metadata": {}, "source": [ "We can display the structure of the array, as well as the domain of the first variable (note that all variables have the same domain)." ] }, { "cell_type": "code", "execution_count": 4, "id": "347b7e18", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Array x: [x[0], x[1], x[2], x[3], x[4], x[5]]\n", "Domain of any variable in x: 0..5\n", "Array y: [y[0], y[1], y[2], y[3], y[4], y[5]]\n", "Domain of any variable in y: 0 1\n" ] } ], "source": [ "print(\"Array x: \", x)\n", "print(\"Domain of any variable in x: \", x[0].dom)\n", "\n", "print(\"Array y: \", y)\n", "print(\"Domain of any variable in y: \", y[0].dom)" ] }, { "cell_type": "raw", "id": "19125845", "metadata": {}, "source": [ "To start, we need to impose that no two nodes can be succeeded by the same node (otherwise, we have not a structure in term of cycles). Also, we ensure that a node and its successor belongs to the same cycle (by means of a constraint *Element*)." ] }, { "cell_type": "code", "execution_count": 5, "id": "f7cd740d", "metadata": {}, "outputs": [], "source": [ "satisfy(\n", " AllDifferent(x), \n", " \n", " # ensuring correct cycles\n", " [y[i] == y[x[i]] for i in range(n)]\n", ");" ] }, { "cell_type": "markdown", "id": "9c60eb3b", "metadata": {}, "source": [ "We can display the internal representation of the posted constraints; this way, although a little bit technical, we can see that the arguments of the constraints are correct." ] }, { "cell_type": "code", "execution_count": 6, "id": "474b0cef", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "allDifferent(list:[x[0], x[1], x[2], x[3], x[4], x[5]])\n", "element(list:[y[0], y[1], y[2], y[3], y[4], y[5]], index:x[0], condition:(eq,y[0]))\n", "element(list:[y[0], y[1], y[2], y[3], y[4], y[5]], index:x[1], condition:(eq,y[1]))\n", "element(list:[y[0], y[1], y[2], y[3], y[4], y[5]], index:x[2], condition:(eq,y[2]))\n", "element(list:[y[0], y[1], y[2], y[3], y[4], y[5]], index:x[3], condition:(eq,y[3]))\n", "element(list:[y[0], y[1], y[2], y[3], y[4], y[5]], index:x[4], condition:(eq,y[4]))\n", "element(list:[y[0], y[1], y[2], y[3], y[4], y[5]], index:x[5], condition:(eq,y[5]))\n" ] } ], "source": [ "print(posted())" ] }, { "cell_type": "markdown", "id": "d5b9a806", "metadata": {}, "source": [ "Interestingly, by calling the function *solve()*, we can check that the problem is satisfiable (SAT). We can also display the found solution. Here, we call the function *values()* that collects the values assigned to a specified list of variables." ] }, { "cell_type": "code", "execution_count": 7, "id": "91ad3a0e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x: [0, 1, 2, 3, 4, 5]\n", "y: [0, 0, 0, 0, 0, 0]\n" ] } ], "source": [ "if solve() is SAT:\n", " print(\"x:\", values(x))\n", " print(\"y:\", values(y))" ] }, { "cell_type": "markdown", "id": "5ef1c761", "metadata": {}, "source": [ "This is quite disappointing: cycles are just self-loops.\n", "Besides, all cycles have the same number (index) 0." ] }, { "cell_type": "markdown", "id": "d4a6a0ab", "metadata": {}, "source": [ "Let us prevent self-loops and let us avoid arcs with negative weights." ] }, { "cell_type": "code", "execution_count": 8, "id": "9d1e4578", "metadata": {}, "outputs": [], "source": [ "satisfy(\n", " # disabling infeasible arcs\n", " [x[i] != j for i in range(n) for j in range(n) if i == j or weights[i][j] < 0]\n", ");" ] }, { "cell_type": "markdown", "id": "c87b78f1", "metadata": {}, "source": [ "Let us run again the solver:" ] }, { "cell_type": "code", "execution_count": 9, "id": "42161113", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x: [2, 3, 0, 4, 5, 1]\n", "y: [0, 0, 0, 0, 0, 0]\n" ] } ], "source": [ "if solve() is SAT:\n", " print(\"x:\", values(x))\n", " print(\"y:\", values(y))" ] }, { "cell_type": "markdown", "id": "6efaa815", "metadata": {}, "source": [ "This is better. We have now two cycles (assuming that the ouptut of the solver is $[2,3,0,4,5,1]$ for x): (0,2,0) and (1,3,4,5,1). \n", "However, they have the same index, and the second one is too long." ] }, { "cell_type": "markdown", "id": "5d1c7ae7", "metadata": {}, "source": [ "This is the right time to limit the length of cycles with a constraint *BinPacking* (we indicate that the size of each arc is 1 in any bin/cycle):" ] }, { "cell_type": "code", "execution_count": 10, "id": "ba3f7f4b", "metadata": {}, "outputs": [], "source": [ "satisfy(\n", " # each cycle contains at most k arcs\n", " BinPacking(y, sizes=1) <= k\n", ");" ] }, { "cell_type": "markdown", "id": "3058e782", "metadata": {}, "source": [ "Let us run again the solver:" ] }, { "cell_type": "code", "execution_count": 11, "id": "50c368d5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x: [2, 3, 4, 5, 0, 1]\n", "y: [1, 0, 1, 0, 1, 0]\n" ] } ], "source": [ "if solve() is SAT:\n", " print(\"x:\", values(x))\n", " print(\"y:\", values(y))" ] }, { "cell_type": "markdown", "id": "67ece36c", "metadata": {}, "source": [ "This is far better. We have two cycles (assuming that the ouptut of the solver is $[2,3,4,5,0,1]$ for x and $[1,0,1,0,1,0]$ for y)): (0,2,4,0) and (1,3,5,1), both of size 3." ] }, { "cell_type": "markdown", "id": "4c23708b", "metadata": {}, "source": [ "One can note that numbers associated with the cycles are intercheangeable. We don't care if the first encountered one has number 0 and the second one number 1, or the reverse. \n", "This is why (to break some symmetries) we post a constraint *Precedence* on y: " ] }, { "cell_type": "code", "execution_count": 12, "id": "00d878d8", "metadata": {}, "outputs": [], "source": [ "satisfy(\n", " # tag(symmetry-breaking)\n", " Precedence(y)\n", ");" ] }, { "cell_type": "markdown", "id": "c5821a6b", "metadata": {}, "source": [ "Let us run again the solver:" ] }, { "cell_type": "code", "execution_count": 13, "id": "30716f36", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x: [2, 3, 4, 5, 0, 1]\n", "y: [0, 1, 0, 1, 0, 1]\n" ] } ], "source": [ "if solve() is SAT:\n", " print(\"x:\", values(x))\n", " print(\"y:\", values(y))" ] }, { "cell_type": "markdown", "id": "bd6fdee2", "metadata": {}, "source": [ "One can observe that numbers used for cycles are now ordered (their first occurrences are ordered). " ] }, { "cell_type": "markdown", "id": "1d275b12", "metadata": {}, "source": [ "The last thing to do is to post the objective: " ] }, { "cell_type": "code", "execution_count": 14, "id": "a317b7da", "metadata": {}, "outputs": [], "source": [ "maximize(\n", " # maximizing the sum of arc weights of selected cycles\n", " Sum(weights[i][x[i]] for i in range(n))\n", ");" ] }, { "cell_type": "markdown", "id": "ef9b0e0b", "metadata": {}, "source": [ "We can run the solver, with this optimization task. Note that we need to check that the status returned by the solver is now OPTIMUM. " ] }, { "cell_type": "code", "execution_count": 15, "id": "907eb50a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x: [2, 3, 4, 5, 0, 1]\n", "y: [0, 1, 0, 1, 0, 1]\n", "objective value: 38\n" ] } ], "source": [ "if solve() is OPTIMUM:\n", " print(\"x: \", values(x))\n", " print(\"y: \", values(y))\n", " print(f\"objective value: {bound()}\")" ] }, { "cell_type": "markdown", "id": "b5ed7e6b", "metadata": {}, "source": [ "It seems that the solution we got earlier was already the best one." ] }, { "cell_type": "markdown", "id": "b64ee584", "metadata": {}, "source": [ "Finally, we give below the model in one piece. Here the data is expected to be given by the user (in a command line)." ] }, { "cell_type": "raw", "id": "30dc8f40", "metadata": { "raw_mimetype": "text/x-python" }, "source": [ "from pycsp3 import *\n", "\n", "weights, k = data\n", "n = len(weights)\n", "\n", "# x[i] is the successor node of node i (in the cycle where i belongs)\n", "x = VarArray(size=n, dom=range(n))\n", "\n", "# y[i] is the cycle (index) where the node i belongs\n", "y = VarArray(size=n, dom=range(n))\n", "\n", "satisfy(\n", " AllDifferent(x),\n", "\n", " # ensuring correct cycles\n", " [y[i] == y[x[i]] for i in range(n)],\n", "\n", " # disabling infeasible arcs\n", " [x[i] != j for i in range(n) for j in range(n) if i == j or weights[i][j] < 0],\n", "\n", " # each cycle has k as maximum length\n", " BinPacking(y, sizes=1) <= k,\n", "\n", " # tag(symmetry-breaking)\n", " Precedence(y)\n", ")\n", "\n", "maximize(\n", " # maximizing the sum of arc weights of selected cycles\n", " Sum(weights[i][x[i]] for i in range(n))\n", ")" ] } ], "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.7.12" } }, "nbformat": 4, "nbformat_minor": 5 }