<jupyter_start><jupyter_text>Gene  Code<jupyter_code>from importer import *
from helpers import Distributions
from config import Configuration

class Gene:
    
    inno_tracker = 0
    
    def __init__(self, generation=None, copy=False, gene=None):
        if not copy:
            self.inno_num = Gene.inno_tracker 
            Gene.inno_tracker+=1
            self.origin = generation
        else:
            self.inno_num = gene.inno_num
            self.origin = gene.origin
      
    def age(self, curGeneration):
        return curGeneration - self.origin
    
    def __lt__(self, other):
        return self.inno_num < other.inno_num
    
    def __eq__(self, other):
        return self.inno_num == other.inno_num
    
    def __repr__(self):
        return self.describe()

class NodeGene(Gene):
        
    def __init__(self, _type=None, generation=None, copy=False, gene=None):
        if not copy:
            assert generation > 0, 'Generation {} must be > 0'.format(generation)
            assert NodeGene.isValidType(_type), 'Invalid type (\'{}\') for NodeGene'.format(_type) #temporary
            super().__init__(generation=generation)
            self.type = _type
            self.bias = Configuration.DEFAULT_NODE_BIAS
            self.expressed  = Configuration.DEFAULT_NODE_EXPRESSED
            self.activation = Configuration.DEFAULT_NODE_ACTIVATION
        else:
            super().__init__(copy=True, gene=gene)
            self.type = gene.type
            self.bias = gene.bias
            self.expressed = gene.expressed
            self.activation = gene.activation
        
    @staticmethod
    def isValidType(_type):
        return _type in ['hidden', 'input', 'output']
        
    def copy(self, maintain_bias=False):        
        clone= NodeGene(copy=True, gene=self)
        if not maintain_bias:
            clone.bias = Configuration.DEFAULT_NODE_BIAS
        return clone
    
    def describe(self):
        return '<NodeGene-origin:{},inno_num:{},type:\'{}\',bias:{},activation:\'{}\'>'.format(self.origin,self.inno_num, self.type,
                                                                                                    self.bias, self.activation)
    

class ConnectionGene(Gene):
    
    def __init__(self, inNode=None, outNode=None, generation=None, copy=False, gene=None):
        if not copy:
            super().__init__(generation)
            self.inNode = inNode
            self.outNode = outNode
            self.weight = Distributions.sample_normal()
            self.expressed = True
            self.type = 'connection'
        else:
            super().__init__(copy=True, gene=gene)
            self.inNode = gene.inNode
            self.outNode = gene.outNode
            self.weight = gene.weight
            self.expressed = gene.expressed
        
    def copy(self, maintain_weights=False):
        clone = ConnectionGene(copy=True, gene=self)
        if not maintain_weights:
            clone.weight = Distributions.sample_normal()
        return clone
    
    def describe(self):
        return '<ConnectionGene-origin:{},inno_num:{},inNode:{},outNode:{},weight:{},expressed:{}>'.format(self.origin, self.inno_num, 
                                                                                                   self.inNode, self.outNode, 
                                                                                                   self.weight, self.expressed)


class PseudoGene(Gene):
    
    def __init__(self, inno_num=None):
        super().__init__(inno_num)<jupyter_output><empty_output><jupyter_text>Testing<jupyter_code>a = NodeGene('hidden', 1)
a
b = a.copy()
b
c = ConnectionGene(1, 2, 1)
c
d = NodeGene('hidden', 3)
d
c = ConnectionGene(1, 2, 5)
c
c, d<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10265859436693867, 'fraction_numerical': 0.0031587259805211897, 'mean_word_length': 1.880970432145565, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '583971', 'license': 'MIT', 'n_tokens_mistral': 1095, 'n_tokens_neox': 1039, 'n_words': 214, 'path': 'notebooks/Gene.ipynb', 'repo_name': 'kdonbekci/neuroevolution', 'size': '3799'}