@@ -334,6 +334,72 @@ def test_migration_uses_map_elites_deduplication(self):
334334 self .assertEqual (migrant_program .metrics ["combined_score" ], 0.9 ,
335335 "Migrant should preserve high score" )
336336
337+ def test_migration_skips_duplicate_code_on_target_island (self ):
338+ """Test that migration skips programs if target island already has identical code"""
339+ # Create a program on island 0
340+ prog_island_0 = Program (
341+ id = "prog_island_0" ,
342+ code = "def shared_code(): return 42" , # This code will be on both islands
343+ language = "python" ,
344+ metrics = {
345+ "complexity" : 50.0 ,
346+ "diversity" : 30.0 ,
347+ "score" : 0.8 ,
348+ "combined_score" : 0.8
349+ },
350+ metadata = {"island" : 0 , "generation" : 3 },
351+ )
352+ self .db .add (prog_island_0 )
353+
354+ # Create a program with IDENTICAL CODE on island 1 (target island)
355+ prog_island_1 = Program (
356+ id = "prog_island_1" ,
357+ code = "def shared_code(): return 42" , # Same exact code
358+ language = "python" ,
359+ metrics = {
360+ "complexity" : 50.0 ,
361+ "diversity" : 30.0 ,
362+ "score" : 0.7 , # Different score, but same code
363+ "combined_score" : 0.7
364+ },
365+ metadata = {"island" : 1 , "generation" : 3 },
366+ )
367+ self .db .add (prog_island_1 , target_island = 1 )
368+
369+ # Set generations to trigger migration
370+ self .db .island_generations [0 ] = 3
371+ self .db .island_generations [1 ] = 3
372+
373+ # Count programs before migration
374+ island_1_before = len ([pid for pid in self .db .islands [1 ] if pid in self .db .programs ])
375+
376+ # Trigger migration (island 0 should try to migrate to island 1)
377+ self .db .migrate_programs ()
378+
379+ # Count programs after migration
380+ island_1_after = len ([pid for pid in self .db .islands [1 ] if pid in self .db .programs ])
381+
382+ # Check if any new programs were added to island 1
383+ # Currently this will ADD a duplicate because we don't check for code duplication
384+ # After the fix, island_1_after should equal island_1_before (no new programs)
385+
386+ # Count programs with the shared code on island 1
387+ island_1_programs = [self .db .programs [pid ] for pid in self .db .islands [1 ] if pid in self .db .programs ]
388+ shared_code_count = sum (1 for p in island_1_programs if p .code == "def shared_code(): return 42" )
389+
390+ # CRITICAL TEST: Should be exactly 1 (the original prog_island_1)
391+ # Migration should be skipped because identical code already exists
392+ # This will FAIL with current implementation
393+ self .assertEqual (shared_code_count , 1 ,
394+ f"Should not migrate duplicate code - found { shared_code_count } programs with identical code on island 1" )
395+
396+ # Verify no unnecessary migration occurred
397+ # The only program with this code should be the original
398+ if shared_code_count == 1 :
399+ shared_code_programs = [p for p in island_1_programs if p .code == "def shared_code(): return 42" ]
400+ self .assertEqual (shared_code_programs [0 ].id , "prog_island_1" ,
401+ "Original program should remain, no migrant copy needed" )
402+
337403
338404if __name__ == '__main__' :
339405 unittest .main ()
0 commit comments