├── .gitignore ├── SA_VRP_Points ├── randomSol.m ├── calculateCost.m ├── isFeasible.m ├── plotSolution.m ├── initModel.m ├── createNeibor.m ├── sa.m └── hk48.tsp ├── SA_VRP_tspInstance ├── randomSol.m ├── calculateCost.m ├── isFeasible.m ├── initModel.m ├── createNeibor.m~ ├── sa.m ├── createNeibor.m └── hk48.tsp └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /SA_VRP_Points/randomSol.m: -------------------------------------------------------------------------------- 1 | function res = randomSol(model) 2 | res = randperm(model.city+model.veh-1); 3 | end -------------------------------------------------------------------------------- /SA_VRP_tspInstance/randomSol.m: -------------------------------------------------------------------------------- 1 | function res = randomSol(model) 2 | res = randperm(model.city+model.veh-1); 3 | end -------------------------------------------------------------------------------- /SA_VRP_Points/calculateCost.m: -------------------------------------------------------------------------------- 1 | function res = calculateCost(route,model) 2 | city = model.city; 3 | veh = model.veh; 4 | maps = model.maps; 5 | 6 | route = [city+veh route city+veh]; 7 | res = 0; 8 | 9 | for i = 1:length(route)-1 10 | res = res + maps(route(i),route(i+1)); 11 | end 12 | 13 | end -------------------------------------------------------------------------------- /SA_VRP_tspInstance/calculateCost.m: -------------------------------------------------------------------------------- 1 | function res = calculateCost(route,model) 2 | city = model.city; 3 | veh = model.veh; 4 | maps = model.maps; 5 | 6 | route = [city+veh route city+veh]; 7 | res = 0; 8 | 9 | for i = 1:length(route)-1 10 | res = res + maps(route(i),route(i+1)); 11 | end 12 | 13 | end -------------------------------------------------------------------------------- /SA_VRP_Points/isFeasible.m: -------------------------------------------------------------------------------- 1 | function res = isFeasible(route,model) 2 | len = length(route); 3 | if route(1)>model.city||route(len)>model.city 4 | res = 0; return 5 | end 6 | 7 | for i = 2:len 8 | if route(i)>model.city && route(i-1)>model.city % not use all vehicles 9 | res = 0; return 10 | end 11 | end 12 | 13 | res = 1; 14 | end -------------------------------------------------------------------------------- /SA_VRP_tspInstance/isFeasible.m: -------------------------------------------------------------------------------- 1 | function res = isFeasible(route,model) 2 | len = length(route); 3 | if route(1)>model.city||route(len)>model.city 4 | res = 0; return 5 | end 6 | 7 | for i = 2:len 8 | if route(i)>model.city && route(i-1)>model.city % not use all vehicles 9 | res = 0; return 10 | end 11 | end 12 | 13 | res = 1; 14 | end -------------------------------------------------------------------------------- /SA_VRP_tspInstance/initModel.m: -------------------------------------------------------------------------------- 1 | function model = initModel() 2 | 3 | city = 47 ; % city number 4 | veh = 2 ; % vehicle number 5 | model.city = city; 6 | model.veh = veh; 7 | 8 | mapsName = 'hk48.tsp'; 9 | maps = fileread(mapsName); 10 | maps = str2num(maps); 11 | for i = 2:veh 12 | for j = 1:city+veh-1 13 | maps(j,(city+i)) = maps(j,(city+i-1)); 14 | maps((city+i),j) = maps(j,(city+i-1)); 15 | end 16 | end 17 | model.maps = maps; 18 | 19 | end -------------------------------------------------------------------------------- /SA_VRP_Points/plotSolution.m: -------------------------------------------------------------------------------- 1 | function plotSolution(route,model) 2 | city = model.city; 3 | veh = model.veh; 4 | x0 = model.x0; 5 | y0 = model.y0; 6 | x = model.x; 7 | y = model.y; 8 | 9 | oneRoute = find(route>city); 10 | From = [0 oneRoute]+1; 11 | To = [oneRoute city+veh]-1; 12 | routes = cell(veh,1); 13 | subplot(1,2,1); 14 | 15 | for i = 1:veh 16 | routes{i} = route(From(i):To(i)); 17 | end 18 | 19 | colors=hsv(veh); 20 | 21 | for r = 1:veh 22 | X = [x0 x(routes{r}) x0]; 23 | Y = [y0 y(routes{r}) y0]; 24 | color = 0.8*colors(r,:); 25 | 26 | plot(X,Y,'-o',... 27 | 'Color',color,... 28 | 'LineWidth',2,... 29 | 'MarkerSize',10,... 30 | 'MarkerFaceColor','white'); 31 | hold on; 32 | end 33 | 34 | plot(x0,y0,'ks',... 35 | 'LineWidth',2,... 36 | 'MarkerSize',18,... 37 | 'MarkerFaceColor','yellow'); 38 | 39 | hold off; 40 | grid on; 41 | axis equal; 42 | 43 | 44 | end -------------------------------------------------------------------------------- /SA_VRP_tspInstance/createNeibor.m~: -------------------------------------------------------------------------------- 1 | function newRoute = createNeibor(route,model,mode) 2 | while(1) 3 | switch mode 4 | case 1 5 | newRoute = Swap(route); 6 | case 2 7 | % Do Reversion 8 | newRoute=Reversion(route); 9 | 10 | case 3 11 | % Do Insertion 12 | newRoute=Insertion(route); 13 | end 14 | 15 | if(isFeasible(newRoute,model)) 16 | break; 17 | end 18 | end 19 | end 20 | 21 | function newRoute = Swap(route) 22 | n = numel(route); 23 | 24 | i = randsample(n,2); 25 | i1 = i(1); 26 | i2 = i(2); 27 | newRoute = route; 28 | newRoute([i1 i2]) = route([i2 i1]); 29 | end 30 | 31 | function newRoute = Reversion() 32 | n=numel(q); 33 | 34 | i=randsample(n,2); 35 | i1=min(i(1),i(2)); 36 | i2=max(i(1),i(2)); 37 | 38 | qnew=q; 39 | qnew(i1:i2)=q(i2:-1:i1); 40 | 41 | end 42 | 43 | 44 | function newRoute = Insertion() 45 | 46 | 47 | end 48 | -------------------------------------------------------------------------------- /SA_VRP_Points/initModel.m: -------------------------------------------------------------------------------- 1 | function model = initModel() 2 | 3 | city = 100 ; % city number except depot 4 | veh = 4 ; % vehicle number 5 | model.city = city; 6 | model.veh = veh; 7 | 8 | xmin=0; 9 | xmax=200; 10 | 11 | ymin=0; 12 | ymax=200; 13 | 14 | 15 | 16 | maps = zeros(city+veh,city+veh); 17 | 18 | x=randi([xmin xmax],1,city); 19 | y=randi([ymin ymax],1,city); 20 | 21 | x0 = 100; 22 | y0 = 100; 23 | model.x0 = x0; 24 | model.y0 = y0; 25 | 26 | 27 | % for showing multiple vehicles 28 | offset = 30; 29 | x1=randi([xmin xmax/2-offset],1,city/4); 30 | x2=randi([xmin xmax/2-offset],1,city/4); 31 | x3=randi([xmax/2+offset xmax],1,city/4); 32 | x4=randi([xmax/2+offset xmax],1,city/4); 33 | 34 | y1=randi([ymin ymax/2-offset],1,city/4); 35 | y2=randi([ymin ymax/2-offset],1,city/4); 36 | y3=randi([ymax/2+offset ymax],1,city/4); 37 | y4=randi([ymax/2+offset ymax],1,city/4); 38 | x = [x1 x2 x3 x4]; 39 | y = [y1 y3 y2 y4]; 40 | 41 | 42 | for k = 1:veh 43 | x = [x x0]; 44 | y = [y y0]; 45 | end 46 | 47 | model.x = x; 48 | model.y = y; 49 | 50 | n = city+veh; 51 | 52 | for i = 1:n 53 | for j = i:n 54 | maps(j,i) = sqrt((x(i)-x(j))^2+(y(i)-y(j))^2); 55 | maps(i,j) = maps(j,i); 56 | end 57 | end 58 | 59 | model.maps = maps; 60 | 61 | end -------------------------------------------------------------------------------- /SA_VRP_tspInstance/sa.m: -------------------------------------------------------------------------------- 1 | clc; 2 | clear; 3 | close all; 4 | 5 | T0 = 10000000 ; % initial temperature 6 | r = 0.999 ; % temperature damping rate 7 | Ts = 0.001 ; % stop temperature 8 | 9 | model = initModel(); 10 | 11 | % initialization 12 | while(1) 13 | route = randomSol(model); 14 | if(isFeasible(route,model)) 15 | break; 16 | end 17 | end 18 | 19 | cost = calculateCost(route,model); 20 | T = T0; 21 | min = cost; 22 | 23 | cnt = 1; 24 | 25 | % SA 26 | while(T > Ts) 27 | flag = '#'; 28 | mode = randi([1 3]); 29 | newRoute = createNeibor(route,model,mode); 30 | newCost = calculateCost(newRoute,model); 31 | delta = newCost - cost; 32 | 33 | if(delta < 0) 34 | cost = newCost; 35 | route = newRoute; 36 | flag = '*'; 37 | else 38 | p=exp(-delta/T); 39 | if rand <= p 40 | cost = newCost; 41 | route = newRoute; 42 | flag = '^'; 43 | end 44 | end 45 | 46 | if cost < min 47 | min = cost; 48 | end 49 | 50 | costArr(cnt) = cost; 51 | 52 | T = T*r; % annealing 53 | disp([flag 'Iteration ' num2str(cnt) ': Best Cost = ' num2str(cost) ' T = ' num2str(T)]); 54 | cnt = cnt+1; 55 | 56 | end 57 | disp(min); 58 | plot(costArr); 59 | -------------------------------------------------------------------------------- /SA_VRP_Points/createNeibor.m: -------------------------------------------------------------------------------- 1 | function newRoute = createNeibor(route,model,mode) 2 | while(1) 3 | switch mode 4 | case 1 5 | newRoute = Swap(route); 6 | case 2 7 | % Do Reversion 8 | newRoute=Reversion(route); 9 | 10 | case 3 11 | % Do Insertion 12 | newRoute=Insertion(route); 13 | end 14 | 15 | if(isFeasible(newRoute,model)) 16 | break; 17 | end 18 | end 19 | end 20 | 21 | function newRoute = Swap(route) 22 | n = numel(route); 23 | 24 | i = randsample(n,2); 25 | i1 = i(1); 26 | i2 = i(2); 27 | newRoute = route; 28 | newRoute([i1 i2]) = route([i2 i1]); 29 | end 30 | 31 | function newRoute = Reversion(route) 32 | n=numel(route); 33 | 34 | i=randsample(n,2); 35 | i1=min(i(1),i(2)); 36 | i2=max(i(1),i(2)); 37 | 38 | newRoute=route; 39 | newRoute(i1:i2)=route(i2:-1:i1); 40 | 41 | end 42 | 43 | 44 | function newRoute = Insertion(route) 45 | n=numel(route); 46 | 47 | i=randsample(n,2); 48 | i1=i(1); 49 | i2=i(2); 50 | 51 | if i1 Ts) 35 | for k = 1:iter 36 | mode = randi([1 3]); 37 | newRoute = createNeibor(route,model,mode); 38 | newCost = calculateCost(newRoute,model); 39 | delta = newCost - cost; 40 | 41 | if(delta < 0) 42 | cost = newCost; 43 | route = newRoute; 44 | else 45 | p=exp(-delta/T); 46 | if rand() <= p 47 | cost = newCost; 48 | route = newRoute; 49 | 50 | end 51 | end 52 | end 53 | 54 | costArray(cnt) = cost; 55 | if cost