Exemplo n.º 1
0
    def test_random(self):
        print(">> LogicProgram.random(variables, values, rule_min_size, rule_max_size, delay=1)")

        # No delay
        for i in range(self.__nb_unit_test):
            variables = ["x"+str(i) for i in range(random.randint(1,self.__nb_variables))]
            values = []

            for var in range(len(variables)):
                values.append([val for val in range(random.randint(2,self.__nb_values))])

            min_body_size = 0
            max_body_size = random.randint(min_body_size, len(variables))

            p = LogicProgram.random(variables, values, min_body_size, max_body_size)
            #eprint(p.to_string())

            self.assertEqual(p.get_variables(), variables)
            self.assertEqual(p.get_values(), values)

            for r in p.get_rules():
                self.assertTrue(len(r.get_body()) >= min_body_size)
                self.assertTrue(len(r.get_body()) <= max_body_size)

            states = p.states()

            for s in states:
                for var in range(len(s)):
                    matched = False
                    conclusion = -1
                    for r in p.get_rules():
                        if r.get_head_variable() == var and r.matches(s):
                            matched = True
                            if conclusion == -1: # stored first conclusion
                                conclusion = r.get_head_value()
                            else: # check conflict
                                self.assertEqual(conclusion, r.get_head_value())
                    self.assertTrue(matched)

            # No cross-matching
            for r1 in p.get_rules():
                for r2 in p.get_rules():
                    if r1 == r2 or r1.get_head_variable() != r2.get_head_variable():
                        continue
                    #eprint(r1)
                    #eprint(r2)
                    #eprint()
                    self.assertFalse(r1.cross_matches(r2))

        # Delay
        for i in range(self.__nb_unit_test):
            variables = ["x"+str(i) for i in range(random.randint(1,self.__nb_variables))]
            values = []

            for var in range(len(variables)):
                values.append([val for val in range(random.randint(2,self.__nb_values))])

            min_body_size = 0
            max_body_size = random.randint(min_body_size, len(variables))
            delay = random.randint(1, self.__max_delay)

            p = LogicProgram.random(variables, values, min_body_size, max_body_size, delay)
            #eprint(p.logic_form())

            extended_variables = variables.copy()
            extended_values = values.copy()
            for d in range(1,delay):
                extended_variables += [var+"_"+str(d) for var in variables]
                extended_values += values

            self.assertEqual(p.get_variables(), variables)
            self.assertEqual(p.get_values(), values)

            for r in p.get_rules():
                self.assertTrue(len(r.get_body()) >= min_body_size)
                #self.assertTrue(len(r.get_body()) <= max_body_size)

            p_ = LogicProgram(extended_variables, extended_values,[])
            states = p_.states()

            for s in states:
                for var in range(len(variables)):
                    matched = False
                    conclusion = -1
                    for r in p.get_rules():
                        if r.get_head_variable() == var and r.matches(s):
                            matched = True
                            if conclusion == -1: # stored first conclusion
                                conclusion = r.get_head_value()
                            else: # check conflict
                                self.assertEqual(conclusion, r.get_head_value())
                    self.assertTrue(matched)
Exemplo n.º 2
0
def test_random_LP(algorithm,
                   nb_variables,
                   nb_values,
                   max_body_size,
                   delay=1,
                   train_size=None):

    max_body_size = max(0, max_body_size)

    results_time = []
    results_common = []
    results_missing = []
    results_over = []
    results_precision = []

    for run in range(run_tests):

        variables = ["x" + str(i) for i in range(nb_variables)]
        values = []

        for var in range(len(variables)):
            values.append([val for val in range(nb_values)])

        min_body_size = 0

        p = LogicProgram.random(variables, values, min_body_size,
                                max_body_size, delay)
        serie_size = delay + random.randint(delay, 10)
        time_series = p.generate_all_time_series(serie_size)

        #eprint(p.logic_form())

        random.shuffle(time_series)
        train = time_series
        test = []

        if train_size is not None:
            if isinstance(train_size, float):  # percentage
                last_obs = max(int(train_size * len(time_series)), 1)
            else:  # exact number of transitions
                last_obs = train_size
            train = time_series[:last_obs]
            test = time_series[last_obs:]

        #eprint(train)

        if run == 0:
            eprint(">>> Start Training on ", len(train), "/", len(time_series),
                   " time series of size ", len(time_series[0]), " (",
                   round(100 * len(train) / len(time_series), 2), "%)")

        eprint("\r>>> run: ", run + 1, "/", run_tests, end='')
        start = time.time()
        model = algorithm.fit(p.get_variables(), p.get_values(), time_series)
        end = time.time()
        results_time.append(round(end - start, 3))

        common, missing, over = p.compare(model)

        #eprint(">>> Original:")
        #eprint(P.to_string())

        #eprint(">>> Learned:")
        #eprint(model.to_string())

        #eprint(">>> Logic Program comparaison:")
        #eprint(">>>> Common: "+str(len(common))+"/"+str(len(P.get_rules()))+"("+str(round(100 * len(common) / len(P.get_rules()),2))+"%)")
        #eprint(">>>> Missing: "+str(len(missing))+"/"+str(len(P.get_rules()))+"("+str(round(100 * len(missing) / len(P.get_rules()),2))+"%)")
        #eprint(">>>> Over: "+str(len(over))+"/"+str(len(P.get_rules()))+"("+str(round(100 * len(over) / len(model.get_rules()),2))+"%)")

        results_common.append(len(common))
        results_missing.append(len(missing))
        results_over.append(len(over))

        if len(test) == 0:
            test = train

        pred = [(s[:-1], model.next_state(s[:-1])) for s in test]
        test = [(s[:-1], s[-1]) for s in test]
        precision = round(LogicProgram.precision(test, pred), 2)

        #eprint(test)
        #eprint(pred)

        #eprint(">>> Prediction precision")
        #eprint(">>>> " + str(round(precision * 100,2)) + "%")

        results_precision.append(precision)

    run_time = round(sum(results_time) / run_tests, 3)
    common = sum(results_common) / run_tests
    missing = sum(results_missing) / run_tests
    over = sum(results_over) / run_tests
    precision = sum(results_precision) / run_tests

    eprint()
    eprint(">>> Run time: " + str(run_time) + "s")
    eprint(">>> Logic Program comparaison:")
    eprint(">>>> AVG Common: " + str(common) + "/" + str(len(p.get_rules())) +
           "(" + str(round(100 * common / len(p.get_rules()), 2)) + "%)")
    eprint(">>>> AVG Missing: " + str(missing) + "/" +
           str(len(p.get_rules())) + "(" +
           str(round(100 * missing / len(p.get_rules()), 2)) + "%)")
    eprint(">>>> AVG Over: " + str(over) + "/" + str(len(p.get_rules())) +
           "(" + str(round(100 * over / len(model.get_rules()), 2)) + "%)")

    eprint(">>> Prediction precision")
    eprint(">>>> AVG accuracy: " + str(round(precision * 100, 2)) + "%")

    return round(precision * 100, 2)
Exemplo n.º 3
0
    def test_fit(self):
        print(">> LFkT.fit(variables, values, time_series)")

        # No transitions
        variables = [
            "x" + str(i) for i in range(random.randint(1, self.__nb_variables))
        ]
        values = []

        for var in range(len(variables)):
            values.append(
                [val for val in range(random.randint(2, self.__nb_values))])

        min_body_size = 0
        max_body_size = random.randint(min_body_size, len(variables))
        delay_original = random.randint(2, self.__max_delay)
        p = LogicProgram.random(variables, values, min_body_size,
                                max_body_size, delay_original)
        p_ = LFkT.fit(p.get_variables(), p.get_values(), [])
        self.assertEqual(p_.get_variables(), p.get_variables())
        self.assertEqual(p_.get_values(), p.get_values())
        self.assertEqual(p_.get_rules(), [])

        for i in range(self.__nb_unit_test):
            #eprint("\rTest ", i+1, "/", self.__nb_unit_test, end='')

            # Generate transitions
            variables = [
                "x" + str(i)
                for i in range(random.randint(1, self.__nb_variables))
            ]
            values = []

            for var in range(len(variables)):
                values.append([
                    val for val in range(random.randint(2, self.__nb_values))
                ])

            min_body_size = 0
            max_body_size = random.randint(min_body_size, len(variables))
            delay_original = random.randint(2, self.__max_delay)

            p = LogicProgram.random(variables, values, min_body_size,
                                    max_body_size, delay_original)
            time_series = p.generate_all_time_series(delay_original * 10)

            #eprint(p.logic_form())
            #eprint(time_series)

            p_ = LFkT.fit(p.get_variables(), p.get_values(), time_series)
            rules = p_.get_rules()

            #eprint(p_.logic_form())

            for variable in range(len(p.get_variables())):
                for value in range(len(p.get_values()[variable])):
                    #eprint("var="+str(variable)+", val="+str(value))
                    pos, neg, delay = LFkT.interprete(p.get_variables(),
                                                      p.get_values(),
                                                      time_series, variable,
                                                      value)

                    #eprint("pos: ", pos)

                    # Each positive is explained
                    for s in pos:
                        cover = False
                        for r in rules:
                            if r.get_head_variable() == variable \
                               and r.get_head_value() == value \
                               and r.matches(s):
                                cover = True
                        #if not cover:
                        #    eprint(p_)
                        #    eprint(s)
                        self.assertTrue(cover)  # One rule cover the example

                    #eprint("neg: ", neg)

                    # No negative is covered
                    for s in neg:
                        cover = False
                        for r in rules:
                            if r.get_head_variable() == variable \
                               and r.get_head_value() == value \
                               and r.matches(s):
                                cover = True
                        self.assertFalse(cover)  # no rule covers the example

                    # All rules are minimals
                    for r in rules:
                        if r.get_head_variable(
                        ) == variable and r.get_head_value() == value:
                            for (var, val) in r.get_body():
                                r.remove_condition(var)  # Try remove condition

                                conflict = False
                                for s in neg:
                                    if r.matches(
                                            s):  # Cover a negative example
                                        conflict = True
                                        break

                                # # DEBUG:
                                if not conflict:
                                    eprint("not minimal " + r.to_string())
                                    eprint(neg)

                                self.assertTrue(conflict)
                                r.add_condition(var, val)  # Cancel removal
Exemplo n.º 4
0
    def test_interprete(self):
        print(">> LFkT.interprete(transitions, variable, value)")

        for i in range(self.__nb_unit_test):
            #eprint("Start test ", i, "/", self.__nb_unit_test)
            # Generate transitions
            variables = [
                "x" + str(i)
                for i in range(random.randint(1, self.__nb_variables))
            ]
            values = []

            for var in range(len(variables)):
                values.append([
                    val for val in range(random.randint(2, self.__nb_values))
                ])

            min_body_size = 0
            max_body_size = random.randint(min_body_size, len(variables))
            delay_original = random.randint(1, self.__max_delay)

            #eprint("Generating random program")
            #eprint("variables: ", variables)
            #eprint("Values: ", values)
            #eprint("delay: ", delay_original)
            p = LogicProgram.random(variables, values, min_body_size,
                                    max_body_size, delay_original)
            #eprint("Generating series...")
            time_series = p.generate_all_time_series(delay_original)

            var = random.randint(0, len(p.get_variables()) - 1)
            val = random.randint(0, len(p.get_values()[var]) - 1)

            #eprint("interpreting...")
            pos, neg, delay = LFkT.interprete(p.get_variables(),
                                              p.get_values(), time_series, var,
                                              val)

            # DBG
            #eprint("variables: ", variables)
            #eprint("values", values)
            #eprint("delay: ", delay_original)
            #eprint(p.logic_form())
            #eprint(time_series)
            #eprint("var: ", var)
            #eprint("val: ", val)
            #eprint("pos: ", pos)
            #eprint("neg: ",neg)
            #eprint("delay detected: ", delay)

            # All pos are valid
            for s in pos:
                for serie in time_series:
                    for id in range(len(serie) - delay):
                        s1 = serie[id:id + delay].copy()
                        s1.reverse()
                        s1 = [y for x in s1 for y in x]
                        #eprint(s1)
                        #eprint(s)
                        s2 = serie[id + delay]
                        if s1 == s:
                            self.assertEqual(s2[var], val)
                            break
            # All neg are valid
            for s in neg:
                for serie in time_series:
                    for id in range(len(serie) - delay):
                        s1 = serie[id:id + delay].copy()
                        s1.reverse()
                        s1 = [y for x in s1 for y in x]
                        s2 = serie[id + delay]
                        if s1 == s:
                            self.assertTrue(s2[var] != val)
                            break

            # All transitions are interpreted
            #eprint("var/val: ", var, "/", val)
            #eprint("delay: ", delay)
            #eprint("Time serie: ", time_series)
            for serie in time_series:
                #eprint("checking: ", serie)
                for id in range(delay, len(serie)):
                    s1 = serie[id - delay:id].copy()
                    s1.reverse()
                    s1 = [y for x in s1 for y in x]
                    s2 = serie[id]
                    #eprint("s1: ", s1, ", s2: ", s2)
                    #eprint("pos: ", pos)
                    #eprint("neg: ", neg)
                    if s2[var] == val:
                        self.assertTrue(s1 in pos)
                        self.assertFalse(s1 in neg)
                    else:
                        self.assertFalse(s1 in pos)
                        self.assertTrue(s1 in neg)

            # delay valid
            global_delay = 1
            for serie_1 in time_series:
                for id_state_1 in range(len(serie_1) - 1):
                    state_1 = serie_1[id_state_1]
                    next_1 = serie_1[id_state_1 + 1]
                    # search duplicate with different future
                    for serie_2 in time_series:
                        for id_state_2 in range(len(serie_2) - 1):
                            state_2 = serie_2[id_state_2]
                            next_2 = serie_2[id_state_2 + 1]

                            # Non-determinism detected
                            if state_1 == state_2 and next_1[var] != next_2[
                                    var]:
                                local_delay = 2
                                id_1 = id_state_1
                                id_2 = id_state_2
                                while id_1 > 0 and id_2 > 0:
                                    previous_1 = serie_1[id_1 - 1]
                                    previous_2 = serie_2[id_2 - 1]
                                    if previous_1 != previous_2:
                                        break
                                    local_delay += 1
                                    id_1 -= 1
                                    id_2 -= 1
                                global_delay = max(global_delay, local_delay)
                                self.assertTrue(local_delay <= delay)
            self.assertEqual(delay, global_delay)
Exemplo n.º 5
0
    def test_fit(self):
        print(">> LUST.fit(variables, values, transitions)")

        # No transitions
        variables = [
            "x" + str(i) for i in range(random.randint(1, self.__nb_variables))
        ]
        values = []

        for var in range(len(variables)):
            values.append(
                [val for val in range(random.randint(2, self.__nb_values))])

        min_body_size = 0
        max_body_size = random.randint(min_body_size, len(variables))
        p = LogicProgram.random(variables, values, min_body_size,
                                max_body_size)
        p_ = LUST.fit(p.get_variables(), p.get_values(), [])
        self.assertEqual(len(p_), 1)
        p_ = p_[0]
        self.assertEqual(p_.get_variables(), p.get_variables())
        self.assertEqual(p_.get_values(), p.get_values())
        self.assertEqual(p_.get_rules(), [])

        for i in range(self.__nb_unit_test):
            #eprint("test: ", i, "/", self.__nb_unit_test)

            variables = [
                "x" + str(i)
                for i in range(random.randint(1, self.__nb_variables))
            ]
            values = []

            for var in range(len(variables)):
                values.append([
                    val for val in range(random.randint(2, self.__nb_values))
                ])

            nb_programs = random.randint(1, self.__max_programs)
            transitions = []

            for j in range(nb_programs):
                # Generate transitions
                min_body_size = 0
                max_body_size = random.randint(min_body_size, len(variables))

                p = LogicProgram.random(variables, values, min_body_size,
                                        max_body_size)
                transitions += p.generate_all_transitions()

                #eprint(p.logic_form())
            #eprint(transitions)

            P = LUST.fit(p.get_variables(), p.get_values(), transitions)
            #rules = p_.get_rules()

            # Generate transitions
            predictions = []
            for p in P:
                #eprint(p.logic_form())
                predictions += p.generate_all_transitions()

            # Remove incomplete states
            #predictions = [ [s1,s2] for s1,s2 in predictions if -1 not in s2 ]

            #eprint("Expected: ", transitions)
            #eprint("Predicted: ", predictions)

            # All original transitions are predicted
            for s1, s2 in transitions:
                self.assertTrue([s1, s2] in predictions)

            # All predictions are in original transitions
            for s1, s2 in predictions:
                #eprint(s1,s2)
                self.assertTrue([s1, s2] in transitions)
Exemplo n.º 6
0
    def test_interprete(self):
        print(">> LUST.interprete(variables, values, transitions)")

        for i in range(self.__nb_unit_test):
            #eprint("test: ", i, "/", self.__nb_unit_test)

            # No transitions
            variables = [
                "x" + str(i)
                for i in range(random.randint(1, self.__nb_variables))
            ]
            values = []

            for var in range(len(variables)):
                values.append([
                    val for val in range(random.randint(2, self.__nb_values))
                ])

            min_body_size = 0
            max_body_size = random.randint(min_body_size, len(variables))
            p = LogicProgram.random(variables, values, min_body_size,
                                    max_body_size)

            var = random.randint(0, len(p.get_variables()) - 1)
            val = random.randint(0, len(p.get_values()[var]) - 1)

            DC, DS = LUST.interprete(p.get_variables(), p.get_values(), [])
            self.assertEqual(DC, [])
            self.assertEqual(DS, [])

            # Regular case
            variables = [
                "x" + str(i)
                for i in range(random.randint(1, self.__nb_variables))
            ]
            values = []

            for var in range(len(variables)):
                values.append([
                    val for val in range(random.randint(2, self.__nb_values))
                ])

            nb_programs = random.randint(1, self.__max_programs)
            transitions = []

            for j in range(nb_programs):
                # Generate transitions
                min_body_size = 0
                max_body_size = random.randint(min_body_size, len(variables))

                p = LogicProgram.random(variables, values, min_body_size,
                                        max_body_size)
                transitions += p.generate_all_transitions()

                #eprint(p.logic_form())
            #eprint(transitions)

            var = random.randint(0, len(p.get_variables()) - 1)
            val = random.randint(0, len(p.get_values()[var]) - 1)

            DC, DS = LUST.interprete(p.get_variables(), p.get_values(),
                                     transitions)
            D = []
            ND = []

            for s1, s2 in transitions:
                deterministic = True
                for s3, s4 in transitions:
                    if s1 == s3 and s2 != s4:
                        ND.append([s1, s2])
                        deterministic = False
                        break
                if deterministic:
                    D.append([s1, s2])

            #eprint("DC: ",DC)
            #eprint("DS: ",DS)
            #eprint("D: ",D)
            #eprint("ND: ",ND)

            # All deterministic are only in DC
            for s1, s2 in D:
                self.assertTrue([s1, s2] in DC)
                for s in DS:
                    self.assertTrue([s1, s2] not in s)

            # All DC are deterministic
            for s1, s2 in DC:
                self.assertTrue([s1, s2] in D)

            # All non deterministic sets are set
            for s in DS:
                for s1, s2 in s:
                    occ = 0
                    for s3, s4 in s:
                        if s1 == s3 and s2 == s4:
                            occ += 1
                    self.assertEqual(occ, 1)

            # All input origin state appears in each DS TODO
            for s1, s2 in ND:
                for s in DS:
                    occurs = False
                    for s3, s4 in s:
                        if s1 == s3:
                            occurs = True
                    self.assertTrue(occurs)

            # All DS are deterministic
            for s in DS:
                for s1, s2 in s:
                    for s3, s4 in s:
                        if s1 == s3:
                            self.assertTrue(s2 == s4)