def test_copy(self): eprint(">> ContinuumRule.copy(self)") for i in range(self.__nb_unit_test): head_var_id = random.randint(0, self.__max_variables) head_domain = Continuum.random(self.__min_value, self.__max_value) size = random.randint(1, self.__max_variables) body = [] locked = [] for j in range(size): var_id = random.randint(0, self.__max_variables) domain = Continuum.random(self.__min_value, self.__max_value) if var_id not in locked: body.append((var_id, domain)) locked.append(var_id) r_ = ContinuumRule(head_var_id, head_domain, body) r = r_.copy() self.assertEqual(r.get_head_variable(), head_var_id) self.assertEqual(r.get_head_value(), head_domain) for e in body: self.assertTrue(e in r.get_body()) for e in r.get_body(): self.assertTrue(e in body) self.assertEqual(r.get_head_variable(), r_.get_head_variable()) self.assertEqual(r.get_head_value(), r_.get_head_value()) for e in r_.get_body(): self.assertTrue(e in r.get_body()) for e in r.get_body(): self.assertTrue(e in r_.get_body())
def test_remove_condition(self): print(">> ContinuumRule.remove_condition(self, variable)") # Empty rule for i in range(self.__nb_unit_test): variables, domains = self.random_system() var = random.randint(0, len(variables) - 1) val = Continuum.random(domains[var].get_min_value(), domains[var].get_max_value()) r = ContinuumRule(var, val) var = random.randint(0, len(variables) - 1) self.assertFalse(r.has_condition(var)) r.remove_condition(var) self.assertFalse(r.has_condition(var)) while r.size() == 0: r = self.random_rule(variables, domains) var, val = random.choice(r.get_body()) self.assertTrue(r.has_condition(var)) r.remove_condition(var) self.assertFalse(r.has_condition(var))
def random_system(self): # generates variables/domains nb_variables = random.randint(1, self.__max_variables) variables = ["x"+str(var) for var in range(nb_variables)] domains = [ Continuum.random(self.__min_value, self.__max_value, self.__min_domain_size) for var in variables ] return variables, domains
def test_has_condition(self): print(">> ContinuumRule.has_condition(self, variable)") for i in range(self.__nb_unit_test): # Empty rule variables, domains = self.random_system() var = random.randint(0, len(variables)) val = Continuum.random(self.__min_value, self.__max_value) r = ContinuumRule(var, val) for var in range(len(variables)): self.assertEqual(r.has_condition(var), False) # Regular rule variables, domains = self.random_system() while r.size() == 0: r = self.random_rule(variables, domains) body = r.get_body() appears = [] for var, val in body: appears.append(var) self.assertEqual(r.has_condition(var), True) for var in range(len(variables)): if var not in appears: self.assertEqual(r.has_condition(var), False)
def random_rule(self, variables, domains): var = random.randint(0,len(variables)-1) var_domain = domains[var] val = Continuum.random(var_domain.get_min_value(), var_domain.get_max_value(), self.__min_continuum_size) min_size = random.randint(0, len(variables)) max_size = random.randint(min_size, len(variables)) return ContinuumRule.random(var, val, variables, domains, min_size, max_size)
def test_constructor_empty(self): eprint(">> ContinuumRule.__init__(self, head_variable, head_value)") for i in range(self.__nb_unit_test): var_id = random.randint(0, self.__max_variables) domain = Continuum.random(self.__min_value, self.__max_value) c = ContinuumRule(var_id, domain) self.assertEqual(c.get_head_variable(), var_id) self.assertEqual(c.get_head_value(), domain) self.assertEqual(c.get_body(), [])
def test_static_random(self): eprint(">> Continuum.random(min_value, max_value)") for i in range(self.__nb_unit_test): # Valid values min = random.uniform(self.__min_value, self.__max_value) max = random.uniform(min, self.__max_value) c = Continuum.random(min, max) self.assertTrue(c.get_min_value() >= min and c.get_min_value() <= max) self.assertTrue(isinstance(c.min_included(), bool)) self.assertFalse(c.is_empty()) # with min size min_size = 0 while min_size == 0: min_size = random.uniform(-100, 0) self.assertRaises(ValueError, Continuum.random, min, max, min_size) min_size = random.uniform(0, self.__max_value) if min_size >= (max - min): self.assertRaises(ValueError, Continuum.random, min, max, min_size) c = Continuum.random(min, max) self.assertTrue(c.get_min_value() >= min and c.get_min_value() <= max) self.assertTrue(isinstance(c.min_included(), bool)) self.assertFalse(c.is_empty()) # Invalid values min = random.uniform(self.__min_value, self.__max_value) max = random.uniform(self.__min_value, min - 0.001) self.assertRaises(ValueError, Continuum.random, min, max)
def test_copy(self): eprint(">> Continuum.copy(self)") for i in range(self.__nb_unit_test): # Emptyset c_ = Continuum() c = c_.copy() self.assertTrue(c.is_empty()) self.assertEqual(c.get_min_value(), None) self.assertEqual(c.get_max_value(), None) self.assertEqual(c.min_included(), None) self.assertEqual(c.max_included(), None) # Non empty min = random.uniform(self.__min_value, self.__max_value) max = random.uniform(min, self.__max_value) min_included = random.choice([True, False]) max_included = random.choice([True, False]) c_ = Continuum(min, max, min_included, max_included) c = c_.copy() self.assertFalse(c.is_empty()) self.assertEqual(c.get_min_value(), min) self.assertEqual(c.get_max_value(), max) self.assertEqual(c.min_included(), min_included) self.assertEqual(c.max_included(), max_included)
def test_constructor_empty(self): eprint(">> Continuum.__init__(self)") for i in range(self.__nb_unit_test): c = Continuum() self.assertTrue(c.is_empty()) self.assertEqual(c.get_min_value(), None) self.assertEqual(c.get_max_value(), None) self.assertEqual(c.min_included(), None) self.assertEqual(c.max_included(), None)
def test_size(self): eprint(">> Continuum.size(self)") for i in range(self.__nb_unit_test): # empty set c = Continuum() self.assertEqual(c.size(), 0.0) # regular c = Continuum.random(self.__min_value, self.__max_value) if not c.is_empty(): self.assertEqual(c.size(), c.get_max_value() - c.get_min_value())
def test_static_random(self): eprint(">> ContinuumRule.random(min_value, max_value)") for i in range(self.__nb_unit_test): variables, domains = self.random_system() # rule characteristics var = random.randint(0, len(variables) - 1) var_domain = domains[var] val = Continuum.random(var_domain.get_min_value(), var_domain.get_max_value()) min_size = random.randint(0, len(variables)) max_size = random.randint(min_size, len(variables)) r = ContinuumRule.random(var, val, variables, domains, min_size, max_size) # Check head self.assertEqual(r.get_head_variable(), var) self.assertEqual(r.get_head_value(), val) # Check body self.assertTrue(r.size() >= min_size) self.assertTrue(r.size() <= max_size) appears = [] for var, val in r.get_body(): self.assertTrue(var >= 0 and var < len(variables)) self.assertTrue(domains[var].includes(val)) self.assertFalse(var in appears) appears.append(var) # min > max min_size = random.randint(0, len(variables)) max_size = random.randint(-100, min_size - 1) self.assertRaises(ValueError, ContinuumRule.random, var, val, variables, domains, min_size, max_size) # min > nb variables min_size = random.randint(len(variables) + 1, len(variables) + 100) max_size = random.randint(min_size, len(variables) + 100) self.assertRaises(ValueError, ContinuumRule.random, var, val, variables, domains, min_size, max_size) # max > nb variables min_size = random.randint(0, len(variables)) max_size = random.randint(len(variables) + 1, len(variables) + 100) self.assertRaises(ValueError, ContinuumRule.random, var, val, variables, domains, min_size, max_size)
def test___eq__(self): print(">> ContinuumRule.__eq__(self, other)") for i in range(self.__nb_unit_test): variables, domains = self.random_system() r = self.random_rule(variables, domains) self.assertTrue(r == r) self.assertFalse(r != r) r_ = self.random_rule(variables, domains) if r.get_head_variable() != r_.get_head_variable(): self.assertFalse(r == r_) if r.get_head_value() != r_.get_head_value(): self.assertFalse(r == r_) if r.get_body() != r_.get_body(): self.assertFalse(r == r_) # different type self.assertFalse(r == "") self.assertFalse(r == 0) self.assertFalse(r == []) self.assertFalse(r == [1, 2, 3]) # different size r_ = self.random_rule(variables, domains) while r_.size() == r.size(): r_ = self.random_rule(variables, domains) r_.set_head_variable(r.get_head_variable()) r_.set_head_value(r.get_head_value()) self.assertFalse(r == r_) # Same size, same head while r.size() == 0: r = self.random_rule(variables, domains) r_ = r.copy() var, val = random.choice(r.get_body()) new_val = val while new_val == val: new_val = Continuum.random(domains[var].get_min_value(), domains[var].get_max_value()) r_.set_condition(var, new_val) self.assertFalse(r == r_)
def test_precision(self): print(">> ContinuumLogicProgram.precision(expected, predicted)") self.assertEqual(ContinuumLogicProgram.precision([],[]), 1.0) # Equal programs for i in range(self.__nb_unit_test): variables, domains = self.random_system() nb_states = random.randint(1,100) expected = [] predicted = [] for j in range(nb_states): s1 = [ random.uniform(d.get_min_value(),d.get_max_value()) for d in domains ] s2 = [ random.uniform(d.get_min_value(),d.get_max_value()) for d in domains ] s2_ = [ Continuum.random(d.get_min_value(), d.get_max_value()) for d in domains ] expected.append( (s1,s2) ) predicted.append( (s1,s2_) ) precision = ContinuumLogicProgram.precision(expected, predicted) error = 0 for j in range(len(expected)): s1, s2 = expected[j] s1_, s2_ = predicted[j] for k in range(len(s2)): if not s2_[k].includes(s2[k]): error += 1 total = nb_states * len(variables) self.assertEqual( precision, 1.0 - (error / total) ) #eprint("precision: ", precision) # error of size state_id = random.randint(0, len(expected)-1) modif = random.randint(1,len(expected[state_id])) expected[state_id] = ( expected[state_id][0][:-modif], expected[state_id][1] ) self.assertRaises(ValueError, ContinuumLogicProgram.precision, expected, predicted)
def test___init__(self): print(">> ContinuumLogicProgram.__init__(self, variables, domains, rules)") for i in range(self.__nb_unit_test): variables, domains = self.random_system() rules = [] for j in range(random.randint(0,self.__nb_rules)): r = self.random_rule(variables, domains) rules.append(r) p = ContinuumLogicProgram(variables, domains, rules) self.assertEqual(p.get_variables(), variables) self.assertEqual(p.get_domains(), domains) self.assertEqual(p.get_rules(), rules) modif = random.randint(1,len(variables)) self.assertRaises(ValueError, ContinuumLogicProgram, variables, domains[:-modif], rules) for var in range(0, modif): domains.append(Continuum.random(self.__min_value, self.__max_value, self.__min_domain_size)) self.assertRaises(ValueError, ContinuumLogicProgram, variables, domains, rules)
def test_includes(self): eprint(">> Continuum.includes(self, element)") for i in range(self.__nb_unit_test): # bad argument type c = Continuum.random(self.__min_value, self.__max_value) self.assertRaises(TypeError, c.includes, "test") # float argument #---------------- # empty set includes nothing c = Continuum() value = random.uniform(self.__min_value, self.__max_value) self.assertFalse(c.includes(value)) c = Continuum.random(self.__min_value, self.__max_value) # Before min value = c.get_min_value() while value == c.get_min_value(): value = random.uniform(c.get_min_value() - 100.0, c.get_min_value()) self.assertFalse(c.includes(value)) # on min bound self.assertEqual(c.includes(c.get_min_value()), c.min_included()) # Inside value = c.get_min_value() while value == c.get_min_value() or value == c.get_max_value(): value = random.uniform(c.get_min_value(), c.get_max_value()) self.assertTrue(c.includes(value)) # on max bound self.assertEqual(c.includes(c.get_max_value()), c.max_included()) # after max bound value = c.get_max_value() while value == c.get_max_value(): value = random.uniform(c.get_max_value(), c.get_max_value() + 100.0) self.assertFalse(c.includes(value)) # int argument #-------------- # empty set includes nothing c = Continuum() value = random.randint(int(self.__min_value), int(self.__max_value)) self.assertFalse(c.includes(value)) c = Continuum.random(self.__min_value, self.__max_value) while int(c.get_max_value()) - int(c.get_min_value()) <= 1: min = random.uniform(self.__min_value, self.__max_value) max = random.uniform(min, self.__max_value) c = Continuum.random(min, max) #eprint(c.to_string()) # Before min value = random.randint(int(c.get_min_value() - 100), int(c.get_min_value()) - 1) self.assertFalse(c.includes(value)) # on min bound self.assertEqual(c.includes(c.get_min_value()), c.min_included()) # Inside value = random.randint( int(c.get_min_value()) + 1, int(c.get_max_value()) - 1) #eprint(value) self.assertTrue(c.includes(value)) # on max bound self.assertEqual(c.includes(c.get_max_value()), c.max_included()) # after max bound value = random.randint( int(c.get_max_value()) + 1, int(c.get_max_value() + 100)) self.assertFalse(c.includes(value)) # continuum argument #-------------------- # 0) c is empty set c = Continuum() c_ = Continuum() self.assertTrue(c.includes(c_)) # empty set VS empty set c_ = Continuum.random(self.__min_value, self.__max_value) while c_.is_empty(): c_ = Continuum.random(self.__min_value, self.__max_value) self.assertFalse(c.includes(c_)) # empty set VS non empty # 1) c is non empty c = Continuum.random(self.__min_value, self.__max_value) self.assertTrue(c.includes(Continuum())) # non empty VS empty set self.assertTrue(c.includes(c)) # includes itself # 1.1) Lower bound over c_ = Continuum.random(c.get_min_value(), self.__max_value) while c_.is_empty(): c_ = Continuum.random(c.get_min_value(), self.__max_value) value = c.get_min_value() while value == c.get_min_value(): value = random.uniform(c.get_min_value() - 100, c.get_min_value()) c_.set_lower_bound(value, random.choice([True, False])) self.assertFalse(c.includes(c_)) # 1.2) on min bound c_ = Continuum.random(c.get_min_value(), self.__max_value) while c_.is_empty(): c_ = Continuum.random(c.get_min_value(), self.__max_value) c_.set_lower_bound(c.get_min_value(), random.choice([True, False])) if not c.min_included() and c_.min_included(): # one value over self.assertFalse(c.includes(c_)) # 1.3) upper bound over c_ = Continuum.random(self.__min_value, c.get_max_value()) while c_.is_empty(): c_ = Continuum.random(self.__min_value, c.get_max_value()) value = c.get_max_value() while value == c.get_max_value(): value = random.uniform(c.get_max_value(), c.get_max_value() + 100) c_.set_upper_bound(value, random.choice([True, False])) self.assertFalse(c.includes(c_)) # 1.4) on upper bound c_ = Continuum.random(self.__min_value, c.get_max_value()) while c_.is_empty(): c_ = Continuum.random(self.__min_value, c.get_max_value()) c_.set_upper_bound(c.get_max_value(), random.choice([True, False])) if not c.max_included() and c_.max_included(): # one value over self.assertFalse(c.includes(c_)) # 1.5) inside min = c.get_min_value() while min == c.get_min_value(): min = random.uniform(c.get_min_value(), c.get_max_value()) max = c.get_max_value() while max == c.get_max_value(): max = random.uniform(min, c.get_max_value()) c_ = Continuum(min, max, random.choice([True, False]), random.choice([True, False])) self.assertTrue(c.includes(c_)) self.assertFalse(c_.includes(c))
def test_intersects(self): eprint(">> Continuum.intersects(self, continuum)") for i in range(self.__nb_unit_test): c = Continuum.random(self.__min_value, self.__max_value) c_ = Continuum() # emptyset self.assertFalse(c.intersects(c_)) self.assertFalse(c_.intersects(c)) self.assertFalse(c_.intersects(c_)) # stricly before c = Continuum.random(self.__min_value, self.__max_value) c_ = Continuum.random(c.get_min_value() - 100, c.get_min_value()) self.assertFalse(c.intersects(c_)) self.assertFalse(c_.intersects(c)) # touching on lower bound c = Continuum.random(self.__min_value, self.__max_value) c_ = Continuum.random(c.get_min_value() - 100, c.get_min_value()) c_.set_upper_bound(c.get_min_value(), True) self.assertEqual(c.intersects(c_), c.min_included()) self.assertEqual(c_.intersects(c), c.min_included()) c_.set_upper_bound(c.get_min_value(), False) self.assertFalse(c.intersects(c_)) self.assertFalse(c_.intersects(c)) # strictly after c = Continuum.random(self.__min_value, self.__max_value) c_ = Continuum.random(c.get_max_value(), c.get_max_value() + 100) self.assertFalse(c.intersects(c_)) self.assertFalse(c_.intersects(c)) # touching on lower bound c = Continuum.random(self.__min_value, self.__max_value) c_ = Continuum.random(c.get_max_value(), c.get_max_value() + 100) c_.set_lower_bound(c.get_max_value(), True) self.assertEqual(c.intersects(c_), c.max_included()) self.assertEqual(c_.intersects(c), c.max_included()) c_.set_lower_bound(c.get_max_value(), False) self.assertFalse(c.intersects(c_)) self.assertFalse(c_.intersects(c)) # same (not empty) c = Continuum.random(self.__min_value, self.__max_value) while c.is_empty(): c = Continuum.random(self.__min_value, self.__max_value) self.assertTrue(c.includes(c)) # smaller c_ = Continuum.random(c.get_min_value(), c.get_max_value()) while c_.get_min_value() == c.get_min_value() and c_.get_max_value( ) == c.get_max_value(): c_ = Continuum.random(c.get_min_value(), c.get_max_value()) self.assertTrue(c.intersects(c_)) self.assertTrue(c_.intersects(c)) # bigger c_ = Continuum.random(c.get_min_value() - 100, c.get_max_value() + 100) while c_.get_min_value() >= c.get_min_value() or c_.get_max_value( ) <= c.get_max_value(): c_ = Continuum.random(c.get_min_value() - 100, c.get_max_value() + 100) #eprint(c.to_string()) #eprint(c_.to_string()) self.assertTrue(c.intersects(c_)) self.assertTrue(c_.intersects(c))
def test__eq__(self): eprint(">> Continuum.__eq__(self, continuum)") for i in range(self.__nb_unit_test): # emptyset c = Continuum() self.assertTrue(Continuum() == Continuum()) self.assertTrue(c == Continuum()) self.assertTrue(c == c) self.assertFalse(Continuum() != Continuum()) self.assertFalse(c != Continuum()) self.assertFalse(c != c) c = Continuum.random(self.__min_value, self.__max_value) self.assertTrue(c == c) self.assertFalse(c != c) self.assertEqual(c == Continuum(), c.is_empty()) c_ = Continuum.random(self.__min_value, self.__max_value) if c.is_empty() and c_.is_empty(): self.assertTrue(c == c_) self.assertTrue(c != c_) if c.is_empty() != c_.is_empty(): self.assertFalse(c == c_) self.assertTrue(c != c_) if c.get_min_value() != c_.get_min_value(): self.assertFalse(c == c_) self.assertTrue(c != c_) if c.get_max_value() != c_.get_max_value(): self.assertFalse(c == c_) self.assertTrue(c != c_) if c.min_included() != c_.min_included(): self.assertFalse(c == c_) self.assertTrue(c != c_) if c.max_included() != c_.max_included(): self.assertFalse(c == c_) self.assertTrue(c != c_) # exaustive modifications if not c.is_empty(): c_ = c.copy() value = random.uniform(1, 100) c_.set_lower_bound(c.get_min_value() - value, True) self.assertFalse(c == c_) self.assertTrue(c != c_) c_.set_lower_bound(c.get_min_value() - value, False) self.assertFalse(c == c_) self.assertTrue(c != c_) c_ = c.copy() c_.set_lower_bound(c.get_min_value(), not c.min_included()) self.assertFalse(c == c_) self.assertTrue(c != c_) c_ = c.copy() value = random.uniform(1, 100) c_.set_upper_bound(c.get_min_value() + value, True) self.assertFalse(c == c_) self.assertTrue(c != c_) c_.set_upper_bound(c.get_min_value() + value, False) self.assertFalse(c == c_) self.assertTrue(c != c_) c_ = c.copy() c_.set_upper_bound(c.get_max_value(), not c.max_included()) self.assertFalse(c == c_) self.assertTrue(c != c_) # different type self.assertFalse(c == "test") self.assertFalse(c == 0) self.assertFalse(c == True) self.assertFalse(c == [])
def test_set_condition(self): print(">> ContinuumRule.set_condition(self, variable, value)") for i in range(self.__nb_unit_test): variables, domains = self.random_system() var = random.randint(0, len(variables) - 1) # empty rule r = self.random_rule(variables, domains) r = ContinuumRule(r.get_head_variable(), r.get_head_value(), []) for j in range(len(variables)): var = random.randint(0, len(variables) - 1) val = Continuum.random(domains[var].get_min_value(), domains[var].get_max_value()) size = r.size() exist = r.has_condition(var) r.set_condition(var, val) # condition updated self.assertEqual(r.get_condition(var), val) # change rather than add when variable exist in a condition if exist: self.assertEqual(r.size(), size) else: self.assertEqual(r.size(), size + 1) # Ordering ensured prev = -1 for v, val in r.get_body(): v > prev prev = v # regular rule r = self.random_rule(variables, domains) for j in range(len(variables)): var = random.randint(0, len(variables) - 1) val = Continuum.random(domains[var].get_min_value(), domains[var].get_max_value()) size = r.size() exist = r.has_condition(var) #eprint("r: ", r) #eprint("var: ", var) #eprint("val: ", val) r.set_condition(var, val) # condition updated self.assertEqual(r.get_condition(var), val) # change rather than add when variable exist in a condition if exist: self.assertEqual(r.size(), size) else: self.assertEqual(r.size(), size + 1) # Ordering ensured prev = -1 for v, val in r.get_body(): v > prev prev = v
def test_constructor_full(self): eprint( ">> Continuum.__init__(self, min_value=None, max_value=None, min_included=None, max_included=None)" ) for i in range(self.__nb_unit_test): # Valid continuum #----------------- min = random.uniform(self.__min_value, self.__max_value) max = random.uniform(min, self.__max_value) min_included = random.choice([True, False]) max_included = random.choice([True, False]) c = Continuum(min, max, min_included, max_included) self.assertFalse(c.is_empty()) self.assertEqual(c.get_min_value(), min) self.assertEqual(c.get_max_value(), max) self.assertEqual(c.min_included(), min_included) self.assertEqual(c.max_included(), max_included) # Implicit emptyset #------------------- min = random.uniform(self.__min_value, self.__max_value) c = Continuum(min, min, False, False) self.assertTrue(c.is_empty()) self.assertEqual(c.get_min_value(), None) self.assertEqual(c.get_max_value(), None) self.assertEqual(c.min_included(), None) self.assertEqual(c.max_included(), None) # Invalid Continuum #-------------------- max = random.uniform(self.__min_value, min - 0.001) self.assertRaises(ValueError, Continuum, min, max, min_included, max_included) # Invalid number of arguments #------------------------------- self.assertRaises(ValueError, Continuum, min) self.assertRaises(ValueError, Continuum, min, max) self.assertRaises(ValueError, Continuum, min, max, min_included) self.assertRaises(ValueError, Continuum, min, max, min_included, max_included)
def test_least_revision(self): eprint(">> ACEDIA.least_revision(rule, state_1, state_2)") for i in range(self.__nb_unit_test): variables, domains = self.random_system() state_1 = self.random_state(variables, domains) state_2 = self.random_state(variables, domains) # not matching #-------------- rule = self.random_rule(variables, domains) while rule.matches(state_1): rule = self.random_rule(variables, domains) self.assertRaises(ValueError, ACEDIA.least_revision, rule, state_1, state_2) # matching #-------------- rule = self.random_rule(variables, domains) while not rule.matches(state_1): rule = self.random_rule(variables, domains) head_var = rule.get_head_variable() target_val = state_2[rule.get_head_variable()] # Consistent head_value = Continuum() while not head_value.includes(target_val): head_value = Continuum.random( domains[head_var].get_min_value(), domains[head_var].get_max_value()) rule.set_head_value(head_value) self.assertRaises(ValueError, ACEDIA.least_revision, rule, state_1, state_2) # Empty set head rule.set_head_value(Continuum()) LR = ACEDIA.least_revision(rule, state_1, state_2) lg = rule.copy() lg.set_head_value(Continuum(target_val, target_val, True, True)) self.assertTrue(lg in LR) nb_valid_revision = 1 for var, val in rule.get_body(): state_value = state_1[var] # min rev ls = rule.copy() new_val = val.copy() new_val.set_lower_bound(state_value, False) if not new_val.is_empty(): ls.set_condition(var, new_val) self.assertTrue(ls in LR) nb_valid_revision += 1 # max rev ls = rule.copy() new_val = val.copy() new_val.set_upper_bound(state_value, False) if not new_val.is_empty(): ls.set_condition(var, new_val) self.assertTrue(ls in LR) nb_valid_revision += 1 self.assertEqual(len(LR), nb_valid_revision) #eprint(nb_valid_revision) # usual head head_value = Continuum.random(domains[head_var].get_min_value(), domains[head_var].get_max_value()) while head_value.includes(target_val): head_value = Continuum.random( domains[head_var].get_min_value(), domains[head_var].get_max_value()) rule.set_head_value(head_value) LR = ACEDIA.least_revision(rule, state_1, state_2) lg = rule.copy() head_value = lg.get_head_value() if target_val <= head_value.get_min_value(): head_value.set_lower_bound(target_val, True) else: head_value.set_upper_bound(target_val, True) lg.set_head_value(head_value) self.assertTrue(lg in LR) nb_valid_revision = 1 for var, val in rule.get_body(): state_value = state_1[var] # min rev ls = rule.copy() new_val = val.copy() new_val.set_lower_bound(state_value, False) if not new_val.is_empty(): ls.set_condition(var, new_val) self.assertTrue(ls in LR) nb_valid_revision += 1 # max rev ls = rule.copy() new_val = val.copy() new_val.set_upper_bound(state_value, False) if not new_val.is_empty(): ls.set_condition(var, new_val) self.assertTrue(ls in LR) nb_valid_revision += 1 self.assertEqual(len(LR), nb_valid_revision)
def test_set_upper_bound(self): eprint(">> Continuum.set_upper_bound(self, value, included)") for i in range(self.__nb_unit_test): # Empty set c = Continuum() self.assertRaises(TypeError, c.set_upper_bound, "string", True) self.assertRaises(TypeError, c.set_upper_bound, "string", False) self.assertRaises(TypeError, c.set_upper_bound, 0.5, 10) value = random.uniform(self.__min_value, self.__max_value) # extend with exclusion gives empty set, mistake expected from user # or both min and max will be changed and constructor must be used self.assertRaises(ValueError, c.set_upper_bound, value, False) c.set_upper_bound(value, True) # Empty set to one value interval self.assertEqual(c, Continuum(value, value, True, True)) # Regular continuum # over min value c = Continuum.random(self.__min_value, self.__max_value) value = random.uniform(self.__min_value, c.get_min_value()) while value == c.get_min_value(): value = random.uniform(self.__min_value, c.get_min_value()) self.assertRaises(ValueError, c.set_upper_bound, value, True) self.assertRaises(ValueError, c.set_upper_bound, value, False) # on min value c = Continuum.random(self.__min_value, self.__max_value) c_old = c.copy() value = c.get_min_value() if not c.max_included() or not c.min_included(): c.set_upper_bound(value, False) self.assertEqual(c, Continuum()) # continuum reduced to empty set else: c.set_upper_bound(value, True) self.assertEqual(c.get_max_value(), value) self.assertEqual(c.max_included(), True) self.assertEqual(c.get_max_value(), c.get_min_value()) self.assertEqual(c.get_min_value(), c_old.get_min_value()) self.assertEqual(c.min_included(), c_old.min_included()) # other valid value c = Continuum.random(self.__min_value, self.__max_value) c_old = c.copy() value = random.uniform(c.get_min_value(), self.__max_value) while value == c.get_min_value(): value = random.uniform(c.get_min_value(), self.__max_value) c.set_upper_bound(value, True) self.assertEqual(c.get_max_value(), value) self.assertEqual(c.max_included(), True) c = Continuum.random(self.__min_value, self.__max_value) c_old = c.copy() value = random.uniform(c.get_min_value(), self.__max_value) while value == c.get_min_value(): value = random.uniform(c.get_min_value(), self.__max_value) c.set_upper_bound(value, False) self.assertEqual(c.get_max_value(), value) self.assertEqual(c.max_included(), False)
def test_to_string(self): eprint(">> Continuum.to_string(self)") for i in range(self.__nb_unit_test): c = Continuum() self.assertEqual(c.to_string(), u"\u2205") c = Continuum.random(self.__min_value, self.__max_value) if c.is_empty(): self.assertEqual(c.to_string(), u"\u2205") out = "" if c.min_included(): out += "[" else: out += "]" out += str(c.get_min_value()) + "," + str(c.get_max_value()) if c.max_included(): out += "]" else: out += "[" self.assertEqual(c.to_string(), out) self.assertEqual(c.__str__(), out) self.assertEqual(c.__repr__(), out)
def test_fit(self): eprint(">> ACEDIA.fit(variables, values, transitions)") for i in range(self.__nb_unit_test): eprint("\rTest ", i + 1, "/", self.__nb_unit_test, end='') # Generate transitions epsilon = random.choice([0.1, 0.25, 0.3, 0.5]) variables, domains = self.random_system() p = ContinuumLogicProgram.random(variables, domains, 1, len(variables), epsilon) #eprint("Progam: ", p) # Valid and realistic epsilon #epsilon = round(random.uniform(0.1,1.0), 2) #while epsilon == 1.0: # epsilon = round(random.uniform(0.1,1.0), 2) t = p.generate_all_transitions(epsilon) #sys.exit() #eprint("Transitions: ") #for s1, s2 in t: # eprint(s1, s2) #eprint("Transitions: ", t) p_ = ACEDIA.fit(p.get_variables(), p.get_domains(), t) rules = p_.get_rules() #eprint("learned: ", p_) # All transitions are realized #------------------------------ for head_var in range(len(p.get_variables())): for s1, s2 in t: for idx, val in enumerate(s2): realized = 0 for r in rules: if r.get_head_variable( ) == idx and r.get_head_value().includes( val) and r.matches(s1): realized += 1 break if realized <= 0: eprint("head_var: ", head_var) eprint("s1: ", s1) eprint("s2: ", s2) eprint("learned: ", p_) self.assertTrue( realized >= 1) # One rule realize the example # All rules are minimals #------------------------ for r in rules: #eprint("r: ", r) # Try reducing head min #----------------------- r_ = r.copy() h = r_.get_head_value() if h.get_min_value() + epsilon <= h.get_max_value(): r_.set_head_value( Continuum(h.get_min_value() + epsilon, h.get_max_value(), h.min_included(), h.max_included())) #eprint("spec: ", r_) conflict = False for s1, s2 in t: if not r_.get_head_value().includes( s2[r_.get_head_variable()]) and r_.matches( s1): # Cover a negative example conflict = True #eprint("conflict") break if not conflict: eprint("Non minimal rule: ", r) eprint("head can be specialized into: ", r_.get_head_variable(), "=", r_.get_head_value()) self.assertTrue(conflict) # Try reducing head max #----------------------- r_ = r.copy() h = r_.get_head_value() if h.get_max_value() - epsilon >= h.get_min_value(): r_.set_head_value( Continuum(h.get_min_value(), h.get_max_value() - epsilon, h.min_included(), h.max_included())) #eprint("spec: ", r_) conflict = False for s1, s2 in t: if not r_.get_head_value().includes( s2[r_.get_head_variable()]) and r_.matches( s1): # Cover a negative example conflict = True #eprint("conflict") break if not conflict: eprint("Non minimal rule: ", r) eprint("head can be generalized to: ", r_.get_head_variable(), "=", r_.get_head_value()) self.assertTrue(conflict) # Try extending condition #------------------------- for (var, val) in r.get_body(): # Try extend min r_ = r.copy() if val.get_min_value( ) - epsilon >= domains[var].get_min_value(): val_ = val.copy() if not val_.min_included(): val_.set_lower_bound(val_.get_min_value(), True) else: val_.set_lower_bound( val_.get_min_value() - epsilon, False) r_.set_condition(var, val_) #eprint("gen: ", r_) conflict = False for s1, s2 in t: if not r_.get_head_value().includes( s2[r_.get_head_variable()]) and r_.matches( s1): # Cover a negative example conflict = True #eprint("conflict") break if not conflict: eprint("Non minimal rule: ", r) eprint("condition can be generalized: ", var, "=", val_) self.assertTrue(conflict) # Try extend max r_ = r.copy() if val.get_max_value( ) + epsilon <= domains[var].get_max_value(): val_ = val.copy() if not val_.max_included(): val_.set_upper_bound(val_.get_max_value(), True) else: val_.set_upper_bound( val_.get_max_value() + epsilon, False) r_.set_condition(var, val_) #eprint("gen: ", r_) conflict = False for s1, s2 in t: if not r_.get_head_value().includes( s2[r_.get_head_variable()]) and r_.matches( s1): # Cover a negative example conflict = True #eprint("conflict") break if not conflict: eprint("Non minimal rule: ", r) eprint("condition can be generalized: ", var, "=", val_) self.assertTrue(conflict) eprint()
def test_dominates(self): print(">> ContinuumRule.dominates(self, rule)") for i in range(self.__nb_unit_test): variables, domains = self.random_system() var = random.randint(0, len(variables) - 1) r = self.random_rule(variables, domains) # Undominated rule: var = emptyset if anything r0 = ContinuumRule(r.get_head_variable(), Continuum()) self.assertTrue(r0.dominates(r0)) # r1 is a regular rule r1 = self.random_rule(variables, domains) while r1.size() == 0: r1 = self.random_rule(variables, domains) var = r.get_head_variable() val = Continuum.random(domains[var].get_min_value(), domains[var].get_max_value()) r1 = ContinuumRule(var, val, r1.get_body()) # set head self.assertTrue(r0.dominates(r1)) self.assertFalse(r1.dominates(r0)) # r2 is precision of r1 (head specification) r2 = r1.copy() var = r2.get_head_variable() val = r2.get_head_value() while r2.get_head_value( ) == val or not r2.get_head_value().includes(val): val = Continuum.random(val.get_min_value(), val.get_max_value()) r2.set_head_value(val) self.assertEqual(r0.dominates(r2), True) self.assertEqual(r2.dominates(r0), False) self.assertEqual(r2.dominates(r1), True) self.assertEqual(r1.dominates(r2), False) # r3 is a generalization of r1 (body generalization) r3 = r1.copy() var, val = random.choice(r3.get_body()) val_ = val modif_min = random.uniform(0, self.__max_value) modif_max = random.uniform(0, self.__max_value) while val_ == val or not val_.includes(val): val_ = Continuum.random(val.get_min_value() - modif_min, val.get_max_value() + modif_max) r3.set_condition(var, val_) self.assertEqual(r0.dominates(r3), True) self.assertEqual(r3.dominates(r0), False) self.assertEqual(r3.dominates(r1), True) self.assertEqual(r1.dominates(r3), False) # r4 is unprecision of r1 (head generalization) r4 = r1.copy() var = r4.get_head_variable() val = r4.get_head_value() modif_min = random.uniform(0, self.__max_value) modif_max = random.uniform(0, self.__max_value) while r4.get_head_value() == val or not val.includes( r4.get_head_value()): val = Continuum(val.get_min_value() - modif_min, val.get_max_value() + modif_max, random.choice([True, False]), random.choice([True, False])) r4.set_head_value(val) self.assertEqual(r0.dominates(r4), True) self.assertEqual(r4.dominates(r0), False) self.assertEqual(r4.dominates(r1), False) self.assertEqual(r1.dominates(r4), True) # r5 is specialization of r1 (body specialization) r5 = r1.copy() var, val = random.choice(r5.get_body()) val_ = val while val_ == val or not val.includes(val_): val_ = Continuum.random(val.get_min_value(), val.get_max_value()) r5.set_condition(var, val_) self.assertEqual(r0.dominates(r5), True) self.assertEqual(r5.dominates(r0), False) self.assertEqual(r5.dominates(r1), False) self.assertEqual(r1.dominates(r5), True) # r6 is a random rule r6 = self.random_rule(variables, domains) # head var difference if r6.get_head_variable() != r1.get_head_variable(): self.assertFalse(r6.dominates(r1)) self.assertFalse(r1.dominates(r6)) r6 = ContinuumRule(r1.get_head_variable(), r6.get_head_value(), r6.get_body()) # same head var #eprint("r1: ", r1) #eprint("r6: ", r6) # head val inclusion if not r1.get_head_value().includes(r6.get_head_value()): self.assertFalse(r6.dominates(r1)) r6 = ContinuumRule(r1.get_head_variable(), r1.get_head_value(), r6.get_body()) # same head var, same head val # body subsumption if r1.dominates(r6): for var, val in r1.get_body(): self.assertTrue(val.includes(r6.get_condition(var))) # body subsumption if r6.dominates(r1): for var, val in r6.get_body(): self.assertTrue(val.includes(r1.get_condition(var))) # incomparable if not r1.dominates(r6) and not r6.dominates(r1): #eprint("r1: ", r1) #eprint("r6: ", r6) conflicts = False dominant_r1 = False dominant_r6 = False for var, val in r1.get_body(): # condition not appearing if not r6.has_condition(var): conflicts = True break # value not included if not r6.get_condition(var).includes( val) and not val.includes(r6.get_condition(var)): conflicts = True break # local dominates if val.includes(r6.get_condition(var)): dominant_r1 = True # local dominated if r6.get_condition(var).includes(val): if dominant_r1: conflicts = True break for var, val in r6.get_body(): # condition not appearing if not r1.has_condition(var): conflicts = True break # value not included if not r1.get_condition(var).includes( val) and not val.includes(r1.get_condition(var)): conflicts = True break # local dominates if val.includes(r1.get_condition(var)): dominant_r6 = True if dominant_r1: conflicts = True break # local dominated if r1.get_condition(var).includes(val): if dominant_r6: conflicts = True break self.assertTrue(conflicts)