Professional Documents
Culture Documents
function [OptimalTour,mincost]=Untitled2(~)
% Demo to demonstrate how pdist() can find distances between all points of
2 sets of points.
% Requires the Statistics and Machine Learning Toolbox because of the
pdist() and squareform() functions.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 28;
NumOfCities=10;
G=[0,0];
G1=[1,3]; G2=[1,7]; G3=[3,9]; G4=[5,3]; G5=[1,1];
G6=[9,5]; G7=[9,9]; G8=[11,1]; G9=[15,7];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
points1=[G];
points2=[G1;G2;G3;G4;G5;G6;G7;G8;G9];
% Plot all the points
% Get the distance from every point to every other point.
pDist = pdist([points1;points2]);
% That's hard to interpret though, so
% let's reshape that into a nice table
D = squareform(pDist)
%B=tril(D);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Primes=primes(NumOfCities*10);
NumOfDataSets=1;
for i=2:NumOfCities
NumOfDataSets=NumOfDataSets+nchoosek(NumOfCities,i);
end
Data(NumOfDataSets).S=[];
Data(NumOfDataSets).l=0;
Data(NumOfDataSets).cost=inf;
Data(NumOfDataSets).pre=[];
Data(NumOfDataSets).m=[];
LookUpTable(NumOfDataSets)=0;
Data(1).S=[1];
Data(1).l=1;
Data(1).cost=0;
Data(1).Pre=[];
Data(1).m=[];
for s=2:NumOfCities
Data(s).S=[Data(1).S,s];
Data(s).l=s;
Data(s).cost=D(s,1);
Data(s).Pre=1;
Data(s).m=1;
LUT=calcLUT(Data(s).S,s,Primes);
LookUpTable(s)=LUT;
end
IndexStartPrevStep=2; %index into Data that marks the beginning of the
previous step
IndexLastStep=NumOfCities; %index into Data that marks the end of the
previous step
CurrentData=IndexLastStep; %index into Data that marks the current dataset
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This is the main Dynamic Programming loop
for s=3:NumOfCities
%generate possible sets with s-1 cities out of the possible N-1 cities
%(not including city 1)
TempSets=nchoosek(2:NumOfCities,s-1);
NumOfSets=size(TempSets);
for j=1:NumOfSets(1) %iterate through all the sets
for k=1:NumOfSets(2) %iterate through all the elements in each set
SminuskSet=[1,TempSets(j,1:k-1),TempSets(j,k+1:NumOfSets(2))]; %this is the
set S-{k}
candidatecost(2:length(SminuskSet))=inf;
indices=[];
for mm=2:length(SminuskSet) %choose a city in S-{k} that will be last
LUV=calcLUT(SminuskSet,SminuskSet(mm),Primes);
index=find(LUV==LookUpTable(IndexStartPrevStep:IndexLastStep));
index=index+IndexStartPrevStep-1;
if index==0
candidatecost(mm)=inf;
else
candidatecost(mm)=Data(index).cost+D(SminuskSet(mm),TempSets(j,k));
indices(mm)=index;
end
end
[mincost,indexcost]=min(candidatecost(2:end));
CurrentData=CurrentData+1;
Data(CurrentData).S=[1,TempSets(j,:)];
Data(CurrentData).l=TempSets(j,k);
Data(CurrentData).cost=mincost;
Data(CurrentData).Pre=indices(indexcost+1);
Data(CurrentData).m=SminuskSet(indexcost+1);
LookUpTable(CurrentData)=calcLUT(Data(CurrentData).S,TempSets(j,k),Primes);
end
end
IndexStartPrevStep=IndexLastStep+1;
IndexLastStep=CurrentData;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
mm=0;
%Now add the distance back from the last city to city 1
for i=IndexStartPrevStep:IndexLastStep
mm=mm+1;
candidatecost(mm)=Data(i).cost+D(Data(i).l,1);
end
%Find the one that minimizes the total distance
[mincost,indexcost]=min(candidatecost);
Temp=Data(IndexStartPrevStep+indexcost-1);
%Generate the optimal tour by traversing back from the last city to its
%predecessors
OptimalTour=1;
while ~isempty(Temp.Pre)
OptimalTour=[OptimalTour,Temp.l];
Temp=Data(Temp.Pre);
end
ZY=min(candidatecost)
OptimalTour=[OptimalTour,1];
W=OptimalTour;
function LUT=calcLUT(vec,last,Primes)
j=length(vec);
LUT=Primes(last);
for i=2:j
LUT=LUT*Primes(vec(i));
end
Contoh 2 PSO
function [rute_optimum, jarak_minimum,t]=psofortsp(ba,bb,np,itmax)
%dx=matriks jarak
%ba,bb,np = batas atas (10),batas bawah (-10), jumlah partikel (50)
%itmax = iterasi maksimum
%rute_optimum = rute tsp terbaik (optimal)
%jarak_minimum = jarak dari rute tsp yang terbaik
%t = waktu komputasi
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fontSize = 28;
NumOfCities=10;
G1=[1,3]; G2=[1,7]; G3=[3,9]; G4=[5,3]; G5=[1,1];
G6=[9,5]; G7=[9,9]; G8=[11,1]; G9=[15,7]; G=[0,0];
points1=[G];
points2=[G1;G2;G3;G4;G5;G6;G7;G8;G9];
% Plot all the points
% Get the distance from every point to every other point.
pDist = pdist([points1;points2]);
% That's hard to interpret though, so
% let's reshape that into a nice table
dx = squareform(pDist);
A=tril(dx);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%step 1:
t=cputime;
%inisialisasi secara random partikel xi dan kecepatan vi dalam ruang
%pencarian problem p-dimensi
np=50; %jumlah partikel
c=10;
nk=c;
ba=10; %batas atas
bb=-10; %batas bawah
x=rand(np,nk)*(ba-bb)+bb; %inisialisasi partikel xi
v=rand(np,nk); %inisialisasi kecepatan vi
%mengurutkan nilai random secara ascending untuk mendapatkan rute
[min1 perm]=sort(x,2); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
perm_tsp=[perm perm(:,1)] %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Iteration parameters
N_iter=500; % Total number of iterations
fitnessMSE = ones(1,N_iter);
d=10; % Dimension of the search variables
minposisi=-30;
maksposisi=30;
Lb=minposisi*ones(1,d);
Ub=maksposisi*ones(1,d);
function z=Fun(u)
%% Booth's function V
z=(u(1)+2.*u(2)-7).^2+(2.*u(1)+u(2)-5).^2;