Fools Modeling-Simulated Annealing Algorithm

This blog is based on the video of teacher Qingfeng (https://www.bilibili.com/video/BV1hK41157JL?t=2141)
. The relevant pictures from teacher Qingfeng’s lesson plan will be used in the blog.


1. Description of the problem:

Let’s take a look at the following questions

Insert picture description here
Insert picture description here
Insert picture description here
Students who have studied dp will find that the last three of the above four problems can basically be solved by dp. Yes, it is possible, but here we look at another approach.

We found that these problems can be transformed into: the problem of finding the maximum value of a certain objective function.In general problems, we are all seeking the minimum value.If the maximum value of the ball is encountered, a negative sign can also be added to find the minimum value.


2. Preliminary thoughts :

Since we are seeking the minimum value of a function, we can traverse the domain of this function. For some math problems in the exam, we often only need to define the element difference of the enumeration to the order of 0.01 to solve the problem, but put In actual problems, the order of magnitude of 0.01 is not accurate enough, but when we increase the accuracy, the complexity of the algorithm also increases, which shows that violent algorithms are not feasible.

Let's first introduce a concept:
heuristic algorithm: in the process of searching for the optimal solution, the information obtained in the original search process is used, and this information will optimize our search process. (The popular point is to use memory for time)

The hill climbing method we introduce below is a heuristic algorithm:
first look at the following function
Insert picture description here

1. Randomly find an initial solution in the solution space

2.According to the position of the initial solution, we can choose to go left or right (the smaller the distance, the better, but it will increase the amount of calculation), if the function value decreases to the left (assuming we are looking for the minimum value), Then we go to the left.

3. Repeat the process of 2 until we find a minimum

The method itself is not difficult, but the method has a big limitation: it is easy to find a local optimal solution.

To solve this problem, we need to introduce a simulated annealing algorithm.


3. Simulated annealing algorithm :

3.1: Introduction to the simulated annealing algorithm:

The simulated annealing algorithm is derived from the principle of solid annealing. It is a probability-based algorithm that heats the solid to a sufficiently high level, and then slowly cools it. When heated, the internal particles of the solid become disordered with the increase in temperature, and the internal energy increases. Large, and the particles gradually become orderly when cooled slowly, reaching an equilibrium state at each temperature, and finally reaching the ground state at room temperature, and the internal energy is reduced to a minimum.

3.2 Similarities and differences with the hill climbing method: The hill climbing method
we proposed above is to facilitate the introduction of simulated annealing. The main disadvantage of the hill climbing method is that it is easy to find the local optimal solution. The fundamental reason for it is that the direction we go is limited by the size of the function value each time. It's too dead, and we don't consider the direction that causes the function value to decrease. The simulated return algorithm is improved on this point.

3.3 concrete realization

Insert picture description here

If we refuse, isn't it the climbing method, then we definitely cannot refuse.
Insert picture description here

In order to determine p, we need to find a function
y=e^(-x) with a range of 0~1, which is a function with a value range of 0~1, and decreases as x increases. Then we can change the function as
Insert picture description here
this is still not the final form, because we are in search early, in order to make a wider range of search, we need the early relatively large p, whereas the latter is relatively small, then p affected by the time we need to function in deformation

Insert picture description here
Ct is a variable affected by time.
Insert picture description here
This formula is derived from some physical knowledge

3.4 code:

Methods used in the code (non-beginners can skip it)

rand(x,y);//产生由随机生成的0~1的数组成的x*y的矩阵

scatter(x,y,'*r')//在x,y处绘制一个*r形式的点

title()//给函数设置标题

num2str()//数字转化成字符串

zeros(x,y);//生成一个x,y型的0矩阵

Total code:


%% 绘制函数的图形
x = -3:0.01:3;
y = 11*sin(x) + 7*cos(5*x);
figure
plot(x,y,'b-')
hold on  % 不关闭图形,继续在上面画图

%% 参数初始化
narvs = 1; % 变量个数
T0 = 100;   % 初始温度
T = T0; % 迭代中温度会发生改变,第一次迭代时温度就是T0
maxgen = 300;  % 最大迭代次数
Lk = 200;  % 每个温度下的迭代次数
alfa = 0.95;  % 温度衰减系数
x_lb = -3; % x的下界
x_ub = 3; % x的上界

%%  随机生成一个初始解
x0 = zeros(1,narvs);
for i = 1: narvs
    x0(i) = x_lb(i) + (x_ub(i)-x_lb(i))*rand(1);    
end
y0 = Obj_fun1(x0); % 计算当前解的函数值
h = scatter(x0,y0,'*r');  % scatter是绘制二维散点图的函数(这里返回h是为了得到图形的句柄,未来我们对其位置进行更新)

%% 定义一些保存中间过程的量,方便输出结果和画图
max_y = y0;     % 初始化找到的最佳的解对应的函数值为y0
MAXY = zeros(maxgen,1); % 记录每一次外层循环结束后找到的max_y (方便画图)

%% 模拟退火过程
for iter = 1 : maxgen  % 外循环, 我这里采用的是指定最大迭代次数
    for i = 1 : Lk  % 内循环,在每个温度下开始迭代
        y = randn(1,narvs);  % 生成1行narvs列的N(0,1)随机数
        z = y / sqrt(sum(y.^2)); % 根据新解的产生规则计算z
        x_new = x0 + z*T; % 根据新解的产生规则计算x_new的值
        %%此生成新解的方法参考matlab模拟退火
        % 如果这个新解的位置超出了定义域,就对其进行调整
        for j = 1: narvs
            if x_new(j) < x_lb(j)
                r = rand(1);
                x_new(j) = r*x_lb(j)+(1-r)*x0(j);
            elseif x_new(j) > x_ub(j)
                r = rand(1);
                x_new(j) = r*x_ub(j)+(1-r)*x0(j);
            end
        end
        x1 = x_new;    % 将调整后的x_new赋值给新解x1
        y1 = Obj_fun1(x1);  % 计算新解的函数值
        if y1 > y0    % 如果新解函数值大于当前解的函数值
            x0 = x1; % 更新当前解为新解
            y0 = y1;
        else
            p = exp(-(y0 - y1)/T); % 根据Metropolis准则计算一个概率
            if rand(1) < p   % 生成一个随机数和这个概率比较,如果该随机数小于这个概率
                x0 = x1; % 更新当前解为新解
                y0 = y1;
            end
        end
        % 判断是否要更新找到的最佳的解
        if y0 > max_y  % 如果当前解更好,则对其进行更新
            max_y = y0;  % 更新最大的y
            best_x = x0;  % 更新找到的最好的x
        end
    end
    MAXY(iter) = max_y; % 保存本轮外循环结束后找到的最大的y
    T = alfa*T;   % 温度下降
    pause(0.01)  % 暂停一段时间(单位:秒)后再接着画图
    h.XData = x0;  % 更新散点图句柄的x轴的数据(此时解的位置在图上发生了变化)
    h.YData = Obj_fun1(x0); % 更新散点图句柄的y轴的数据(此时解的位置在图上发生了变化)
end

disp('最佳的位置是:'); disp(best_x)
disp('此时最优值是:'); disp(max_y)

pause(0.5)
h.XData = [];  h.YData = [];  % 将原来的散点删除
scatter(best_x,max_y,'*r');  % 在最大值处重新标上散点
title(['模拟退火找到的最大值为', num2str(max_y)])  % 加上图的标题

%% 画出每次迭代后找到的最大y的图形
figure
plot(1:maxgen,MAXY,'b-');
xlabel('迭代次数');
ylabel('y的值');
toc

Guess you like

Origin blog.csdn.net/jahup/article/details/108986130