@@ -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\n Final solution score: " << calc_total_distance_double (config, context) << ' \n ' ;
515515
516516 return 0 ;
517- }
517+ }
0 commit comments