0

how would I calculate all the savings done by 2Opt without calculating the route distances before and after 2Opt moves and than just getting the difference?

I'll explain what i tried, but i keep getting wrong results. I would like to know if my error is semantic or sintactic. The algorithm itself is working correctly because i can see the drawn result. I have 2 for loops and an if statement. If the if statement is true I subtract the greater sum from the if statement and add the lesser sum. The result should be negative value of the savings, but i get some big number which makes my whole TSP have negative length.

if (rjesenjeDuplikat[vrhPrijePrvogVrha, drugiVrh] + rjesenjeDuplikat[prviVrh, vrhPoslijeDrugogVrha] <
    rjesenjeDuplikat[vrhPrijePrvogVrha, prviVrh] + rjesenjeDuplikat[drugiVrh, vrhPoslijeDrugogVrha])
{
    int zaDodati = rjesenjeDuplikat[vrhPrijePrvogVrha, drugiVrh] + rjesenjeDuplikat[prviVrh, vrhPoslijeDrugogVrha]; // ne radi kako treba
    int zaOduzeti = rjesenjeDuplikat[vrhPrijePrvogVrha, prviVrh] + rjesenjeDuplikat[drugiVrh, vrhPoslijeDrugogVrha];
    ukupnaDuljinaNNH = ukupnaDuljinaNNH + zaDodati - zaOduzeti;
    ...}
user3888540
  • 105
  • 1
  • 1
  • 3
  • Are you initializing `ukupnaDuljinaNNH` before the first time you use it? If not, that could explain the unexpected result. In general, though, an approach like yours should be OK -- you shouldn't need to calculate the lengths of the whole tours (old and new) and then subtract. – LarrySnyder610 Jan 14 '15 at 19:23
  • Thank you for the answer. I actually calculated the NNH distance wrong so it seemed as 2Opt distance calculation was wrong while it was correct the whole time. – user3888540 Jan 15 '15 at 21:19

0 Answers0