<commit_before>import abc
import random
import math
import copy
import config
import compiler_flags
import enums
import debug
import individual
import collections
import internal_exceptions

class SearchStrategy:
    """Abstract class for a search strategy"""
    
    @abc.abstractmethod
    def run(self):
        pass
    
    @abc.abstractmethod
    def summarise(self):
        pass
    
class GA(SearchStrategy):
    """Search using a genetic algorithm"""
    
    def set_child_flags(self, child, the_flags, the_flag_values):
        for idx, flag in enumerate(the_flags):
            if flag in compiler_flags.PPCG.optimisation_flags:
                child.ppcg_flags[flag] = the_flag_values[idx]
            elif flag in compiler_flags.CC.optimisation_flags:
                child.cc_flags[flag] = the_flag_values[idx]
            elif flag in compiler_flags.CXX.optimisation_flags:
                child.cxx_flags[flag] = the_flag_values[idx]
            elif flag in compiler_flags.NVCC.optimisation_flags:
                child.nvcc_flags[flag] = the_flag_values[idx]
            else:
                assert False, "Unknown flag %s" % flag
    
    def one_point(self, mother, father, children):
        """Implementation of 1-point crossover"""
        father_flags = father.all_flag_values()
        mother_flags = mother.all_flag_values()
        assert len(father_flags) == len(mother_flags)
        
        # Compute the crossover indices            
        point1 = 0
        point2 = random.randint(point1, len(mother_flags))
        point3 = len(mother_flags)
        
        child1_flags = []
        child1_flags.extend(mother_flags[point1:point2])
        child1_flags.extend(father_flags[point2:point3])
        child1 = individual.Individual()
        self.set_child_flags(child1, mother.all_flags(), child1_flags)
        # We handle the crossover of the --sizes flag in a special manner as the
        # values of this flag are not simple scalar values
        the_sizes_flag = compiler_flags.PPCG.flag_map[compiler_flags.PPCG.sizes]
        child1.ppcg_flags[the_sizes_flag] = compiler_flags.SizesFlag.crossover(self, mother.ppcg_flags[the_sizes_flag], father.ppcg_flags[the_sizes_flag])
        
        if children == 1:
            return [child1]
        
        child2_flags = []
        child2_flags.extend(father_flags[point1:point2])
        child2_flags.extend(mother_flags[point2:point3])
        child2 = individual.Individual()
        self.set_child_flags(child2, mother.all_flags(), child2_flags)
        # We handle the crossover of the --sizes flag in a special manner as the
        # values of this flag are not simple scalar values
        the_sizes_flag = compiler_flags.PPCG.flag_map[compiler_flags.PPCG.sizes]
        child2.ppcg_flags[the_sizes_flag] = compiler_flags.SizesFlag.crossover(self, father.ppcg_flags[the_sizes_flag], mother.ppcg_flags[the_sizes_flag])
        
        return [child1, child2]
          
    def two_point(self, mother, father, children):
        """Implementation of 2-point crossover"""
        father_flags = father.all_flag_values()
        mother_flags = mother.all_flag_values()
        if len(father_flags) != len(mother_flags):
            print father.ID
            print mother.ID
            debug.exit_message("""Disparity in mother and father flags. 
Father has: %s
Mother has: %s""" \
% (' '.join(flag.name for flag in father.all_flags()),
   ' '.join(flag.name for flag in mother.all_flags())))
        
        # Compute the crossover indices            
        point1 = 0
        point2 = random.randint(point1, len(mother_flags))
        point3 = random.randint(point2, len(mother_flags))
        point4 = len(mother_flags)
        
        child1_flags = []
        child1_flags.extend(mother_flags[point1:point2])
        child1_flags.extend(father_flags[point2:point3])
        child1_flags.extend(mother_flags[point3:point4])
        child1 = individual.Individual()
        self.set_child_flags(child1, mother.all_flags(), child1_flags)
        # We handle the crossover of the --sizes flag in a special manner as the
        # values of this flag are not simple scalar values
        the_sizes_flag = compiler_flags.PPCG.flag_map[compiler_flags.PPCG.sizes]
        child1.ppcg_flags[the_sizes_flag] = compiler_flags.SizesFlag.crossover(self, mother.ppcg_flags[the_sizes_flag], father.ppcg_flags[the_sizes_flag])
        
        if children == 1:
            return [child1]
        
        child2_flags = []
        child2_flags.extend(father_flags[point1:point2])
        child2_flags.extend(mother_flags[point2:point3])
        child2_flags.extend(father_flags[point3:point4])
        child2 = individual.Individual()
        self.set_child_flags(child1, mother.all_flags(), child2_flags)
        # We handle the crossover of the --sizes flag in a special manner as the
        # values of this flag are not simple scalar valuesself.set_child_flags(child2, mother.all_flags(), child2_flags)<commit_msg>Removed debug code in favour of assert statement<commit_after>import abc
import random
import math
import copy
import config
import compiler_flags
import enums
import debug
import individual
import collections
import internal_exceptions

class SearchStrategy:
    """Abstract class for a search strategy"""
    
    @abc.abstractmethod
    def run(self):
        pass
    
    @abc.abstractmethod
    def summarise(self):
        pass
    
class GA(SearchStrategy):
    """Search using a genetic algorithm"""
    
    def set_child_flags(self, child, the_flags, the_flag_values):
        for idx, flag in enumerate(the_flags):
            if flag in compiler_flags.PPCG.optimisation_flags:
                child.ppcg_flags[flag] = the_flag_values[idx]
            elif flag in compiler_flags.CC.optimisation_flags:
                child.cc_flags[flag] = the_flag_values[idx]
            elif flag in compiler_flags.CXX.optimisation_flags:
                child.cxx_flags[flag] = the_flag_values[idx]
            elif flag in compiler_flags.NVCC.optimisation_flags:
                child.nvcc_flags[flag] = the_flag_values[idx]
            else:
                assert False, "Unknown flag %s" % flag
    
    def one_point(self, mother, father, children):
        """Implementation of 1-point crossover"""
        father_flags = father.all_flag_values()
        mother_flags = mother.all_flag_values()
        assert len(father_flags) == len(mother_flags)
        
        # Compute the crossover indices            
        point1 = 0
        point2 = random.randint(point1, len(mother_flags))
        point3 = len(mother_flags)
        
        child1_flags = []
        child1_flags.extend(mother_flags[point1:point2])
        child1_flags.extend(father_flags[point2:point3])
        child1 = individual.Individual()
        self.set_child_flags(child1, mother.all_flags(), child1_flags)
        # We handle the crossover of the --sizes flag in a special manner as the
        # values of this flag are not simple scalar values
        the_sizes_flag = compiler_flags.PPCG.flag_map[compiler_flags.PPCG.sizes]
        child1.ppcg_flags[the_sizes_flag] = compiler_flags.SizesFlag.crossover(self, mother.ppcg_flags[the_sizes_flag], father.ppcg_flags[the_sizes_flag])
        
        if children == 1:
            return [child1]
        
        child2_flags = []
        child2_flags.extend(father_flags[point1:point2])
        child2_flags.extend(mother_flags[point2:point3])
        child2 = individual.Individual()
        self.set_child_flags(child2, mother.all_flags(), child2_flags)
        # We handle the crossover of the --sizes flag in a special manner as the
        # values of this flag are not simple scalar values
        the_sizes_flag = compiler_flags.PPCG.flag_map[compiler_flags.PPCG.sizes]
        child2.ppcg_flags[the_sizes_flag] = compiler_flags.SizesFlag.crossover(self, father.ppcg_flags[the_sizes_flag], mother.ppcg_flags[the_sizes_flag])
        
        return [child1, child2]
          
    def two_point(self, mother, father, children):
        """Implementation of 2-point crossover"""
        father_flags = father.all_flag_values()
        mother_flags = mother.all_flag_values()
        assert len(father_flags) == len(mother_flags)
        
        # Compute the crossover indices            
        point1 = 0
        point2 = random.randint(point1, len(mother_flags))
        point3 = random.randint(point2, len(mother_flags))
        point4 = len(mother_flags)
        
        child1_flags = []
        child1_flags.extend(mother_flags[point1:point2])
        child1_flags.extend(father_flags[point2:point3])
        child1_flags.extend(mother_flags[point3:point4])
        child1 = individual.Individual()
        self.set_child_flags(child1, mother.all_flags(), child1_flags)
        # We handle the crossover of the --sizes flag in a special manner as the
        # values of this flag are not simple scalar values
        the_sizes_flag = compiler_flags.PPCG.flag_map[compiler_flags.PPCG.sizes]
        child1.ppcg_flags[the_sizes_flag] = compiler_flags.SizesFlag.crossover(self, mother.ppcg_flags[the_sizes_flag], father.ppcg_flags[the_sizes_flag])
        
        if children == 1:
            return [child1]
        
        child2_flags = []
        child2_flags.extend(father_flags[point1:point2])
        child2_flags.extend(mother_flags[point2:point3])
        child2_flags.extend(father_flags[point3:point4])
        child2 = individual.Individual()
        self.set_child_flags(child1, mother.all_flags(), child2_flags)
        # We handle the crossover of the --sizes flag in a special manner as the
        # values of this flag are not simple scalar valuesself.set_child_flags(child2, mother.all_flags(), child2_flags)
{'fraction_non_alphanumeric': 0.08412455334354263, 'fraction_numerical': 0.014497192445125064, 'mean_word_length': 2.798371461806902, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5960992', 'n_tokens_mistral': 2997, 'n_tokens_neox': 2887, 'n_words': 636}