@@ -90,11 +90,25 @@ impl DecomposeSearch {
9090 ( refinement_ctx, route_indices)
9191 } ) ;
9292
93- // merge evolution results into one insertion_ctx
94- let mut insertion_ctx = decomposed. into_iter ( ) . fold (
95- InsertionContext :: new_empty ( refinement_ctx. problem . clone ( ) , refinement_ctx. environment . clone ( ) ) ,
96- |insertion_ctx, decomposed| merge_best ( decomposed, original_insertion_ctx, insertion_ctx) ,
97- ) ;
93+ // get new and old parts and detect if there was any improvement in any part
94+ let ( ( new_parts, old_parts) , improvements) : ( ( Vec < _ > , Vec < _ > ) , Vec < _ > ) =
95+ decomposed. into_iter ( ) . map ( |decomposed| get_solution_parts ( decomposed, original_insertion_ctx) ) . unzip ( ) ;
96+
97+ let has_improvements = improvements. iter ( ) . any ( |is_improvement| * is_improvement) ;
98+
99+ let mut insertion_ctx = if has_improvements {
100+ improvements. into_iter ( ) . zip ( new_parts. into_iter ( ) . zip ( old_parts. into_iter ( ) ) ) . fold (
101+ InsertionContext :: new_empty ( refinement_ctx. problem . clone ( ) , refinement_ctx. environment . clone ( ) ) ,
102+ |accumulated, ( is_improvement, ( new_part, old_part) ) | {
103+ merge_parts ( if is_improvement { new_part } else { old_part } , accumulated)
104+ } ,
105+ )
106+ } else {
107+ new_parts. into_iter ( ) . fold (
108+ InsertionContext :: new_empty ( refinement_ctx. problem . clone ( ) , refinement_ctx. environment . clone ( ) ) ,
109+ |accumulated, new_part| merge_parts ( new_part, accumulated) ,
110+ )
111+ } ;
98112
99113 insertion_ctx. restore ( ) ;
100114 finalize_insertion_ctx ( & mut insertion_ctx) ;
@@ -248,24 +262,23 @@ fn decompose_insertion_context(
248262 . and_then ( |contexts| if contexts. len ( ) > 1 { Some ( contexts) } else { None } )
249263}
250264
251- fn merge_best (
265+ fn get_solution_parts (
252266 decomposed : ( RefinementContext , HashSet < usize > ) ,
253267 original_insertion_ctx : & InsertionContext ,
254- accumulated : InsertionContext ,
255- ) -> InsertionContext {
268+ ) -> ( ( SolutionContext , SolutionContext ) , bool ) {
256269 let ( decomposed_ctx, route_indices) = decomposed;
257- let decomposed_insertion_ctx = decomposed_ctx. ranked ( ) . next ( ) . expect ( GREEDY_ERROR ) ;
270+ let decomposed_insertion_ctx = decomposed_ctx. into_individuals ( ) . next ( ) . expect ( GREEDY_ERROR ) ;
258271 let environment = original_insertion_ctx. environment . clone ( ) ;
259272
260273 let ( partial_insertion_ctx, _) = create_partial_insertion_ctx ( original_insertion_ctx, environment, route_indices) ;
261274 let goal = partial_insertion_ctx. problem . goal . as_ref ( ) ;
262275
263- let source_solution = if goal. total_order ( decomposed_insertion_ctx, & partial_insertion_ctx) == Ordering :: Less {
264- & decomposed_insertion_ctx. solution
265- } else {
266- & partial_insertion_ctx. solution
267- } ;
276+ let is_improvement = goal. total_order ( & decomposed_insertion_ctx, & partial_insertion_ctx) == Ordering :: Less ;
277+
278+ ( ( decomposed_insertion_ctx. solution , partial_insertion_ctx. solution ) , is_improvement)
279+ }
268280
281+ fn merge_parts ( source_solution : SolutionContext , accumulated : InsertionContext ) -> InsertionContext {
269282 let mut accumulated = accumulated;
270283 let dest_solution = & mut accumulated. solution ;
271284
0 commit comments