Esempio n. 1
0
def handle_iif(expr, split_expr, dictionary):
    """ create the 4 rules related to the iif expression """

    symb = "=>"

    not_cdt = "!(%s)" % split_expr[0]
    not_cc = "!(%s)" % split_expr[2]

    rules = []
    rules.append(Rule(expr, [split_expr[0], symb, split_expr[2]], dictionary))
    rules.append(Rule(expr, [not_cdt, symb, not_cc], dictionary))
    rules.append(Rule(expr, [split_expr[2], symb, split_expr[0]], dictionary))
    rules.append(Rule(expr, [not_cc, symb, not_cdt], dictionary))

    return rules
Esempio n. 2
0
    def __init__(self, text: str, number: int):
        self.text: str = text.rstrip()
        if '\n' in self.text:
            raise GeneratorError(_MULTILINE_STRING_ERROR, number)

        self.number: int = number
        self.rules: list[Rule] = Rule.extract(number, text)
Esempio n. 3
0
    def evolve(self, epoch=1500):
        while (self._generation < epoch):
            for individual in self._population:
                self._evaluate_fitness(individual)
            self._display_population_fitness(self._population)
            self._population = self._generate_offspring(self._population)
            best = self._get_best_individual(self._population)
            if (best.fitness == self._max_fitness):
                break
            self._reset_population_fitness(self._population)

            if (self._generation != 0 and (self._generation + 1) % 5 == 0):
                self.evalute_test(self._population)
                self._reset_population_fitness(self._population)

            self._generation += 1

        rules = Rule.generate_rules_from_chromosome(best.chromosome,
                                                    self._feature_size,
                                                    self._rule_size)

        self.evalute_test(self._population)
        print("[BEST RULE BASE]\n")
        for rule in rules:
            print("RULE: {} {}".format(rule.feature, rule.label))

        draw_graph(self._generation_info, epoch, self._crossover_probability,
                   self._mutation_probabilty, self._rule_count,
                   self._population_size, len(self._test_data))
        self.test_rules(rules)
        self.floating_point_to_rules(rules)
Esempio n. 4
0
    def _evaluate_fitness(self, individual):
        rules = Rule.generate_rules_from_chromosome(individual.chromosome,
                                                    self._feature_size,
                                                    self._rule_size)

        for data in self._data:
            for rule in rules:
                if (self._does_rule_match_data(rule.feature, data.feature)):
                    if (rule.label == int(data.label)):
                        individual.fitness += 1
                    break
Esempio n. 5
0
    def _evaluate_fitness(self, individual, use_test=False):
        rules = Rule.generate_rules_from_chromosome(individual.chromosome,
                                                    self._feature_size,
                                                    self._rule_size)

        dataset = self._test_data if use_test else self._train_data
        for data in dataset:
            for rule in rules:
                if (self._does_rule_match(data.features, rule.feature)):
                    if (data.prediction == rule.label):
                        individual.fitness += 1
                    break
Esempio n. 6
0
def create_rule(expr, dictionary):
    """ create a list of Rule object with all the rules needed to resolve the
    given expression
    """

    split = split_expr(expr)
    if None in split:
        error(-1)
    if '<' in split[1]:
        rules = handle_iif(expr, split, dictionary)
    else:
        rules = [Rule(expr, split, dictionary)]

    if td.Error in rules:
        error(-1)

    return rules
    def evolve(self, epoch=1000):

        while (self._generation < epoch):
            self.__evaluate_population_fitness(self._population)
            self._display_population_fitness(self._population)
            self._population = self._generate_offspring(self._population)
            best = self._get_best_individual(self._population)
            if (best.fitness == self._max_fitness):
                break
            self._reset_population_fitness(self._population)
            self._generation += 1

        rules = Rule.generate_rules_from_chromosome(best.chromosome,
                                                    self._feature_size,
                                                    self._rule_size)

        print("[BEST RULE BASE]")
        for rule in rules:
            rule.feature.append(rule.label)
            print("{}".format(rule.feature))

        draw_graph(self._generation_info, epoch, "Adaptive", "Adaptive",
                   self._rule_count, self._population_size)
Esempio n. 8
0
    def evolve(self, epochs=2000):
        while (self._generation < epochs):
            for individual in self._population:
                self._evaluate_fitness(individual)
            self._display_population_fitness(self._population)
            self._population = self._generate_offspring(self._population)
            best = self._get_best_individual(self._population)
            if (best.fitness == self._max_fitness):
                break
            self._reset_population_fitness(self._population)
            self._generation += 1

        rules = Rule.generate_rules_from_chromosome(best.chromosome,
                                                    self._feature_size,
                                                    self._rule_size)

        print("[BEST RULE BASE]")
        for rule in rules:
            print("RULE: {} {}".format(rule.feature, rule.label))

        draw_graph(self._generation_info, epochs, self._crossover_probability,
                   self._mutation_probabilty, self._rule_count,
                   self._population_size)