-
Notifications
You must be signed in to change notification settings - Fork 0
/
perform2opt.m
74 lines (73 loc) · 3.33 KB
/
perform2opt.m
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
%%Copyright 2017 UON Leapheng, Calvin Ng, Francis Ng, Sharaful-Ilmi Paduman
%
% This file is part of NC-Code-Improvement.
%
% NC-Code-Improvement is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% NC-Code-Improvement is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with NC-Code-Improvement. If not, see <http://www.gnu.org/licenses/>.
%%
function [bestSequence] = perform2opt(initialSequence,blockConnections,distance)
%{
Objective: Find two random numbers in the sequence. These will split the
sequence into two segments. Consider A as the smaller random number and
B as the larger. The first segment is the combination of the values
from the starting point (1) up to A, and the values from B+1 up to the
end point. The remaining segment composed of values from A+1 to B. The
only alteration that could be done is to flip this segment. If the
resulting cost improved, then accept the new sequence.
Note: The choice of the fixed segment solves the problem of the
discontinuity at the ends of the sequence, and assures that the
first element of the sequence will always be 1.
Input:
initialSequence = the input sequence of blocks
initialCost = the cost of the initial sequence
blockConnections = the globalConnectionList indices of the closest
connection points between two blocks
block = the structured block data
where:
block.indices = the line structure indices of the lines of code
contained within the block
block.curveType = the type of block: 'point', 'open' contour,
or 'closed' contour
block.connectionPoints = the [x y] values of the candidate
connection points for the block to be connected to other
blocks
block.connectionIndices = the line structure indices of the
lines containing the connection points
block.globalListIndices = the global list indices of the
connection points
block.length = the length of the contour described by the lines
of code contained within the block
distance = the distance matrix
Output:
bestSequence = the improved sequence of blocks
bestCost = the cost of the improved block sequence
%}
%% Initialize the Output
bestSequence=initialSequence;
bestCost=cost(initialSequence,blockConnections,distance);
%% Starting Criterion
if length(initialSequence)<(2*2), return; end
%% Dislocation Points
while (1)
A=randi(length(initialSequence));
if A<length(initialSequence), break; end
end
while (1)
B=randi(length(initialSequence));
if B>A, break; end
end
%% Segment Manipulation
currentSequence=horzcat(initialSequence(1:A),initialSequence(flip(A+1:B)),initialSequence(B+1:end));
currentCost=cost(currentSequence,blockConnections,distance);
if currentCost<bestCost, bestSequence=currentSequence; end
end