@@ -852,14 +852,27 @@ bool TWRoute::is_valid_addition_for_tw(const Input& input,
852852 ? j.services [v_type]
853853 : j.setups [v_type] + j.services [v_type];
854854
855- auto oc = order_choice (input,
856- *current_job,
857- job_action_time,
858- b,
859- current,
860- next,
861- current_load,
862- check_max_load);
855+ // Use next info after insertion range for ordering decision,
856+ // except if there are still jobs to insert after j, in which case
857+ // we might have tighter constraints.
858+ auto tighter_next = next;
859+ if (current_job + 1 < last_job) {
860+ const auto & next_j = input.jobs [*(current_job + 1 )];
861+
862+ assert (next.travel <= next.latest );
863+ tighter_next.latest =
864+ std::min (next.latest - next.travel , next_j.tws .back ().end );
865+ tighter_next.travel = v.duration (j.index (), next_j.index ());
866+ }
867+
868+ const auto oc = order_choice (input,
869+ *current_job,
870+ job_action_time,
871+ b,
872+ current,
873+ tighter_next,
874+ current_load,
875+ check_max_load);
863876
864877 if (!oc.add_job_first && !oc.add_break_first ) {
865878 // Infeasible insertion.
@@ -1226,13 +1239,26 @@ void TWRoute::replace(const Input& input,
12261239 ? j.services [v_type]
12271240 : j.setups [v_type] + j.services [v_type];
12281241
1229- auto oc = order_choice (input,
1230- *current_job,
1231- job_action_time,
1232- b,
1233- current,
1234- next,
1235- current_load);
1242+ // Use next info after insertion range for ordering decision,
1243+ // except if there are still jobs to insert after j, in which case
1244+ // we might have tighter constraints.
1245+ auto tighter_next = next;
1246+ if (current_job + 1 < last_job) {
1247+ const auto & next_j = input.jobs [*(current_job + 1 )];
1248+
1249+ assert (next.travel <= next.latest );
1250+ tighter_next.latest =
1251+ std::min (next.latest - next.travel , next_j.tws .back ().end );
1252+ tighter_next.travel = v.duration (j.index (), next_j.index ());
1253+ }
1254+
1255+ const auto oc = order_choice (input,
1256+ *current_job,
1257+ job_action_time,
1258+ b,
1259+ current,
1260+ tighter_next,
1261+ current_load);
12361262
12371263 assert (oc.add_job_first xor oc.add_break_first );
12381264 if (oc.add_break_first ) {
0 commit comments