-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMappingFunction.m
More file actions
29 lines (24 loc) · 909 Bytes
/
MappingFunction.m
File metadata and controls
29 lines (24 loc) · 909 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function [xy, totalDist, dmat] = MappingFunction(xy, pop, popSize, p, n)
% Number of swaps to make
changes = ceil(n*p);
for i=1:changes
% Selecting two random cities and swaping
randx = ceil(size(xy, 1) *rand());
randy = ceil(size(xy, 1) *rand());
temp = xy(randx, :);
xy(randx, :) = xy(randy, :); % change @ x
xy(randy, :) = temp; % change @ y
% Recalculating the distance matrix
nPoints = size(xy,1);
a = meshgrid(1:nPoints);
dmat = reshape(sqrt(sum((xy(a,:)-xy(a',:)).^2,2)),nPoints,nPoints);
end
% Evaluating every solution in the population (calculating total distances)
for p = 1:popSize
d = dmat(pop(p,nPoints),pop(p,1));
for k = 2:nPoints
d = d + dmat(pop(p,k-1),pop(p,k));
end
totalDist(p) = d;
end
end