C++实现坐标轮换法;黄金分割一维搜索;外推内插初始区间(最优化计算)

本文主要是介绍C++实现坐标轮换法;黄金分割一维搜索;外推内插初始区间(最优化计算),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

编程实现坐标轮换法,子问题求解采用外推内插+抛物线逼近 或 外推内插+黄金分割。

外推内插法用来确定初始搜索区间。

黄金分割用来缩小搜索区间,最终取区间中心作为一维搜索结果。

main.cpp

~main.cpp
void run();
void waitAndExit();int main() {run();waitAndExit();
}

 

 

 

imp.cpp

#include<iostream>
using std::cout;
using std::endl;#include<vector>
using std::vector;#include<cassert>#include<string>
using std::string;#include<iomanip>const float cc = 0.01f; //cc for convergence condition
const float cc_gold = 0.01f;
const float golde = 0.618034f;
const int dim = 3;float target(float x1, float x2, float x3) {return 3 * x1 * x1 + 2 * x2 * x2 + 1 * x3 * x3;
}float target(vector<float>x) {return target(x[0], x[1], x[2]);
}float target(vector<float>x, float dimdata, int dim) {x[dim] = dimdata;return target(x);
}float norm2(vector<float>x) {float result = 0.0f;for (int i = 0; i < x.size(); i++) {result += x[i] * x[i];}return sqrtf(result);
}vector<float> substract(vector<float> x, vector<float> y) {//perform r = x -y;vector<float> r;size_t length = x.size();r.resize(length);for (int i = 0; i < length; i++) {r[i] = x[i] - y[i];}return r;
}void printV(vector<float>x, string prefix = "",int indent = 0) {while (indent-->0){cout << "\t";}if (prefix.size() > 0)cout << prefix << " ";for (int i = 0; i < x.size(); i++) {cout << x[i] << " ";}cout << endl;
}void swap(float&x, float&y) {float tmp = x;x = y;y = tmp;
}void run() {cout.setf(std::ios::fixed);cout << std::setprecision(6); //set output formatsvector<float> X0(dim, 0);vector<float> Xn(dim, 0);vector<float> Dx(dim, 1e9f); // to hold Xn+1 -Xnfor (int i = 0; i < dim; i++) X0[i] = i + 1; //given X0, See 8-1, Page 119Xn = X0;// to hold the result from Extrapolation and interpolation method, "left" and "right" to initialize golden split intervalfloat left;	 float right;	float center; int current_dim = 0; //for cyclic coordinate method, update by dimx = (dimx +1 )% totaldimdo {cout << "DIM[" << current_dim+1 << "]" << endl;float step = 1;float factor = 1;float x1 = Xn[current_dim];float x2 = x1 + step * factor++;float x3 = x2 + step * factor++;float xkprevprev = x1;float xkprev = x2;float xk = x3;//extrapolation------------------------------------------------------------------cout << "\textra~ and inter~ method:" << endl;cout << "\tinit " << xkprevprev << " " << xkprev << " " << xk << endl;if (target(Xn, xkprevprev, current_dim) > target(Xn, xkprev, current_dim)) {//do nothing}else if (target(Xn, xkprevprev, current_dim) < target(Xn, xkprev, current_dim)) {step = -1; factor = 1; //reset factorxkprev = xkprevprev + step * factor++;xk = xkprev + step * factor++;}else {//unkonwn error}while (target(Xn, xk, current_dim) < target(Xn, xkprev, current_dim)) {xkprevprev = xkprev;xkprev = xk;xk = xk + step * factor++;cout << "\tproc " << xkprevprev << "\t" << xkprev << "\t" << xk << endl;}//extrapolation------------------------------------------------------------------// interpolation------------------------------------------------------------------float xknext = (xk + xkprev) / 2.f;if (target(Xn, xknext, current_dim) < target(Xn, xkprev, current_dim)) {left = xkprev;center = xknext;right = xk;}else if (target(Xn, xknext, current_dim) > target(Xn, xkprev, current_dim)){left = xkprevprev;center = xkprev;right = xknext;}else {//unkonwn error}if (left > right) swap(left, right);cout << "\tinte " << left << "\t" << center<< "\t" << right << endl;// interpolation------------------------------------------------------------------//golden split------------------------------------------------------------------cout << "\n\t" << "golden split" << endl;while (right - left > cc_gold) {float r_split = left + golde * (right - left);float l_split = left + (1 - golde) * (right - left); //need to modify herecout << "\t" << left << "\t" << l_split << "\t" << r_split << "\t" << right << endl;if (target(Xn, l_split, current_dim) < target(Xn, r_split, current_dim)) {right = r_split;}else{left = l_split;}}float xi = (left + right) / 2.f;  //golden split result//golden split------------------------------------------------------------------vector<float>Xnnext = Xn;Xnnext[current_dim] = xi; //update Xn+1Dx = substract(Xnnext, Xn); // compute DxprintV(Xn, "Xn");printV(Xnnext, "Xn+1");cout << "F(X) = " << target(Xnnext) << "        <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" <<  endl;printV(Dx, "DX");cout << "Norm2DX " << norm2(Dx) << endl << endl << endl;Xn = Xnnext;current_dim = (current_dim + 1) % dim; //switch to next dim } while (norm2(Dx) > cc);printV(Xn, "Final");cout << "F(X) = " << target(Xn) << endl;return;
}void waitAndExit() {cout << "Press Enter to exit." << endl;getchar();return;
}

cslayee#163.com

 

这篇关于C++实现坐标轮换法;黄金分割一维搜索;外推内插初始区间(最优化计算)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/266698

相关文章

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

认识、理解、分类——acm之搜索

普通搜索方法有两种:1、广度优先搜索;2、深度优先搜索; 更多搜索方法: 3、双向广度优先搜索; 4、启发式搜索(包括A*算法等); 搜索通常会用到的知识点:状态压缩(位压缩,利用hash思想压缩)。

hdu1240、hdu1253(三维搜索题)

1、从后往前输入,(x,y,z); 2、从下往上输入,(y , z, x); 3、从左往右输入,(z,x,y); hdu1240代码如下: #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#inc

【C++ Primer Plus习题】13.4

大家好,这里是国中之林! ❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看← 问题: 解答: main.cpp #include <iostream>#include "port.h"int main() {Port p1;Port p2("Abc", "Bcc", 30);std::cout <<

C++包装器

包装器 在 C++ 中,“包装器”通常指的是一种设计模式或编程技巧,用于封装其他代码或对象,使其更易于使用、管理或扩展。包装器的概念在编程中非常普遍,可以用于函数、类、库等多个方面。下面是几个常见的 “包装器” 类型: 1. 函数包装器 函数包装器用于封装一个或多个函数,使其接口更统一或更便于调用。例如,std::function 是一个通用的函数包装器,它可以存储任意可调用对象(函数、函数

C++11第三弹:lambda表达式 | 新的类功能 | 模板的可变参数

🌈个人主页: 南桥几晴秋 🌈C++专栏: 南桥谈C++ 🌈C语言专栏: C语言学习系列 🌈Linux学习专栏: 南桥谈Linux 🌈数据结构学习专栏: 数据结构杂谈 🌈数据库学习专栏: 南桥谈MySQL 🌈Qt学习专栏: 南桥谈Qt 🌈菜鸡代码练习: 练习随想记录 🌈git学习: 南桥谈Git 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈�

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

让树莓派智能语音助手实现定时提醒功能

最初的时候是想直接在rasa 的chatbot上实现,因为rasa本身是带有remindschedule模块的。不过经过一番折腾后,忽然发现,chatbot上实现的定时,语音助手不一定会有响应。因为,我目前语音助手的代码设置了长时间无应答会结束对话,这样一来,chatbot定时提醒的触发就不会被语音助手获悉。那怎么让语音助手也具有定时提醒功能呢? 我最后选择的方法是用threading.Time

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo