{"id":31752074,"url":"https://github.com/filippoairaldi/casadi-nlp","last_synced_at":"2025-10-09T16:55:40.356Z","repository":{"id":63773961,"uuid":"561039493","full_name":"FilippoAiraldi/casadi-nlp","owner":"FilippoAiraldi","description":"Nonlinear Progamming with CasADi","archived":false,"fork":false,"pushed_at":"2025-09-22T17:35:17.000Z","size":3666,"stargazers_count":23,"open_issues_count":1,"forks_count":2,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-09-30T10:50:04.024Z","etag":null,"topics":["casadi","nonlinear-optimization","sensitivity-analysis"],"latest_commit_sha":null,"homepage":"https://casadi-nlp.readthedocs.io/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/FilippoAiraldi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2022-11-02T20:10:28.000Z","updated_at":"2025-09-22T10:05:22.000Z","dependencies_parsed_at":"2025-04-28T10:38:52.220Z","dependency_job_id":"ab349511-e294-4ef4-bdd7-f6b13ca29315","html_url":"https://github.com/FilippoAiraldi/casadi-nlp","commit_stats":{"total_commits":385,"total_committers":3,"mean_commits":"128.33333333333334","dds":"0.49090909090909096","last_synced_commit":"7724de4a4bd218d4fe2cc2bd8b1726be114cb9ab"},"previous_names":[],"tags_count":75,"template":false,"template_full_name":null,"purl":"pkg:github/FilippoAiraldi/casadi-nlp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FilippoAiraldi%2Fcasadi-nlp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FilippoAiraldi%2Fcasadi-nlp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FilippoAiraldi%2Fcasadi-nlp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FilippoAiraldi%2Fcasadi-nlp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FilippoAiraldi","download_url":"https://codeload.github.com/FilippoAiraldi/casadi-nlp/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FilippoAiraldi%2Fcasadi-nlp/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279001778,"owners_count":26083173,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-09T02:00:07.460Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["casadi","nonlinear-optimization","sensitivity-analysis"],"created_at":"2025-10-09T16:55:38.308Z","updated_at":"2025-10-09T16:55:40.342Z","avatar_url":"https://github.com/FilippoAiraldi.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# NonLinear Programming with CasADi\n\n**C**a**s**ADi-**NLP** (**csnlp**, for short) is a library that provides classes\nand utilities to model, solve and analyse nonlinear (but not only) programmes (NLPs) for\noptimization purposes.\n\n\u003e |   |   |\n\u003e |---|---|\n\u003e | **Documentation** | \u003chttps://casadi-nlp.readthedocs.io/en/stable/\u003e         |\n\u003e | **Download**      | \u003chttps://pypi.python.org/pypi/csnlp/\u003e                  |\n\u003e | **Source code**   | \u003chttps://github.com/FilippoAiraldi/casadi-nlp/\u003e        |\n\u003e | **Report issues** | \u003chttps://github.com/FilippoAiraldi/casadi-nlp/issues/\u003e |\n\n[![PyPI version](https://badge.fury.io/py/csnlp.svg)](https://badge.fury.io/py/csnlp)\n[![Source Code License](https://img.shields.io/badge/license-MIT-blueviolet)](https://github.com/FilippoAiraldi/casadi-nlp/blob/main/LICENSE)\n![Python 3.9](https://img.shields.io/badge/python-\u003e=3.9-green.svg)\n\n[![Tests](https://github.com/FilippoAiraldi/casadi-nlp/actions/workflows/tests.yml/badge.svg)](https://github.com/FilippoAiraldi/casadi-nlp/actions/workflows/tests.yml)\n[![Docs](https://readthedocs.org/projects/casadi-nlp/badge/?version=stable)](https://casadi-nlp.readthedocs.io/en/stable/?badge=stable)\n[![Downloads](https://static.pepy.tech/badge/csnlp)](https://www.pepy.tech/projects/csnlp)\n[![Maintainability](https://qlty.sh/gh/FilippoAiraldi/projects/casadi-nlp/maintainability.svg)](https://qlty.sh/gh/FilippoAiraldi/projects/casadi-nlp)\n[![Code Coverage](https://qlty.sh/gh/FilippoAiraldi/projects/casadi-nlp/coverage.svg)](https://qlty.sh/gh/FilippoAiraldi/projects/casadi-nlp)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n\n---\n\n## Features\n\n**csnlp** builds on top of the [CasADi](https://web.casadi.org/)\nframework [[1]](#1) to model the optimization problems and perform symbolic\ndifferentiation, and heavily relies on the [IPOPT](https://github.com/coin-or/Ipopt)\nsolver [[2]](#2) (though the package allows the user to seamlessly switch to other\nsolvers supported by CasADi). While it is similar in functionality (and was inspired by)\nthe CasADi's\n[Opti Stack](https://web.casadi.org/api/html/dd/dc6/classcasadi_1_1Opti.html) (see\n[this blog post](https://web.casadi.org/blog/opti/) for example), it is more tailored to\nresearch as\n\n1. it is more flexible, since it is written in Python and allows the user to easily\n   access all the constituents of the optimization problem (e.g. the objective function,\n   constraints, dual variables, bounds, etc.)\n\n2. it is more modular, since it allows the base `csnlp.Nlp` class to be wrapped with\n   additional functionality (e.g. sensitivity, Model Predictive Control, etc.), and it\n   provides parallel implementations in case of multistarting in the `csnlp.multistart`\n   module.\n\nThe package offers also tools for the sensitivity analysis of NLPs, solving them with\nmultiple initial conditions, as well as for building MPC controllers. The library is not\nmeant to be a faster alternative to `casadi.Opti`, but rather a more flexible and\nmodular one for research purposes.\n\n---\n\n## Installation\n\n### Using `pip`\n\nYou can use `pip` to install **csnlp** with the command\n\n```bash\npip install csnlp\n```\n\n**csnlp** has the following dependencies\n\n- Python 3.9 or higher\n- [NumPy](https://pypi.org/project/numpy/)\n- [CasADi](https://pypi.org/project/casadi/)\n- [Joblib](https://joblib.readthedocs.io/)\n\n### Using source code\n\nIf you'd like to play around with the source code instead, run\n\n```bash\ngit clone https://github.com/FilippoAiraldi/casadi-nlp.git\n```\n\nThe `main` branch contains the main releases of the packages (and the occasional post\nrelease). The `experimental` branch is reserved for the implementation and test of new\nfeatures and hosts the release candidates. You can then install the package to edit it\nas you wish as\n\n```bash\npip install -e /path/to/casadi-nlp\n```\n\n---\n\n## Getting started\n\nHere we provide a compact example on how **csnlp** can be employed to build and solve\nan optimization problem. Similar to\n[Opti](https://web.casadi.org/api/html/dd/dc6/classcasadi_1_1Opti.html), we instantiate\na class which represents the NLP and allows us to create its variables and parameters\nand model its constraints and objective. For example, suppose we'd like to solve the\nproblem\n\n$$\n\\min_{x,y}{ (1 - x)^2 + 0.2(y - x^2)^2 \\text{ s.t. } (p/2)^2 \\le (x + 0.5)^2 + y^2 \\le p^2 }\n$$\n\nWe can do so with the following code:\n\n```python\nfrom csnlp import Nlp\n\nnlp = Nlp()\nx = nlp.variable(\"x\")[0]  # create primal variable x\ny = nlp.variable(\"y\")[0]  # create primal variable y\np = nlp.parameter(\"p\")  # create parameter p\n\n# define the objective and constraints\nnlp.minimize((1 - x) ** 2 + 0.2 * (y - x**2) ** 2)\ng = (x + 0.5) ** 2 + y**2\nnlp.constraint(\"c1\", (p / 2) ** 2, \"\u003c=\", g)\nnlp.constraint(\"c2\", g, \"\u003c=\", p**2)\n\nnlp.init_solver()  # initializes IPOPT under the hood\nsol = nlp.solve(pars={\"p\": 1.25})  # solves the NLP for parameter p=1.25\n\nx_opt = sol.vals[\"x\"]   # optimal values can be retrieved via the dict .vals\ny_opt = sol.value(y)  # or the .value method\n```\n\nHowever, the package also allows to seamlessly enhance the standard `csnlp.Nlp` with\ndifferent capabilities. For instance, when the problem is highly nonlinear and\nnecessitates to be solved with multiple initial conditions, the `csnlp.multistart`\nmodule offers various solutions to parallelize the computations (see, e.g.,\n`csnlp.multistart.ParallelMultistartNlp`). The `csnlp.wrappers` module offers instead a\nset of wrappers that can be used to augment the NLP with additional capabilities without\nmodifying the original NLP instance: as of now, wrappers have been implemented for\n\nThe package also allows to enhance the NLP with different capabilities with, e.g.,\nmultistart (see `csnlp.MultistartNlp`) or by wrapping it. As of now, wrappers have been\nimplemented for\n\n- sensitivity analysis (see `csnlp.wrappers.NlpSensitivity` [[3]](#3))\n- Model Predictive Control (see `csnlp.wrappers.Mpc` [[4]](#4) and\n  `csnlp.wrappers.ScenarioBasedMpc` [[5]](#5))\n- NLP scaling (see `csnlp.wrappers.NlpScaling` and `csnlp.core.scaling`).\n\nFor example, if we'd like to compute the sensitivity $\\frac{\\partial y}{\\partial p}$ of\nthe optimal primal variable $y$ with respect to the parameter $p$, we just need to wrap\nthe `csnlp.Nlp` instance with the `csnlp.wrappers.NlpSensitivity` wrapper, which is\nspecialized in differentiating the optimization problem. This in turn allows us to\ncompute the first-order $\\frac{\\partial y}{\\partial p}$ and second sensitivities\n$\\frac{\\partial^2 y}{\\partial p^2}$ (`dydp` and `d2ydp2`, respectively) as such:\n\n```python\nfrom csnlp import wrappers\n\nnlp = wrappers.NlpSensitivity(nlp)\ndydp, d2ydp2 = nlp.parametric_sensitivity()\n```\n\nIn other words, these sensitivities provide the jacobian and hessian\nthat locally approximate the solution w.r.t. the parameter $p$. As\nshown in the corresponding example but not in this quick demonstation, the sensitivity\ncan be also computed for any generic expression $z(x(p),\\lambda(p),p)$ that is a\nfunction of the primal $x$ and dual $\\lambda$ variables, and the parameters\n$p$. Moreover, the sensitivity computations can be carried out symbolically (more\ndemanding) or numerically (more stable and reliable).\n\nSimilarly, a `csnlp.Nlp` instance can be wrapped in a `csnlp.wrappers.Mpc` wrapper\nthat makes it easier to build such finite-horizon optimal controllers for model-based\ncontrol applications.\n\n---\n\n## Examples\n\nOur [examples](https://github.com/FilippoAiraldi/casadi-nlp/tree/main/examples)\nsubdirectory contains example applications of this package in NLP optimization,\nsensitivity analysis, scaling of NLPs, and optimal control.\n\n---\n\n## License\n\nThe repository is provided under the MIT License. See the LICENSE file included with\nthis repository.\n\n---\n\n## Author\n\n[Filippo Airaldi](https://www.tudelft.nl/staff/f.airaldi/), PhD Candidate\n[f.airaldi@tudelft.nl | filippoairaldi@gmail.com]\n\n\u003e [Delft Center for Systems and Control](https://www.tudelft.nl/en/me/about/departments/delft-center-for-systems-and-control/)\nin [Delft University of Technology](https://www.tudelft.nl/en/)\n\nCopyright (c) 2024 Filippo Airaldi.\n\nCopyright notice: Technische Universiteit Delft hereby disclaims all copyright interest\nin the program “csnlp” (Nonlinear Progamming with CasADi) written by the Author(s).\nProf. Dr. Ir. Fred van Keulen, Dean of ME.\n\n---\n\n## References\n\n\u003ca id=\"1\"\u003e[1]\u003c/a\u003e\nAndersson, J.A.E., Gillis, J., Horn, G., Rawlings, J.B., and Diehl, M. (2019).\n[CasADi: a software framework for nonlinear optimization and optimal control](https://link.springer.com/article/10.1007/s12532-018-0139-4).\nMathematical Programming Computation, 11(1), 1–36.\n\n\u003ca id=\"2\"\u003e[2]\u003c/a\u003e\nWachter, A. and Biegler, L.T. (2006).\n[On the implementation of an interior-point filter line-search algorithm for large-scale nonlinear programming](https://link.springer.com/article/10.1007/s10107-004-0559-y).\nMathematical Programming, 106(1), 25–57.\n\n\u003ca id=\"3\"\u003e[3]\u003c/a\u003e\nBüskens, C. and Maurer, H. (2001).\n[Sensitivity analysis and real-time optimization of parametric nonlinear programming problems](https://link.springer.com/chapter/10.1007/978-3-662-04331-8_1).\nIn M. Grötschel, S.O. Krumke, and J. Rambau (eds.), Online Optimization of Large Scale Systems, 3–16. Springer, Berlin, Heidelberg\n\n\u003ca id=\"4\"\u003e[4]\u003c/a\u003e\nRawlings, J.B., Mayne, D.Q. and Diehl, M., 2017.\n[Model Predictive Control: theory, computation, and design (Vol. 2)](https://sites.engineering.ucsb.edu/~jbraw/mpc/).\nMadison, WI: Nob Hill Publishing.\n\n\u003ca id=\"5\"\u003e[5]\u003c/a\u003e\nSchildbach, G., Fagiano, L., Frei, C. and Morari, M., 2014.\n[The Scenario Approach for stochastic Model Predictive Control with bounds on closed-loop constraint violations](https://www.sciencedirect.com/science/article/pii/S0005109814004166).\nAutomatica, 50(12), pp.3009-3018.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffilippoairaldi%2Fcasadi-nlp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffilippoairaldi%2Fcasadi-nlp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffilippoairaldi%2Fcasadi-nlp/lists"}