当前位置: 首页 > news >正文

[最优化理论] 梯度下降法 + 精确线搜索(单峰区间搜索 + 黄金分割)C++ 代码

这是我的课程作业,用了 Eigen 库,最后的输出是 latex 的表格的一部分

具体内容就是 梯度下降法 + 精确线搜索(单峰区间搜索 + 黄金分割)

从书本的 Matlab 代码转译过来的其实,所以应该是一看就懂了

这里定义了两个测试函数 fun 和 fun2

整个最优化方法包装在 SteepestDescent 类里面

用了模板封装类,这样应该是 double 和 Eigne 的 Vector 都可以支持的

用了 tuple 返回值,用了 functional 接受函数形参,所以应该要 C++11 以上进行编译

#include "3rdparty/Eigen/Eigen/Dense"#include <cstdint>
#include <fstream>
#include <functional>
#include <iostream>
#include <string>
#include <tuple>#ifndef DEBUG
#    define DEBUG 0
#endifusing namespace Eigen;template<class YClass, class XClass>
class SteepestDescent
{
public:SteepestDescent(std::function<YClass(XClass)> const& fun,std::function<XClass(XClass)> const& gfun,double                               delta,double                               epsilon): m_fun(fun), m_gfun(gfun), m_delta(delta), m_epsilon(epsilon) {};/*** @brief Find single peak interval.** It will stop if the number of iterations exceeds the given upper limit.** @param fun Target function.* @param alpha0 Start point.* @param h Search direction.** @return XClass Left end of single peak interval.* @return XClass Right end of single peak interval.* @return XClass Inner point of single peak interval.* 1 represents same direction w.r.t. h, -1 represents reversed direction w.r.t. h.*/std::tuple<XClass, XClass, XClass> ForwardBackward(XClass alpha0, XClass h);/*** @brief Find a minimum of a function inside a specified interval.** @param fun Target function.* @param a Left end of interval.* @param b Right end of interval.* @param delta Tolerable error of input variable.* @param epsilon Tolerable error of target function value.** @return bool Is early stop. Let interpolation points to be p, q, if fun(a) < fun(p) and fun(q) > fun(b)* @return XClass Minimum point.* @return YClass Function value of minimum point.*/std::tuple<bool, XClass, YClass> GoldenSectionSearch(XClass a, XClass b);/*** @brief Run Forward Backward and Golden Section Search** @param fun Target function.* @param gfun Gredient of target function.* @param x0 Start point.* @param h Search direction.* @param delta Tolerable error of input variable.* @param epsilon Tolerable error of target function value.* @return std::tuple<YClass, YClass, uint32_t>*/std::tuple<XClass, YClass, uint32_t> ForwardBackwardAndGoldenSectionSearch(XClass x0);/*** @brief Run Armijo Search** @param fun Target function.* @param gfun Gredient of target function.* @param x0 Start point.* @param h Search direction.* @param delta Tolerable error of input variable.* @param epsilon Tolerable error of target function value.* @return std::tuple<YClass, YClass, uint32_t>*/std::tuple<XClass, YClass, uint32_t> ArmijoSearch(XClass x0);private:std::function<YClass(XClass)> m_fun;std::function<XClass(XClass)> m_gfun;double                        m_delta;double                        m_epsilon;
};template<class YClass, class XClass>
std::tuple<XClass, XClass, XClass> SteepestDescent<YClass, XClass>::ForwardBackward(XClass alpha0, XClass h)
{uint32_t k = 0, max_k = 500;bool     reversed = false;XClass alpha1 = alpha0, alpha = alpha0;YClass phi0 = m_fun(alpha0), phi1 = m_fun(alpha0);double t = 1e-2;while (k < max_k){alpha1 = alpha0 + t * h;phi1   = m_fun(alpha1);// forward searchif (phi1 < phi0){t      = 2.0 * t;alpha  = alpha0;alpha0 = alpha1;phi0   = phi1;}else{// backward searchif (k == 0){t     = -t;alpha = alpha1;}// find another endelse{break;}}++k;}#if DEBUGstd::cout << "ForwardBackward total iteration = " << std::endl;std::cout << k << std::endl;
#endifXClass left  = t > 0.0 ? alpha : alpha1;XClass right = t < 0.0 ? alpha : alpha1;return {left, right, alpha0};
}template<class YClass, class XClass>
std::tuple<bool, XClass, YClass> SteepestDescent<YClass, XClass>::GoldenSectionSearch(XClass a, XClass b)
{uint32_t k = 0, max_k = 500;double t = (sqrt(5) - 1.0) / 2.0;XClass h = b - a;XClass p = a + (1 - t) * h, q = a + t * h;YClass phia = m_fun(a), phib = m_fun(b);YClass phip = m_fun(p), phiq = m_fun(q);bool is_early_stop = false;if (phia < phip && phiq > phib){is_early_stop = true;#if DEBUGstd::cout << "GoldenSectionSearch total it eration = " << std::endl;std::cout << k << std::endl;
#endifreturn {is_early_stop, a, phia};}while (((abs(phip - phia) > m_epsilon) || (h.norm() > m_delta)) && k < max_k){if (phip < phiq){b = q;q = p;phib = phiq;phiq = phip;h = b - a;p = a + (1 - t) * h;phip = m_fun(p);}else{a = p;p = q;phia = phip;phip = phiq;h = b - a;q = a + t * h;phiq = m_fun(q);}++k;}#if DEBUGstd::cout << "GoldenSectionSearch total iteration = " << std::endl;std::cout << k << std::endl;
#endifif (phip <= phiq){return {is_early_stop, p, phip};}else{return {is_early_stop, q, phiq};}
}template<class YClass, class XClass>
std::tuple<XClass, YClass, uint32_t> SteepestDescent<YClass, XClass>::ForwardBackwardAndGoldenSectionSearch(XClass x0)
{uint32_t k = 0, max_k = 5000;YClass phi_min = m_fun(x0);#if DEBUG// file pointerstd::fstream fout;// opens an existing csv file or creates a new file.fout.open("SteepestDescent.csv", std::ios::out | std::ios::trunc);// Insert the data to filefout << x0[0] << ", " << x0[1] << ", " << phi_min << "\n";
#endifwhile (k < max_k){Vector2d h = -m_gfun(x0);if (h.norm() < m_epsilon){return {x0, phi_min, k};}auto [left, right, inner] = ForwardBackward(x0, h);auto [is_early_stop, x1, phix1] = GoldenSectionSearch(left, right);if (is_early_stop){x1    = inner;phix1 = m_fun(x1);}x0      = x1;phi_min = phix1;++k;#if DEBUGstd::cout << "iteration " << k << ":" << std::endl;std::cout << "h = " << std::endl;std::cout << h << std::endl;std::cout << "left pointer = " << std::endl;std::cout << left << std::endl;std::cout << "right pointer = " << std::endl;std::cout << right << std::endl;std::cout << "inner pointer = " << std::endl;std::cout << inner << std::endl;std::cout << "current point = " << std::endl;std::cout << x1 << std::endl;std::cout << "current evaluation = " << std::endl;std::cout << phix1 << std::endl;// Insert the data to filefout << x0[0] << ", " << x0[1] << ", " << phi_min << "\n";
#endif}return {x0, phi_min, k};
}template<class YClass, class XClass>
std::tuple<XClass, YClass, uint32_t> SteepestDescent<YClass, XClass>::ArmijoSearch(XClass x0)
{uint32_t k = 0, max_k = 5000;YClass phi_min = m_fun(x0);double rho   = 0.5;double sigma = 0.4;while (k < max_k){Vector2d h = -m_gfun(x0);if (h.norm() < m_epsilon){return {x0, phi_min, k};}uint32_t m  = 0;uint32_t mk = 0;while (m < 20) // Armijo Search{phi_min = m_fun(x0 + pow(rho, m) * h);if (phi_min < m_fun(x0) + sigma * pow(rho, m) * (-pow(h.norm(), 2.0))){mk = m;break;}m = m + 1;}x0 = x0 + pow(rho, mk) * h;++k;}return {x0, phi_min, k};
}double fun(Vector2d x) { return 100.0 * pow(pow(x[0], 2.0) - x[1], 2.0) + pow(x[0] - 1, 2.0); }Vector2d gfun(Vector2d x)
{return Vector2d(400.0 * x[0] * (pow(x[0], 2.0) - x[1]) + 2.0 * (x[0] - 1.0), -200.0 * (pow(x[0], 2.0) - x[1]));
}double fun2(Vector2d x) { return 3.0 * pow(x[0], 2.0) + 2.0 * pow(x[1], 2.0) - 4.0 * x[0] - 6.0 * x[1]; }Vector2d gfun2(Vector2d x) { return Vector2d(6.0 * x[0] - 4.0, 4.0 * x[1] - 6.0); }int main()
{std::vector<Vector2d> points {Vector2d(0.0, 0.0),Vector2d(2.0, 1.0),Vector2d(1.0, -1.0),Vector2d(-1.0, -1.0),Vector2d(-1.2, 1.0),Vector2d(10.0, 10.0)};SteepestDescent<double, Vector2d> sd(fun, gfun, 1e-4, 1e-5);std::fstream fout_result_1, fout_result_2;fout_result_1.open("ForwardBackwardAndGoldenSectionSearch_Result.csv", std::ios::out | std::ios::trunc);fout_result_2.open("ArmijoSearch_Result.csv", std::ios::out | std::ios::trunc);fout_result_1 << "初始点 ($x_0$) & 目标函数值 ($f(x_k)$) & 迭代次数 ($k$) \\\\"<< "\n";fout_result_1 << "\\midrule"<< "\n";fout_result_2 << "初始点 ($x_0$) & 目标函数值 ($f(x_k)$) & 迭代次数 ($k$) \\\\"<< "\n";fout_result_2 << "\\midrule"<< "\n";for (size_t i = 0; i < points.size(); ++i){auto [x, val, k] = sd.ForwardBackwardAndGoldenSectionSearch(points[i]);fout_result_1 << "$(" << points[i][0] << ", " << points[i][1] << ")^T$ & " << val << " & " << k << " \\\\"<< "\n";auto [x2, val2, k2] = sd.ArmijoSearch(points[i]);fout_result_2 << "$(" << points[i][0] << ", " << points[i][1] << ")^T$ & " << val2 << " & " << k2 << " \\\\"<< "\n";}fout_result_1.close();fout_result_2.close();
}

相关文章:

  • 软件工程 - 第8章 面向对象建模 - 2 静态建模
  • 08_Collection集合2
  • 关于我离破500粉丝感受
  • Vue3中reactive和ref对比
  • 二叉树的基本操作实现包括创建二叉树、插入节点、搜索节点、删除节点、遍历二叉树等详解
  • Python安装步骤介绍
  • 【无标题】心灯
  • 【Redis】Redis缓存使用问题
  • 基于springboot实现的垃圾分类管理系统
  • Linux(13):例行性工作排程
  • 带头双向循环链表:一种高效的数据结构
  • 自动化横行时代,手工测试如何突破重围?测试之路...
  • 第一章 Windows环境下的JDK安装与环境配置
  • 【c】角谷猜想
  • Vue实现可拖拽边界布局
  • JavaScript 如何正确处理 Unicode 编码问题!
  • 【附node操作实例】redis简明入门系列—字符串类型
  • 【译】React性能工程(下) -- 深入研究React性能调试
  • angular组件开发
  • HashMap ConcurrentHashMap
  • isset在php5.6-和php7.0+的一些差异
  • Javascript设计模式学习之Observer(观察者)模式
  • LeetCode18.四数之和 JavaScript
  • Mysql5.6主从复制
  • React组件设计模式(一)
  • SwizzleMethod 黑魔法
  • vue--为什么data属性必须是一个函数
  • 订阅Forge Viewer所有的事件
  • 排序算法之--选择排序
  • 盘点那些不知名却常用的 Git 操作
  • 区块链共识机制优缺点对比都是什么
  • 腾讯大梁:DevOps最后一棒,有效构建海量运营的持续反馈能力
  • 微信开放平台全网发布【失败】的几点排查方法
  • 小程序测试方案初探
  • Mac 上flink的安装与启动
  • 阿里云移动端播放器高级功能介绍
  • $.type 怎么精确判断对象类型的 --(源码学习2)
  • (10)工业界推荐系统-小红书推荐场景及内部实践【排序模型的特征】
  • (Oracle)SQL优化技巧(一):分页查询
  • (ros//EnvironmentVariables)ros环境变量
  • (zz)子曾经曰过:先有司,赦小过,举贤才
  • (分布式缓存)Redis分片集群
  • (附源码)流浪动物保护平台的设计与实现 毕业设计 161154
  • (六) ES6 新特性 —— 迭代器(iterator)
  • (转)Android学习笔记 --- android任务栈和启动模式
  • (转)LINQ之路
  • (转)visual stdio 书签功能介绍
  • (转)总结使用Unity 3D优化游戏运行性能的经验
  • .class文件转换.java_从一个class文件深入理解Java字节码结构
  • .NET CORE 3.1 集成JWT鉴权和授权2
  • .Net Core webapi RestFul 统一接口数据返回格式
  • .net core 微服务_.NET Core 3.0中用 Code-First 方式创建 gRPC 服务与客户端
  • .net 简单实现MD5
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地定义和使用弱事件
  • @AutoConfigurationPackage的使用