Modifies the bits of the current object by mixing the bits of the two Source bits, or Parents. The method randomly selects a crossover point and then copies bits from Source1 from the first bit to the crossover bit and then copies bits from Source2 from the crossover bit to the last bit. Thus, the current bits object becomes the child of the two parent bits.
Namespace: RSGeneticAlgorithm
Parameters
Source1
Type: TRSGABits
Source2
Type: TRSGABits
Return Value
Type: TRSGABits
The following example randomly mixes the chromosome bits of two parents into a child chromosome (all represented by TRSGABits in the TRSGAIndividual). It first selects the crossover bit using a CrossOver method and then mixes the bits:
Delphi
|
procedure TRSCustomGeneticAlgorithm.Crossover(const Parent1, Parent2, Child1,
Child2: TRSGAIndividual);
var
CrossBit: Integer;
begin
if Random >= CrossoverProbability then Exit;
case CrossoverMethod of
cmGeneBoundary:
if (CrossoverMethod = cmGeneBoundary) and (Genes.Count > 1) then
CrossBit := Genes[Random(Genes.Count-1)+1].Offset
else // cmBit
CrossBit := Random(Child1.Bits.Size);
cmByteBoundary:
if GeneSize < 16 then // need at least 2 bytes
CrossBit := Random(Child1.Bits.Size)
else
CrossBit := (Random(Child1.Bits.Size div 8)+1)*8;
cmWordBoundary:
if GeneSize < 32 then // need at least 2 words
CrossBit := Random(Child1.Bits.Size)
else
CrossBit := (Random(Child1.Bits.Size div 16)+1)*16;
cmLongwordBoundary:
if GeneSize < 64 then // need at least 2 long words
CrossBit := Random(Child1.Bits.Size)
else
CrossBit := (Random(Child1.Bits.Size div 32)+1)*32;
else
// cmBitBoundary:
CrossBit := Random(Child1.Bits.Size);
end;
Child1.Bits.CrossOver(Parent1.Bits, Parent2.Bits, CrossBit);
if Child2 <> nil then
Child2.Bits.CrossOver(Parent2.Bits, Parent1.Bits, CrossBit);
end;
|