function varargout = ig2(varargin)
% IG2 M-file for ig2.fig
% IG2, by itself, creates a new IG2 or raises the existing
% singleton*.
%
% H = IG2 returns the handle to a new IG2 or the handle to
% the existing singleton*.
%
% IG2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in IG2.M with the given input arguments.
%
% IG2('Property','Value',...) creates a new IG2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before ig2_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to ig2_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 ig2
% Last Modified by GUIDE v2.5 07-Jul-2015 21:51:29
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ig2_OpeningFcn, ...
'gui_OutputFcn', @ig2_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 ig2 is made visible.
function ig2_OpeningFcn(hObject, eventdata, handles, varargin)
set(handles.edit1,'String',0.1);
set(handles.edit2,'String',0.3);
set(handles.edit3,'String',2);
set(handles.edit4,'String',10);
set(handles.edit5,'String',50);
set(handles.axes2,'XLim',[0 1000],'YLim',[0 1000]);
title('Mapa das cidades')
xlabel('Distancia (Km)')
ylabel('Distancia (Km)')
data = [{0 0}];
set(handles.uitable2,'data',data);
handles.output = hObject;
guidata(hObject, handles);
% UIWAIT makes ig2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = ig2_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 pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
clc
load dados_caixeiro;
tesp = 0.1;
ti = clock;
format shortG;
data = [{0 0}];
set(handles.uitable2,'data',data);
set(handles.text11,'String','Calculando...')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 1 - Definir parâmetros do AG
taxa_mut = str2num(get(handles.edit1,'String')); % Taxa de mutação
taxa_rec = str2num(get(handles.edit2,'String')); % Taxa de recombinação
nGenes = str2num(get(handles.edit3,'String')); % Número de genes recombinados
x = str2num(get(handles.edit4,'String')); % Tamanho da população
nGer = str2num(get(handles.edit5,'String')); % Número de gerações (CRITÉRIO DE PARADA)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 2 - Gerar População Inicial (x indivíduos, aleatoriamente)
ger = 1; % Primeira geração
for k = 1:x
populacao(k,:) = randperm(10);
end
n = ceil(x * taxa_rec); % Calcula número de indivíduos gerados por recombinação
m = ceil(x * taxa_mut); % Calcula número de indivíduos gerados por mutação
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 3 - Recombinação
for ger = 1:nGer
%%%% 3.1 - Selecionar dois pais (por torneio)
for k = 1:n
sorteados = sorteio(populacao,5);
pais = torneio(sorteados, distancias, 2);
%%%% 3.2 - Selecionar genes que serão recombinados
genes = randperm(10);
genes = genes(1:nGenes);
%%%% 3.3 - Gerar filhos (recombinar genes)
filhos(k,:) = pais(1,:);
alelos = zeros(1,length(genes));
for t = 1:length(genes)
alelos(t) = pais(1,genes(t));
end
for t = 1:length(alelos)
aux(t) = ordem(pais(2,:),alelos(t));
end
aux = zeros(1,length(alelos));
aux = [aux;alelos]';
aux = sortrows(aux,1);
aux = aux';
for t = 1:length(genes)
filhos(k,genes(t)) = aux(2,t);
end
%%%% 3.4 - Voltar para 3.1 até que n filhos sejam gerados
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 4 - Mutação
%%%% 4.1 - Selecionar Indivíduo na população
ind_mut = randperm(x);
for k = 1:m
mut(k,:) = populacao(ind_mut(k),:);
%%%% 4.2 - Selecionar Genes que Sofrerão Mutação
genes = randperm(10);
genes = genes(1:2);
%%%% 4.3 - Gerar indivíduo por mutação
aux2 = mut(k,genes(1));
mut(k,genes(1))= mut(k,genes(2));
mut(k,genes(2)) = aux2;
%%%% 4.4 - Voltar para 4.1 até que m indivíduos sejam gerados
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 5 - Seleção
%%%% 5.1 - Da população original, selecionar x - n - m indivíduos (por torneio)
elite = torneio(populacao, distancias, x-n-m);
%%%% 5.2 - Formar nova população, adicionando os indivíduos gerados em 3 e 4
clear populacao;
populacao = [elite;filhos;mut];
%%%% 5.3 - Incrementar contador de gerações
ger = ger + 1;
%%%% 5.4 - Retornar para 3 até que o número de gerações definido em 1 seja alcançado
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 6 - Apresentar Solução Incumbente (graficar o caminho)
solucao = torneio(populacao, distancias, 1);
solucao = [solucao solucao(1)];
custo_solucao = calcula_custos(solucao, distancias);
hold off
plot(handles.axes2,1,1)
hold on
grid on
for k = 1:11
x_min(k) = posx(solucao(k));
y_min(k) = posy(solucao(k));
end
a = strcat('Geracao',{' '},num2str(ger-1));
plot(handles.axes2,x_min,y_min,'--r','LineWidth',3)
text(50,120,a,'FontWeight', 'bold','FontSize',12,'Color','b')
for k = 1:10
plot(handles.axes2,posx(k),posy(k),'s',...
'MarkerSize',20,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[0.5,0.5,0.5])
text(posx(k)-15,posy(k),num2str(k),'FontWeight', 'bold','FontSize',12,'Color','y')
end
title('Mapa das cidades')
xlabel('Distancia (Km)')
ylabel('Distancia (Km)')
drawnow
data = get(handles.uitable2,'data');
data = [data;{ger-1 num2str(custo_solucao)}];
set(handles.uitable2,'data',data);
tic
while toc < tesp
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 7 - Armazena Registro
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tf = clock;
t = etime(tf,ti);
var = xlsread('memorial.xls');
[L,C] = size(var);
celula = strcat('A',num2str(L+2));
d = {date, t, taxa_mut, taxa_rec, x, nGenes, nGer, custo_solucao};
xlswrite('memorial.xls', d, 'Dados', celula)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
a = '-----------------------------------------------------------------------------------------------------------------------------';
b = 'Problema do caixeiro viajante - Solução via algoritmo genético';
d = '---PARÂMETROS DO AG:---------------------------------------------------------------------------------------';
e = strcat('Taxa de mutação:____________________',num2str(taxa_mut));
f = strcat('Taxa de recombinação:________________',num2str(taxa_rec));
g = strcat('Tamanho da população:_______________',num2str(x));
h = strcat('Número de genes recombinados:________',num2str(nGenes));
k = '---RESULTADOS:--------------------------------------------------------------------------------------------------
没有合适的资源?快使用搜索试试~ 我知道了~
3 - AG + Interface grafica.rar_matlab
共8个文件
m:5个
mat:1个
xls:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 156 浏览量
2022-07-15
21:11:28
上传
评论
收藏 23KB RAR 举报
温馨提示
Algoritmo genetico mais grafico para o problema do caixeiro viajante
资源推荐
资源详情
资源评论
收起资源包目录
3 - AG + Interface grafica.rar (8个子文件)
3 - AG + Interface grafica
ordem.m 149B
ig2.fig 8KB
calcula_custos.m 472B
ig2.m 23KB
dados_caixeiro.mat 816B
torneio.m 487B
memorial.xls 49KB
sorteio.m 244B
共 8 条
- 1
资源评论
局外狗
- 粉丝: 66
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功