【优化求解】基于遗传算法实现优化求解matlab代码

1 简介

函数优化是遗传算法的经典应用领域,也是遗传算法进行性能评价的常用算例.对于一些非线性,多模型,多目标的函数优化问题,用其它优化方法较难求解,而用遗传算法则可以方便地得到较好的结果.用Matlab实现了函数优化的遗传算法源程序.

遗传算法的流程如图 1 所示。其主要组成部分包括:  

​(1)编码方式。遗传算法通常根据问题本身进行编码,并将问题的有效解决方案转化为遗传算法的搜索空间。工业中常用的编码方法包括实数编码,二进制编码,整数编码和数据结构编码。 

(2)适应度函数。适应度函数,也称为目标函数,是对整个个体与其适应度之间的对应关系的描述。具有高适应性的个体中包含的高质量基因具有较高的传递给后代的概率,而具有低适应性的个体的遗传概率较低。  

(3)遗传操作。基本的遗传操作包括:选择、交叉、变异。  

a)选择。选择操作基于个体适应度评估,选择群体中具有较高适应度的个体,并且消除具有较低适应度的个体。当然不同的选择操作也会带来不同的结果,有效的选择操作可以显著的提高搜索的效率和速度,减少无用的计算量。  

常见的选择方法有:基于比例的适应度分配方法,期望值选择方法,基于排名的适应度分配方法,轮盘赌选择方法等。  

b)交叉。在自然界生物进化过程中,两条染色体通过基因重组形成新的染色体,因此交叉操作是遗传算法的核心环节。交叉算子的设计需要根据具体的问题具体分析,编码操作和交叉操作互相辅助,交叉产生的新的个体必须满足染色体的编码规律。父代染色体的优良性状最大程度上的遗传给下一代染色体,在此期间也能能够产生一些较好的性状。  

常见的交叉算子包括实质重组,中间重组,离散重组,线性重组,二进制交叉,单点交叉,均匀交叉,多点交叉和减少代理交叉。  

c) 变异。通过随机选择的方法改变染色体上的遗传基因。变异本身可以被视为随机算法,严格来说,是用于生成新个体的辅助算法。  

几个与浮点数编码和二进制编码个体匹配的交叉运算:单点交叉,均匀交叉,算术交叉,两点交叉和多点交叉。

2 部分代码

function varargout = yichuan(varargin)
% YICHUAN MATLAB code for yichuan.fig
%     YICHUAN, by itself, creates a new YICHUAN or raises the existing
%     singleton*.
%
%     H = YICHUAN returns the handle to a new YICHUAN or the handle to
%     the existing singleton*.
%
%     YICHUAN('CALLBACK',hObject,eventData,handles,...) calls the local
%     function named CALLBACK in YICHUAN.M with the given input arguments.
%
%     YICHUAN('Property','Value',...) creates a new YICHUAN or raises the
%     existing singleton*. Starting from the left, property value pairs are
%     applied to the GUI before yichuan_OpeningFcn gets called. An
%     unrecognized property name or invalid value makes property application
%     stop. All inputs are passed to yichuan_OpeningFcn via varargin.
%
%     *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
%     instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help yichuan

% Last Modified by GUIDE v2.5 15-Jul-2016 21:52:11

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                  'gui_Singleton',  gui_Singleton, ...
                  'gui_OpeningFcn', @yichuan_OpeningFcn, ...
                  'gui_OutputFcn',  @yichuan_OutputFcn, ...
                  'gui_LayoutFcn', [] , ...
                  'gui_Callback',   []);
if nargin && ischar(varargin{1})
   gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
  [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
   gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before yichuan is made visible.
function yichuan_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject   handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)
% varargin   command line arguments to yichuan (see VARARGIN)

% Choose default command line output for yichuan
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes yichuan wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = yichuan_OutputFcn(hObject, eventdata, handles) 
% varargout cell array for returning output args (see VARARGOUT);
% hObject   handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject   handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)
global x
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%绘制题中函数图形%%%%%%%%%%%%%%%%%%%%%%%%%%%
x=linspace(-1,2);
y=10+x.*cos(5*pi*x);

axes(handles.axes1)
plot(x,y,'r')
title('函数曲线图')
xlabel('x')
ylabel('y')


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject   handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)
global BitLength         %全局变量,计算如果满足求解精度至少需要编码的长度 
global boundsbegin       %全局变量,自变量的起始点 
global boundsend         %全局变量,自变量的终止点 
=
end
Generation=Generation-1;%Generation加1、减1的操作是为了能记录各代中的最佳函数值xmax(Generation)
targetfunvalue=targetfun(xmax)  
[Besttargetfunvalue,nmax]=max(targetfunvalue) 
Bestpopulation=xmax(nmax)   
%绘制经过遗传运算后的适应度曲线
axes(handles.axes2)
hand1=plot(1:Generation,ymax);
set(hand1,'linestyle','-','linewidth',1,'marker','*','markersize',8) 
hold on;  

hand2=plot(1:Generation,ymean); 
set(hand2,'color','k','linestyle','-','linewidth',1, 'marker','h','markersize',8) 
xlabel('进化代数');
ylabel('最大和平均适应度');
xlim([1 Generationnmax]);
legend('最大适应度','平均适应度');
box off;
hold off;    


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject   handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)
clearall;  
close all;

3 仿真结果

4 参考文献

[1]宋建萍. "函数优化的遗传算法代码实现." 软件导刊 12.002(2013):40-42.

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Matlab科研辅导帮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值