Skip to content

Commit f7689d4

Browse files
committed
changes
1 parent 2fe7822 commit f7689d4

13 files changed

+257
-151
lines changed

change_in_path_length.m

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function result = change_in_path_length(path, i, j, distances)
2+
3+
before = distances(path(r(i, path)), path(r(i+1,path))) + distances(path(r(i+1+j, path)), path(r(i+2+j, path)));
4+
5+
after = distances(path(r(i, path)), path(r(i+1+j, path))) + distances(path(r(i+1, path)), path(r(i+2+j, path)));
6+
7+
result = after - before;
8+
end

change_path.m

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function path = change_path(path, i, j)
2+
3+
oldPath = path;
4+
%fprintf('i: %i, j: %i\n', i, j);
5+
%fprintf('oldPath: %s\n', sprintf('%i ', oldPath));
6+
% exchange edge cities
7+
path(r(i+1, path)) = oldPath(r(i+1+j, path));
8+
path(r(i+1+j, path)) = oldPath(r(i+1, path));
9+
10+
% change direction of intermediate path
11+
for k=1:j-1
12+
path(r(i+1+k, path)) = oldPath(r(i+1+j-k, path));
13+
end
14+
15+
%fprintf('newPath: %s\n', sprintf('%i ', path));
16+
end

dPsoOpt.m

-120
This file was deleted.

distancePath.m

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
function dist = distancePath(data, path)
1+
function dist = distancePath(distances, path)
22

33
dist = 0.0;
44

55
for e=1:1:size(path,2)-1
6-
7-
dist = dist + pdist([data(path(e),:); data(path(e+1),:)], 'euclidean');
6+
7+
dist = dist + distances(path(e), path(e+1));
88
end
99

10-
dist = dist + pdist([data(path(end),:); data(path(1),:)], 'euclidean');
10+
dist = dist + distances(path(end), path(1));
1111
end

findGlobalBestFullPath.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
end
66

77
globalBestCoord = globalBest;
8-
globalBestDist = distancePath(globalBest, path);
8+
globalBestDist = distancePath(distance(globalBest), path);
99

1010
for d=1:1:size(personalBest,3)
1111

12-
personalBestDist = distancePath(personalBest(:,:,d), path);
12+
personalBestDist = distancePath(distance(personalBest(:,:,d)), path);
1313

1414
if personalBestDist < globalBestDist
1515
globalBestCoord = personalBest(:,:,d);

findGlobalBestPermutation.m

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
function [ globalBestPath, globalBestDist ] = findGlobalBestPermutation( data, personalBest, globalBest )
1+
function [ globalBestPath, globalBestDist ] = findGlobalBestPermutation( distances, personalBest, globalBest )
22

33
if nargin == 2
44
globalBest = personalBest(1,:);
55
end
66

77
globalBestPath = globalBest;
8-
globalBestDist = distancePath(data, globalBestPath);
8+
globalBestDist = distancePath(distances, globalBestPath);
99

1010
for d=1:1:size(personalBest,1)
1111

12-
personalBestDist = distancePath(data, personalBest(d,:));
12+
personalBestDist = distancePath(distances, personalBest(d,:));
1313

1414
if personalBestDist < globalBestDist
15-
globalBestPath = personalBest(d,:);
15+
globalBestPath = personalBest(d,:);
1616
globalBestDist = personalBestDist;
1717
end
1818
end

getListOfEdgeExchanges.m

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
function list = getListOfEdgeExchanges( ~, ispath, shallpath )
1+
function list = getListOfEdgeExchanges( ispath, shallpath )
22

3+
list = [];
4+
35
if size(ispath,2) ~= size(shallpath,2)
46
return
57
end
68

7-
list = [];
9+
if sum( ispath == shallpath ) == size(shallpath,2)
10+
return
11+
end
812

913
while sum( ispath == shallpath ) ~= size(shallpath,2)
1014

initializeSwarmMemberPermutation.m

+25-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
switch(type)
44
case 'random'
55
particlePos = randomInitialization( data, quantity );
6+
7+
case 'randomFif'
8+
particlePos = randomInitializationFif( data, quantity );
69

710
otherwise
811
particlePos = randomInitialization( data, quantity );
@@ -14,7 +17,7 @@
1417
particlePos = zeros(quantity, size(data,1));
1518

1619
if quantity > factorial(size(data,1))
17-
return
20+
quantity = factorial(size(data,1));
1821
end
1922

2023
for p=1:1:quantity % iterate through particles
@@ -29,3 +32,24 @@
2932
end
3033
end
3134
end
35+
36+
function particlePos = randomInitializationFif( data, quantity )
37+
38+
particlePos = zeros(quantity, size(data,1));
39+
40+
if quantity > factorial(size(data,1))
41+
quantity = factorial(size(data,1));
42+
end
43+
44+
for p=1:1:quantity % iterate through particles
45+
46+
while true
47+
newPermutation = [1 randsample(2:size(data,1), size(data,1)-1)];
48+
49+
if ~ismember(newPermutation, particlePos, 'rows')
50+
particlePos(p,:) = newPermutation;
51+
break
52+
end
53+
end
54+
end
55+
end

opt2.m

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function [path, total_length] = opt2(path, distances)
2+
3+
n = size(path,2);
4+
5+
for i=1:n
6+
for j=1:n-3
7+
if change_in_path_length(path, i, j, distances) < 0
8+
path = change_path(path, i, j);
9+
end
10+
end
11+
end
12+
13+
total_length = distancePath(distances, path);
14+
end

psoOpt.m

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
end
5757

5858
% find new personalBest for city n
59-
distpersonalBest = distancePath( personalBest(:,:,p), path );
60-
distAktPos = distancePath( particlePos(:,:,p), path );
59+
distpersonalBest = distancePath( distance(personalBest(:,:,p)), path );
60+
distAktPos = distancePath( distance(particlePos(:,:,p)), path );
6161
if distAktPos < distpersonalBest
6262
personalBest(n,:,p) = particlePos(n,:,p);
6363
end
@@ -105,6 +105,6 @@
105105
travelPoints = globalBest;
106106

107107
% calculate the total_length of the cycle
108-
total_length = total_length_of_cycle(distance(travelPoints), path);
108+
total_length = distancePath(distance(travelPoints), path);
109109
end
110110

0 commit comments

Comments
 (0)