Skip to content

Commit 81c9039

Browse files
Minor evolved program fixes
1 parent 9b82d0e commit 81c9039

10 files changed

Lines changed: 78 additions & 86 deletions

File tree

examples/tsp_tour_minimization/evolved_program/TSP.cpp

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -241,52 +241,52 @@ void solve(const Config& config, Context& context) { // the found solution will
241241
if (i % 100 == 0) { std::cout << "# --------- Iteration: " << i << '\n'; }
242242
int improved_times = 0;
243243

244-
// Optimized restart strategy with better time-quality tradeoff
245-
start_time = high_resolution_clock::now();
246-
double time_elapsed = duration_cast<duration<double>>(high_resolution_clock::now() - start_total_time).count();
244+
// Optimized restart strategy with better time-quality tradeoff
245+
start_time = high_resolution_clock::now();
246+
double time_elapsed = duration_cast<duration<double>>(high_resolution_clock::now() - start_total_time).count();
247+
248+
// Time-aware restart strategy with progressive intensification
249+
if (time_elapsed > 40.0 && i > 60) {
250+
double quality_ratio = context.path_distance_double / context.best_path_distance_double;
247251

248-
// Time-aware restart strategy with progressive intensification
249-
if (time_elapsed > 40.0 && i > 60) {
250-
double quality_ratio = context.path_distance_double / context.best_path_distance_double;
251-
252-
// More aggressive exploitation strategy
253-
if (quality_ratio < 1.006) {
254-
// Elite solution - intensive local search
255-
if (i % 3 == 0) {
256-
shake_from_best(config, context, 1); // Light perturbation
257-
} else {
258-
restore_best_path(config, context);
259-
}
260-
} else if (quality_ratio < 1.020) {
261-
// Good solution - balanced approach
262-
if (i % 4 == 0) {
263-
shake_from_best(config, context, 3); // Moderate perturbation
264-
} else {
265-
generate_greedy_solution(config, context, i);
266-
convert_solution_to_path(config, context);
267-
}
268-
} else {
269-
// Poor solution - fresh start with greedy
270-
generate_greedy_solution(config, context, i);
271-
convert_solution_to_path(config, context);
272-
}
273-
} else if (i <= 50 || context.best_path_distance_double == inf_double) {
274-
// Early phase: rapid exploration
252+
// More aggressive exploitation strategy
253+
if (quality_ratio < 1.006) {
254+
// Elite solution - intensive local search
275255
if (i % 3 == 0) {
276-
generate_random_solution(config, context);
256+
shake_from_best(config, context, 1); // Light perturbation
277257
} else {
278-
generate_greedy_solution(config, context, i);
258+
restore_best_path(config, context);
279259
}
280-
convert_solution_to_path(config, context);
281-
} else {
282-
// Middle phase: balanced exploration
283-
if (i % 8 == 0) {
284-
generate_random_solution(config, context);
260+
} else if (quality_ratio < 1.020) {
261+
// Good solution - balanced approach
262+
if (i % 4 == 0) {
263+
shake_from_best(config, context, 3); // Moderate perturbation
285264
} else {
286265
generate_greedy_solution(config, context, i);
266+
convert_solution_to_path(config, context);
287267
}
268+
} else {
269+
// Poor solution - fresh start with greedy
270+
generate_greedy_solution(config, context, i);
288271
convert_solution_to_path(config, context);
289272
}
273+
} else if (i <= 50 || context.best_path_distance_double == inf_double) {
274+
// Early phase: rapid exploration
275+
if (i % 3 == 0) {
276+
generate_random_solution(config, context);
277+
} else {
278+
generate_greedy_solution(config, context, i);
279+
}
280+
convert_solution_to_path(config, context);
281+
} else {
282+
// Middle phase: balanced exploration
283+
if (i % 8 == 0) {
284+
generate_random_solution(config, context);
285+
} else {
286+
generate_greedy_solution(config, context, i);
287+
}
288+
convert_solution_to_path(config, context);
289+
}
290290
end_time = high_resolution_clock::now();
291291

292292
calc_and_save_total_distance(config, context);
@@ -297,7 +297,7 @@ void solve(const Config& config, Context& context) { // the found solution will
297297

298298
// local 2opt search
299299
start_time = high_resolution_clock::now();
300-
improved_times = local_2_opt_search(config, context);
300+
improved_times = local_2_opt_search(config, context);
301301
end_time = high_resolution_clock::now();
302302

303303
if (config.distance_type != DistanceType::Double) {
@@ -385,37 +385,37 @@ void solve(const Config& config, Context& context) { // the found solution will
385385
store_path_as_best(config, context); // also updates best path distance
386386
}
387387

388-
if (config.random_k_opt_depth_after_first_iteration) {
389-
// More aggressive depth adaptation for better solution quality
390-
double progress = static_cast<double>(i) / config.restarts_number;
391-
double quality_factor = context.path_distance_double / context.best_path_distance_double;
392-
double time_elapsed = duration_cast<duration<double>>(high_resolution_clock::now() - start_total_time).count();
393-
double time_remaining = 159.0 - time_elapsed;
394-
395-
// More aggressive time utilization for solution quality
396-
double time_factor = std::min(1.0, time_remaining / 60.0);
397-
398-
if (progress < 0.3) {
399-
// Early phase - deeper exploration
400-
max_k_opt_depth = 22 + (rand() % (10 + static_cast<int>(8 * time_factor)));
401-
} else if (progress < 0.7) {
402-
// Middle phase - balanced depth
403-
if (quality_factor < 1.006) {
404-
max_k_opt_depth = 26 + (rand() % (12 + static_cast<int>(6 * time_factor)));
405-
} else {
406-
max_k_opt_depth = 24 + (rand() % (10 + static_cast<int>(5 * time_factor)));
407-
}
388+
if (config.random_k_opt_depth_after_first_iteration) {
389+
// More aggressive depth adaptation for better solution quality
390+
double progress = static_cast<double>(i) / config.restarts_number;
391+
double quality_factor = context.path_distance_double / context.best_path_distance_double;
392+
double time_elapsed = duration_cast<duration<double>>(high_resolution_clock::now() - start_total_time).count();
393+
double time_remaining = 159.0 - time_elapsed;
394+
395+
// More aggressive time utilization for solution quality
396+
double time_factor = std::min(1.0, time_remaining / 60.0);
397+
398+
if (progress < 0.3) {
399+
// Early phase - deeper exploration
400+
max_k_opt_depth = 22 + (rand() % (10 + static_cast<int>(8 * time_factor)));
401+
} else if (progress < 0.7) {
402+
// Middle phase - balanced depth
403+
if (quality_factor < 1.006) {
404+
max_k_opt_depth = 26 + (rand() % (12 + static_cast<int>(6 * time_factor)));
408405
} else {
409-
// Late phase - focused depth for elite solutions
410-
if (quality_factor < 1.004 && time_remaining > 30.0) {
411-
max_k_opt_depth = 28 + (rand() % (10 + static_cast<int>(6 * time_factor)));
412-
} else {
413-
max_k_opt_depth = 24 + (rand() % (8 + static_cast<int>(4 * time_factor)));
414-
}
406+
max_k_opt_depth = 24 + (rand() % (10 + static_cast<int>(5 * time_factor)));
407+
}
408+
} else {
409+
// Late phase - focused depth for elite solutions
410+
if (quality_factor < 1.004 && time_remaining > 30.0) {
411+
max_k_opt_depth = 28 + (rand() % (10 + static_cast<int>(6 * time_factor)));
412+
} else {
413+
max_k_opt_depth = 24 + (rand() % (8 + static_cast<int>(4 * time_factor)));
415414
}
416-
max_k_opt_depth = std::min(max_k_opt_depth, config.max_k_opt_depth);
417-
max_k_opt_depth = std::max(18, max_k_opt_depth);
418415
}
416+
max_k_opt_depth = std::min(max_k_opt_depth, config.max_k_opt_depth);
417+
max_k_opt_depth = std::max(18, max_k_opt_depth);
418+
}
419419

420420
if (i % 100 == 0) { std::cout << '\n'; }
421421
}
@@ -514,4 +514,4 @@ int main(int argc, char** argv) {
514514
std::cout << "\n\nFinal solution score: " << calc_total_distance_double(config, context) << '\n';
515515

516516
return 0;
517-
}
517+
}

examples/tsp_tour_minimization/evolved_program/config.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
{
22
"cities_number": 1000,
3-
"input_path": "/Users/dark-creator/solomon/self/UTSP-alpha_evolve/src/input.txt",
4-
"output_path": "/Users/dark-creator/solomon/self/UTSP-alpha_evolve/src/output.txt",
5-
"logging_best_solutions_path": "/Users/dark-creator/solomon/self/UTSP-alpha_evolve/src/best_solutions.log",
6-
"logging_restarts_solutions_path": "/Users/dark-creator/solomon/self/UTSP-alpha_evolve/src/restarts_solutions.log",
3+
"input_path": "input.txt",
4+
"output_path": "output.txt",
75
"use_heat_map_as_initial_weights": false,
86
"candidates_source": "knn",
97
"candidates_number": 10,

examples/tsp_tour_minimization/evolved_program/heat_map_inference.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ def calc_heat_map(cities: np.ndarray) -> np.ndarray:
4242
output_temp_file.flush()
4343
os.fsync(output_temp_file.fileno())
4444

45-
os.replace(tmp_path, args.out)
45+
os.replace(tmp_path, args.out)

examples/tsp_tour_minimization/evolved_program/heat_map_train.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99

1010
# You can train graph neural networks here (actually you can edit the whole file).
1111
# Save them in str(BASE_DIR / "pretrained") directory and use them in `heat_map_inference.py`.
12-
# But mind the training time, it should not exceed 6 minutes (python3.11, ubuntu 22.04, nvidia A100 40 GB GPU).
12+
# But mind the training time, it should not exceed 8 minutes (python3.11, ubuntu 22.04, nvidia A100 40 GB GPU).
1313

1414
# In the original UTSP paper the heat map matrix is used, but upon the closer look on the authors code, they did not use it (they used simple K nearest neighbours as candidates).
1515
# So, the SOTA metrics were achieved without a heat map matrix, but maybe GNN approach is not wrong by its nature, maybe if trained properly it can help 2'opt and k'opt algorithms to find the best solution faster.
1616

17-
# Possible GNN step (just in case, implement if you like): hamiltonian cycle constraint, loss on exact 2 degree for each node, etc.
18-
1917

2018
if __name__ == "__main__":
21-
print("Sample output to log")
19+
print("Sample output to log")

examples/tsp_tour_minimization/evolved_program/include/additional.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,4 @@ bool apply_3_opt_move(const Config& config, Context& context, int i, int j, int
150150
// Similar implementations for Int32 and Int64 would go here...
151151

152152
return false;
153-
}
153+
}

examples/tsp_tour_minimization/evolved_program/include/context.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ struct Config {
2323
int cities_number;
2424
std::string input_path;
2525
std::string output_path;
26-
std::string logging_best_solutions_path;
27-
std::string logging_restarts_solutions_path;
2826
bool use_heat_map_as_initial_weights;
2927
CandidatesSource candidates_source;
3028
int candidates_number;
@@ -44,8 +42,6 @@ struct Config {
4442
cities_number = config["cities_number"];
4543
input_path = config["input_path"];
4644
output_path = config["output_path"];
47-
logging_best_solutions_path = config["logging_best_solutions_path"];
48-
logging_restarts_solutions_path = config["logging_restarts_solutions_path"];
4945
use_heat_map_as_initial_weights = config["use_heat_map_as_initial_weights"];
5046

5147
if (config["candidates_source"].get<std::string>() == "knn") {
@@ -276,4 +272,4 @@ void restore_best_path(const Config& config, Context& context) {
276272
context.path[i].prev = context.best_path[i].prev;
277273
context.path[i].next = context.best_path[i].next;
278274
}
279-
}
275+
}

examples/tsp_tour_minimization/evolved_program/include/local_2_opt_search.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,4 @@ int local_2_opt_search(const Config& config, Context& context) {
8787
while (improve_by_2_opt_move(config, context)) { ++improved_times; };
8888

8989
return improved_times;
90-
}
90+
}

examples/tsp_tour_minimization/evolved_program/include/local_k_opt_search.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,4 @@ int local_k_opt_search(const Config& config, Context& context, int max_k_opt_dep
245245
while (improve_by_k_opt_move(config, context, max_k_opt_depth)) { ++improved_times; };
246246

247247
return improved_times;
248-
}
248+
}

examples/tsp_tour_minimization/evolved_program/include/random_solution.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ void generate_random_solution(const Config& config, Context& context) {
3030
context.solution[i] = current_city;
3131
context.is_city_selected[current_city] = true;
3232
}
33-
}
33+
}

examples/tsp_tour_minimization/evolved_program/include/utils.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,4 @@ double expected_optimal_tsp_length_2d(long long n, double width, double height)
181181
double area = width * height;
182182
double expected_length = BHH_CONSTANT_2D * std::sqrt(static_cast<double>(n) * area);
183183
return expected_length;
184-
}
184+
}

0 commit comments

Comments
 (0)