{"id":15047432,"url":"https://github.com/patwie/cppnumericalsolvers","last_synced_at":"2025-05-16T10:06:17.998Z","repository":{"id":17511034,"uuid":"20298959","full_name":"PatWie/CppNumericalSolvers","owner":"PatWie","description":"a lightweight header-only C++17 library of numerical optimization methods for nonlinear functions based on Eigen","archived":false,"fork":false,"pushed_at":"2024-01-03T14:58:33.000Z","size":635,"stargazers_count":872,"open_issues_count":2,"forks_count":201,"subscribers_count":53,"default_branch":"main","last_synced_at":"2024-10-29T18:07:10.999Z","etag":null,"topics":["bfgs","cpp11","eigen","gradient-computation","lbfgs","lbfgs-solver","lbfgsb-solver","mathematics","minimization","minimization-algorithm","newton","numerical-optimization-methods","optim","optimisation","optimization","optimization-algorithms","optimization-tools","solver","solvers"],"latest_commit_sha":null,"homepage":"","language":"C++","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/PatWie.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}},"created_at":"2014-05-29T15:45:13.000Z","updated_at":"2024-10-25T14:57:53.000Z","dependencies_parsed_at":"2024-10-26T15:11:39.868Z","dependency_job_id":"2e25c842-b876-423d-b8a8-705adb494d2e","html_url":"https://github.com/PatWie/CppNumericalSolvers","commit_stats":{"total_commits":142,"total_committers":20,"mean_commits":7.1,"dds":0.6901408450704225,"last_synced_commit":"5abb2ca3c5722dfd4e913deef6621a4d7d2f3292"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PatWie%2FCppNumericalSolvers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PatWie%2FCppNumericalSolvers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PatWie%2FCppNumericalSolvers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PatWie%2FCppNumericalSolvers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PatWie","download_url":"https://codeload.github.com/PatWie/CppNumericalSolvers/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247974717,"owners_count":21026742,"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","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":["bfgs","cpp11","eigen","gradient-computation","lbfgs","lbfgs-solver","lbfgsb-solver","mathematics","minimization","minimization-algorithm","newton","numerical-optimization-methods","optim","optimisation","optimization","optimization-algorithms","optimization-tools","solver","solvers"],"created_at":"2024-09-24T20:58:11.601Z","updated_at":"2025-04-09T04:06:13.840Z","avatar_url":"https://github.com/PatWie.png","language":"C++","readme":"# CppNumericalSolvers (A header-only C++17 optimization library)\n\nCppNumericalSolvers is a header-only C++17 optimization library providing a\nsuite of solvers for both unconstrained and constrained optimization problems.\nThe library is designed for efficiency, modern C++ compliance, and easy\nintegration into existing projects. It is distributed under a permissive\nlicense, making it suitable for commercial use.\n\nKey Features:\n- **Unconstrained Optimization**: Supports algorithms such as **Gradient Descent, Conjugate Gradient, Newton's Method, BFGS, L-BFGS, and Nelder-Mead**.\n- **Constrained Optimization**: Provides support for **L-BFGS-B** and **Augmented Lagrangian methods**.\n- **Expression Templates**: Enable efficient evaluation of function expressions without unnecessary allocations.\n- **First- and Second-Order Methods**: Support for both **gradient-based** and **Hessian-based** optimizations.\n- **Differentiation Utilities**: Tools to check gradient and Hessian correctness.\n- **Header-Only**: Easily integrate into any C++17 project with no dependencies.\n\n\n## Quick Start\n\n### **Minimizing a Function (BFGS Solver)**\n\n```cpp\n#include \u003ciostream\u003e\n#include \"cppoptlib/function.h\"\n#include \"cppoptlib/solver/bfgs.h\"\n\nusing namespace cppoptlib::function;\n\nclass ExampleFunction : public FunctionXd\u003cExampleFunction\u003e {\npublic:\n  EIGEN_MAKE_ALIGNED_OPERATOR_NEW\n\n    /**\n     * The function is defined as:\n     *\n     *     f(x) = 5 * x₀² + 100 * x₁² + 5\n     *\n     * Its gradient is given by:\n     *\n     *     ∇f(x) = [10 * x₀, 200 * x₁]ᵀ\n     *\n     * And its Hessian is:\n     *\n     *     ∇²f(x) = [ [10,   0],\n     *                 [ 0, 200] ]\n     */\n  ScalarType operator()(const VectorType \u0026x, VectorType *gradient = nullptr,\n                        MatrixType *hessian = nullptr) const override {\n    if (gradient) {\n      *gradient = VectorType::Zero(2);\n      (*gradient)[0] = 2 * 5 * x[0];\n      (*gradient)[1] = 2 * 100 * x[1];\n    }\n\n    if (hessian) {\n      *hessian = MatrixType::Zero(2, 2);\n      (*hessian)(0, 0) = 10;\n      (*hessian)(0, 1) = 0;\n      (*hessian)(1, 0) = 0;\n      (*hessian)(1, 1) = 200;\n    }\n\n    return 5 * x[0] * x[0] + 100 * x[1] * x[1] + 5;\n  }\n};\n\nint main() {\n    ExampleFunction f;\n    Eigen::VectorXd x(2);\n    x \u003c\u003c -1, 2;\n\n    using Solver = cppoptlib::solver::Bfgs\u003cExampleFunction\u003e;\n    auto init_state = f.GetState(x);\n    Solver solver;\n    auto [solution_state, solver_progress] = solver.Minimize(f, init_state);\n\n    std::cout \u003c\u003c \"Optimal solution: \" \u003c\u003c solution_state.x.transpose() \u003c\u003c std::endl;\n    return 0;\n}\n```\n\n### **Solving a Constrained Optimization Problem**\n\nCppNumericalSolvers allows solving constrained optimization problems using the\n**Augmented Lagrangian** method. Below, we solve the problem:\n\n```\nmin f(x) = x_0 + x_1\n```\n\nsubject to the constraint:\n\n```\nx_0^2 + x_1^2 = 2\n```\n\n```cpp\n#include \u003ciostream\u003e\n#include \"cppoptlib/function.h\"\n#include \"cppoptlib/solver/augmented_lagrangian.h\"\n#include \"cppoptlib/solver/lbfgs.h\"\n\nusing namespace cppoptlib::function;\nusing namespace cppoptlib::solver;\n\nclass SumObjective : public FunctionXd\u003cSumObjective\u003e {\n public:\n  ScalarType operator()(const VectorType \u0026x, VectorType *gradient = nullptr) const {\n    if (gradient) *gradient = VectorType::Ones(2);\n    return x.sum();\n  }\n};\n\nclass Circle : public FunctionXd\u003cCircle\u003e {\n public:\n  ScalarType operator()(const VectorType \u0026x, VectorType *gradient = nullptr) const {\n    if (gradient) *gradient = 2 * x;\n    return x.squaredNorm();\n  }\n};\n\nint main() {\n  SumObjective::VectorType x(2);\n  x \u003c\u003c 2, 10;\n\n  FunctionExpr objective = SumObjective();\n  FunctionExpr circle = Circle();\n\n  ConstrainedOptimizationProblem prob(\n      objective,\n      // 1. Equality constraint: circle(x) - 2 == 0, forcing the solution onto\n      // the circle's boundary.\n      {FunctionExpr(circle - 2)},\n      // 2. Inequality constraint: 2 - circle(x) \u003e= 0, ensuring the solution\n      // remains inside the circle.\n      {FunctionExpr(2 - circle)});\n\n  Lbfgs\u003cFunctionExpr2d1\u003e inner_solver;\n  AugmentedLagrangian solver(prob, inner_solver);\n\n  AugmentedLagrangeState\u003cdouble\u003e l_state(x, /* num_eq=*/1,\n                                            /* num_ineq=*/1,\n                                            /* penalty=*/1.0);\n  auto [solution, solver_state] = solver.Minimize(prob, l_state);\n\n  std::cout \u003c\u003c \"Optimal x: \" \u003c\u003c solution.x.transpose() \u003c\u003c std::endl;\n  return 0;\n}\n\n```\n\n\n## Use in Your Project\n\nEither use Bazel or CMake:\n\n```sh\ngit clone https://github.com/PatWie/CppNumericalSolvers.git\n```\n\nCompile with:\n\n```sh\ng++ -std=c++17 -Ipath/to/CppNumericalSolvers/include myfile.cpp -o myprogram\n```\n\n\n\n\n### Citing this implementation\n\nIf you find this implementation useful and wish to cite it, please use the following bibtex entry:\n\n```bibtex\n@misc{wieschollek2016cppoptimizationlibrary,\n  title={CppOptimizationLibrary},\n  author={Wieschollek, Patrick},\n  year={2016},\n  howpublished={\\url{https://github.com/PatWie/CppNumericalSolvers}},\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatwie%2Fcppnumericalsolvers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpatwie%2Fcppnumericalsolvers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatwie%2Fcppnumericalsolvers/lists"}