基于遗传算法和模拟退火算法改进的混合模拟退火算法(解决求函数极值问题,MATLAB代码已实现)_改进模拟退火算法-程序员宅基地

技术标签: 智能优化方法  混合模拟退火  函数优化  

基本思想:

混合模拟退火算法时遗传算法和模拟退火算法的结合,在混合模拟退火算法中使用了大量的样本作为问题的可能解决方案而不是将单个样本作为一个问题的可能解决方案。对遗传算法中适应的概念进行相应改进。

混合模拟退火的算法步骤如下:

1)将系统温度T设置为足够高的值。

2)随机的初始化人口。

3)人口随机初始化从现有种群中重复生成每个新种群,直到系统温度T达到一个令人满意的最小值。

1)执行N/2次;

2)从N个人口中随机选择两个父母;

3执行交叉操作产生两个后代,然后进行变异操作

4)在子女和父母之间进行玻尔兹曼试验;

5玻尔兹曼实验获胜者复写父母代染色体

6)周期性地降低系统温度T

4得出最优解

其中涉及到的相关改进操作如下:

交叉:
1.如果选择的父母要进行交叉,在他们的染色体上的一个随机点进行单点交叉产生两个孩子,然后转到变异操作。
2.如果父母不在交叉概率范围内,父母将进行比较。更好的父进程会覆盖另一个父进程。不进行变异操作。

变异:
1.生成的每个子节点将执行步骤24
2.随机选择邻居作比较,一个后代的n个邻居的扰动为N*Pn
3.比较扰动状态下的解,如果邻居比孩子好,就把孩子换掉。
4.基于变异操作重新复写子代染色体

玻尔兹曼试验:
1.用抛硬币的方式选择一个或两个接受/拒绝。
2.如果选择双重接受/拒绝,则取Ei = Eparent1 + Eparent2;Ej = Echild1 + Echild2 
3.
如果选择单一的接受/拒绝,取Ei = Eparent;Ej = Echild。这样做是为了测试每个孩子的父母。
4.最后用实验的获胜的来复写父代染色体

clear;
tic;
%%%%%%%%%%%%%%%%%%%%%%%%%%%INPUT ARGUMENTS%%%%%%%Sir Joel, dito po%%%%%%%%%
CostF = 2; % | 1 - DE JONGS | 2 - AXIS PARALLEL HYPER-ELLIPSOID | 3 - ROTATED HYPER-ELLIPSOID | 4 - RASTRIGINS | ow - ACKLEYS |
nVar = 3;
VarSize = zeros(nVar);
VarMin = -5.12; %upper bound of variable value
VarMax = 5.12; %lower bound of variable value
MaxIt = 100000;
T0 = 100000;
Tf = 0.0000000000000001;
alpha = 0.7;
nPop = 1000;
nMove = 50;
mu = 0.05;
sigma = 0.9;
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%Initialization Section%%%%%%%%%%%%%%%%%%%%%%%%%%%
initial_T = T0;                       %initial temperature of the system
cooling_stop = Tf;      %cooling stops when it reaches this temperature
test_func = CostF;  %sets the number of w/c test function to be solved
popsize = nPop;                         %Population Size
pc = sigma;                               %crossover rate
pm = mu;                               %mutation rate
cooling_ratio = alpha;      %sets the cooling ratio to 0.8  i.e. 0.7 < 0.8 < 0.9
ub = VarMax;
lb = VarMin;
ulb = ub;        %upper and lower bound
tpl = nVar;        %dimensions
num_neigh = nMove;                 %number of neighbors to consider
iteration_array = zeros(1);
fittest_array = zeros(1);
solution_array = VarSize;
%%
%%%%%%%%%%%%%%%%%%%%%%%%%HYBRID SIMULATED ANNEALING%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%I
cooling_sched = zeros(1);               %pre-allocation for speed
cooling_sched(1) = initial_T;                 %initializes the cooling schedule T0
%II
Chromosome = zeros(popsize,tpl);
for i=1:popsize
    Chromosome(i,:) = 2*ulb*(rand(1,tpl)-0.5);  %initializing first generation
end
%%
sched = 1;                              %index / iteration
while cooling_sched(sched) > cooling_stop     %iteration will stop if the cooling temperature reached less than 0.00000001 迭代终止条件
    T = cooling_sched(sched);               %设置温度T的值
    %执行N/2次
    for j=1:popsize/2
        %III.a.i. Select two parents at random随机选择两个父母
        red = abs(floor(popsize*rand(1))) + 1;  %two random chromosomes to compete
        blu = abs(floor(popsize*rand(1))) + 1;  %red and blu hold the index of the parents
        %III.a.ii. Generate two offsprings产生两个后代
        %%Recombination Operator (CROSSOVER)重新结合,交叉
        pc_trial = rand(1);
        if pc_trial < pc     %if trial made it in the crossover rate是否实验在交叉率下进行
            cp = floor(abs((tpl-1)*rand(1)))+1;   %random crossover point随机交叉点
            Child_Chromosome(1,:) = CROSSOVER(Chromosome(red,:),Chromosome(blu,:),cp,tpl);     %crossover red and blu两个父母进行交叉
            Child_Chromosome(2,:) = CROSSOVER(Chromosome(blu,:),Chromosome(red,:),cp,tpl);     %they will have two children产生两个孩子
            %%Neighborhood Operator (MUTATION)变异操作
            for k=1:2
                x_sol = Child_Chromosome(k,:);               
                for i=1:num_neigh
                    adrs = abs(floor(popsize*rand(1))) + 1; %获取人口中某个邻居的随机地址
                    x_tmp = Chromosome(adrs,:);    %随机选择额一个邻居做比较. with a decreasing amount of randomness
                    if OBJFUNC(x_tmp,tpl,test_func) < OBJFUNC(x_sol,tpl,test_func)  %比较之后选择好的
                        x_sol = x_tmp;
                    elseif OBJFUNC(x_tmp,tpl,test_func) > OBJFUNC(x_sol,tpl,test_func)  %if not, change the solution if it is lucky
                        delta = OBJFUNC(x_tmp,tpl,test_func) - OBJFUNC(x_sol,tpl,test_func);
                        p = P(delta,T);
                        q = rand(1);
                        if q <= p
                            x_sol = x_tmp; 
                        end
                    end
                end
                Child_Chromosome(k,:) = x_sol;           %基于变异操作重新复写子代染色体
            end
            %%III.a.iii. Boltzman Trials进行玻尔兹曼实验
            ARpossibility = rand(1);                    % <0.5 - Single Acceptance/Rejection | >=0.5 - Double Acceptance/Rejection
            if ARpossibility < 0.5 %%Case 1: Double Acceptance/Rejection
                E1 = OBJFUNC(Chromosome(red,:),tpl,test_func) + OBJFUNC(Chromosome(blu,:),tpl,test_func);
                E2 = OBJFUNC(Child_Chromosome(1,:),tpl,test_func) + OBJFUNC(Child_Chromosome(2,:),tpl,test_func);
                bp = BOLTZMAN(E1,E2,T);
                bp_trial = rand(1);
                if bp_trial >= bp
                    %%III.a.iv. Overwrite Parents with the Trial Winner
                    Chromosome(red,:) = Child_Chromosome(1,:);
                    Chromosome(blu,:) = Child_Chromosome(2,:);
                end
            else %%Case 2: Single Acceptance/Rejection
                E1 = OBJFUNC(Chromosome(red,:),tpl,test_func);
                E2 = OBJFUNC(Child_Chromosome(1,:),tpl,test_func);
                bp = BOLTZMAN(E1,E2,T);
                bp_trial = rand(1);
                if bp_trial >= bp
                    %%III.a.iv. Overwrite Parents with the Trial Winner
                    Chromosome(red,:) = Child_Chromosome(1,:);  %offsprings wins the trial
                end

                E1 = OBJFUNC(Chromosome(red,:),tpl,test_func);
                E2 = OBJFUNC(Child_Chromosome(2,:),tpl,test_func);
                bp = BOLTZMAN(E1,E2,T);
                bp_trial = rand(1);
                if bp_trial >= bp
                    %%III.a.iv. Overwrite Parents with the Trial Winner
                    Chromosome(red,:) = Child_Chromosome(2,:);  %offsprings wins the trial
                end

                E1 = OBJFUNC(Chromosome(blu,:),tpl,test_func);
                E2 = OBJFUNC(Child_Chromosome(1,:),tpl,test_func);
                bp = BOLTZMAN(E1,E2,T);
                bp_trial = rand(1);
                if bp_trial >= bp
                    %%III.a.iv. Overwrite Parents with the Trial Winner
                    Chromosome(blu,:) = Child_Chromosome(1,:);  %offsprings wins the trial
                end

                E1 = OBJFUNC(Chromosome(blu,:),tpl,test_func);
                E2 = OBJFUNC(Child_Chromosome(2,:),tpl,test_func);
                bp = BOLTZMAN(E1,E2,T);
                bp_trial = rand(1);
                if bp_trial >= bp
                    %%III.a.iv. Overwrite Parents with the Trial Winner
                    Chromosome(blu,:) = Child_Chromosome(2,:);  %offsprings wins the trial
                end
            end
            %%
        else                    %if the whole trial did not make it inside the crossover rate, it will have a tournament
            if OBJFUNC(Chromosome(red,:),tpl,test_func) > OBJFUNC(Chromosome(blu,:),tpl,test_func)    %competition
                Chromosome(red,:) = Chromosome(blu,:);      %Blue Wins the tournament and overwrites Red
            else
                Chromosome(blu,:) = Chromosome(red,:);      %Red Wins the tournament and overwrites Blue
            end
        end
    end
    %III.b. Periodically Lower T周期性的降低温度T
    %%
    %Post-Iteration-Evaluation
    F_obj = zeros(1);
    for i=1:popsize
       F_obj(i) = OBJFUNC(Chromosome(i,:),tpl,test_func);
    end
    %%
    fittest = F_obj(1);
    for i=1:popsize
        fi = 1;
        if fittest > F_obj(i)
           fittest = F_obj(i);
           fi = i;
        end
    end
    %%
    fittest_array(sched) = fittest;
    iteration_array(sched) = sched;
    solution_array(sched,:) = Chromosome(fi,:);

    cooling_sched(sched+1) = T*(cooling_ratio)^sched;
    sched = sched+1;
    if sched > MaxIt
        break;
    end
end
%%
%SOLUTION
if test_func == 1
fprintf('====================DE JONGS FUNCTION=============================\n');
elseif test_func == 2
fprintf('==============AXIS PARALLEL HYPER-ELLIPSOID FUNCTION==============\n');
elseif test_func == 3
fprintf('===============ROTATED HYPER-ELLIPSOID FUNCTION====================\n');
elseif test_func == 4
fprintf('====================RASTRIGINS FUNCTION===========================\n');
else
fprintf('=====================ACKLEYS FUNCTION=============================\n');
end
fprintf('==================HYBRID SIMULATED ANNEALING=======================\n');
fprintf('Population Size: %d\tCrossover Rate: %.2f\tMutation Rate: %.2f\tCooling Ratio: %.1f\n',popsize,pc,pm,cooling_ratio);
fprintf('Minimum Energy:\t\t\t\t\t\t\t\t\t%.16f\nTotal Runtime:\t\t\t\t\t\t\t\t\t%f seconds\nFinal Temperature:\t\t\t\t\t\t\t\t%.16f\n',fittest,toc,cooling_sched(sched));
fprintf('Global Minimum is at:\t\t\t\t\t\t');
disp(Chromosome(fi,:))
fprintf('===================================================================\n');
%%
figure
subplot(2,1,1);
plot(iteration_array,fittest_array);
legend('Cost Function Value');
xlabel('Generation');
ylabel('Fitness of fittest Chromosome');

solution_array = transpose(solution_array);
subplot(2,1,2);
plot(iteration_array,solution_array);
xlabel('Generation');
ylabel('Solution of fittest Chromosome');
%%

对于一个基准测试函数所做实验结果如下:

参数设置:

人口数:1000  交叉率:0.9 变异率:0.05  冷却比率:0.7

最小能量值:0.0149937292529736

邻居数:50

最大迭代次数:100000

初试温度:T0 = 100000

最终温度:Tf = 0.000000000000001;

温度下降率:alpha = 0.7;

目标函数值:0.0000005086849880

运行时间:1.743826 s

最终冷却温度:0.0000000000000000

全局最小值(-0.0444   -0.1056    0.0432)

鉴于很多同学问相关完整的代码实现,我把完整的代码和大家分享,希望能给大家帮助啦~

项目完整代码

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/xs1997/article/details/103357654

智能推荐

Chrome 扩展程序——Imagus:图片放大预览工具-程序员宅基地

文章浏览阅读3.3w次,点赞2次,收藏2次。主要介绍 Imagus 的功能及应用,Imagus 是一款简单实用的图片放大预览工具。_imagus

python 描述符_Python黑魔法之描述符-程序员宅基地

文章浏览阅读113次。引言Descriptors(描述符)是Python语言中一个深奥但很重要的一个黑魔法,它被广泛应用于Python语言的内核,熟练掌握描述符将会为Python程序员的工具箱添加一个额外的技巧。本文我将讲述描述符的定义以及一些常见的场景,并且在文末会补充一下__getattr__,__getattribute__,__getitem__这三个同样涉及到属性访问的魔术方法。描述符的定义descr__g..._python revealaccess

jenkins自动化部署及三种构建部署方式_jenkins自动和手动部署-程序员宅基地

文章浏览阅读504次。jenkins自动化部署及三种构建部署方式jenkins是基于java开发的一种持续集成工具,用于监控持续重复的工作,功能包括。1、持续的软件版本发布/测试2、监控外部调用执行项目Jenkins其实很早之前就有了,最近火起来的原因是,大家都在关注devops,关注如何来做持续集成,持续交付,如何来做CI/CD。Jenkins作为持续集成的工具,他其实只是一个平台或者是一个大的框架,它的工作完全就是依靠插件,也就是说你想使用什么功能,你就找到什么样的插件。1.2.jenkins好处1、我在工作中部_jenkins自动和手动部署

GMSK调制解调误码率matlab仿真_gmsk码间干扰-程序员宅基地

文章浏览阅读1.6k次,点赞25次,收藏35次。GMSK(高斯最小频移键控)是一种连续相位的频移键控(CPFSK)调制方法,它在数字通信中得到了广泛应用,特别是在移动通信系统中。GMSK通过限制频率偏差的累积来减少带外辐射,并通过使用高斯滤波器对基带信号进行预调制来平滑相位路径。_gmsk码间干扰

Reader类和Writer类_sreader_writer_uoml-程序员宅基地

文章浏览阅读3k次。从键盘读入用户的输入,并显示在屏幕上1. 效果图2. Java代码package com.example.demo.file;import java.io.InputStreamReader;import java.io.OutputStreamWriter;/** * @Description Reader类和Writer类 * @author 大都督 * @date 2..._sreader_writer_uoml

python编码问题之encode、decode、codecs模块_python中encode在什么模块-程序员宅基地

文章浏览阅读2.1k次。原文链接先说说编解码问题编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(encode)成另一种编码。 Eg:str1.decode('gb2312') #将gb2312编码的字符串转换成unicode编码str2.encode('gb2312') #将unicode编码..._python中encode在什么模块

随便推点

extjs中treepanel属性和方法_ext.tree.treepanel 展开节点适应宽度-程序员宅基地

文章浏览阅读363次。树控件由Ext.tree.TreePanel类定义,TreePanel类继承自Panel面板。TreePanel是ExtJS中最多能的组件之一,它非常适合用于展示分层的数据。树的使用是很频繁的,对树节点的各种操作已经和数据库的互动操作,这些都是需要掌握的。_ext.tree.treepanel 展开节点适应宽度

ORACLE常用傻瓜问题1000问-程序员宅基地

文章浏览阅读678次。1. oracle安装完成后的初始口令?  internal/oracle   sys/change_on_install   system/manager   scott/tiger   sysman/oem_temp 2. orACLE9IAS WEB CACHE的初始默认用户和密码? administrator/administrator 3. oracle 8.0.5怎么创建数据库?

iOS UISearchBar改变搜索框的颜色_ios searchbar 设置搜索狂颜色-程序员宅基地

文章浏览阅读2.1k次。[objc] view plain copy //搜索框 - (UISearchBar *)searchBar{ if (_searchBar == nil) { _searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 27, KScreenWidth_ios searchbar 设置搜索狂颜色

ADADELTA AN ADAPTIVE LEARNING RATE METHOD_adadelta一种自适应学习率方法-程序员宅基地

文章浏览阅读527次。ADADELTA: AN ADAPTIVE LEARNING RATE METHOD参考:[自适应学习率调整AdaDelta](https://www.cnblogs.com/neopenx/p/4768388.html)我们提出了一种新的梯度下降的逐维学习率方法ADADELTA。该方法仅使用一阶信息随时间动态地适应,并且除了一般的随机梯度下降外,具有最小的计算开销。该方法不需要人工调整学习速率,对噪声梯度信息、不同模型结构选择、不同数据模式和超参数选择具有鲁棒性。与其他方法相比,我们在分布式集群环境下_adadelta一种自适应学习率方法

从零开始一个微信小程序版知乎_微信小程序开发 知乎-程序员宅基地

文章浏览阅读1.5k次。以前工作没直接进行过小程序的开发,最近闲了下来就赶紧学习一下。因为从零开始,所以没有使用任何框架及UI库,记录一下本次开发中踩过的坑吧~展示效果(界面样式设计与交互来自iOS 4.8.0版本知乎App):动态效果请移步到GitHub查看。一、开始前的准备申请账号:根据小程序注册文档,填写信息和提交相应的资料,就可以拥有自己的小程序帐号。开发工具:微信开发者工具数据来源:Easy M..._微信小程序开发 知乎

centos uwsgi配置_在CentOS 7下安装uwsgi_uwsgi离线安装centos-程序员宅基地

文章浏览阅读153次。https://blog.csdn.net/weixin_35886269/article/details/112941217_uwsgi离线安装centos

推荐文章

热门文章

相关标签