首页 > 其他分享 >模拟退火

模拟退火

时间:2023-09-15 09:34:02浏览次数:35  
标签:network Luke Delta step 模拟退火 double

模拟退火

解决问题——数值计算

一般解决步骤

多元方程:

\[\begin {cases} {f_{1L}}(x)={f_{1R}}(x)\\ {f_{2L}}(x)={f_{2R}}(x)\\ \ \ \ \ \ \ \ \ \dots\dots\\ {f_{nL}}(x)={f_{nR}}(x)\\ \end {cases} \]

评价函数:

\[\Huge {\rm{A = }}\sum\limits_{i = 1}^n {} \frac{{{{({f_{iL}}(x) - {f_{iR}}(x))}^2}}}{{{f_{iL}}{{(x)}^2} + {f_{iR}}{{(x)}^2}}} \]

结束标识

当 \(A<\Large\epsilon\),其中\(\epsilon\)是一个极小的数字可以是1e-7左右。

或者

当逼近程序运行次数\(\large n\small> N\)时(\(N\)取一个极大的数字)

原理

模拟退火主要想要解决的是,在一些方程组中无法求得各元的解析解时,可以通过不断逼近的方法求得数值解。但是在使用逼近法计算评价函数的时候,可能会陷入局部最优解的困局,而模拟退火算法就可以以概率P跳出局部最优解,达到最优解。

重要变量

非优接受概率P——解决局部最优解问题

非优接受概率是指,当随机设立的增量(\(\Delta x)\)加上当前状态(\(x\)),即\(x+\Delta x\)带入的评价函数算出的评价值\(A\)变小了,但是不采纳该次x的概率。

即 有 \(x+\Delta x\) 满足较优解时,
则取

\[x=\begin {cases} x\ \ ,\ \ P\small<\normalsize p\\ x+\Delta x\ \ ,\ \ P\small\geq\normalsize1-p\\ \end {cases} \]

其中\(p\)就是非优接受概率,\(P\)为\([0,1]\)内随机数。

步长缩小量——解决逼近程度过小问题

步长缩小量是指增量\(\Delta x(Step)\)的大小变化,\(\Delta x\)的变化可以选取以下两种形式:
1.指数型缩小 \(\Large\rightarrow\) 每次选取的 \(\Delta x_{i+1}=0.9\Delta x_{i}\);
2.分数型缩小 \(\Large\rightarrow\) \(s=\frac{\Large s_{\tiny 0}}{\Large t}\) \(s_0\)是指初始步长(\(\Delta x_0\)),\(t\)为迭代次数(\(n\));

样题

题目——A Star not a Tree?(**模拟退火)

Description

Luke wants to upgrade his home computer network from 10mbs to 100mbs. His existing network uses 10base2 (coaxial) cables that allow you to connect any number of computers together in a linear arrangement. Luke is particulary proud that he solved a nasty NP-complete problem in order to minimize the total cable length.
Unfortunately, Luke cannot use his existing cabling. The 100mbs system uses 100baseT (twisted pair) cables. Each 100baseT cable connects only two devices: either two network cards or a network card and a hub. (A hub is an electronic device that interconnects several cables.) Luke has a choice: He can buy 2N-2 network cards and connect his N computers together by inserting one or more cards into each computer and connecting them all together. Or he can buy N network cards and a hub and connect each of his N computers to the hub. The first approach would require that Luke configure his operating system to forward network traffic. However, with the installation of Winux 2007.2, Luke discovered that network forwarding no longer worked. He couldn't figure out how to re-enable forwarding, and he had never heard of Prim or Kruskal, so he settled on the second approach: N network cards and a hub.
Luke lives in a loft and so is prepared to run the cables and place the hub anywhere. But he won't move his computers. He wants to minimize the total length of cable he must buy.

输入

The first line of input contains a positive integer N <= 100, the number of computers. N lines follow; each gives the (x,y) coordinates (in mm.) of a computer within the room. All coordinates are integers between 0 and 10,000.

输出

Output consists of one number, the total length of the cable segments, rounded to the nearest mm.

Sample Input Sample Output
4 28284
0 0
0 10000
10000 10000
10000 0

样码

点击查看代码
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

const double PI = acos(-1.0);
int n;
double x[110], y[110];

double dist(double cx, double cy) {//目标函数
	double r = 0.0;
	for (int i = 0; i < n; i++)
		r += sqrt((cx-x[i]) * (cx-x[i]) + (cy-y[i]) * (cy-y[i]));
	return r;
}

int main() {
	double tx, ty, r, dx, dy, step, s0, P;
	double a, b, c, d;
	int t = 0;
	
	scanf("%d", &n);
	a = c = 1e9;
	b = d = 0;
	for (int i = 0; i < n; i++) {	// 输入, 同时确定包围盒[a,b]*[c,d] 
		scanf("%lf%lf", &x[i], &y[i]);
		if (x[i] < a)
			a = x[i];
		if (x[i] > b)
			b = x[i];
		if (y[i] < c)
			c = y[i];
		if (y[i] > d)
			d = y[i];
	}
	srand(100);
	s0 = (b - a > d - c ? b - a : d - c);
	step = s0;
	tx = (a + b) / 2;
	ty = (c + d) / 2;
	r = dist(tx, ty);
	while (step > 1e-3 && t < 1000) {
		double theta = (rand() / 32768.0) * PI * 2;	// 选随机方向(角度) 
		dx = step * cos(theta);
		dy = step * sin(theta);
		double r1 = dist(tx + dx, ty + dy);//位移向量
		P = 0.05;	// 非优接受概率0.02~0.07 
		if (r1 < r || (rand() / 32768.0) < P) {
			r = r1;
			tx += dx, ty += dy;
		}
		step *= 0.95;				// 指数型缩小 
//		step = s0 / (1 + t);		// 倒数型缩小 
		t++;
	}
	printf("%d\n", (int)(r + 0.5));
	return 0;
}
//rand()/32768.0—[0,1]随机浮点数

标签:network,Luke,Delta,step,模拟退火,double
From: https://www.cnblogs.com/wzu-hqj/p/17679758.html

相关文章

  • 「学习笔记」浅入模拟退火
    什么是退火?来自百度百科退火是一种金属热处理工艺,指的是将金属缓慢加热到一定温度,保持足够时间,然后以适宜速度冷却。目的是降低硬度,改善切削加工性;降低残余应力,稳定尺寸,减少变形与裂纹倾向;细化晶粒,调整组织,消除组织缺陷。准确的说,退火是一种对材料的热处理工艺,包括金属材料、非......
  • 模拟退火算法代码
    当参加数学建模竞赛时,模拟退火算法是一个常用的解题方法之一。以下是一个简单的模拟退火算法的代码示例,用于解决旅行商问题(TSP):点击查看代码importmathimportrandomdefdistance(point1,point2):#计算两个点之间的欧几里德距离returnmath.sqrt((point1[0]-poi......
  • 堆优化模拟退火(List-Based Simulated Annealing|List-Based SA|LBSA|模拟退火) 算法
    堆优化模拟退火(List-BasedSimulatedAnnealing)算法引入堆优化模拟退火(List-BasedSimulatedAnnealing,简称LBSA)是一种对模拟退火的优化算法。由Shi-huaZhan,[1],[2]JuanLin,[1:1]Ze-junZhang,[1:2]Yi-wenZhong[1:3],[2:1]提出。(以下我们以求最小值为例)解释我们......
  • 模拟退火
    模拟退火(SimulateAnneal)是一种用于解决问题方案数极大且非单峰函数的随机化算法,原理与金属退火类似。每次随机出一个新解,若新解更优则接受,否则以一个与温度和与最优解的差相关的概率接受它。降温模拟退火有三个参数:初始温度\(T_0\),降温系数\(\Delta\),终止温度\(T_k\).其中......
  • 模拟退火
    引入模拟退火是一种随机化算法,当一个问题方案数量极大且非单峰时,我们常使用模拟退火过程先引入几个参数:当前最优解\(E_0\),新解\(E\),解变动量\(\DeltaE\)(即\(E\)与\(E_0\)的差),上一个被接受的解\(E_1\),初温\(T_0\),末温\(T_k\),当前温度\(T\),温度变动量\(\Delt......
  • 优化算法——模拟退火算法
    模拟退火算法原理爬山法是一种贪婪的方法,对于一个优化问题,其大致图像(图像地址)如下图所示:其目标是要找到函数的最大值,若初始化时,初始点的位置在处,则会寻找到附近的局部最大值点处,由于点出是一个局部最大值点,故对于爬山法来讲,该算法无法跳出局部最大值点。若初始点选择在处,根据爬......
  • 基于模拟退火优化算法的三维装箱优化matlab仿真,优化重量利用率和空间利用率
    1.算法仿真效果matlab2022a仿真结果如下:2.算法涉及理论知识概要模拟退火算法来源于固体退火原理,是一种基于概率的算法,将固体加温至充分高,再让其徐徐冷却,加温时,固体内部粒子随温升变为无序状,内能增大,而徐徐冷却时粒子渐趋有序,在每个温度都达到平衡态,最后在常温时达到基态,内能减......
  • 基于模拟退火优化算法的三维装箱优化matlab仿真,优化重量利用率和空间利用率
    1.算法仿真效果matlab2022a仿真结果如下:     2.算法涉及理论知识概要      模拟退火算法来源于固体退火原理,是一种基于概率的算法,将固体加温至充分高,再让其徐徐冷却,加温时,固体内部粒子随温升变为无序状,内能增大,而徐徐冷却时粒子渐趋有序,在每个温度都达到平......
  • 模拟退火
    模拟退火模拟退火是一种随机化算法,当一个问题的方案数极大(甚至是无穷的)而且不是一个单峰函数的时候,我们可以考虑用模拟退火来解决,当然这只能给我们骗更多的分,想通过的话有一定的难度。优点根据爬山算法的过程,我们发现,爬山算法只能看到当前的最优解,而如果后面又有更优的解,爬山算......
  • 基于SA模拟退火优化的TWVRP路径规划matlab仿真
    1.算法仿真效果matlab2022a仿真结果如下:2.算法涉及理论知识概要模拟退火算法(simulatedannealing,SAA)来源于固体退火原理,是一种基于概率的算法。模拟退火算法来源于固体退火原理,是一种基于概率的算法,将固体加温至充分高,再让其徐徐冷却,加温时,固体内部粒子随温升变为无序状,内能增......