def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.randomizer = Randomizer(Random())

        selector_configuration = [
            EvolutionRandomSelectorConfiguration.create(),
            EvolutionRouletteSelectorConfiguration.create()
        ]
        self.configuration = EvolutionConfiguration.create(
            selector_configuration, inversion_chance=0, mutation_chance=0, crossover_chance=0)
        self.create_rule_population()
        self.create_grammar_statistics()
        self.create_rule_adding()

        self.sut = EvolutionService(self.randomizer)

        self.rules = [
            Rule(Symbol('S'), Symbol('NP'), Symbol('VP')),
            Rule(Symbol('VP'), Symbol('VP'), Symbol('PP')),
            Rule(Symbol('VP'), Symbol('V'), Symbol('NP')),
            TerminalRule(Symbol('VP'), Symbol('eats')),
            Rule(Symbol('PP'), Symbol('P'), Symbol('NP')),
            Rule(Symbol('NP'), Symbol('Det'), Symbol('N')),
            TerminalRule(Symbol('NP'), Symbol('she')), TerminalRule(Symbol('V'), Symbol('eats')),
            TerminalRule(Symbol('P'), Symbol('with')), TerminalRule(Symbol('N'), Symbol('fish')),
            TerminalRule(Symbol('N'), Symbol('fork')), TerminalRule(Symbol('Det'), Symbol('a'))
        ]
 def __init__(self, randomizer, run_no, cyk_service_variant=None):
     self.cyk_service_variant = cyk_service_variant if cyk_service_variant is not None \
         else CykServiceVariationManager(False)
     self.randomizer = randomizer
     self.configuration = None
     self.rule_adding = AddingRuleSupervisor.default(randomizer)
     self.grammar_estimator = None
     self.induction = self.cyk_service_variant.create_cyk_service(randomizer, self.rule_adding)
     self.evolution = EvolutionService(randomizer)
     self.stop_criteria = [NoStopCriteriaSpecified()]
     self.run_no = run_no
class GcsRunner(object):
    def __init__(self, randomizer, run_no, cyk_service_variant=None):
        self.cyk_service_variant = cyk_service_variant if cyk_service_variant is not None \
            else CykServiceVariationManager(False)
        self.randomizer = randomizer
        self.configuration = None
        self.rule_adding = AddingRuleSupervisor.default(randomizer)
        self.grammar_estimator = None
        self.induction = self.cyk_service_variant.create_cyk_service(randomizer, self.rule_adding)
        self.evolution = EvolutionService(randomizer)
        self.stop_criteria = [NoStopCriteriaSpecified()]
        self.run_no = run_no

    def create_stop_criteria(self):
        self.stop_criteria = [
                                 FitnessStopCriteria(self.grammar_estimator, self.configuration),
                                 StepStopCriteria(self.configuration),
                                 TimeStopCriteria(self.configuration)
                             ]

    def _random_symbol_id(self, configuration):
        return self.randomizer.randint(
            RulePopulation.symbol_shift(),
            RulePopulation.symbol_shift() + configuration.rule.max_non_terminal_symbols)

    def generate_random_rules(self, provided_rules):
        rules = set()
        rules |= set(provided_rules)
        while len(rules) < self.configuration.rule.random_starting_population_size:
            rules.add(Rule(Symbol(self._random_symbol_id(self.configuration)),
                           Symbol(self._random_symbol_id(self.configuration)),
                           Symbol(self._random_symbol_id(self.configuration))))

        return list(rules)

    def add_initial_rules(self, initial_rules, rule_population, grammar_statistics):
        for rule in initial_rules:
            rule_population.add_rule(rule, self.randomizer)
            grammar_statistics.on_added_new_rule(rule)

    def perform_gcs(self, initial_rules, configuration, grammar_estimator,
                    grammar_statistics, sentences):
        self.configuration = configuration
        self.rule_adding.configuration = self.configuration.rule.adding
        self.grammar_estimator = grammar_estimator
        self.create_stop_criteria()

        rule_population = self.cyk_service_variant.create_rule_population(
            self.configuration.rule.starting_symbol, self.configuration.rule.universal_symbol,
            max_non_terminal_symbols=self.configuration.rule.max_non_terminal_symbols)

        self.add_initial_rules(self.generate_random_rules(initial_rules), rule_population,
                               grammar_statistics)

        evolution_step = 0

        # print('')
        while not any(cr() for cr in self.stop_criteria):
            # print('.', end='')
            evolution_step_estimator = EvolutionStepEstimator()
            self.induction.perform_cyk_for_all_sentences(rule_population, sentences,
                                                         evolution_step_estimator,
                                                         self.configuration.induction,
                                                         grammar_statistics)

            self.grammar_estimator.append_step_estimation(evolution_step, evolution_step_estimator)

            if self.configuration.should_run_evolution:
                self.evolution.run_genetic_algorithm(grammar_statistics, rule_population,
                                                     self.rule_adding, self.configuration.evolution)

            evolution_step += 1
            self._post_step_actions(evolution_step)

        stop_reasoning = next(cr for cr in self.stop_criteria if cr.has_been_fulfilled())
        fitness_reached = self.grammar_estimator['fitness'].get_global_max()
        # for x in rule_population.get_all_non_terminal_rules():
        #     print(x)
        # for x in rule_population.get_terminal_rules():
        #     print(x)

        return rule_population, stop_reasoning, fitness_reached, evolution_step

    def _post_step_actions(self, step):
        pass
class TestEvolution(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.randomizer = Randomizer(Random())

        selector_configuration = [
            EvolutionRandomSelectorConfiguration.create(),
            EvolutionRouletteSelectorConfiguration.create()
        ]
        self.configuration = EvolutionConfiguration.create(
            selector_configuration, inversion_chance=0, mutation_chance=0, crossover_chance=0)
        self.create_rule_population()
        self.create_grammar_statistics()
        self.create_rule_adding()

        self.sut = EvolutionService(self.randomizer)

        self.rules = [
            Rule(Symbol('S'), Symbol('NP'), Symbol('VP')),
            Rule(Symbol('VP'), Symbol('VP'), Symbol('PP')),
            Rule(Symbol('VP'), Symbol('V'), Symbol('NP')),
            TerminalRule(Symbol('VP'), Symbol('eats')),
            Rule(Symbol('PP'), Symbol('P'), Symbol('NP')),
            Rule(Symbol('NP'), Symbol('Det'), Symbol('N')),
            TerminalRule(Symbol('NP'), Symbol('she')), TerminalRule(Symbol('V'), Symbol('eats')),
            TerminalRule(Symbol('P'), Symbol('with')), TerminalRule(Symbol('N'), Symbol('fish')),
            TerminalRule(Symbol('N'), Symbol('fork')), TerminalRule(Symbol('Det'), Symbol('a'))
        ]

    def create_rule_population(self):
        self.starting_symbol = Symbol('S')
        self.rule_population = RulePopulation(self.starting_symbol)

    def create_grammar_statistics(self):
        configuration = ClassicalStatisticsConfiguration.default()
        configuration.base_fitness = 5
        configuration.classical_fitness_weight = 1
        configuration.fertility_weight = 1
        configuration.positive_weight = 1
        configuration.negative_weight = 1

        self.grammar_statistics = GrammarStatistics(
            configuration, self.randomizer, ClassicRuleStatistics(),
            ClassicFitness())

    def create_rule_adding(self):
        configuration = AddingRulesConfiguration.create(
            crowding_factor=2,
            crowding_size=3,
            elitism_size=2,
            max_non_terminal_rules=19
        )

        adding_strategies = [SimpleAddingRuleStrategy(),
                             AddingRuleWithCrowdingStrategy(),
                             AddingRuleWithElitismStrategy()]

        self.rule_adding = AddingRuleSupervisor(self.randomizer, configuration, adding_strategies)

    def simulate_induction_part_work(self, rules):
        for rule in rules:
            self.rule_adding.add_rule(rule, self.rule_population, self.grammar_statistics)

        for rule in rules:
            rule_usage_info = ClassicRuleUsageInfo(True, 1)
            positive_usages = self.randomizer.randint(1, 4)
            for _ in range(positive_usages):
                self.grammar_statistics.on_rule_usage(rule, rule_usage_info)

            rule_usage_info.positive_sentence = False
            self.grammar_statistics.on_rule_usage(rule, rule_usage_info)

            self.grammar_statistics.fitness.get(self.grammar_statistics, rule)

    def get_symbols_from_rules(self, rules):
        return {y for y in chain.from_iterable(
            (x.parent, x.left_child, x.right_child) for x in rules)}

    def assert_contains_rules(self, rules, rules_pool):
        for rule in rules:
            assert_that(rules_pool, has_item(rule))

    def count_rules_that_has_changed(self, old):
        changed_rules = 0
        for rule in self.rule_population.get_all_non_terminal_rules():
            changed_rules += 1 if rule not in old else 0
        return changed_rules

    def test_given_no_operator_used_rule_population_should_remain_unchanged(self):
        # Given:
        self.simulate_induction_part_work(self.rules)

        old_population = copy.deepcopy(self.rule_population)

        # When:
        self.sut.run_genetic_algorithm(self.grammar_statistics, self.rule_population,
                                       self.rule_adding, self.configuration)

        # Then:
        self.assert_contains_rules(self.rule_population.get_all_non_terminal_rules(),
                                   list(old_population.get_all_non_terminal_rules()))

        old_symbols = self.get_symbols_from_rules(old_population.get_all_non_terminal_rules())
        new_symbols = self.get_symbols_from_rules(self.rule_population.get_all_non_terminal_rules())
        assert_that(old_symbols, has_items(*new_symbols))

    def promote_those_rules_to_elite(self, rules):
        for rule in rules:
            rule_usage_info = ClassicRuleUsageInfo(True, 1)
            positive_usages = 5
            for _ in range(positive_usages):
                self.grammar_statistics.on_rule_usage(rule, rule_usage_info)

    def test_given_high_operator_usage_rule_population_should_expect_some_major_change(self):
        # Given:
        self.configuration.operators.inversion.chance = 1
        self.configuration.operators.mutation.chance = 1
        self.configuration.operators.crossover.chance = 1

        self.simulate_induction_part_work(self.rules)
        self.promote_those_rules_to_elite(self.rules[0:2])

        elite = list(self.rules[0:2])

        old_population = copy.deepcopy(self.rule_population)

        # When:
        self.sut.run_genetic_algorithm(self.grammar_statistics, self.rule_population,
                                       self.rule_adding, self.configuration)

        # Then:
        self.assert_contains_rules(elite, list(old_population.get_all_non_terminal_rules()))

        assert_that(self.count_rules_that_has_changed(old_population.get_all_non_terminal_rules()),
                    is_(greater_than_or_equal_to(2)))

        old_symbols = self.get_symbols_from_rules(old_population.get_all_non_terminal_rules())
        new_symbols = self.get_symbols_from_rules(self.rule_population.get_all_non_terminal_rules())
        assert_that(old_symbols, not_(has_items(*new_symbols)))