/ Published in: MatLab
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
--- do_experiments.m % Usage of heuristics in console: % [p,l,d]=shortest_path('d198.txt'); % [p2,l2,d2]=insertion_heuristics('d198.txt',@farthest_insertion); % [p2,l2,d2]=insertion_heuristics('d198.txt',@arbitrary_insertion); function results=do_experiments() data_files={'d198.txt';'d1291.txt';'rat575.txt'}; number_of_experiments=10; results=[]; end end --- shortest_path.m % calculates shortest path using nearest neighborhood distances=distance(read_location_data(data_file)); end outside=(1:n); end outside(outside==city)=[]; --- distance.m function d = distance(a) end; end; --- read_location_data.m function positions = read_location_data(data_file) filename=regexp(data_file,'\w*(?=\.)','match'); --- nearest_neighbor.m function nearest_city = nearest_neighbor(distances,last_city,outside) min_distance = inf; if neighbors_distance < min_distance min_distance = neighbors_distance; end end end --- total_length_of_cycle.m total_length = 0; end --- insertion_heuristics.m % generic insertion algorithm function [path,total_length,distances] = insertion_heuristics(data_file,insertion_rule,idx_initial_city) distances=distance(read_location_data(data_file)); end outside=(1:n); end old_path=path; outside(outside==entering_city)=[]; end min_increase_in_length = inf; global path_length increase_in_length = after-before; if increase_in_length < min_increase_in_length min_increase_in_length = increase_in_length; end end % if index is greater than the path length, turn the index for one round function result = r(index) global path_length if index > path_length result = index - path_length; else result = index; end --- farthest_insertion.m max_distance=0; end end end --- arbitrary_insertion.m --- opt2.m global n end end end result=after-before; old_path=path; % exchange edge cities % change direction of intermediate path for k=1:j-1 end % if index is greater than the path length, turn index one round off function result = r(index) global n if index > n result = index - n; else result = index; end