# liblgd **Repository Path**: yizhangss/liblgd ## Basic Information - **Project Name**: liblgd - **Description**: C++ library of the Lévy-Gradient Descent (L-GD) method - **Primary Language**: C++ - **License**: GPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-09-10 - **Last Updated**: 2025-02-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # liblgd #### 介绍 C++ library of the Lévy-Gradient Descent (L-GD) method #### 软件架构 软件架构说明 #### 安装教程 算法库默认使用 Cmake 编译工具进行编译,可在不同平台生成相应的可执行或工程文件。请用户自行下载安装 Cmake 软件后按如下方法进行编译。此方法适用于 MacOS 或 Linux 系统 (默认的编译器为 gcc,用户可在 CMakeLists.txt 中自行修改编译器与安装地址),Windows 用户请使用 VS studio 等编译工具新建项目并拷贝 src/lib 文件夹下所有文件至新项目并编译动态库。 ```shell mkdir build cd build cmake .. make make install ``` #### 使用说明 1. xxxx 2. xxxx 3. xxxx #### Examples ```cpp #include "lgd.h" #include "iostream" #include "cmath" #include "vector" #include "string" #define pi (atan(1.0)*4.0) enum gradient_type_e { Dnull, Dx, Dy }; struct gaussian_para { double mu_x, mu_y, sigma_x, sigma_y, rho; }; double gaussian_distribution(double x, double y, const gaussian_para &p, gradient_type_e mode = Dnull) { double part = -0.5*(pow((x-p.mu_x)/p.sigma_x,2) -2*p.rho*(x-p.mu_x)*(y-p.mu_y)/(p.sigma_x*p.sigma_y) + pow((y-p.mu_y)/p.sigma_y,2)) / (1.0-p.rho*p.rho); double part2; if (mode == Dnull) { return exp(part)/(2*pi*p.sigma_x*p.sigma_y*sqrt(1-p.rho*p.rho)); } else if (mode == Dx) { part2 = -1.0*((x-p.mu_x)/(p.sigma_x*p.sigma_x) - p.rho*(y-p.mu_y)/(p.sigma_x*p.sigma_y))/(1.0-p.rho*p.rho); return part2*exp(part)/(2*pi*p.sigma_x*p.sigma_y*sqrt(1-p.rho*p.rho)); } else if (mode == Dy) { part2 = -1.0*((y-p.mu_y)/(p.sigma_y*p.sigma_y) - p.rho*(x-p.mu_x)/(p.sigma_x*p.sigma_y))/(1.0-p.rho*p.rho); return part2*exp(part)/(2*pi*p.sigma_x*p.sigma_y*sqrt(1-p.rho*p.rho)); } else { std::string err_str = "Invalid calculation mode."; throw err_str; } } static std::vector para; double evaluate(void *instance, const double *x, double *g, const int n) { double fx = 0.0; g[0] = g[1] = 0.0; for (int i = 0; i < para.size(); i++) { fx += -1.0*gaussian_distribution(x[0], x[1], para[i]); g[0] += -1.0*gaussian_distribution(x[0], x[1], para[i], Dx); g[1] += -1.0*gaussian_distribution(x[0], x[1], para[i], Dy); } fx += 12.82906044; g[0] *= 2.0*fx; g[1] *= 2.0*fx; return fx*fx; } double evaluate_fx_only(void *instance, const double *x, const int n) { double fx = 0.0; for (int i = 0; i < para.size(); i++) { fx += -1.0*gaussian_distribution(x[0], x[1], para[i]); } fx += 12.82906044; return fx*fx; } int progress(void *instance, lgd_float best_fx, const lgd_float *best_x, const lgd_float *best_g, const lgd_float *x, const lgd_float *g, const lgd_para *param, const int n_size, const int curr_k, const int best_k) { std::cerr << "fight time: " << curr_k << ", best fx: " << best_fx << ", found at fight time: " << best_k << std::endl; std::cerr << "\033[1A\033[K"; return 0; } int main(int argc, char *argv[]) { gaussian_para tmp_p; tmp_p.mu_x = 0.50; tmp_p.mu_y = 0.40; tmp_p.sigma_x = 0.15; tmp_p.sigma_y = 0.10; tmp_p.rho = 0.2; para.push_back(tmp_p); tmp_p.mu_x = 0.60; tmp_p.mu_y = 0.70; tmp_p.sigma_x = 0.10; tmp_p.sigma_y = 0.20; tmp_p.rho = 0.0; para.push_back(tmp_p); lgd_float fx; lgd_float x[2] = {0.25, 0.20}; lgd_float low[2] = {0.0, 0.0}; lgd_float hig[2] = {1.0, 1.0}; lgd_para my_para = lgd_default_parameters(); my_para.fight_times = 2000; my_para.alpha = 0.01; my_para.mu = 0.001; lgd_float x_mean[2] = {0.0, 0.0}; lgd_float x_stddev[2] = {0.0, 0.0}; int ret = lgd_solver_dynamic(&x_mean[0], &x_stddev[0], evaluate, progress, &fx, &x[0], 2, &my_para, nullptr, &low[0], &hig[0]); //int ret = lgd_solver_pseudo_dynamic(&x_mean[0], &x_stddev[0], evaluate_fx_only, progress, &fx, &x[0], 2, &my_para, nullptr, // &low[0], &hig[0]); std::cerr << lgd_error_str(ret) << std::endl; std::clog << "best-fx = " << fx << std::endl; std::clog << "best-model: " << x[0] << " " << x[1] << std::endl; std::clog << "mean-model: " << x_mean[0] << " " << x_mean[1] << std::endl; std::clog << "mean-stddev: " << x_stddev[0] << " " << x_stddev[1] << std::endl; std::clog << "x-range: " << x_mean[0] - x_stddev[0] << " " << x_mean[0] + x_stddev[0] << std::endl; std::clog << "y-range: " << x_mean[1] - x_stddev[1] << " " << x_mean[1] + x_stddev[1] << std::endl; return 0; } ``` #### 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request #### 码云特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. 码云官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解码云上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是码云最有价值开源项目,是码云综合评定出的优秀开源项目 5. 码云官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. 码云封面人物是一档用来展示码云会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)