此片文章主要参考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# 实现)的更多相关文章

  1. 算法实例-C#-快速排序-QuickSort

    算法实例 ##排序算法Sort## ### 快速排序QuickSort ### bing搜索结果 http://www.bing.com/knows/search?q=%E5%BF%AB%E9%80% ...

  2. 模拟退火算法-[HDU1109]

    模拟退火算法的原理模拟退火算法来源于固体退火原理,将固体加温至充分高,再让其徐徐冷却,加温时,固体内部粒子随温升变为无序状,内能增大,而徐徐冷却时粒子渐趋有序,在每个温度都达到平衡态,最后在常温时达到 ...

  3. 【高级算法】模拟退火算法解决3SAT问题(C++实现)

    转载请注明出处:http://blog.csdn.net/zhoubin1992/article/details/46453761 ---------------------------------- ...

  4. 模拟退火算法(SA)求解TSP 问题(C语言实现)

    这篇文章是之前写的智能算法(遗传算法(GA).粒子群算法(PSO))的补充.其实代码我老早之前就写完了,今天恰好重新翻到了,就拿出来给大家分享一下,也当是回顾与总结了. 首先介绍一下模拟退火算法(SA ...

  5. 原创:工作指派问题解决方案---模拟退火算法C实现

    本文忽略了对于模拟退火的算法的理论讲解,读者可参考相关的博文或者其他相关资料,本文着重于算法的实现: /************************************************ ...

  6. BZOJ 3680: 吊打XXX【模拟退火算法裸题学习,爬山算法学习】

    3680: 吊打XXX Time Limit: 10 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 3192  Solved: 1198[Sub ...

  7. Adaboost 算法实例解析

    Adaboost 算法实例解析 1 Adaboost的原理 1.1 Adaboost基本介绍 AdaBoost,是英文"Adaptive Boosting"(自适应增强)的缩写,由 ...

  8. OI骗分神器——模拟退火算法

    前言&&为什么要学模拟退火 最近一下子学了一大堆省选算法,所以搞一个愉快一点的东西来让娱乐一下 其实是为了骗到更多的分,然后证明自己的RP. 说实话模拟退火是一个集物理与IT多方面知识 ...

  9. 模拟退火算法 R语言

    0 引言 模拟退火算法是用来解决TSP问题被提出的,用于组合优化. 1 原理 一种通用的概率算法,用来在一个打的搜索空间内寻找命题的最优解.它的原理就是通过迭代更新当前值来得到最优解.模拟退火通常使用 ...

随机推荐

  1. 以流方式读写文件:文件菜单打开一个文件,文件内容显示在RichTexBox中,执行复制、剪切、粘贴后,通过文件菜单可以保存修改后的文件。

    MainWindow.xaml文件 <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&q ...

  2. JAVA入门[19]-Hibernate简单示例

    一.Hibernate简介 在很多场景下,我们不需要使用JdbcTemplate直接操作SQL语句,这时候可以用ORM工具来节省数大量的的代码和开发时间.ORM工具能够把注意力从容易出错的SQL代码转 ...

  3. How to use GitHub with Android Studio

    转载于:http://www.wfuyu.com/technology/22499.html 目前业界主流的版本管理工具主要是 svn/git.svn是1种集中式的代码管理工具,而git是1种散布式的 ...

  4. Targets选项下Other linker flags的设置

    -ObjC:加了这个参数后,链接器就会把静态库中所有的Objective-C类和分类都加载到最后的可执行文件中 -all_load:会让链接器把所有找到的目标文件都加载到可执行文件中,但是千万不要随便 ...

  5. Python学习日记:day2

    1.格式化输出 name = input("请输入你的名字:") age =input("请输入你的年龄:") job =input("请输入你的工作 ...

  6. 669. Trim a Binary Search Tree

      Given a binary search tree and the lowest and highest boundaries as `L`and `R`, trim the tree so t ...

  7. ArcGIS 网络分析[8.6] 资料6 创建网络分析图层及进行路径分析

    基于上篇所介绍的内容,就说说如何利用访问到的网络数据集,在Map中添加网络数据集图层.创建网络分析图层中的路径图层,并执行路径分析示例.

  8. lesson - 9 Linux系统软件包管理

    1. rpm工具rpm Redhat Package Manager, 设计理念是开放的,不仅仅是在RedHat平台上,在SUSE上也是可以使用的. rpm包名字构成由-和.分成了若干部分,如abrt ...

  9. alias 命令详解

    alias 命令 作用:  设置命令别名,可以将较长的命令进行简化,使用alias 时,用户必须使用单引号将原来的命令引起来,防止特殊字符导致错误. 如要永久生效则将alias 命令存放到bash 的 ...

  10. redis咋么实现分布式锁,redis分布式锁的实现方式,redis做分布式锁 积极正义的少年

    前言 分布式锁一般有三种实现方式:1. 数据库乐观锁:2. 基于Redis的分布式锁:3. 基于ZooKeeper的分布式锁.本篇博客将介绍第二种方式,基于Redis实现分布式锁.虽然网上已经有各种介 ...