I want to terminate the program when route distance becomes 75. But in command window program keeps running till the condition of iterations is achieved.
1 view (last 30 days)
Show older comments
function genetic4()
xy =[
6.9409 0.2259
1.7989 5.2270
3.1137 4.5593
8.8533 3.2828
7.6421 0.9238
5.0609 5.2346
3.0046 9.0919
8.6061 8.6808
2.0093 5.1440
4.3699 2.3990
9.8835 3.0795
9.0423 3.9674
5.7818 1.1668
7.7035 2.2236
7.6342 0.8472
5.5940 9.9058
7.0005 5.8606
4.8557 6.3250
4.7683 1.9913
7.4014 1.7087
7.4623 4.4927
2.0258 4.0282
0.8051 1.0827
7.0566 1.7103
4.4705 6.1112
3.4823 4.9517
1.4722 2.7032
9.2913 3.0202
3.8684 0.0541
7.6120 0.0102
0.5414 5.1897
8.7149 2.4217
7.1843 0.8910
6.1193 6.3243
0.6303 8.1332 ];
N = size(xy,1);
a = meshgrid(1:N);
distance_matrix = reshape(sqrt(sum((xy(a,:)-xy(a',:)).^2,2)),N,N);
population = 100;
iterations = 1e4;
figure('Name','City Locations','Numbertitle','on');
plot(xy(:,1),xy(:,2),'k.');
[nr,nc] = size(distance_matrix);
if N ~= nr N ~= nc
error('Invalid XY or distance_matrix inputs!')
end
n = N;
population = 4*ceil(population/4);
iterations = max(1,round(real(iterations(1))));
pop = zeros(population,n);
for k = 1:population
pop(k,:) = randperm(n);
end
global_min = Inf;
total_dist = zeros(1,population);
dist_history = zeros(1,iterations);
tmp_pop = zeros(4,n);
new_pop = zeros(population,n);
pfig = figure('Name','genetic | Current Best
Solution','Numbertitle','off');
for iter = 1:iterations
for p = 1:population
d = distance_matrix(pop(p,n),pop(p,1));
for k = 2:n
d = d + distance_matrix(pop(p,k-1),pop(p,k));
end
total_dist(p) = d;
end
[min_dist,index] = min(total_dist);
dist_history(iter) = min_dist;
if min_dist < global_min
global_min = min_dist;
if global_min>75.000
% global_min = 80.152;
best_route = pop(index,:);
figure(pfig);
route = best_route([1:n 1]);
plot(xy(route,1),xy(route,2),'r.-');
title(sprintf('Total Distance = %1.4f, Iteration =
%d',min_dist,iter));
end
end
rand_pair = randperm(population);
for p = 4:4:population
routes = pop(rand_pair(p-3:p),:);
dists = total_dist(rand_pair(p-3:p));
[~,idx] = min(dists);
best_of_4_route = routes(idx,:);
ins_pts = sort(ceil(n*rand(1,2)));
I = ins_pts(1);
J = ins_pts(2);
for k = 1:4
tmp_pop(k,:) = best_of_4_route;
switch k
case 2
tmp_pop(k,I:J) = fliplr(tmp_pop(k,I:J));
case 3
tmp_pop(k,[I J]) = tmp_pop(k,[J I]);
case 4
tmp_pop(k,I:J) = tmp_pop(k,[I+1:J I]);
otherwise
end
display(best_of_4_route);
display(d);
end
new_pop(p-3:p,:) = tmp_pop;
end
pop = new_pop;
end
end
1 Comment
Answers (1)
Jan
on 18 Apr 2013
It is not a good idea to let us guess, in which lines you want to perform "I want to terminate the program when route distance becomes 75". I find:
if global_min>75.000
...
end
But there is no trial to terminate the program, as far as I can see. Do you want to add a return command there?
1 Comment
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!