Skip to content

Instantly share code, notes, and snippets.

@ytomino
Created March 6, 2011 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ytomino/857122 to your computer and use it in GitHub Desktop.
Save ytomino/857122 to your computer and use it in GitHub Desktop.
The crossover operator for Genetic Algorithm
function Crossover (X, Y : Array_Type; Start_Index : Index_Type) return Array_Type is
Result : Array_Type := X;
Start_Item : constant Element_Type := X (Start_Index);
Index : Index_Type := Start_Index;
begin
while Y (Index) /= Start_Item loop
for I in X'Range loop
if X (I) = Y (Index) then
Result (Index) := Y (Index);
Index := I;
goto Found;
end if;
end loop;
raise Program_Error;
<<Found>> null;
end loop;
Result (Index) := Y (Index);
return Result;
end Crossover;
generic
type Index_Type is (<>);
type Element_Type is private;
type Array_Type is array (Index_Type) of Element_Type;
function Crossover (X, Y : Array_Type; Start_Index : Index_Type) return Array_Type;
pragma Pure (Crossover);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment