Skip to content

Commit 04cead0

Browse files
authored
Merge pull request #26 from guidantas21/main
Merge da branch randomize_construction
2 parents 50d020e + ded3b43 commit 04cead0

6 files changed

Lines changed: 317 additions & 174 deletions

File tree

include/ASP.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ASP {
1717
Flight m_dummy_flight;
1818

1919
public:
20-
enum class Neighborhood : uint8_t { IntraSwap, InterSwap, IntraMove, InterMove };
20+
enum class Neighborhood : uint8_t { IntraSwap, InterSwap, IntraMove, InterMove, WorstFlight };
2121
std::vector<Flight> flights;
2222

2323
// Constructive heuristics
@@ -37,6 +37,7 @@ class ASP {
3737
bool best_improvement_inter_swap(Solution &solution);
3838
bool best_improvement_intra_move(Solution &solution);
3939
bool best_improvement_inter_move(Solution &solution);
40+
bool move_worst_flight(Solution &Solution);
4041

4142
// Methaheuristics
4243

@@ -49,7 +50,7 @@ class ASP {
4950
// Perturbations
5051

5152
void random_inter_block_swap(Solution &solution);
52-
void best_improvement_free_space(Solution &solution);
53+
bool best_improvement_free_space(Solution &solution);
5354

5455

5556
ASP(Instance &instance);

src/GILS.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,28 +100,38 @@ Solution ASP::GILS_RVND(const size_t max_iterations, const size_t max_ils_iterat
100100
Solution best_found;
101101
best_found.objective = std::numeric_limits<uint32_t>::max();
102102

103-
for (size_t iteration = 0; iteration < max_iterations; ++iteration) {
104-
std::cout << iteration << std::endl;
103+
std::cout << ">> GILS-RVND\n";
104+
105+
for (size_t iteration = 1; iteration <= max_iterations; ++iteration) {
106+
107+
std::cout << "\n[" << iteration << "/" << max_iterations << "]" << '\t';
108+
109+
std::cout << "Best found: " << best_found.objective << '\n';
110+
111+
Solution solution = rand_lowest_release_time_insertion(flights);
105112

106-
Solution solution = lowest_release_time_insertion(flights);
107-
108113
Solution local_best = solution;
109114

115+
std::cout << "\tInitial solution: " << solution.objective << '\n';
116+
110117
RVND(solution);
111118

112119
size_t ils_iteration = 1;
113120

114121
while (ils_iteration <= max_ils_iterations) {
115-
size_t max_pertubation_iters =
122+
auto max_pertubation_iters =
116123
1 + static_cast<size_t>(std::ceil(alpha * static_cast<double>(m_instance.get_num_runways() / 2)));
117124

118-
for (size_t perturbation_iteration = 0; perturbation_iteration < max_pertubation_iters;
125+
for (size_t perturbation_iteration = 1; perturbation_iteration < max_pertubation_iters;
119126
++perturbation_iteration) {
120127

121128
// if (perturbation_iteration < max_pertubation_iters / 2 ) random_inter_block_swap(solution);
122129
// else best_improvement_free_space(solution);
123130

124-
best_improvement_free_space(solution);
131+
if (not best_improvement_free_space(solution)) {
132+
random_inter_block_swap(solution);
133+
break;
134+
}
125135
}
126136
RVND(solution);
127137

@@ -131,11 +141,14 @@ Solution ASP::GILS_RVND(const size_t max_iterations, const size_t max_ils_iterat
131141
}
132142
++ils_iteration;
133143
}
134-
144+
std::cout << "\tLocal best solution: " << local_best.objective;
135145
if (local_best.objective < best_found.objective) {
136146
best_found = local_best;
147+
std::cout << "\t(New best solution!)";
137148
}
149+
std::cout << '\n';
138150
}
151+
std::cout << "\nBest found: " << best_found.objective << '\n';
139152
return best_found;
140153
}
141154

src/RVND.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
void ASP::RVND(Solution &solution) { // NOLINT
55
std::vector<Neighborhood> neighborhoods{Neighborhood::IntraSwap, Neighborhood::InterSwap, Neighborhood::IntraMove,
6-
Neighborhood::InterMove};
6+
Neighborhood::InterMove, Neighborhood::WorstFlight};
77

88
bool improved = false;
99
size_t current_neighborhood = 0;
@@ -23,6 +23,9 @@ void ASP::RVND(Solution &solution) { // NOLINT
2323
case Neighborhood::InterMove:
2424
improved = best_improvement_inter_move(solution);
2525
break;
26+
case Neighborhood::WorstFlight:
27+
improved = move_worst_flight(solution);
28+
break;
2629
}
2730
if (improved) {
2831
neighborhoods = {Neighborhood::IntraSwap, Neighborhood::InterSwap, Neighborhood::IntraMove,

src/construction.cpp

Lines changed: 16 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -189,38 +189,38 @@ Solution ASP::lowest_release_time_insertion(std::vector<Flight> &flights) {
189189
return solution;
190190
}
191191

192-
size_t choose_runway(std::vector<size_t> start_time){
192+
size_t choose_runway(std::vector<size_t> start_time) {
193193
std::vector<float> values;
194194
float soma = 0;
195195

196196
// std::cout << "\nIniciando escolha:\n";
197197

198-
//inverte os valores
199-
for(size_t i = 0; i < start_time.size(); i++){
200-
values.push_back((float)1/start_time[i]);
198+
// inverte os valores
199+
for (size_t i = 0; i < start_time.size(); i++) {
200+
values.push_back((float)1 / start_time[i]);
201201
soma += values[i];
202202
// std::cout << "Start time: " << start_time[i] << std::endl;
203203
}
204204

205-
//calcula as probabilidades
206-
for(size_t i = 0; i < values.size(); i++){
207-
values[i] = floor((values[i]/soma)*100);
205+
// calcula as probabilidades
206+
for (size_t i = 0; i < values.size(); i++) {
207+
values[i] = floor((values[i] / soma) * 100);
208208
// std::cout << "Probabilidade: " << values[i] << std::endl;
209209
}
210210

211-
//calcula os intervalos
211+
// calcula os intervalos
212212
soma = 0;
213-
for(size_t i = 0; i < values.size(); i++){
213+
for (size_t i = 0; i < values.size(); i++) {
214214
values[i] = soma + values[i];
215215
soma = values[i];
216216
// std::cout << "Intervalo: " << values[i] << std::endl;
217217
}
218218

219-
//escolho um numero
219+
// escolho um numero
220220
size_t numero = rand() % (int)soma + 1;
221221

222-
for(size_t i = 0; i < values.size(); i++){
223-
if(numero <= values[i]){
222+
for (size_t i = 0; i < values.size(); i++) {
223+
if (numero <= values[i]) {
224224
// std::cout << "Choosed: " << i << std::endl;
225225
return i;
226226
}
@@ -292,10 +292,11 @@ Solution ASP::rand_lowest_release_time_insertion(std::vector<Flight> &flights) {
292292
(start_time[choosed_runway] - current_flight.get_release_time()) * current_flight.get_delay_penalty();
293293
solution.runways[choosed_runway].prefix_penalty.push_back(
294294
solution.runways[choosed_runway]
295-
.prefix_penalty[solution.runways[choosed_runway].sequence.size()] // the prefix of the last flight in the
296-
// runwawy
295+
.prefix_penalty[solution.runways[choosed_runway].sequence.size()] // the prefix of the last flight in
296+
// the runwawy
297297
+ current_flight_penalty); // O prefix penalty dele é o prefix penalty do anterior mais o penalty dele
298-
solution.runways[choosed_runway].sequence.emplace_back(candidate_list.back()); // Coloca ele no final daquela pista
298+
solution.runways[choosed_runway].sequence.emplace_back(
299+
candidate_list.back()); // Coloca ele no final daquela pista
299300

300301
Flight &candidate = candidate_list.back().get(); // Facilitar a escrita
301302

@@ -315,102 +316,3 @@ Solution ASP::rand_lowest_release_time_insertion(std::vector<Flight> &flights) {
315316

316317
return solution;
317318
}
318-
319-
// Solution ASP::rand_lowest_release_time_insertion(std::vector<Flight> &flights) {
320-
// Solution solution(m_instance);
321-
322-
// // Initialization of the candidate list
323-
// std::vector<std::reference_wrapper<Flight>> candidate_list;
324-
// candidate_list.reserve(m_instance.get_num_flights());
325-
326-
// for (size_t i = 0; i < m_instance.get_num_flights(); ++i) {
327-
// candidate_list.emplace_back(flights[i]);
328-
// }
329-
// std::vector<size_t> candidates_position(m_instance.get_num_flights());
330-
331-
// // Ordering of the candidate list by release time
332-
// std::sort(candidate_list.begin(), candidate_list.end(), [](const auto flight_a, const auto flight_b) {
333-
// return flight_a.get().get_release_time() > flight_b.get().get_release_time();
334-
// });
335-
336-
// size_t qtd_runways = m_instance.get_num_runways();
337-
338-
// // Put the "runway.size()"'s lowests release time flights
339-
// for (size_t runway_i = 0; runway_i < m_instance.get_num_runways(); ++runway_i) {
340-
// size_t idx = (rand() % std::min(qtd_runways, candidate_list.size())) + (candidate_list.size() - std::min(qtd_runways, candidate_list.size())); //index of the selected flight in candidate list
341-
342-
// solution.runways[runway_i].sequence.emplace_back(candidate_list[idx]); // Coloca ele no final daquela pista
343-
// solution.runways[runway_i].prefix_penalty.push_back(0); // O penalty dele é zero pois é o primeiro
344-
345-
// Flight &candidate = candidate_list[idx].get(); // Facilitar a escrita
346-
347-
// // Atualiza seus valores
348-
// candidate.position = 0;
349-
// candidate.runway = runway_i;
350-
// candidate.start_time = candidate.get_release_time();
351-
352-
// // Apaga ele da lista de candidatos
353-
// for(size_t i = idx; i < candidate_list.size()-1; i++){
354-
// candidate_list[i] = candidate_list[i+1];
355-
// }
356-
// candidate_list.pop_back();
357-
// }
358-
359-
// // Insert all the flights in the solution
360-
// size_t best_runway;
361-
// size_t lowest_start_time;
362-
// size_t start_time;
363-
// while (!candidate_list.empty()) {
364-
365-
// // Search the best runway which the start time be the lowest possible
366-
// best_runway = 0;
367-
// lowest_start_time = std::numeric_limits<size_t>::max();
368-
369-
// size_t idx = (rand() % std::min(qtd_runways, candidate_list.size())) + (candidate_list.size() - std::min(qtd_runways, candidate_list.size())); //index of the selected flight in candidate list
370-
// const Flight &current_flight = candidate_list[idx].get(); // the flight who will be insert
371-
372-
// for (size_t runway_i = 0; runway_i < m_instance.get_num_runways(); ++runway_i) {
373-
// const Flight &prev_flight =
374-
// solution.runways[runway_i].sequence.back().get(); // the actual last flight in the runway
375-
376-
// const uint32_t earliest = prev_flight.start_time + prev_flight.get_runway_occupancy_time() +
377-
// m_instance.get_separation_time(prev_flight.get_id(), current_flight.get_id());
378-
379-
// start_time = std::max(earliest, current_flight.get_release_time());
380-
381-
// if (start_time < lowest_start_time) {
382-
// lowest_start_time = start_time;
383-
// best_runway = runway_i;
384-
// }
385-
// }
386-
387-
// uint32_t current_flight_penalty =
388-
// (lowest_start_time - current_flight.get_release_time()) * current_flight.get_delay_penalty();
389-
// solution.runways[best_runway].prefix_penalty.push_back(
390-
// solution.runways[best_runway]
391-
// .prefix_penalty[solution.runways[best_runway].sequence.size()] // the prefix of the last flight in the
392-
// // runwawy
393-
// + current_flight_penalty); // O prefix penalty dele é o prefix penalty do anterior mais o penalty dele
394-
// solution.runways[best_runway].sequence.emplace_back(candidate_list[idx]); // Coloca ele no final daquela pista
395-
396-
// Flight &candidate = candidate_list[idx].get(); // Facilitar a escrita
397-
398-
// // Atualiza seus valores
399-
// candidate.position = solution.runways[best_runway].sequence.size() - 1;
400-
// candidate.runway = best_runway;
401-
// candidate.start_time = lowest_start_time;
402-
403-
// solution.runways[best_runway].penalty += current_flight_penalty;
404-
// solution.objective += current_flight_penalty;
405-
406-
// // Apaga ele da lista de candidatos
407-
// for(size_t i = idx; i < candidate_list.size()-1; i++){
408-
// candidate_list[i] = candidate_list[i+1];
409-
// }
410-
// candidate_list.pop_back();
411-
// }
412-
413-
// assert(solution.test_feasibility(m_instance));
414-
415-
// return solution;
416-
// }

0 commit comments

Comments
 (0)