Ceres简单应用-求解(Powell's Function)鲍威尔函数最小值
2023-08-07 10:42:28 来源:博客园 小 中
(相关资料图)
\(\quad\)现在考虑一个稍微复杂一点的例子—鲍威尔函数的最小化。
\(\quad{}\) \(x=[x_1,x_2,x_3,x_4]\) 并且
\[\begin{array}{l}f_{1}(x)=x_{1}+10 x_{2} \\f_{2}(x)=\sqrt{5}\left(x_{3}-x_{4}\right) \\f_{3}(x)=\left(x_{2}-2 x_{3}\right)^{2} \\f_{4}(x)=\sqrt{10}\left(x_{1}-x_{4}\right)^{2} \\ \\ F(x)=\left[f_{1}(x), f_{2}(x), f_{3}(x), f_{4}(x)\right]\end{array}\]\(\quad{}\) \(F(x)\) 是一个拥有四个参数的函数,并且拥有四个残差块,我们希望找到一个 \(x\) 使得下面的式子得到最小化:
\[\frac{1}{2}||F(x)||^2\]\(\quad{}\)同样,第一步是定义对目标函子中的项进行评估的函子。 这是评估的代码,翻译过来不太准确,或者说这其实是在定义残差块。对 \(f_4(x_1,x_4)\) 的定义如下:
struct F4 { template bool operator()(const T* const x1, const T* const x4, T* residual) const { residual[0] = sqrt(10.0) * (x1[0] - x4[0]) * (x1[0] - x4[0]); return true; }};
\(\quad{}\) 同样,我们也可以类似的定义 \(F_1\), \(F_2\) 和 \(F_3\) 去评估计算 \(f_1(x_1,x_2)\),\(f_2(x_3,x_4)\),\(f_3(x_2,x_3)\)。这样的话,问题将会被定义为如下的格式:
// 赋予初值double x1 = 3.0; double x2 = -1.0; double x3 = 0.0; double x4 = 1.0;Problem problem;// Add residual terms to the problem using the autodiff// wrapper to get the derivatives automatically.problem.AddResidualBlock( new AutoDiffCostFunction(new F1), nullptr, &x1, &x2);problem.AddResidualBlock( new AutoDiffCostFunction(new F2), nullptr, &x3, &x4);problem.AddResidualBlock( new AutoDiffCostFunction(new F3), nullptr, &x2, &x3);problem.AddResidualBlock( new AutoDiffCostFunction(new F4), nullptr, &x1, &x4);
\(\quad{}\) 请注意,每个 ResidualBlock
仅依赖于对应残差对象所依赖的两个参数,而不依赖于所有四个参数。
#include #include "ceres/ceres.h"#include "gflags/gflags.h"#include "glog/logging.h"#include "ceres/internal/port.h"using ceres::AutoDiffCostFunction;using ceres::CostFunction;using ceres::Problem;using ceres::Solve;using ceres::Solver;struct F1{ template bool operator()(const T *const x1, const T *const x2, T *residual) const { // f1 = x1 + 10 * x2; residual[0] = x1[0] + 10.0 * x2[0]; return true; }};struct F2{ template bool operator()(const T *const x3, const T *const x4, T *residual) const { // f2 = sqrt(5) (x3 - x4) residual[0] = sqrt(5.0) * (x3[0] - x4[0]); return true; }};struct F3{ template bool operator()(const T *const x2, const T *const x3, T *residual) const { // f3 = (x2 - 2 x3)^2 residual[0] = (x2[0] - 2.0 * x3[0]) * (x2[0] - 2.0 * x3[0]); return true; }};struct F4{ template bool operator()(const T *const x1, const T *const x4, T *residual) const { // f4 = sqrt(10) (x1 - x4)^2 residual[0] = sqrt(10.0) * (x1[0] - x4[0]) * (x1[0] - x4[0]); return true; }};DEFINE_string(minimizer, "trust_region", "Minimizer type to use, choices are: line_search & trust_region");int main(int argc, char **argv){ GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); google::InitGoogleLogging(argv[0]); double x1 = 3.0; double x2 = -1.0; double x3 = 0.0; double x4 = 1.0; Problem problem; // Add residual terms to the problem using the autodiff // wrapper to get the derivatives automatically. The parameters, x1 through // x4, are modified in place. problem.AddResidualBlock( new AutoDiffCostFunction(new F1), nullptr, &x1, &x2); problem.AddResidualBlock( new AutoDiffCostFunction(new F2), nullptr, &x3, &x4); problem.AddResidualBlock( new AutoDiffCostFunction(new F3), nullptr, &x2, &x3); problem.AddResidualBlock( new AutoDiffCostFunction(new F4), nullptr, &x1, &x4); Solver::Options options; // LOG_IF(FATAL, // !ceres::StringToMinimizerType(CERES_GET_FLAG(FLAGS_minimizer), // &options.minimizer_type)) // << "Invalid minimizer: " << CERES_GET_FLAG(FLAGS_minimizer) // << ", valid options are: trust_region and line_search."; options.max_num_iterations = 100; options.linear_solver_type = ceres::DENSE_QR; options.minimizer_progress_to_stdout = true; // clang-format off std::cout << "Initial x1 = " << x1 << ", x2 = " << x2 << ", x3 = " << x3 << ", x4 = " << x4 << "\n"; // clang-format on // Run the solver! Solver::Summary summary; Solve(options, &problem, &summary); std::cout << summary.FullReport() << "\n"; // clang-format off std::cout << "Final x1 = " << x1 << ", x2 = " << x2 << ", x3 = " << x3 << ", x4 = " << x4 << "\n"; // clang-format on return 0;}
\(\quad{}\) 编译并运行可得到如下输出:
Initial x1 = 3, x2 = -1, x3 = 0, x4 = 1iter cost cost_change |gradient| |step| tr_ratio tr_radius ls_iter iter_time total_time 0 1.075000e+02 0.00e+00 1.55e+02 0.00e+00 0.00e+00 1.00e+04 0 5.89e-05 5.71e-04 1 5.036190e+00 1.02e+02 2.00e+01 2.16e+00 9.53e-01 3.00e+04 1 1.80e-04 7.80e-04 2 3.148168e-01 4.72e+00 2.50e+00 6.23e-01 9.37e-01 9.00e+04 1 3.90e-05 8.32e-04 3 1.967760e-02 2.95e-01 3.13e-01 3.08e-01 9.37e-01 2.70e+05 1 3.65e-05 8.79e-04 4 1.229900e-03 1.84e-02 3.91e-02 1.54e-01 9.37e-01 8.10e+05 1 3.57e-05 9.23e-04 5 7.687123e-05 1.15e-03 4.89e-03 7.69e-02 9.37e-01 2.43e+06 1 3.54e-05 9.67e-04 6 4.804625e-06 7.21e-05 6.11e-04 3.85e-02 9.37e-01 7.29e+06 1 3.52e-05 1.01e-03 7 3.003028e-07 4.50e-06 7.64e-05 1.92e-02 9.37e-01 2.19e+07 1 3.53e-05 1.05e-03 8 1.877006e-08 2.82e-07 9.54e-06 9.62e-03 9.37e-01 6.56e+07 1 3.53e-05 1.10e-03 9 1.173223e-09 1.76e-08 1.19e-06 4.81e-03 9.37e-01 1.97e+08 1 3.58e-05 1.15e-03 10 7.333425e-11 1.10e-09 1.49e-07 2.40e-03 9.37e-01 5.90e+08 1 3.54e-05 1.19e-03 11 4.584044e-12 6.88e-11 1.86e-08 1.20e-03 9.37e-01 1.77e+09 1 3.51e-05 1.24e-03 12 2.865573e-13 4.30e-12 2.33e-09 6.02e-04 9.37e-01 5.31e+09 1 3.55e-05 1.28e-03 13 1.791438e-14 2.69e-13 2.91e-10 3.01e-04 9.37e-01 1.59e+10 1 3.55e-05 1.32e-03 14 1.120029e-15 1.68e-14 3.64e-11 1.51e-04 9.37e-01 4.78e+10 1 3.54e-05 1.37e-03Solver Summary (v 1.13.0-eigen-(3.3.4)-lapack-suitesparse-(5.1.2)-cxsparse-(3.1.9)-openmp) Original ReducedParameter blocks 4 4Parameters 4 4Residual blocks 4 4Residual 4 4Minimizer TRUST_REGIONDense linear algebra library EIGENTrust region strategy LEVENBERG_MARQUARDT Given UsedLinear solver DENSE_QR DENSE_QRThreads 1 1Linear solver threads 1 1Linear solver ordering AUTOMATIC 4Cost:Initial 1.075000e+02Final 1.120029e-15Change 1.075000e+02Minimizer iterations 15Successful steps 15Unsuccessful steps 0Time (in seconds):Preprocessor 0.0005 Residual evaluation 0.0000 Jacobian evaluation 0.0005 Linear solver 0.0001Minimizer 0.0009Postprocessor 0.0000Total 0.0014Termination: CONVERGENCE (Gradient tolerance reached. Gradient max norm: 3.642190e-11 <= 1.000000e-10)Final x1 = 0.000146222, x2 = -1.46222e-05, x3 = 2.40957e-05, x4 = 2.40957e-05
\(\quad{}\) 其实我们很容易看出函数的最优解在 \(x_1=0, x_2=0,x_3=0,x_4=0\) ,而最终的计算结果虽然并不是完全为\(0\) 但是也基本接近。
关键词:
相关文章
- Ceres简单应用-求解(Powell's Function)鲍威尔函数最小值
- 国家发改委:推动合理增加消费信贷;宁银消费金融增资至29.11亿元;利率最低至3.7%!消费贷“卷”到地方银行丨21消费金融参考
- 98亿元!粤水电投建1.5GW风光热储牧多能互补项目
- 浙江世宝8月7日快速回调
- 甘肃能化:8月4日获融资买入962.61万元
- 22个!河南新一批“绿色发展领跑计划”项目来了
- 对购买新房实施补贴 南京发布楼市优化新政
- 惠科股份终止创业板IPO 原拟募集资金95亿元
- 一金一银!安徽运动员成都大运会再传捷报
- 邮储银行落地全国首笔公正转型贷款 助力绿色低碳转型
- ST贵人8月7日快速上涨
- 外媒:消息人士曝,日本政府最早8月下旬排放福岛核污染水入海
- 恒瑞医药8月7日在互动平台表示 公司及下属分子公司、所有派驻机构没有网传情形
- 内盘原油期货主力开盘涨超2%
- 海洋王:为京津冀等地区洪灾救援现场提供照明保障
- 7月份中国电商物流指数继续上涨
- 中国央行今日进行30亿元7天期逆回购操作
- N华虹今日上市 开盘大涨13%
- 医药股延续跌势 丽珠集团逼近跌停
- 超导概念股快速拉升
- 情绪乐观
- 基金投资的变与不变
- 牛市要来了,得拿出态度才行
- 通化东宝研究:话说医药反腐对胰岛素产业的影响...
- 河北加快恢复重建 保障群众正常生产生活
- 李大霄:大国牛或从东方地平线上冉冉升起 中国股市或从3200点航母甲板上正式起飞
- 广州医药股份有限公司到访云南白药
- 中证转债低开0.03%
- 东方财富、贵州茅台等58股上周获融资净买入超亿元
- 科创板N华虹上市首日涨超13%
热文推荐

国家发改委:推动合理增加消费信贷;宁银消费金融增资至29.11亿元;利率最低至3.7%!消费贷“卷”到地方银行丨21消费金融参考
21世纪资管研究院吴霜,实习生于兴子综合整理【监管与......更多>
排行推荐

甘肃能化:8月4日获融资买入962.61万元
同花顺数据中心显示,甘肃能化8月4日获融资买入962 6... 更多>

22个!河南新一批“绿色发展领跑计划”项目来了
8月4日,记者从省工业和信息化厅获悉,为加强产融合作... 更多>

对购买新房实施补贴 南京发布楼市优化新政
人民网南京8月5日电(王丹丹)对购买新建商品住房的实... 更多>

惠科股份终止创业板IPO 原拟募集资金95亿元
惠科股份终止创业板IPO原拟募集资金95亿元 更多>
总投资159.4亿元—— 西部装机容量最大抽水蓄能电站开工
本报北京8月6日电 记者张翼从国家电网获悉,国家电网... 更多>
德国氢能发展——基础设施、政策激励、国际合作全方位推进
德国氢能发展——基础设施、政策激励、国际合作全方位... 更多>
生肖运程|属猪健康状态佳,出门宜配白色外衣,可增进财运
8月7日,猪:健康状态佳,保持饮食均衡。出门宜配白色... 更多>
完成党和人民赋予的使命任务
今年夏天,多地遭遇强降雨,引发山洪和泥石流等地质灾... 更多>
海口文旅节庆活动“非遗艺术”走进芳园国际艺术村
本报8月6日讯(记者陈钰婷祝勇)8月5日至6日,海口文旅... 更多>
如何找到非凡影音的文件位置
在日常生活中,当我们使用数字设备时,我们会遇到各种... 更多>
即将降温!湖北西部暴雨上线,这些地方灾害风险较大
最近湖北的高温天气不断发展@湖北天气发布体感温度报... 更多>
研究料30年间中国慢阻肺经济负担全球最重,或达1.4万亿美元
2019年330万人因其死亡,十年间死亡率增长14%……作为... 更多>
松花江干流哈尔滨站预计11日凌晨出现洪峰
本报6日讯(记者吴玉玺)6日,记者从省水文水资源中心... 更多>
中国央行公开市场本周共530亿元逆回购到期
中国央行公开市场本周共530亿元逆回购到期,其中周一... 更多>