Skip to content

Commit 4d0fb14

Browse files
author
Tomasz bla Fortuna
committed
Slight update - arbitrary intersections in GenomeSplitOrganism
+ statistical test making a case for this mode.
1 parent fe625e2 commit 4d0fb14

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

demo_case.py

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def __repr__(self):
5656

5757
class CodeHacker(GenomeSplitOrganism):
5858

59+
chromosome_intersections = 2
5960
genome = genome
6061

6162
def get_code(self, lock):
@@ -159,5 +160,6 @@ def main():
159160
i += 1
160161
ph.gen()
161162

163+
162164
if __name__ == '__main__':
163165
main()
39 KB
Binary file not shown.

pygene/organism.py

+24-17
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,8 @@ class GenomeSplitOrganism(Organism):
398398
This organism can work better in situation where `connected'
399399
genes are located close to each other on the genome.
400400
"""
401-
401+
chromosome_intersections = 2
402+
402403
def mate(self, partner):
403404
"""
404405
Mates this organism with another organism to
@@ -418,30 +419,36 @@ def mate(self, partner):
418419
# g.g.g.g.g.g
419420
# ^- split in 5:
420421
# G.G.G.G.G.G
421-
intersection = randint(0, len(self.genome))
422-
# gene by gene, we assign our and partner's genes randomly
422+
423+
# Generate two random intersections
424+
intersections = set(randrange(0, len(self.genome))
425+
for i in range(self.chromosome_intersections))
426+
427+
intersections = list(sorted(intersections))
428+
429+
source_a = self.genes
430+
source_b = partner.genes
431+
# gene by gene, we assign our and partner's genes
423432
for i, name in enumerate(sorted(self.genome.keys())):
424-
cls = self.genome[name]
433+
if i in intersections:
434+
source_a, source_b = source_b, source_a
425435

426-
ourGene = self.genes.get(name, None)
427-
if not ourGene:
428-
ourGene = cls()
436+
gene_a = source_a.get(name, None)
437+
if not gene_a:
438+
gene_a = self.genome[name]()
429439

430-
partnerGene = partner.genes.get(name, None)
431-
if not partnerGene:
432-
partnerGene = cls()
440+
gene_b = source_b.get(name, None)
441+
if not gene_b:
442+
gene_b = self.genome[name]()
433443

434-
# randomly assign genes to first or second child
435-
if i < intersection:
436-
genotype1[name] = ourGene
437-
genotype2[name] = partnerGene
438-
else:
439-
genotype1[name] = partnerGene
440-
genotype2[name] = ourGene
444+
# assign genes to first or second child
445+
genotype1[name] = gene_a
446+
genotype2[name] = gene_b
441447

442448
# got the genotypes, now create the child organisms
443449
child1 = self.__class__(**genotype1)
444450
child2 = self.__class__(**genotype2)
451+
445452
return (child1, child2)
446453

447454
class MendelOrganism(BaseOrganism):

0 commit comments

Comments
 (0)