模拟退火算法实例(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 原理 一种通用的概率算法,用来在一个打的搜索空间内寻找命题的最优解.它的原理就是通过迭代更新当前值来得到最优解.模拟退火通常使用 ...
随机推荐
- hdu 5078 Osu! (2014 acm 亚洲区域赛鞍山 I)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5078 Osu! Time Limit: 2000/1000 MS (Java/Others) ...
- 【SqlServer系列】语法定义符号解析
1 概述 在数据库函数定义中,经常用到这些符号:<>,::=,[],{},|,..,(),!! .这篇文章简要概述这些符号. 2 具体内容 2.1 <> < ...
- 如何在Gulp中提高Browserify的打包速度
使用Browserify打包js时如果项目变得越来越大,编译时间就会相应变得越来越长.使用官方的插件watchify是个比较有效的提高速度方案. 提速原理 watchify的用法和gulp的watch ...
- Elasticsearch短语搜索——match_phrase
找出一个属性中的独立单词是没有问题的,但有时候想要精确匹配一系列单词或者短语 . 比如, 我们想执行这样一个查询,仅匹配同时包含 "rock" 和 "climbing&q ...
- 自学Zabbix3.5.3-监控项item-key
1. 温习 Zabbix server是Zabbix软件的中心进程. Server执行polling和trapping来采集数据,评估是否触发触发器,发送报警给用户.它是Zabbix agent和pr ...
- IdentityServer(12)- 使用 ASP.NET Core Identity
IdentityServer具有非常好的扩展性,其中用户及其数据(包括密码)部分你可以使用任何想要的数据库进行持久化. 如果需要一个新的用户数据库,那么ASP.NET Core Identity是你的 ...
- C#winform向Txt文件传值,不重复录入且不清空
private void textLog_TextChanged(object sender, EventArgs e) { FileStream fs = new FileStream(@" ...
- Netty学习笔记(一):接收nodejs模拟表单上传的文件
好久不写博客了,也好久不写代码了,这两天临时遇上一个事情,觉得不难,加上觉得手有些生,就动手做了一下,结果遇上了不少坑,有新坑,有老坑,痛苦无比,现在总算差不多了,赶紧记录下来,希望以后不再重复这种痛 ...
- 《算法 (第4版)》【PDF】下载
<算法 (第4版)>[PDF]下载链接: https://u253469.ctfile.com/fs/253469-231196349 (第4版)>[PDF]" TITL ...
- EditText禁用系统键盘,光标可以继续使用
在项目中有时候需要使用到自己的键盘,那这个时候就不希望系统键盘在弹出,而且光标还要继续显示,其实一个方法就可以简单实现 /** * 禁止Edittext弹出软件盘,光标依然正常显示. */ publi ...