void neighbour_choose_and_move_to_next( ant_struct *a , long int phase ) /* FUNCTION: Choose for an ant probabilistically a next city among all unvisited cities in the current city's candidate list. If this is not possible, choose the closest next INPUT: pointer to ant the construction step "phase" OUTPUT: none (SIDE)EFFECT: ant moves to the chosen city */ { long int i, help; long int current_city; double rnd, partial_sum = 0., sum_prob = 0.0; /* double *prob_of_selection; */ /* stores the selection probabilities of the nearest neighbor cities */ double *prob_ptr; if ( (q_0 > 0.0) && (ran01( &seed ) < q_0) ) { /* with a probability q_0 make the best possible choice according to pheromone trails and heuristic information */ /* we first check whether q_0 > 0.0, to avoid the very common case of q_0 = 0.0 to have to compute a random number, which is expensive computationally */ neighbour_choose_best_next(a, phase); return; } prob_ptr = prob_of_selection; current_city = (*a).tour[phase-1]; /* current_city city of ant k */ DEBUG( assert ( current_city >= 0 && current_city < n ); ) for ( i = 0 ; i < nn_ants ; i++ ) { if ( (*a).visited[instance.nn_list[current_city][i]] ) prob_ptr[i] = 0.0; /* city already visited */ else { DEBUG( assert ( instance.nn_list[current_city][i] >= 0 && instance.nn_list[current_city][i] < n ); ) prob_ptr[i] = total[current_city][instance.nn_list[current_city][i]]; sum_prob += prob_ptr[i]; } } if (sum_prob <= 0.0) { /* All cities from the candidate set are tabu */ choose_best_next( a, phase ); } else { /* at least one neighbor is eligible, chose one according to the selection probabilities */ rnd = ran01( &seed ); rnd *= sum_prob; i = 0; partial_sum = prob_ptr[i]; while ( partial_sum <= rnd ) { i++; partial_sum += prob_ptr[i]; } DEBUG( assert ( 0 <= i && i < nn_ants); ); DEBUG( assert ( prob_ptr[i] >= 0.0); ); help = instance.nn_list[current_city][i]; DEBUG( assert ( help >= 0 && help < n ); ) DEBUG( assert ( (*a).visited[help] == FALSE ); ) (*a).tour[phase] = help; /* instance.nn_list[current_city][i]; */ (*a).visited[help] = TRUE; } }