Is it possible to increase the performance by representing the values of the variables in each iteration of the loop?

I'm doing a genetic algorithm to solve the TSP problem and I'm trying to plot the result in each iteration, but I'm obtaining a bad performance. I would like to know how to improve the code to achieve better efficiency.
Is it possible to increase the performance by representing the values of the variables in each iteration of the loop?
while iga<maxit
%.......
iga=iga+1; %
rte = pop(1,:); %Best Route
subplot(1,2,1)
plot(model.x(rte),model.y(rte),'r','Marker','o','MarkerEdgeColor','black', 'MarkerFaceColor','white','MarkerSize',12);
title(sprintf('Total Distance = %1.2f, Iteration = %d',cost(1),iga));
vxlabels = arrayfun(@(n) {sprintf('%d', n)}, (1:length(model.dt.Points))'); %Number of each point
Hpl = text(model.x, model.y, vxlabels, 'FontWeight', 'bold', 'HorizontalAlignment',...
'center', 'BackgroundColor', 'none','FontSize',10)
subplot(1,2,2)
plot((1:iga-1),minc(1:iga-1),(1:iga-1),meanc(1:iga-1));
title('Convergence');
drawnow limitrate;
%.......
end

댓글 수: 4

Could you explain what you mean by "representing each iteration of the loop" ?
I'm plotting the value of minc and meanc (2 vectors) every loop iteraton and the performans is really bad.
If you are talking about graphics performance, then you could plot less often, or you could improve the code for doing the plotting.
But perhaps you are talking about the performance of your GA algorithm in the sense of it taking a long time per iteration, or in the sense of it taking a long time to make useful improvements in position?
I am talking about graphics performance.
Without representing the algorithm it takes 2 seconds, however with the representation it takes 110 seconds
I don't know how can I improve the code for doing the plot with better performance.

댓글을 달려면 로그인하십시오.

 채택된 답변

ax1 = subplot(1,2,1);
ax2 = subplot(1,2,2);
linehandle1 = line(ax1, nan, nan, 'r', 'Marker', 'o', 'MarkerEdgeColor', 'black', 'MarkerFaceColor', 'white', 'MarkerSize', 12);
Hpl = [];
title1 = title('Total Distance and iteration');
linehandle2a = line(ax2, nan, nan);
linehandle2b = line(ax2, nan, nan);
title(ax2, 'Convergence');
while iga<maxit
%.......
iga=iga+1; %
rte = pop(1,:); %Best Route
set(linehandl1, 'XData', model.x(rte), 'YData', model.y(rte));
set(title1, 'string', sprintf('Total Distance = %1.2f, Iteration = %d',cost(1),iga));
vxlabels = arrayfun(@(n) {sprintf('%d', n)}, (1:length(model.dt.Points))'); %Number of each point
if ~isempty(Hpl); delete(Hpl); end
Hpl = text(model.x, model.y, vxlabels, 'FontWeight', 'bold', 'HorizontalAlignment',...
'center', 'BackgroundColor', 'none','FontSize',10)
set(linehandle2a, 'XData', 1:iga-1, 'YData', minc(1:iga-1));
set(linehandle2b, 'XData', 1:iga-1, 'YData', meanc(1:iga-1));
drawnow limitrate;
%.......
end
Although it might in theory be more efficient to update all of the text objects rather than creating new ones, there is one text object per label and so changing them all becomes a bit messier. It is possible even without loop, but it involves some advanced features.

댓글 수: 5

Thanks for your answer!
I would like to learn how to update text objects instead of creating new ones to improve efficiency. Where could I learn more about this topic?
Is it the case that model.x and model.y do not change, and that rte is a vector that is consistent length -- that rte is just giving the order to use the values in? This is important for the question of efficiently updating the labels.
Exactly,the labels are always the same. Fuerthermore model.x and model.y never change and the vector rte always has the same length.
ax1 = subplot(1,2,1);
ax2 = subplot(1,2,2);
linehandle1 = line(ax1, nan, nan, 'r', 'Marker', 'o', 'MarkerEdgeColor', 'black', 'MarkerFaceColor', 'white', 'MarkerSize', 12);
Hpl = text(model.x, model.y, {}, 'FontWeight', 'bold', 'HorizontalAlignment',...
'center', 'BackgroundColor', 'none','FontSize',10);
title1 = title('Total Distance and iteration');
linehandle2a = line(ax2, nan, nan);
linehandle2b = line(ax2, nan, nan);
title(ax2, 'Convergence');
while iga<maxit
%.......
iga=iga+1; %
rte = pop(1,:); %Best Route
set(linehandl1, 'XData', model.x(rte), 'YData', model.y(rte));
set(title1, 'string', sprintf('Total Distance = %1.2f, Iteration = %d',cost(1),iga));
vxlabels = num2str(rte(:));
set(Hpl, {'String'}, vxlabels);
set(linehandle2a, 'XData', 1:iga-1, 'YData', minc(1:iga-1));
set(linehandle2b, 'XData', 1:iga-1, 'YData', meanc(1:iga-1));
drawnow limitrate;
%.......
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품

릴리스

R2018b

질문:

2019년 2월 28일

댓글:

2019년 3월 1일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by