csnlp


Namecsnlp JSON
Version 1.5.10 PyPI version JSON
download
home_pageNone
SummaryNonlinear Progamming with CasADi
upload_time2024-04-11 08:49:47
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseMIT
keywords nonlinear-optimization casadi sensitivity-analysis
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Nonlinear Programming with CasADi

**csnlp** provides classes and utilities to model, solve and analyse nonlinear programmes (NLPs) in optimization.

In particular, it makes use of the [CasADi](https://web.casadi.org/) framework [[1]](#1) to model the optimization problems and perform symbolic differentiation, as well as the [IPOPT](https://github.com/coin-or/Ipopt) solver [[2]](#2) (though the package can be adapted to other solvers pretty easily). The package offers also tools for the sensitivity analysis of NLPs, solving them with multiple initial conditions, as well as for building MPC controllers.

[![PyPI version](https://badge.fury.io/py/csnlp.svg)](https://badge.fury.io/py/csnlp)
[![Source Code License](https://img.shields.io/badge/license-MIT-blueviolet)](https://github.com/FilippoAiraldi/casadi-nlp/blob/dev/LICENSE)
![Python 3.9](https://img.shields.io/badge/python->=3.9-green.svg)

[![Tests](https://github.com/FilippoAiraldi/casadi-nlp/actions/workflows/test-dev.yml/badge.svg)](https://github.com/FilippoAiraldi/casadi-nlp/actions/workflows/test-dev.yml)
[![Downloads](https://static.pepy.tech/badge/csnlp)](https://www.pepy.tech/projects/csnlp)
[![Maintainability](https://api.codeclimate.com/v1/badges/d1cf537cff6af1a08508/maintainability)](https://codeclimate.com/github/FilippoAiraldi/casadi-nlp/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/d1cf537cff6af1a08508/test_coverage)](https://codeclimate.com/github/FilippoAiraldi/casadi-nlp/test_coverage)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

---

## Installation

To install the package, run

```bash
pip install csnlp
```

**csnlp** has the following dependencies

- Python 3.9 or higher
- [NumPy](https://pypi.org/project/numpy/)
- [CasADi](https://pypi.org/project/casadi/)
- [Joblib](https://joblib.readthedocs.io/en/latest/)

For playing around with the source code instead, run

```bash
git clone https://github.com/FilippoAiraldi/casadi-nlp.git
```

---

## Usage

Similar to CasADi Opti, we instantiate a class that represents the NLP and allows to model its variables, parameters, constraints and objective. A simple example is below:

```python
from csnlp import Nlp

nlp = Nlp()
x = nlp.variable("x")[0]
y = nlp.variable("y")[0]
p = nlp.parameter("p")

nlp.minimize((1 - x) ** 2 + 0.2 * (y - x**2) ** 2)

g = (x + 0.5) ** 2 + y**2
nlp.constraint("c1", (p / 2) ** 2, "<=", g)
nlp.constraint("c2", g, "<=", p**2)

nlp.init_solver()  # initializes IPOPT under the hood
sol = nlp.solve(pars={"p": 1.25})  # solves the NLP for parameter p=1.25

x_opt = sol.vals["x"]
y_opt = sol.value(y)
```

The package also allows to enhance the NLP with different capabilities with, e.g., multistart (see `csnlp.MultistartNlp`) or by wrapping it. As of now, wrappers have been implemented for

- sensitivity analysis (see `csnlp.wrappers.NlpSensitivity`; based on [[3]](#3))
- Model Predictive Control (see `csnlp.wrappers.Mpc`; [[4]](#4) for details)
- NLP scaling (see `csnlp.wrappers.NlpScaling`).

For instance, to compute the sensitivity of the optimal primal-dual variables `y` with respect to the parameters `p` of the NLP , first we need to augment the capabilities of the NLP with a wrapper specialized in differentiating the optimization problem, and then compute the first-order and second sensitivities (`dydp` and `d2ydp2`, respectively) as such:

```python
from csnlp import wrappers

nlp = wrappers.NlpSensitivity(nlp)
dydp, d2ydp2 = nlp.parametric_sensitivity()
```

In other words, these sensitivities provide the jacobian and hessian that locally approximate the solution w.r.t. the parameters `p`. As shown in the examples, the sensitivity can be also computed for any generic expression `z` that is a function of the primal-dual variables and the parameters, i.e., `z(x(p),lam(p),p)`, and computations can be carried out symbolically or numerically (more stable).

Similarly, an NLP instance can be wrapped in an MPC wrapper that makes it easier to build such controller.

## Examples

Our [examples](https://github.com/FilippoAiraldi/casadi-nlp/tree/dev/examples) subdirectory contains example applications of this package in NLP optimization, sensitivity analysis, scaling of NLPs, and optimal control.

---

## License

The repository is provided under the MIT License. See the LICENSE file included with this repository.

---

## Author

[Filippo Airaldi](https://www.tudelft.nl/staff/f.airaldi/), PhD Candidate [f.airaldi@tudelft.nl | filippoairaldi@gmail.com]

> [Delft Center for Systems and Control](https://www.tudelft.nl/en/3me/about/departments/delft-center-for-systems-and-control/) in [Delft University of Technology](https://www.tudelft.nl/en/)

Copyright (c) 2023 Filippo Airaldi.

Copyright notice: Technische Universiteit Delft hereby disclaims all copyright interest in the program “csnlp” (Nonlinear Progamming with CasADi) written by the Author(s). Prof. Dr. Ir. Fred van Keulen, Dean of 3mE.

---

## References

<a id="1">[1]</a>
Andersson, J.A.E., Gillis, J., Horn, G., Rawlings, J.B., and Diehl, M. (2019). [CasADi: a software framework for nonlinear optimization and optimal control](https://link.springer.com/article/10.1007/s12532-018-0139-4). Mathematical Programming Computation, 11(1), 1–36.

<a id="2">[2]</a>
Wachter, A. and Biegler, L.T. (2006). [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). Mathematical Programming, 106(1), 25–57.

<a id="3">[3]</a>
Büskens, C. and Maurer, H. (2001). [Sensitivity analysis and real-time optimization of parametric nonlinear programming problems](https://link.springer.com/chapter/10.1007/978-3-662-04331-8_1). In M. Grötschel, S.O. Krumke, and J. Rambau (eds.), Online Optimization of Large Scale Systems, 3–16. Springer, Berlin, Heidelberg

<a id="4">[4]</a>
Rawlings, J.B., Mayne, D.Q. and Diehl, M., 2017. [Model Predictive Control: theory, computation, and design (Vol. 2)](https://sites.engineering.ucsb.edu/~jbraw/mpc/). Madison, WI: Nob Hill Publishing.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "csnlp",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "nonlinear-optimization, casadi, sensitivity-analysis",
    "author": null,
    "author_email": "Filippo Airaldi <filippoairaldi@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/35/51/e8c9be25b55409c7ec7a43e70bf6ad0906d2132c2cb2f7a1bebc41c5c9bc/csnlp-1.5.10.tar.gz",
    "platform": null,
    "description": "# Nonlinear Programming with CasADi\r\n\r\n**csnlp** provides classes and utilities to model, solve and analyse nonlinear programmes (NLPs) in optimization.\r\n\r\nIn particular, it makes use of the [CasADi](https://web.casadi.org/) framework [[1]](#1) to model the optimization problems and perform symbolic differentiation, as well as the [IPOPT](https://github.com/coin-or/Ipopt) solver [[2]](#2) (though the package can be adapted to other solvers pretty easily). The package offers also tools for the sensitivity analysis of NLPs, solving them with multiple initial conditions, as well as for building MPC controllers.\r\n\r\n[![PyPI version](https://badge.fury.io/py/csnlp.svg)](https://badge.fury.io/py/csnlp)\r\n[![Source Code License](https://img.shields.io/badge/license-MIT-blueviolet)](https://github.com/FilippoAiraldi/casadi-nlp/blob/dev/LICENSE)\r\n![Python 3.9](https://img.shields.io/badge/python->=3.9-green.svg)\r\n\r\n[![Tests](https://github.com/FilippoAiraldi/casadi-nlp/actions/workflows/test-dev.yml/badge.svg)](https://github.com/FilippoAiraldi/casadi-nlp/actions/workflows/test-dev.yml)\r\n[![Downloads](https://static.pepy.tech/badge/csnlp)](https://www.pepy.tech/projects/csnlp)\r\n[![Maintainability](https://api.codeclimate.com/v1/badges/d1cf537cff6af1a08508/maintainability)](https://codeclimate.com/github/FilippoAiraldi/casadi-nlp/maintainability)\r\n[![Test Coverage](https://api.codeclimate.com/v1/badges/d1cf537cff6af1a08508/test_coverage)](https://codeclimate.com/github/FilippoAiraldi/casadi-nlp/test_coverage)\r\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\r\n\r\n---\r\n\r\n## Installation\r\n\r\nTo install the package, run\r\n\r\n```bash\r\npip install csnlp\r\n```\r\n\r\n**csnlp** has the following dependencies\r\n\r\n- Python 3.9 or higher\r\n- [NumPy](https://pypi.org/project/numpy/)\r\n- [CasADi](https://pypi.org/project/casadi/)\r\n- [Joblib](https://joblib.readthedocs.io/en/latest/)\r\n\r\nFor playing around with the source code instead, run\r\n\r\n```bash\r\ngit clone https://github.com/FilippoAiraldi/casadi-nlp.git\r\n```\r\n\r\n---\r\n\r\n## Usage\r\n\r\nSimilar to CasADi Opti, we instantiate a class that represents the NLP and allows to model its variables, parameters, constraints and objective. A simple example is below:\r\n\r\n```python\r\nfrom csnlp import Nlp\r\n\r\nnlp = Nlp()\r\nx = nlp.variable(\"x\")[0]\r\ny = nlp.variable(\"y\")[0]\r\np = nlp.parameter(\"p\")\r\n\r\nnlp.minimize((1 - x) ** 2 + 0.2 * (y - x**2) ** 2)\r\n\r\ng = (x + 0.5) ** 2 + y**2\r\nnlp.constraint(\"c1\", (p / 2) ** 2, \"<=\", g)\r\nnlp.constraint(\"c2\", g, \"<=\", p**2)\r\n\r\nnlp.init_solver()  # initializes IPOPT under the hood\r\nsol = nlp.solve(pars={\"p\": 1.25})  # solves the NLP for parameter p=1.25\r\n\r\nx_opt = sol.vals[\"x\"]\r\ny_opt = sol.value(y)\r\n```\r\n\r\nThe package also allows to enhance the NLP with different capabilities with, e.g., multistart (see `csnlp.MultistartNlp`) or by wrapping it. As of now, wrappers have been implemented for\r\n\r\n- sensitivity analysis (see `csnlp.wrappers.NlpSensitivity`; based on [[3]](#3))\r\n- Model Predictive Control (see `csnlp.wrappers.Mpc`; [[4]](#4) for details)\r\n- NLP scaling (see `csnlp.wrappers.NlpScaling`).\r\n\r\nFor instance, to compute the sensitivity of the optimal primal-dual variables `y` with respect to the parameters `p` of the NLP , first we need to augment the capabilities of the NLP with a wrapper specialized in differentiating the optimization problem, and then compute the first-order and second sensitivities (`dydp` and `d2ydp2`, respectively) as such:\r\n\r\n```python\r\nfrom csnlp import wrappers\r\n\r\nnlp = wrappers.NlpSensitivity(nlp)\r\ndydp, d2ydp2 = nlp.parametric_sensitivity()\r\n```\r\n\r\nIn other words, these sensitivities provide the jacobian and hessian that locally approximate the solution w.r.t. the parameters `p`. As shown in the examples, the sensitivity can be also computed for any generic expression `z` that is a function of the primal-dual variables and the parameters, i.e., `z(x(p),lam(p),p)`, and computations can be carried out symbolically or numerically (more stable).\r\n\r\nSimilarly, an NLP instance can be wrapped in an MPC wrapper that makes it easier to build such controller.\r\n\r\n## Examples\r\n\r\nOur [examples](https://github.com/FilippoAiraldi/casadi-nlp/tree/dev/examples) subdirectory contains example applications of this package in NLP optimization, sensitivity analysis, scaling of NLPs, and optimal control.\r\n\r\n---\r\n\r\n## License\r\n\r\nThe repository is provided under the MIT License. See the LICENSE file included with this repository.\r\n\r\n---\r\n\r\n## Author\r\n\r\n[Filippo Airaldi](https://www.tudelft.nl/staff/f.airaldi/), PhD Candidate [f.airaldi@tudelft.nl | filippoairaldi@gmail.com]\r\n\r\n> [Delft Center for Systems and Control](https://www.tudelft.nl/en/3me/about/departments/delft-center-for-systems-and-control/) in [Delft University of Technology](https://www.tudelft.nl/en/)\r\n\r\nCopyright (c) 2023 Filippo Airaldi.\r\n\r\nCopyright notice: Technische Universiteit Delft hereby disclaims all copyright interest in the program \u201ccsnlp\u201d (Nonlinear Progamming with CasADi) written by the Author(s). Prof. Dr. Ir. Fred van Keulen, Dean of 3mE.\r\n\r\n---\r\n\r\n## References\r\n\r\n<a id=\"1\">[1]</a>\r\nAndersson, J.A.E., Gillis, J., Horn, G., Rawlings, J.B., and Diehl, M. (2019). [CasADi: a software framework for nonlinear optimization and optimal control](https://link.springer.com/article/10.1007/s12532-018-0139-4). Mathematical Programming Computation, 11(1), 1\u201336.\r\n\r\n<a id=\"2\">[2]</a>\r\nWachter, A. and Biegler, L.T. (2006). [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). Mathematical Programming, 106(1), 25\u201357.\r\n\r\n<a id=\"3\">[3]</a>\r\nB\u00fcskens, C. and Maurer, H. (2001). [Sensitivity analysis and real-time optimization of parametric nonlinear programming problems](https://link.springer.com/chapter/10.1007/978-3-662-04331-8_1). In M. Gr\u00f6tschel, S.O. Krumke, and J. Rambau (eds.), Online Optimization of Large Scale Systems, 3\u201316. Springer, Berlin, Heidelberg\r\n\r\n<a id=\"4\">[4]</a>\r\nRawlings, J.B., Mayne, D.Q. and Diehl, M., 2017. [Model Predictive Control: theory, computation, and design (Vol. 2)](https://sites.engineering.ucsb.edu/~jbraw/mpc/). Madison, WI: Nob Hill Publishing.\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Nonlinear Progamming with CasADi",
    "version": "1.5.10",
    "project_urls": {
        "Bug Tracker": "https://github.com/FilippoAiraldi/casadi-nlp/issues",
        "Homepage": "https://github.com/FilippoAiraldi/casadi-nlp"
    },
    "split_keywords": [
        "nonlinear-optimization",
        " casadi",
        " sensitivity-analysis"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "dbf2a8de6c92b0bed71bcd641698b5c303a2a3c273d2608a7f69bc63a59276b8",
                "md5": "e8c81b983096b64faa51e49533a7f51f",
                "sha256": "4a5fc0c2f0fae7cc6980d9b2c533a57659768f7cb30df4eaa27f412f4087e910"
            },
            "downloads": -1,
            "filename": "csnlp-1.5.10-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e8c81b983096b64faa51e49533a7f51f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 59482,
            "upload_time": "2024-04-11T08:49:45",
            "upload_time_iso_8601": "2024-04-11T08:49:45.542353Z",
            "url": "https://files.pythonhosted.org/packages/db/f2/a8de6c92b0bed71bcd641698b5c303a2a3c273d2608a7f69bc63a59276b8/csnlp-1.5.10-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3551e8c9be25b55409c7ec7a43e70bf6ad0906d2132c2cb2f7a1bebc41c5c9bc",
                "md5": "0f34cd5c520336bfa4eba7c91c959e14",
                "sha256": "e20ac366f9c00a06999e0c6ffe99bb9d2acfe1851e06fb4f9690c3f484252d4f"
            },
            "downloads": -1,
            "filename": "csnlp-1.5.10.tar.gz",
            "has_sig": false,
            "md5_digest": "0f34cd5c520336bfa4eba7c91c959e14",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 76885,
            "upload_time": "2024-04-11T08:49:47",
            "upload_time_iso_8601": "2024-04-11T08:49:47.709713Z",
            "url": "https://files.pythonhosted.org/packages/35/51/e8c9be25b55409c7ec7a43e70bf6ad0906d2132c2cb2f7a1bebc41c5c9bc/csnlp-1.5.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-11 08:49:47",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "FilippoAiraldi",
    "github_project": "casadi-nlp",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "csnlp"
}
        
Elapsed time: 0.21930s