模拟退火算法实例(c++ 与 c# 实现)
此片文章主要参考CSDN博主里头的一篇文章, 将自己的理解写下来,以方便后期的查阅。
一、C++ 实现
1. 已知平面上若干点坐标(xi, yi), 求平面上一点p(x, y) , 到这些点的总距离最小。
思路: 取所有点的均值为目标点。计算全部点与目标点求差值的和,将目标点以一定系数朝着总和的方向移动,得到新的目标点。
// 求最小距离
// 限制条件: 1 <= n <= 100, 0<= xi, yi <= 1e4
#include<iostream>
#include<cstdio>
#include<cdtdlib>
#include<cmatch> using namespace std; struct Pt{
double x, y;
}P[] double sqr(double x) {
return x*x
} // get distance between two point
double dist(Pt a, Pt b)
{
return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y));
} double get_sum(Pt p0, int n)
{
double ret = ;
for(int i = ; i < n; ++i)
ret += dist(p0, p[i]); int main
{
// 设置初始化点数据
// p[n] = { ... ,..., ....}
// 取所有点的平均位置,作为最近点的位置
double x0=, y0 =;
for(int i = ; i < n; ++i)
{
x0 += p[i].x;
y0 += p[i].y;
}
x0 /= n;
y0 /= n; double ans = get_sum((pt){x0, y0}, n); // 当前 目标值
double temp = le5; // 初始化温度, 根据需要设定 while(temp > 0.02) // 0.02 为温度的下限, 若温度为 temp 达到下限, 则停止搜索
{
double x = , y = ;
for(int i = ; i < n; ++i) { // 获取步长的规则根据要求设定
x += (p[i].x - x0) / dist((Pt){x0, y0}, p[i]);
y += (p[i].x - y0) / dist((Pt){x0, y0}, p[i]);
}
// 变化后,新的目标值
// 此处变化的系数应该是逐渐减小的, temp 逐渐减小,符合要求
double tmp = get_sum((Pt){x0 + x * temp, y0 + y * temp}, n); // 进行判断
if(temp < ans)
{
ans = temp;
x0 += x * temp;
y0 += y * temp;
}
// 退火算法的精髓 : 当不满足移动条件时, 也按照一定的概率进行移动
// 注意 : 移动的概率应该逐渐减小
// e n次幂, n 应该小于0
// 假设 random() 的作用 : 产生 0- 1 之间的随机数
else if(Math.exp((ans - temp) / temp) > random())
{
ans = temp;
x0 += x * temp;
y0 += y * temp;
}
temp *= 0.98; // 0.98 为降火速率(范围为0~1, 数字越大,得到的全局最优解概率越高,运行时间越长)
}
printf("The minimal dist is : ");
printf("%.0f\n", ans);
}
二、C# 实现代码
已知空间上若干点(xi, yi, zi), 求空间上包含这些点的最小球半径 R, 以及球心坐标。
思路:球心与这些点的最大距离为半径, 球心与最大距离点生成向量,将球心朝着该向量方向移动若干距离,再计算半径的变化。
namespace Test_BST
{
public class Program
{
static void Main(string[] args)
{
// 初始化输入点
List<Point> originPoints = new List<Point>() { ............};
double radius = AnnealAlgorithm(originPoints);
} private struct Point
{
public double x;
public double y;
public double z;
} // square of a number
private static double Sqr(double x) { return x * x; } // 两点之间的距离
private static double Dist(Point A, Point B)
{
return Math.Sqrt(Sqr(A.x - B.x) + Sqr(A.y - B.y) + Sqr(A.z - B.z));
} // 求最大半径
private static double GetMaxRadius(Point p0, List<Point> pts)
{
double maxRadius = ;
foreach (var point in pts)
{
double radius = Dist(p0, point);
maxRadius = radius > maxRadius ? radius : maxRadius;
} return maxRadius;
} private static double AnnealAlgorithm(List<Point> originPts)
{
Point center = new Point();
center.x = ;
center.y = ;
center.z = ; // 将初始化中心点设置为所有点的代数平均位置
foreach (var pt in originPts)
{
center.x += pt.x;
center.y += pt.y;
center.z += pt.z;
}
center.x /= originPts.Count;
center.y /= originPts.Count;
center.z /= originPts.Count; double temp = 1e3; // 初始温度
double coolingFactor = 0.98; // 降温因子
double ans = GetMaxRadius(center, originPts); // 当前最小半径
var random = new Random(); while (temp > 1e-)
{
Point newCenter = new Point();
double max_r = ;
// 找到与当前中心点距离最远的点,将中心向着改点移动
for (int i = ; i < originPts.Count; i++)
{
double r = Dist(center, originPts[i]);
if (r > max_r)
{
newCenter.x = (originPts[i].x - center.x) / r;
newCenter.y = (originPts[i].y - center.y) / r;
newCenter.z = (originPts[i].z - center.z) / r;
max_r = r;
}
}
newCenter.x = center.x + newCenter.x * temp;
newCenter.y = center.y + newCenter.y * temp;
newCenter.z = center.z + newCenter.z * temp; // 移动后的最大半径
double tmp = GetMaxRadius(newCenter, originPts); if (tmp < ans)
{
center.x += newCenter.x * temp;
center.y += newCenter.y * temp;
center.z += newCenter.z * temp;
}
else if (Math.Exp((ans -tmp)/temp) > random.NextDouble() )
{
center.x += newCenter.x * temp;
center.y += newCenter.y * temp;
center.z += newCenter.z * temp;
} temp *= coolingFactor;
}
double miniRadius = GetMaxRadius(center, originPts);
Console.WriteLine("the cooridnate of the center is {0}, the radius value is {1}", center, miniRadius)); return miniRadius;
}
}
}
参考: http://blog.csdn.net/whai362/article/details/46980471#comments
模拟退火算法实例(c++ 与 c# 实现)的更多相关文章
- 算法实例-C#-快速排序-QuickSort
算法实例 ##排序算法Sort## ### 快速排序QuickSort ### bing搜索结果 http://www.bing.com/knows/search?q=%E5%BF%AB%E9%80% ...
- 模拟退火算法-[HDU1109]
模拟退火算法的原理模拟退火算法来源于固体退火原理,将固体加温至充分高,再让其徐徐冷却,加温时,固体内部粒子随温升变为无序状,内能增大,而徐徐冷却时粒子渐趋有序,在每个温度都达到平衡态,最后在常温时达到 ...
- 【高级算法】模拟退火算法解决3SAT问题(C++实现)
转载请注明出处:http://blog.csdn.net/zhoubin1992/article/details/46453761 ---------------------------------- ...
- 模拟退火算法(SA)求解TSP 问题(C语言实现)
这篇文章是之前写的智能算法(遗传算法(GA).粒子群算法(PSO))的补充.其实代码我老早之前就写完了,今天恰好重新翻到了,就拿出来给大家分享一下,也当是回顾与总结了. 首先介绍一下模拟退火算法(SA ...
- 原创:工作指派问题解决方案---模拟退火算法C实现
本文忽略了对于模拟退火的算法的理论讲解,读者可参考相关的博文或者其他相关资料,本文着重于算法的实现: /************************************************ ...
- BZOJ 3680: 吊打XXX【模拟退火算法裸题学习,爬山算法学习】
3680: 吊打XXX Time Limit: 10 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 3192 Solved: 1198[Sub ...
- Adaboost 算法实例解析
Adaboost 算法实例解析 1 Adaboost的原理 1.1 Adaboost基本介绍 AdaBoost,是英文"Adaptive Boosting"(自适应增强)的缩写,由 ...
- OI骗分神器——模拟退火算法
前言&&为什么要学模拟退火 最近一下子学了一大堆省选算法,所以搞一个愉快一点的东西来让娱乐一下 其实是为了骗到更多的分,然后证明自己的RP. 说实话模拟退火是一个集物理与IT多方面知识 ...
- 模拟退火算法 R语言
0 引言 模拟退火算法是用来解决TSP问题被提出的,用于组合优化. 1 原理 一种通用的概率算法,用来在一个打的搜索空间内寻找命题的最优解.它的原理就是通过迭代更新当前值来得到最优解.模拟退火通常使用 ...
随机推荐
- C#基础知识 简单说明泛型的优点
有关泛型的优缺点在网上有很多篇文章,也足以说明问题,我就不去复制粘贴了(而且内容有些多),由于记性不太好,所以自己做个简单明了的总结. 泛型的优点主要有两个: "性能" " ...
- 关于python的itertools模块
这是一个强大的模块 先来看一下它都有什么工具 无穷循环器 迭代器 参数 结果 ...
- 《修改代码的艺术》【PDF】下载
<修改代码的艺术>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382309 内容简介 <修改代码的艺术>针对大型的. ...
- 微信小程序教学第四章第一节(含视频):小程序中级实战教程:详情-页面制作
详情 - 页面制作 本文配套视频地址: https://v.qq.com/x/page/o0555o20xjd.html 开始前请把 ch4-1 分支中的 code/ 目录导入微信开发工具 这一章节中 ...
- 环境变量配置文件,本地登录前提示信息/etc/issue
让配置文件立即生效:source 配置文件 或 . 配置文件 /etc/profile 例:定义了HISTSIZE=1000 /etc/profile.d/*.sh /etc/bashrc ~/.ba ...
- JavaWeb框架_Struts2_(八)----->Struts2的国际化
这一篇博文拖了蛮久了,现在先把它完成,结束struts2这个版块,当然这只是最基础的部分,做项目还需要更深的理解.下一个web后端的版块准备做Spring框架的学习-嗯,加油! 1. Struts2的 ...
- 【USACO】玉米实验(单调队列)
Description 约翰决定培育新的玉米品种以提高奶牛的产奶效率.约翰所有的土地被分成 N ×N 块,其中第 r行第 c 列的玉米质量为 Ar,c.他打算找一块质量比较均匀的土地开始自己的实验.经 ...
- PHP中public,private,protected,abstract等关键字用法详解
PHP中常用的关键字 在PHP中包含了很多对函数和类进行限制的关键字,常用的通常有abstract,final,interface,public,protected,private,static等等, ...
- 安装MongoDB步骤
1.第一步是从官网下载匹配自己操作系统的安装文件或压缩文件: 2.随便找个文件夹先解压安装文件,然后在C盘根目录建立一个新文件夹命名为mongodb: 3.将打开刚刚安装的文件,将bin文件夹拷贝到C ...
- 第六节 etc/passwd 、etc/shadow 、 useradd 、 groupadd
调优方法原理:禁用atime的修改来节省cpu和内存资源.命令:mount noatime disk 1.配置文件1. /etc/passwd文档结构:由":" 分隔成7个字段&q ...