{
  "__type": "IngestedDoc",
  "__tag": 4010,
  "_content": {
    "Notes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The default method "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "'faq' <optimize.qap-faq>",
              "domain": null,
              "role": "ref",
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " uses the Fast Approximate QAP algorithm "
            },
            {
              "__type": "FootnoteReference",
              "__tag": 4066,
              "label": "1"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "; it typically offers the best combination of speed and accuracy. Method "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "'2opt' <optimize.qap-2opt>",
              "domain": null,
              "role": "ref",
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " can be computationally expensive, but may be a useful alternative, or it can be used to refine the solution returned by another method."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Raises": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Yields": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Methods": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Returns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Parameters",
          "__tag": 4026,
          "children": [
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "res",
              "annotation": "OptimizeResult",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "CrossRef",
                      "__tag": 4002,
                      "value": "OptimizeResult",
                      "reference": {
                        "__type": "LocalRef",
                        "__tag": 4022,
                        "kind": "module",
                        "path": "scipy.optimize._optimize:OptimizeResult"
                      },
                      "kind": "module"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " containing the following fields."
                    }
                  ]
                },
                {
                  "__type": "DefList",
                  "__tag": 4033,
                  "children": [
                    {
                      "__type": "DefListItem",
                      "__tag": 4037,
                      "dt": {
                        "__type": "Paragraph",
                        "__tag": 4045,
                        "children": [
                          {
                            "__type": "Text",
                            "__tag": 4046,
                            "value": "col_ind"
                          }
                        ]
                      },
                      "dd": [
                        {
                          "__type": "Paragraph",
                          "__tag": 4045,
                          "children": [
                            {
                              "__type": "Text",
                              "__tag": 4046,
                              "value": "col_ind"
                            }
                          ]
                        }
                      ]
                    },
                    {
                      "__type": "DefListItem",
                      "__tag": 4037,
                      "dt": {
                        "__type": "Paragraph",
                        "__tag": 4045,
                        "children": [
                          {
                            "__type": "Text",
                            "__tag": 4046,
                            "value": "fun"
                          }
                        ]
                      },
                      "dd": [
                        {
                          "__type": "Paragraph",
                          "__tag": 4045,
                          "children": [
                            {
                              "__type": "Text",
                              "__tag": 4046,
                              "value": "fun"
                            }
                          ]
                        }
                      ]
                    },
                    {
                      "__type": "DefListItem",
                      "__tag": 4037,
                      "dt": {
                        "__type": "Paragraph",
                        "__tag": 4045,
                        "children": [
                          {
                            "__type": "Text",
                            "__tag": 4046,
                            "value": "nit"
                          }
                        ]
                      },
                      "dd": [
                        {
                          "__type": "Paragraph",
                          "__tag": 4045,
                          "children": [
                            {
                              "__type": "Text",
                              "__tag": 4046,
                              "value": "nit"
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Approximates solution to the quadratic assignment problem and the graph matching problem."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Receives": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warnings": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Attributes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Parameters",
          "__tag": 4026,
          "children": [
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "A",
              "annotation": "2-D array, square",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The square matrix "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "A"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " in the objective function above."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "B",
              "annotation": "2-D array, square",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The square matrix "
                    },
                    {
                      "__type": "InlineMath",
                      "__tag": 4057,
                      "value": "B"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " in the objective function above."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "method",
              "annotation": "str in {'faq', '2opt'} (default: 'faq')",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The algorithm used to solve the problem. "
                    },
                    {
                      "__type": "InlineRole",
                      "__tag": 4003,
                      "value": "'faq' <optimize.qap-faq>",
                      "domain": null,
                      "role": "ref",
                      "inventory": null
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " (default) and "
                    },
                    {
                      "__type": "InlineRole",
                      "__tag": 4003,
                      "value": "'2opt' <optimize.qap-2opt>",
                      "domain": null,
                      "role": "ref",
                      "inventory": null
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " are available."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "options",
              "annotation": "dict, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "A dictionary of solver options. All solvers support the following:"
                    }
                  ]
                },
                {
                  "__type": "DefList",
                  "__tag": 4033,
                  "children": [
                    {
                      "__type": "DefListItem",
                      "__tag": 4037,
                      "dt": {
                        "__type": "Paragraph",
                        "__tag": 4045,
                        "children": [
                          {
                            "__type": "Text",
                            "__tag": 4046,
                            "value": "maximize"
                          }
                        ]
                      },
                      "dd": [
                        {
                          "__type": "Paragraph",
                          "__tag": 4045,
                          "children": [
                            {
                              "__type": "Text",
                              "__tag": 4046,
                              "value": "maximize"
                            }
                          ]
                        }
                      ]
                    },
                    {
                      "__type": "DefListItem",
                      "__tag": 4037,
                      "dt": {
                        "__type": "Paragraph",
                        "__tag": 4045,
                        "children": [
                          {
                            "__type": "Text",
                            "__tag": 4046,
                            "value": "partial_match"
                          }
                        ]
                      },
                      "dd": [
                        {
                          "__type": "Paragraph",
                          "__tag": 4045,
                          "children": [
                            {
                              "__type": "Text",
                              "__tag": 4046,
                              "value": "partial_match"
                            }
                          ]
                        }
                      ]
                    },
                    {
                      "__type": "DefListItem",
                      "__tag": 4037,
                      "dt": {
                        "__type": "Paragraph",
                        "__tag": 4045,
                        "children": [
                          {
                            "__type": "Text",
                            "__tag": 4046,
                            "value": "rng"
                          }
                        ]
                      },
                      "dd": [
                        {
                          "__type": "Paragraph",
                          "__tag": 4045,
                          "children": [
                            {
                              "__type": "Text",
                              "__tag": 4046,
                              "value": "rng"
                            }
                          ]
                        }
                      ]
                    }
                  ]
                },
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "For method-specific options, see "
                    },
                    {
                      "__type": "CrossRef",
                      "__tag": 4002,
                      "value": "show_options('quadratic_assignment')",
                      "reference": {
                        "__type": "LocalRef",
                        "__tag": 4022,
                        "kind": "module",
                        "path": "scipy.optimize._optimize:show_options"
                      },
                      "kind": "module"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "."
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Extended Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Quadratic assignment solves problems of the following form:"
            }
          ]
        },
        {
          "__type": "Math",
          "__tag": 4058,
          "value": "\\min_P & \\ {\\ \\text{trace}(A^T P B P^T)}\\\\\n\\mbox{s.t. } & {P \\ \\epsilon \\ \\mathcal{P}}\\\\"
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "where "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "\\mathcal{P}"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is the set of all permutation matrices, and "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "A"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and "
            },
            {
              "__type": "InlineMath",
              "__tag": 4057,
              "value": "B"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " are square matrices."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Graph matching tries to "
            },
            {
              "__type": "Emphasis",
              "__tag": 4047,
              "children": [
                {
                  "__type": "Text",
                  "__tag": 4046,
                  "value": "maximize"
                }
              ]
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " the same objective function. This algorithm can be thought of as finding the alignment of the nodes of two graphs that minimizes the number of induced edge disagreements, or, in the case of weighted graphs, the sum of squared edge weight differences."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Note that the quadratic assignment problem is NP-hard. The results given here are approximations and are not guaranteed to be optimal."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Other Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    }
  },
  "_ordered_sections": [
    "Summary",
    "Extended Summary",
    "Parameters",
    "Attributes",
    "Methods",
    "Returns",
    "Yields",
    "Receives",
    "Other Parameters",
    "Raises",
    "Warns",
    "Warnings",
    "Notes"
  ],
  "item_file": "/scipy/optimize/_qap.py",
  "item_line": 14,
  "item_type": "function",
  "aliases": [
    "scipy.optimize.quadratic_assignment"
  ],
  "example_section_data": {
    "__type": "Section",
    "__tag": 4015,
    "children": [
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "import numpy as np\nfrom scipy.optimize import quadratic_assignment\nrng = np.random.default_rng()\nA = np.array([[0, 80, 150, 170], [80, 0, 130, 100],\n              [150, 130, 0, 120], [170, 100, 120, 0]])\nB = np.array([[0, 5, 2, 7], [0, 0, 3, 8],\n              [0, 0, 0, 3], [0, 0, 0, 0]])\nres = quadratic_assignment(A, B, options={'rng': rng})\nprint(res)\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nThe see the relationship between the returned ``col_ind`` and ``fun``,\nuse ``col_ind`` to form the best permutation matrix found, then evaluate\nthe objective function :math:`f(P) = trace(A^T P B P^T )`.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "perm = res['col_ind']\nP = np.eye(len(A), dtype=int)[perm]\nfun = np.trace(A.T @ P @ B @ P.T)\nprint(fun)\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nAlternatively, to avoid constructing the permutation matrix explicitly,\ndirectly permute the rows and columns of the distance matrix.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "fun = np.trace(A.T @ B[perm][:, perm])\nprint(fun)\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nAlthough not guaranteed in general, ``quadratic_assignment`` happens to\nhave found the globally optimal solution.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "from itertools import permutations\nperm_opt, fun_opt = None, np.inf\nfor perm in permutations([0, 1, 2, 3]):\n    perm = np.array(perm)\n    fun = np.trace(A.T @ B[perm][:, perm])\n    if fun < fun_opt:\n        fun_opt, perm_opt = fun, perm\nprint(np.array_equal(perm_opt, res['col_ind']))\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nHere is an example for which the default method,\n:ref:`'faq' <optimize.qap-faq>`, does not find the global optimum.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "A = np.array([[0, 5, 8, 6], [5, 0, 5, 1],\n              [8, 5, 0, 2], [6, 1, 2, 0]])\nB = np.array([[0, 1, 8, 4], [1, 0, 5, 2],\n              [8, 5, 0, 5], [4, 2, 5, 0]])\nres = quadratic_assignment(A, B, options={'rng': rng})\nprint(res)\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nIf accuracy is important, consider using  :ref:`'2opt' <optimize.qap-2opt>`\nto refine the solution.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "guess = np.array([np.arange(len(A)), res.col_ind]).T\nres = quadratic_assignment(A, B, method=\"2opt\",\n    options = {'rng': rng, 'partial_guess': guess})\nprint(res)\n",
        "execution_status": "success"
      }
    ],
    "title": [],
    "level": 0,
    "target": null
  },
  "see_also": [],
  "signature": {
    "__type": "SignatureNode",
    "__tag": 4029,
    "kind": "function",
    "parameters": [
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "A",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": {
          "__type": "Empty",
          "__tag": 4031
        }
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "B",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": {
          "__type": "Empty",
          "__tag": 4031
        }
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "method",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "faq"
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "options",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "None"
      }
    ],
    "return_annotation": {
      "__type": "Empty",
      "__tag": 4031
    },
    "target_name": "quadratic_assignment"
  },
  "references": [
    ".. [1] J.T. Vogelstein, J.M. Conroy, V. Lyzinski, L.J. Podrazik,",
    "       S.G. Kratzer, E.T. Harley, D.E. Fishkind, R.J. Vogelstein, and",
    "       C.E. Priebe, \"Fast approximate quadratic programming for graph",
    "       matching,\" PLOS one, vol. 10, no. 4, p. e0121002, 2015,",
    "       :doi:`10.1371/journal.pone.0121002`",
    "",
    ".. [2] D. Fishkind, S. Adali, H. Patsolic, L. Meng, D. Singh, V. Lyzinski,",
    "       C. Priebe, \"Seeded graph matching\", Pattern Recognit. 87 (2019):",
    "       203-215, :doi:`10.1016/j.patcog.2018.09.014`",
    "",
    ".. [3] \"2-opt,\" Wikipedia.",
    "       https://en.wikipedia.org/wiki/2-opt"
  ],
  "qa": "scipy.optimize._qap:quadratic_assignment",
  "arbitrary": [],
  "local_refs": [
    "A",
    "B",
    "method",
    "options",
    "res"
  ]
}