augmented-lagrangian


Nameaugmented-lagrangian JSON
Version 0.1.1 PyPI version JSON
download
home_pagehttps://github.com/cshjin/augmented-lagrangian
SummaryA Python implementation of the Augmented Lagrangian method for constrained optimization
upload_time2025-10-16 22:29:49
maintainerNone
docs_urlNone
authorHongwei Jin
requires_python>=3.10
licenseMIT
keywords optimization constrained-optimization lagrangian mathematical-optimization
VCS
bugtrack_url
requirements numpy scipy torch pytest pytest-cov autopep8 isort flake8 mypy sphinx sphinx-rtd-theme matplotlib seaborn
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Augmented Lagrangian

[![PyPI](https://img.shields.io/pypi/v/augmented_lagrangian.svg)](https://pypi.org/project/augmented-lagrangian/)
[![Python Version](https://img.shields.io/pypi/pyversions/augmented_lagrangian.svg)](https://pypi.org/project/augmented-lagrangian/)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)


A Python implementation of the Augmented Lagrangian method for constrained optimization problems.

## Overview

The Augmented Lagrangian method is a powerful technique for solving constrained optimization problems of the form:

```
minimize   f(x)
subject to c_i(x) = 0  for i = 1, ..., m
```

This package provides a flexible and easy-to-use implementation that can handle both single and multiple equality constraints.

## Features

- **Multiple backends**: Support for SciPy (BFGS) and PyTorch (SGD) optimization backends
- **Flexible constraint handling**: Support for single or multiple equality constraints
- **Customizable parameters**: Control penalty parameters, tolerances, and iteration limits
- **Convergence monitoring**: Track optimization progress with detailed history
- **Easy-to-use API**: Simple interface for defining objective and constraint functions
- **GPU acceleration**: PyTorch backend supports GPU acceleration when available

## Installation

```bash
# Basic installation (SciPy backend only)
pip install augmented-lagrangian

# With PyTorch backend support
pip install augmented-lagrangian[pytorch]
```

## Quick Start

Here's a simple example of using the Augmented Lagrangian solver:

```python
import numpy as np
from aug_lag import AugmentedLagrangian

# Define objective function: minimize (x1 - 1)^2 + (x2 - 2)^2
def objective(x):
    return (x[0] - 1)**2 + (x[1] - 2)**2

# Define constraint: x1 + x2 - 3 = 0
def constraint(x):
    return x[0] + x[1] - 3

# Create solver instance
solver = AugmentedLagrangian(
    objective_func=objective,
    constraint_funcs=constraint,
    tolerance=1e-6,
    verbose=True
)

# Solve the problem
x0 = np.array([0.0, 0.0])  # Initial guess
result = solver.solve(x0)

print(f"Solution: x = {result['x']}")
print(f"Objective value: {result['fun']}")
print(f"Constraint violation: {result['constraint_violation']}")
```

## API Reference

### AugmentedLagrangian Class

#### Constructor Parameters

- `objective_func`: Function to minimize f(x)
- `constraint_funcs`: Single constraint function or list of constraint functions
- `backend`: Optimization backend - "scipy" (default) or "pytorch"
- `mu_0`: Initial penalty parameter (default: 1.0)
- `tolerance`: Convergence tolerance (default: 1e-6)
- `rho`: Factor to increase penalty parameter (default: 1.5)
- `max_mu`: Maximum penalty parameter value (default: 1000.0)
- `constraint_tolerance`: Tolerance for constraint satisfaction (default: 1e-4)
- `max_outer_iterations`: Maximum outer iterations (default: 20)
- `max_inner_iterations`: Maximum inner iterations per subproblem (default: 50)
- `verbose`: Whether to print optimization progress (default: True)

#### Methods

- `solve(x0, max_outer_iterations=100, tolerance=1e-6)`: Solve the optimization problem
- `set_functions(objective_func, constraint_funcs)`: Set objective and constraint functions

## PyTorch Backend Example

The PyTorch backend uses SGD optimization and supports GPU acceleration:

```python
import numpy as np
from aug_lag import AugmentedLagrangian

# Define objective and constraint functions (same as before)
def objective(x):
    return (x[0] - 1)**2 + (x[1] - 2)**2

def constraint(x):
    return x[0] + x[1] - 3

# Create solver with PyTorch backend
solver = AugmentedLagrangian(
    objective_func=objective,
    constraint_funcs=constraint,
    backend="pytorch",           # Use PyTorch backend
    max_inner_iterations=200,    # More epochs for SGD
    tolerance=1e-6,
    verbose=True
)

# Solve the problem
x0 = np.array([0.0, 0.0])
result = solver.solve(x0)

print(f"Solution: x = {result['x']}")
print(f"Backend used: {solver.backend}")
```

**Backend Comparison:**
- **SciPy backend**: Uses BFGS algorithm, typically faster convergence, CPU-only
- **PyTorch backend**: Uses SGD algorithm, supports GPU acceleration, good for large-scale problems

## Multiple Constraints Example

```python
import numpy as np
from augmented_lagrangian import AugmentedLagrangian

# Objective function
def objective(x):
    return x[0]**2 + x[1]**2

# Multiple constraints
def constraint1(x):
    return x[0] + x[1] - 1

def constraint2(x):
    return x[0] - x[1]

# Create solver with multiple constraints
solver = AugmentedLagrangian(
    objective_func=objective,
    constraint_funcs=[constraint1, constraint2]
)

# Solve
x0 = np.array([0.0, 0.0])
result = solver.solve(x0)
```

## Algorithm Details

The Augmented Lagrangian method combines the objective function with penalty terms for constraint violations:

```
L_A(x, λ, μ) = f(x) - Σ λ_i * c_i(x) + (μ/2) * Σ c_i(x)²
```

Where:
- `f(x)` is the objective function
- `c_i(x)` are the constraint functions
- `λ_i` are the Lagrange multipliers
- `μ` is the penalty parameter

The algorithm iteratively:
1. Minimizes the augmented Lagrangian with respect to x
2. Updates the Lagrange multipliers: λ := λ - μ * c(x)
3. Increases the penalty parameter if needed
4. Repeats until convergence

## Requirements

- Python >= 3.8
- NumPy >= 1.20.0
- SciPy >= 1.7.0

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## Citation

If you use this package in your research, please consider citing:

```bibtex
@software{augmented_lagrangian,
  title={Augmented Lagrangian: A Python Implementation},
  author={Hongwei Jin},
  year={2025},
  url={https://github.com/cshjin/augmented-lagrangian}
}
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/cshjin/augmented-lagrangian",
    "name": "augmented-lagrangian",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": "Hongwei Jin <jinh@anl.gov>",
    "keywords": "optimization, constrained-optimization, lagrangian, mathematical-optimization",
    "author": "Hongwei Jin",
    "author_email": "Hongwei Jin <jinh@anl.gov>",
    "download_url": "https://files.pythonhosted.org/packages/f4/6e/0c4163d82b7cbbc36476bbdd6251494507b9bd773c49693035469419d3e3/augmented_lagrangian-0.1.1.tar.gz",
    "platform": null,
    "description": "# Augmented Lagrangian\n\n[![PyPI](https://img.shields.io/pypi/v/augmented_lagrangian.svg)](https://pypi.org/project/augmented-lagrangian/)\n[![Python Version](https://img.shields.io/pypi/pyversions/augmented_lagrangian.svg)](https://pypi.org/project/augmented-lagrangian/)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)\n\n\nA Python implementation of the Augmented Lagrangian method for constrained optimization problems.\n\n## Overview\n\nThe Augmented Lagrangian method is a powerful technique for solving constrained optimization problems of the form:\n\n```\nminimize   f(x)\nsubject to c_i(x) = 0  for i = 1, ..., m\n```\n\nThis package provides a flexible and easy-to-use implementation that can handle both single and multiple equality constraints.\n\n## Features\n\n- **Multiple backends**: Support for SciPy (BFGS) and PyTorch (SGD) optimization backends\n- **Flexible constraint handling**: Support for single or multiple equality constraints\n- **Customizable parameters**: Control penalty parameters, tolerances, and iteration limits\n- **Convergence monitoring**: Track optimization progress with detailed history\n- **Easy-to-use API**: Simple interface for defining objective and constraint functions\n- **GPU acceleration**: PyTorch backend supports GPU acceleration when available\n\n## Installation\n\n```bash\n# Basic installation (SciPy backend only)\npip install augmented-lagrangian\n\n# With PyTorch backend support\npip install augmented-lagrangian[pytorch]\n```\n\n## Quick Start\n\nHere's a simple example of using the Augmented Lagrangian solver:\n\n```python\nimport numpy as np\nfrom aug_lag import AugmentedLagrangian\n\n# Define objective function: minimize (x1 - 1)^2 + (x2 - 2)^2\ndef objective(x):\n    return (x[0] - 1)**2 + (x[1] - 2)**2\n\n# Define constraint: x1 + x2 - 3 = 0\ndef constraint(x):\n    return x[0] + x[1] - 3\n\n# Create solver instance\nsolver = AugmentedLagrangian(\n    objective_func=objective,\n    constraint_funcs=constraint,\n    tolerance=1e-6,\n    verbose=True\n)\n\n# Solve the problem\nx0 = np.array([0.0, 0.0])  # Initial guess\nresult = solver.solve(x0)\n\nprint(f\"Solution: x = {result['x']}\")\nprint(f\"Objective value: {result['fun']}\")\nprint(f\"Constraint violation: {result['constraint_violation']}\")\n```\n\n## API Reference\n\n### AugmentedLagrangian Class\n\n#### Constructor Parameters\n\n- `objective_func`: Function to minimize f(x)\n- `constraint_funcs`: Single constraint function or list of constraint functions\n- `backend`: Optimization backend - \"scipy\" (default) or \"pytorch\"\n- `mu_0`: Initial penalty parameter (default: 1.0)\n- `tolerance`: Convergence tolerance (default: 1e-6)\n- `rho`: Factor to increase penalty parameter (default: 1.5)\n- `max_mu`: Maximum penalty parameter value (default: 1000.0)\n- `constraint_tolerance`: Tolerance for constraint satisfaction (default: 1e-4)\n- `max_outer_iterations`: Maximum outer iterations (default: 20)\n- `max_inner_iterations`: Maximum inner iterations per subproblem (default: 50)\n- `verbose`: Whether to print optimization progress (default: True)\n\n#### Methods\n\n- `solve(x0, max_outer_iterations=100, tolerance=1e-6)`: Solve the optimization problem\n- `set_functions(objective_func, constraint_funcs)`: Set objective and constraint functions\n\n## PyTorch Backend Example\n\nThe PyTorch backend uses SGD optimization and supports GPU acceleration:\n\n```python\nimport numpy as np\nfrom aug_lag import AugmentedLagrangian\n\n# Define objective and constraint functions (same as before)\ndef objective(x):\n    return (x[0] - 1)**2 + (x[1] - 2)**2\n\ndef constraint(x):\n    return x[0] + x[1] - 3\n\n# Create solver with PyTorch backend\nsolver = AugmentedLagrangian(\n    objective_func=objective,\n    constraint_funcs=constraint,\n    backend=\"pytorch\",           # Use PyTorch backend\n    max_inner_iterations=200,    # More epochs for SGD\n    tolerance=1e-6,\n    verbose=True\n)\n\n# Solve the problem\nx0 = np.array([0.0, 0.0])\nresult = solver.solve(x0)\n\nprint(f\"Solution: x = {result['x']}\")\nprint(f\"Backend used: {solver.backend}\")\n```\n\n**Backend Comparison:**\n- **SciPy backend**: Uses BFGS algorithm, typically faster convergence, CPU-only\n- **PyTorch backend**: Uses SGD algorithm, supports GPU acceleration, good for large-scale problems\n\n## Multiple Constraints Example\n\n```python\nimport numpy as np\nfrom augmented_lagrangian import AugmentedLagrangian\n\n# Objective function\ndef objective(x):\n    return x[0]**2 + x[1]**2\n\n# Multiple constraints\ndef constraint1(x):\n    return x[0] + x[1] - 1\n\ndef constraint2(x):\n    return x[0] - x[1]\n\n# Create solver with multiple constraints\nsolver = AugmentedLagrangian(\n    objective_func=objective,\n    constraint_funcs=[constraint1, constraint2]\n)\n\n# Solve\nx0 = np.array([0.0, 0.0])\nresult = solver.solve(x0)\n```\n\n## Algorithm Details\n\nThe Augmented Lagrangian method combines the objective function with penalty terms for constraint violations:\n\n```\nL_A(x, \u03bb, \u03bc) = f(x) - \u03a3 \u03bb_i * c_i(x) + (\u03bc/2) * \u03a3 c_i(x)\u00b2\n```\n\nWhere:\n- `f(x)` is the objective function\n- `c_i(x)` are the constraint functions\n- `\u03bb_i` are the Lagrange multipliers\n- `\u03bc` is the penalty parameter\n\nThe algorithm iteratively:\n1. Minimizes the augmented Lagrangian with respect to x\n2. Updates the Lagrange multipliers: \u03bb := \u03bb - \u03bc * c(x)\n3. Increases the penalty parameter if needed\n4. Repeats until convergence\n\n## Requirements\n\n- Python >= 3.8\n- NumPy >= 1.20.0\n- SciPy >= 1.7.0\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## Citation\n\nIf you use this package in your research, please consider citing:\n\n```bibtex\n@software{augmented_lagrangian,\n  title={Augmented Lagrangian: A Python Implementation},\n  author={Hongwei Jin},\n  year={2025},\n  url={https://github.com/cshjin/augmented-lagrangian}\n}\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A Python implementation of the Augmented Lagrangian method for constrained optimization",
    "version": "0.1.1",
    "project_urls": {
        "Documentation": "https://augmented-lagrangian.readthedocs.io",
        "Homepage": "https://github.com/cshjin/augmented-lagrangian",
        "Issues": "https://github.com/cshjin/augmented-lagrangian/issues",
        "Repository": "https://github.com/cshjin/augmented-lagrangian.git"
    },
    "split_keywords": [
        "optimization",
        " constrained-optimization",
        " lagrangian",
        " mathematical-optimization"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "35c57068076bf468cbd59c5bf05946330c27526c3be2ae52ecc5e275c4603588",
                "md5": "ceb0d56b32889e876b0c1a0992dd9af7",
                "sha256": "7000f89a740ec8006b43ba62836390b116daaa028853b9f6d640f9e54415288f"
            },
            "downloads": -1,
            "filename": "augmented_lagrangian-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ceb0d56b32889e876b0c1a0992dd9af7",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 4499,
            "upload_time": "2025-10-16T22:29:48",
            "upload_time_iso_8601": "2025-10-16T22:29:48.048523Z",
            "url": "https://files.pythonhosted.org/packages/35/c5/7068076bf468cbd59c5bf05946330c27526c3be2ae52ecc5e275c4603588/augmented_lagrangian-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "f46e0c4163d82b7cbbc36476bbdd6251494507b9bd773c49693035469419d3e3",
                "md5": "38f521ad2b1792f09f73404548803164",
                "sha256": "b63e89617e50e9e14432ee9b37f0b991f5ee963626fb075ba382de4382354240"
            },
            "downloads": -1,
            "filename": "augmented_lagrangian-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "38f521ad2b1792f09f73404548803164",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 17892,
            "upload_time": "2025-10-16T22:29:49",
            "upload_time_iso_8601": "2025-10-16T22:29:49.125134Z",
            "url": "https://files.pythonhosted.org/packages/f4/6e/0c4163d82b7cbbc36476bbdd6251494507b9bd773c49693035469419d3e3/augmented_lagrangian-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-10-16 22:29:49",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "cshjin",
    "github_project": "augmented-lagrangian",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "numpy",
            "specs": []
        },
        {
            "name": "scipy",
            "specs": []
        },
        {
            "name": "torch",
            "specs": []
        },
        {
            "name": "pytest",
            "specs": [
                [
                    ">=",
                    "6.0"
                ]
            ]
        },
        {
            "name": "pytest-cov",
            "specs": []
        },
        {
            "name": "autopep8",
            "specs": []
        },
        {
            "name": "isort",
            "specs": []
        },
        {
            "name": "flake8",
            "specs": []
        },
        {
            "name": "mypy",
            "specs": []
        },
        {
            "name": "sphinx",
            "specs": []
        },
        {
            "name": "sphinx-rtd-theme",
            "specs": []
        },
        {
            "name": "matplotlib",
            "specs": []
        },
        {
            "name": "seaborn",
            "specs": []
        }
    ],
    "lcname": "augmented-lagrangian"
}
        
Elapsed time: 2.97610s