{"id":13418653,"url":"https://github.com/PatWie/CppNumericalSolvers","last_synced_at":"2025-03-15T03:31:46.422Z","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":243681024,"owners_count":20330152,"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-07-30T22:01:05.055Z","updated_at":"2025-03-15T03:31:46.416Z","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\n### Example Usage: Minimizing the Rosenbrock Function\n\nLet minimize the classic Rosenbrock function using the BFGS solver.\n\n```cpp\n/**\n * @brief Alias for a 2D function supporting second-order differentiability in cppoptlib.\n *\n * This defines a function template specialized for a 2-dimensional input vector,\n * which supports evaluation of the function value and its derivatives.\n *\n * The differentiability level is determined by the Differentiability enum:\n *  - Differentiability::First: Supports first-order derivatives (i.e., the gradient)\n *    without computing the Hessian. This is useful for optimization methods that do not\n *    require second-order information, saving computational effort.\n *  - Differentiability::Second: Supports second-order derivatives, meaning that both the\n *    gradient and Hessian are computed. This level is needed for methods that rely on curvature\n *    information.\n *\n * In this alias, Differentiability::Second is used, so both the gradient and Hessian are\n * assumed to be implemented.\n */\nusing Function2D = cppoptlib::function::Function\u003cdouble, 2, cppoptlib::function::Differentiability::Second\u003e;\n\n/**\n * @brief Implementation of a quadratic function with optional gradient and Hessian computation.\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 * This implementation computes the gradient and Hessian only if the corresponding pointers\n * are provided by the caller.\n */\nclass Function : public Function2D {\npublic:\n  EIGEN_MAKE_ALIGNED_OPERATOR_NEW\n\n  /**\n   * @brief Evaluates the function and optionally its gradient and Hessian.\n   *\n   * @param x Input vector.\n   * @param gradient (Optional) Pointer to store the computed gradient.\n   * @param hessian  (Optional) Pointer to store the computed Hessian.\n   * @return The function value f(x).\n   */\n  scalar_t operator()(const vector_t \u0026x, vector_t *gradient = nullptr,\n                        matrix_t *hessian = nullptr) const override {\n\n    // Even for functions declared as Differentiability::First, the gradient is not always required.\n    // To save computation, we only calculate and store the gradient if a non-null pointer is provided.\n    if (gradient) {\n      gradient-\u003eresize(x.size());\n      // The gradient components:\n      // ∂f/∂x₀ = 2 * 5 * x₀ = 10 * x₀\n      // ∂f/∂x₁ = 2 * 100 * x₁ = 200 * x₁\n      (*gradient)[0] = 2 * 5 * x[0];\n      (*gradient)[1] = 2 * 100 * x[1];\n    }\n\n    // If the Hessian is requested, compute and store it.\n    // (This is applicable only for Differentiability::Second)\n    if (hessian) {\n      hessian-\u003eresize(x.size(), x.size());\n      // Set the Hessian components:\n      // ∂²f/∂x₀² = 10, ∂²f/∂x₁² = 200, and the off-diagonals are 0.\n      (*hessian)(0, 0) = 10;\n      (*hessian)(0, 1) = 0;\n      (*hessian)(1, 0) = 0;\n      (*hessian)(1, 1) = 200;\n    }\n\n    // Return the function value: f(x) = 5*x₀² + 100*x₁² + 5.\n    return 5 * x[0] * x[0] + 100 * x[1] * x[1] + 5;\n  }\n};\n\n\nint main(int argc, char const *argv[]) {\n    // Create an instance of the Rosenbrock function.\n    Rosenbrock f;\n\n    // Initial guess for the solution.\n    Eigen::VectorXd x(2);\n    x \u003c\u003c -1, 2;\n    std::cout \u003c\u003c \"Initial point: \" \u003c\u003c x.transpose() \u003c\u003c std::endl;\n\n    // Evaluate\n    Eigen::VectorXd gradient(2);\n    double value = f(x, \u0026gradient);\n    std::cout \u003c\u003c \"Function value at initial point: \" \u003c\u003c value \u003c\u003c std::endl;\n    std::cout \u003c\u003c \"Gradient at initial point: \" \u003c\u003c gradient \u003c\u003c std::endl;\n\n    // Minimize the Rosenbrock function using the BFGS solver.\n    using Solver = cppoptlib::solver::Bfgs\u003cRosenbrock\u003e;\n    auto init_state = f.GetState(x);\n    Solver solver;\n    auto [solution_state, solver_progress] = solver.Minimize(f, init_state);\n\n    // Display the results of the optimization.\n    std::cout \u003c\u003c \"Optimal solution: \" \u003c\u003c solution_state.x.transpose() \u003c\u003c std::endl;\n    std::cout \u003c\u003c \"Optimal function value: \" \u003c\u003c f(solution_state.x) \u003c\u003c std::endl;\n\n    std::cout \u003c\u003c \"Number of iterations: \" \u003c\u003c solver_progress.num_iterations \u003c\u003c std::endl;\n    std::cout \u003c\u003c \"Solver status: \" \u003c\u003c solver_progress.status \u003c\u003c std::endl;\n\n    return 0;\n}\n```\n\nYou can easily adapt this code for your specific optimization problem by\ndefining your objective function and selecting an appropriate solver from\nCppNumericalSolvers. Dive into the implementation for more details on available\nsolvers and advanced usage.\n\n\nSee the examples for a constrained problem.\n\n### References\n\n**L-BFGS-B**: A LIMITED MEMORY ALGORITHM FOR BOUND CONSTRAINED OPTIMIZATION\n_Richard H. Byrd, Peihuang Lu, Jorge Nocedal and Ciyou Zhu_\n\n**L-BFGS**: Numerical Optimization, 2nd ed. New York: Springer\n_J. Nocedal and S. J. Wright_\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":["TODO scan for Android support in followings"],"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"}