<commit_before>module GeneGenie
  # A Gene is the basic unit of the genetic algorithm. Genes hold the
  # information used to evaluate their fitness.
  # They are combined into new Genes during the optimisation process.
  # @since 0.0.1
  class Gene
    def initialize(information, fitness_evaluator)
      fail ArgumentError, 'information must be Array' unless information.kind_of? Array
      fail ArgumentError, 'information must be Array of Hashes' unless information[0].kind_of? Hash

      @information = information
      @fitness_evaluator = fitness_evaluator
    end

    def to_hashes
      @information
    end

    def fitness
      @fitness ||= @fitness_evaluator.fitness(@information)
    end

    def mutate(mutator)
      @information = mutator.call @information
      self
    end

    def combine(other_gene)
      other_gene_hash = other_gene.to_hashes
      new_information = @information.map.with_index do |part, index|
        new_hash = {}
        part.each do |k, v|
          new_hash[k] = (rand > 0.5) ? v : other_gene_hash[index][k]
        end
      end
      Gene.new(new_information, @fitness_evaluator)
    end

    def <=>(other)
      fitness <=> other.fitness
    end
  end
end
<commit_msg>Ensure that mutation always resets fitness score<commit_after>module GeneGenie
  # A Gene is the basic unit of the genetic algorithm. Genes hold the
  # information used to evaluate their fitness.
  # They are combined into new Genes during the optimisation process.
  # @since 0.0.1
  class Gene
    def initialize(information, fitness_evaluator)
      fail ArgumentError, 'information must be Array' unless information.kind_of? Array
      fail ArgumentError, 'information must be Array of Hashes' unless information[0].kind_of? Hash

      @information = information
      @fitness_evaluator = fitness_evaluator
    end

    def to_hashes
      @information
    end

    def fitness
      @fitness ||= @fitness_evaluator.fitness(@information)
    end

    def mutate(mutator)
      @information = mutator.call @information
      @fitness = nil
      self
    end

    def combine(other_gene)
      other_gene_hash = other_gene.to_hashes
      new_information = @information.map.with_index do |part, index|
        new_hash = {}
        part.each do |k, v|
          new_hash[k] = (rand > 0.5) ? v : other_gene_hash[index][k]
        end
      end
      Gene.new(new_information, @fitness_evaluator)
    end

    def <=>(other)
      fitness <=> other.fitness
    end
  end
end

{'fraction_non_alphanumeric': 0.07507987220447285, 'fraction_numerical': 0.004792332268370607, 'mean_word_length': 2.8069908814589666, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5663912', 'n_tokens_mistral': 757, 'n_tokens_neox': 740, 'n_words': 228}