Exemplo n.º 1
0
    def test_binary_tournament(self):
        """ Test that binary tournament works as expected """
        self.config.max_generations = 3
        self.config.fitness_function_type = FitnessType.FULL_MATCH
        self.config.selection_type = SelectionType.BINARY_TOURNAMENT
        p = Population(self.samples, self.grammar, self.stats)
        mating_pool = p.selection(p.generation)

        super().assertNotEqual(p.generation, mating_pool)
Exemplo n.º 2
0
 def test_random_one_point_crossover(self):
     """ Test that crossover 'random one point' works as expected """
     self.config.max_generations = 3
     self.config.fitness_function_type = FitnessType.BASIC
     self.config.selection_type = SelectionType.BINARY_TOURNAMENT
     self.config.recombination_type = RecombinationType.RANDOM_ONE_POINT_CROSSOVER
     p = Population(self.samples, self.grammar, self.stats)
     mating_pool = p.selection(p.generation)
     p.offspring = p.recombination(mating_pool, p.generation)
     super().assertNotEqual(p.generation, p.offspring)
Exemplo n.º 3
0
 def test_evolve(self):
     """ Tests that an evolution works, preserving a fitted individual """
     self.config.max_generations = 3
     self.config.fitness_function_type = FitnessType.BASIC
     p = Population(self.samples, self.grammar, self.stats)
     self.config.mutation_probability = 0.0
     p.generation[0] = Individual(self.samples, self.grammar, self.stats,
                                  '01110101100101100110010110010101')
     self.config.mutation_probability = 0.5
     p.evolve()
     super().assertLessEqual(0.25, p.generation[0].fitness_value)
Exemplo n.º 4
0
    def test_best_challenge(self):
        """ Tests that the most fitted individual occupies the population's best_individual slot """
        self.config.max_generations = 3
        self.config.fitness_function_type = FitnessType.BASIC
        p = Population(self.samples, self.grammar, self.stats)
        self.config.mutation_probability = 0.0
        p.generation[0] = Individual(self.samples, self.grammar, self.stats,
                                     '01110101100101100110010110010101')
        self.config.mutation_probability = 0.5
        p.evolve()

        super().assertGreaterEqual(p.best_individual.fitness_value, 0.2)
Exemplo n.º 5
0
 def test_mu_lambda_no_elite(self):
     """ Tests that replacement 'mu lambda without elitism' works as expected """
     self.config.replacement_type = ReplacementType.MU_LAMBDA_WITHOUT_ELITISM
     p = Population(self.samples, self.grammar, self.stats)
     mating_pool = p.selection(p.generation)
     p.offspring = p.recombination(mating_pool, p.generation)
     p.generation, p.offspring = p.replacement(p.generation, p.offspring)
     super().assertListEqual(p.offspring, [])
Exemplo n.º 6
0
 def test_mu_plus_lambda(self):
     """ Tests that replacement 'mu plus lambda' works as expected """
     self.config.replacement_type = ReplacementType.MU_PLUS_LAMBDA
     p = Population(self.samples, self.grammar, self.stats)
     mating_pool = p.selection(p.generation)
     p.offspring = p.recombination(mating_pool, p.generation)
     p.generation, p.offspring = p.replacement(p.generation, p.offspring)
     super().assertListEqual(p.offspring, [])
Exemplo n.º 7
0
    def test_sr_update(self):
        """ Check SR is updated if a solution is found for the run """
        stats = Stats()

        self.config.max_generations = 1
        self.config.population_size = 3
        self.config.fitness_function_type = FitnessType.BASIC
        self.config.mutation_probability = 0.0

        self.config.success_threshold = 0.0
        p = Population(self.samples, self.grammar, stats)
        p.generation[0] = Individual(self.samples, self.grammar, stats,
                                     '01110101100101100110010110010101')
        p.evolve()
        super().assertListEqual([True], stats.success_rate_accumulator)

        self.config.success_threshold = 1.0
        self.config.population_size = 1
        p = Population(self.samples, self.grammar, stats)
        p.generation[0] = Individual(self.samples, self.grammar, stats,
                                     '00000000000000000000000000000000')
        p.evolve()
        super().assertListEqual([True, False], stats.success_rate_accumulator)
Exemplo n.º 8
0
 def test_k_tournament(self):
     """ Test that k tournament raises error """
     self.config.selection_type = SelectionType.K_TOURNAMENT
     p = Population(self.samples, self.grammar, self.stats)
     with super().assertRaises(NotImplementedError):
         _ = p.selection(p.generation)
Exemplo n.º 9
0
    def test_initialize(self):
        """ Tests that a population is correctly filled with Individuals """
        p = Population(self.samples, self.grammar, self.stats)

        super().assertIsInstance(p.generation[0], Individual)
Exemplo n.º 10
0
    def test_best_challenge_changes_best_individual(self):
        """ Covers best challenge cases """
        self.config.mutation_probability = 0.0
        self.config.fitness_function_type = FitnessType.BASIC

        p = Population(self.samples, self.grammar, self.stats)
        i1 = Individual(self.samples,
                        self.grammar,
                        self.stats,
                        dna='00000000000000000000000000000000')
        i2 = Individual(self.samples,
                        self.grammar,
                        self.stats,
                        dna='01110101100101100110010110010101')

        # When there's no best individual yet, population's best individual is updated
        p.best_individual = None
        p.generation = [i2]
        p._best_challenge()

        super().assertEqual(p.best_individual, p.generation[0])

        # When a better individual is better fitted in a new generation, population's best individual is updated
        p.best_individual = i1
        p.generation = [i2]
        p._best_challenge()

        super().assertEqual(p.best_individual, p.generation[0])

        # When a worse individual is the most fitted in a new generation, population's best individual remains the same
        p.best_individual = i2
        p.generation = [i1]
        p._best_challenge()

        super().assertEqual(i2, p.best_individual)
Exemplo n.º 11
0
def find_patterns(
    samples: List[str],
    configuration: Union[str, None] = None,
    spacy_language_model_name: Union[str,
                                     None] = None) -> List[Tuple[Any, ...]]:
    """
    Given some samples, this function finds optimized patterns to be used by the Spacy's Rule Based Matcher.
    Args:
        samples: List of strings from where to find common linguistic patterns
        configuration: (str) Optional configuration file path to to be loaded (Fallbacks to default configuration)
        spacy_language_model_name: (str) Optional valid Spacy Language Model (Fallbacks to Spacy's en_core_web_sm)

    Returns: List of patterns found and list of each pattern matching score against the samples

    """
    LOG.info(f'Loading language model {spacy_language_model_name}...')
    if 'en-core-web-sm' not in [
            d.project_name for d in pkg_resources.working_set
    ]:
        LOG.info(
            f'PatternOmatic\'s default spaCy\'s Language Model not installed,'
            f' proceeding to install en_core_web_sm, please wait...')
        spacy_download('en_core_web_sm')

    try:
        nlp = spacy_load(spacy_language_model_name)
    except OSError:
        LOG.warning(
            f'Model {spacy_language_model_name} not found, '
            f'falling back to patternOmatic\'s default language model: en_core_web_sm'
        )

        nlp = spacy_load('en_core_web_sm')

    LOG.info(f'Building Doc instances...')
    samples = [nlp(sample) for sample in samples]

    if isinstance(configuration, str):
        LOG.info(
            f'Setting up configuration from the following path: {configuration}...'
        )
        config = Config(config_file_path=configuration)
    else:
        config = Config()
        LOG.info(f'Existing Config instance found: {config}')

    stats = Stats()

    bnf_g = dgg(samples)

    LOG.info('Starting Execution...')
    for _ in range(0, config.max_runs):
        start = time.monotonic()
        p = Population(samples, bnf_g, stats)
        p.evolve()
        end = time.monotonic()
        stats.add_time(end - start)
        stats.calculate_metrics()

    LOG.info(f'Execution report {stats}')
    stats.persist()

    LOG.info(f'Best individuals for this execution:')
    stats.most_fitted_accumulator.sort(key=lambda i: i.fitness_value,
                                       reverse=True)
    for individual in stats.most_fitted_accumulator:
        LOG.info(f'{individual}')

    return list(
        zip(*[[i.fenotype, i.fitness_value]
              for i in stats.most_fitted_accumulator]))