Skip to content

Instantly share code, notes, and snippets.

@dlennox24
Last active April 3, 2017 00:09
Show Gist options
  • Save dlennox24/994adabea106e80a91fe636426ab32f8 to your computer and use it in GitHub Desktop.
Save dlennox24/994adabea106e80a91fe636426ab32f8 to your computer and use it in GitHub Desktop.
3-opt sudo
do{
minChange = 0;
for(i=0;i<nodes-3;i++){
for(j=i+2;j<nodes-1;j++){
for(k=j+1;k<nodes-2;k++){
base = calcDist(i,i+1) + calcDist(j,j+1) + calcDist(k,k+1);
// 2-opt
opt2_0 = calcDist(i,i+1) + calcDist(j,k) + calcDist(j+1,k+1);
opt2_1 = calcDist(j,j+1) + calcDist(k,i) + calcDist(k+1,i+1);
opt2_2 = calcDist(k,k+1) + calcDist(i,j) + calcDist(i+1,j+1);
// 3-opt
opt3_0 = calcDist(k,i+1) + calcDist(i,j+1) + calcDist(j,k+1);
opt3_1 = calcDist(i,k) + calcDist(j,k+1) + calcDist(j+1,i+1);
opt3_2 = calcDist(k,j) + calcDist(i,j+1) + calcDist(i+1,k+1);
opt3_3 = calcDist(j,i) + calcDist(k,i+1) + calcDist(k+1,j+1);
}
}
}
}while(minChange<0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment