Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* improvementSwap() //{ */
- std::vector<unsigned> tsp_solver::improvementSwap(std::vector<unsigned> solution){
- unsigned first_pos, second_pos;
- int delta_f, change_value = 0;
- for(unsigned i = 1; i < num_cities_ - 1; i++){
- for(unsigned j = i + 1; j < num_cities_; j++){
- /* delta_f = old edges cost - new edges cost */
- if(i + 1 == j){
- delta_f = ( dist_[ solution[i - 1] ][ solution[i] ] + dist_[ solution[i] ][ solution[j] ]
- + dist_[ solution[j] ][ solution[j + 1] ])
- -
- ( dist_[ solution[i - 1] ][ solution[j] ] + dist_[ solution[j] ][ solution[i]]
- + dist_[ solution[i] ][ solution[j + 1]]);
- }else{
- delta_f = ( dist_[ solution[i - 1] ][ solution[i] ] + dist_[ solution[i] ][ solution[i + 1] ]
- + dist_[ solution[j - 1] ][ solution[j] ] + dist_[ solution[j] ][ solution[j + 1] ])
- -
- ( dist_[ solution[i - 1] ][ solution[j] ] + dist_[ solution[j] ][ solution[i + 1] ]
- + dist_[ solution[j - 1] ][ solution[i] ] + dist_[ solution[i] ][ solution[j + 1] ]);
- }
- if(delta_f > change_value){
- change_value = delta_f;
- first_pos = i;
- second_pos = j;
- }
- }
- }
- /* change the solution if generates a cheaper route */
- if(change_value > 0){
- unsigned tmp = solution[first_pos];
- solution[first_pos] = solution[second_pos];
- solution[second_pos] = tmp;
- }
- return solution;
- }
- //}
Advertisement
Add Comment
Please, Sign In to add comment