/** * Function that return a neighbour of the function randomly * if we have nb_sta stations, we pick a position in 0,nb_sta-1 and we select the * corresponding station in circuit */ void AnnealingSolver::get_neighbour(Solution* sol, Solution* old_sol, int recuit_variant) { // Clearing the new sol and copying the old solution into the new one. sol->clear(); sol->copy(old_sol); // Chosing a station randomly, to be removed. Station* station; bool found = false; while(!found) { int station_id = rand() % inst->nb_stations; logn4("We selected the station at position : " + to_string(station_id)); for (Circuit* circuit : *(sol->circuits)) { list<Station*>* stations = circuit->stations; logn5("Circuit " + to_string(circuit->remorque->id) + " whith size " + to_string(stations->size())); if (station_id < stations->size()) { if (stations->size() == 1) { break; } else { // Finding station std::list<Station*>::iterator it = stations->begin(); std::advance(it, station_id); station = *(it); // Removing station from circuit stations->erase(it); found = true; break; } } else station_id -= stations->size(); } } // We remove the station from the circuit if (recuit_variant == 0) { //// INSERT BEST // Chosing a cuircuit where we will insert_best the solution to. int remorque_id = rand() % inst->nb_remorques; logn4("We selected the remorque with id : " + to_string(remorque_id)); Circuit* circuit = sol->circuits->at(remorque_id); circuit->insert_best(station); } else if (recuit_variant == 1) { //// NORMAL INSERT // Chosing a cuircuit where we will insert_best the solution to. int position_id = rand() % (inst->nb_remorques + inst->nb_stations -1); logn4("We selected the position number : " + to_string(position_id)); for (Circuit* circuit : *(sol->circuits)) { list<Station*>* stations = circuit->stations; logn5("Circuit " + to_string(circuit->remorque->id) + " whith size " + to_string(stations->size())); if (position_id < stations->size()) { // Finding position std::list<Station*>::iterator it = stations->begin(); std::advance(it, position_id); logn5("Adding station at position"+ to_string(position_id) + " of remorque : " + to_string(circuit->remorque->id)); // Removing station from circuit stations->insert(it,station); break; } else if (position_id == stations->size()) { logn5("Adding station at last position of remorque : " + to_string(circuit->remorque->id)); stations->push_back(station); break; } else position_id -= stations->size() + 1; } } else { logn1("Please set option --recuit-variant to 0 or 1"); exit(2); } // Update and return sol->update(); return; }