def test_equivalence(): u = SimpleDomain(1, 5) uu = CompositeDomain([u, u]) r = MutableFuzzySet(uu) r.set_value_at((1, 1), 1) r.set_value_at((2, 2), 1) r.set_value_at((3, 3), 1) r.set_value_at((4, 4), 1) r.set_value_at((1, 2), 0.3) r.set_value_at((2, 1), 0.3) r.set_value_at((2, 3), 0.5) r.set_value_at((3, 2), 0.5) r.set_value_at((3, 4), 0.2) r.set_value_at((4, 3), 0.2) r2 = r print("Pocetna relacija je neizrazita relacija ekvivalencije? {}".format( is_fuzzy_equivalence(r))) for i in range(1, 3): r2 = composition_of_binary_relations(r2, r) print("Broj odradjenih kompozicija: {}".format(i)) print("Relacija je: ") for domain_element in r2.domain.domain_elements: print("mu({}) = {}".format(domain_element, r2.get_value_at(domain_element))) print("Ova relacija je neizrazita relacija ekvivalencije? {}".format( is_fuzzy_equivalence(r2)))
def test_composition(): """ Test made for testing composition of binary relations. Test is based on example from slides (example from ha-03b slide 29/66). :return: None """ prvi = SimpleDomain(0, 4, "Pero") prvi2 = SimpleDomain(0, 5, "Pero") drugi = SimpleDomain(0, 5, "Branko") drugi2 = SimpleDomain(0, 3, "Branko") compos1 = CompositeDomain([prvi, prvi2], "Composite1") compos2 = CompositeDomain([drugi, drugi2], "Composite2") # example from ha-03b slide 29/66 my_fuzz1 = MutableFuzzySet(compos1) my_fuzz2 = MutableFuzzySet(compos2) my_fuzz1.set_value_at((0, 0), 0.1) my_fuzz1.set_value_at((0, 1), 0.7) my_fuzz1.set_value_at((0, 2), 0.5) my_fuzz1.set_value_at((0, 3), 0.1) my_fuzz1.set_value_at((1, 0), 0.5) my_fuzz1.set_value_at((1, 1), 1.0) my_fuzz1.set_value_at((1, 2), 0.9) my_fuzz1.set_value_at((1, 3), 0.4) my_fuzz1.set_value_at((1, 3), 0.4) my_fuzz1.set_value_at((2, 0), 0.2) my_fuzz1.set_value_at((2, 1), 0.1) my_fuzz1.set_value_at((2, 2), 0.6) my_fuzz1.set_value_at((2, 3), 0.9) my_fuzz2.set_value_at((0, 0), 1.0) my_fuzz2.set_value_at((0, 1), 0.2) my_fuzz2.set_value_at((1, 0), 0.7) my_fuzz2.set_value_at((1, 1), 0.5) my_fuzz2.set_value_at((2, 0), 0.3) my_fuzz2.set_value_at((2, 1), 0.9) my_fuzz2.set_value_at((3, 0), 0.0) my_fuzz2.set_value_at((3, 1), 0.4) composition_of_binary_relations(my_fuzz1, my_fuzz2)
def test_composition2(): u1 = SimpleDomain(1, 5) u2 = SimpleDomain(1, 4) u3 = SimpleDomain(1, 5) u1u2 = CompositeDomain([u1, u2]) r1 = MutableFuzzySet(u1u2) r1.set_value_at((1, 1), 0.3) r1.set_value_at((1, 2), 1) r1.set_value_at((3, 3), 0.5) r1.set_value_at((4, 3), 0.5) u2u3 = CompositeDomain([u2, u3]) r2 = MutableFuzzySet(u2u3) r2.set_value_at((1, 1), 1) r2.set_value_at((2, 1), 0.5) r2.set_value_at((2, 2), 0.7) r2.set_value_at((3, 3), 1) r2.set_value_at((3, 4), 0.4) my_composition = composition_of_binary_relations(r1, r2) for domain_element in my_composition.domain.domain_elements: print("mu({}) = {}".format( domain_element, my_composition.get_value_at(domain_element)))
def composition_of_binary_relations(fuzzy_set1, fuzzy_set2): """ Calculating composition of two relations. :param fuzzy_set1: FuzzySet (defined on domain UxV) :param fuzzy_set2: FuzzySet (defined on domain VxW) :return: FuzzySet (defined on domain UxW) """ # extracting domain elements of both sets domain_elements1 = fuzzy_set1.domain.domain_elements domain_elements2 = fuzzy_set2.domain.domain_elements # defining UxW space in which composition will be defined u = fuzzy_set1.domain.get_component(0).domain_elements w = fuzzy_set2.domain.get_component(1).domain_elements # creating two SimpleDomain instances which will # create CompositeDomain # REMINDER: SimpleDomain(first, last) does NOT # include last element, so there is +1 (so it will be included) u_domain = SimpleDomain(u[0], u[-1]+1) w_domain = SimpleDomain(w[0], w[-1]+1) len_u = len(u)+1 len_w = len(w)+1 # creating empty array in domain UxW filled with zeros my_array = numpy.zeros(shape=(len_u, len_w)) # iterating over domain elements of first part of # composite domain for element in domain_elements1: # extracting row my_row = [item for item in domain_elements1 if element[0] == item[0]] # iterating over domain elements of second part of composite domain for element2 in domain_elements2: # extracting column my_col = [item for item in domain_elements2 if element2[1] == item[1]] # extracting row and column values (these are the values # that we will consider in finding maximum of minimum) row_values = [fuzzy_set1.member_dict[item] for item in my_row] col_values = [fuzzy_set2.member_dict[item] for item in my_col] # initializing max_min value to zero max_min = 0 for row, col in zip(row_values, col_values): # if min(row, col) is larger than # current max_min, then set max_min to # to new value if min(row, col) > max_min: max_min = min(row, col) # my index has the row index from the first element, # and column index from the second element my_index = (element[0], element2[1]) # set element in my_array on index element[0], element2[1] # to this calculated max_min value my_array[element[0], element2[1]] = max_min # creating composite domain out of two simple domains # created in the beginning compos_domain = CompositeDomain([u_domain, w_domain]) # initializing MutableFuzzy set with compos_domain UxW composition_fuzzy = MutableFuzzySet(compos_domain) # writing calculated values to new composition_fuzzy # FuzzySet for (x, y), value in numpy.ndenumerate(my_array): try: composition_fuzzy.set_value_at((x, y), value) except ValueError: pass return composition_fuzzy
from domain import SimpleDomain from sets import MutableFuzzySet, CalculatedFuzzySet from operations import zadeh_not """ This module is used for defining domains of input and control variables, as well as fuzzy sets used for control of the boat. For example: dangerously_close, small_velocity etc. """ distance_domain = SimpleDomain(0, 1301) velocity_domain = SimpleDomain(20, 51) direction_domain = SimpleDomain(0, 2) acceleration_domain = SimpleDomain(-10, 11) angle_domain = SimpleDomain(-90, 91) correct_direction = MutableFuzzySet(direction_domain) correct_direction.set_value_at(0, 0) correct_direction.set_value_at(1, 1) dangerously_close = CalculatedFuzzySet(distance_domain) dangerously_close.set_calculated_memberships("l", alpha=0.02, beta=0.05) close = CalculatedFuzzySet(distance_domain) close.set_calculated_memberships("l", alpha=0.05, beta=0.07) not_close = zadeh_not(close) small_velocity = CalculatedFuzzySet(velocity_domain) small_velocity.set_calculated_memberships("l", alpha=0.2, beta=0.95) large_acceleration = CalculatedFuzzySet(acceleration_domain)
from fuzzy import CalculatedFuzzySet, StandardFuzzySets, Debug from domain import SimpleDomain ANGLE_DOMAIN = SimpleDomain(-90, 91) DISTANCE_DOMAIN = SimpleDomain(0, 1301) VELOCITY_DOMAIN = SimpleDomain(0, 101) ACCELERATION_DOMAIN = SimpleDomain(-50, 51) TURN_LEFT = CalculatedFuzzySet(ANGLE_DOMAIN, StandardFuzzySets.gamma_function(150, 180)) TURN_RIGHT = CalculatedFuzzySet(ANGLE_DOMAIN, StandardFuzzySets.l_function(0, 30)) CLOSE = CalculatedFuzzySet(DISTANCE_DOMAIN, StandardFuzzySets.l_function(40, 60)) FAR = CalculatedFuzzySet(DISTANCE_DOMAIN, StandardFuzzySets.gamma_function(60, 75)) WRONG_WAY = CalculatedFuzzySet(DISTANCE_DOMAIN, StandardFuzzySets.l_function(0, 1)) TOO_FAST = CalculatedFuzzySet(VELOCITY_DOMAIN, StandardFuzzySets.gamma_function(60, 70)) TOO_SLOW = CalculatedFuzzySet(VELOCITY_DOMAIN, StandardFuzzySets.l_function(25, 50)) SPEED_UP = CalculatedFuzzySet(ACCELERATION_DOMAIN, StandardFuzzySets.lambda_function(55, 60, 65)) SLOW_DOWN = CalculatedFuzzySet(ACCELERATION_DOMAIN, StandardFuzzySets.l_function(40, 50)) if __name__ == '__main__': Debug.print(CLOSE) Debug.print(FAR)
def test_relation_characteristics(): """ Test made for testing characteristics of fuzzy relations. This test will test most of the functions from relation.py that return boolean. :return: None """ u = SimpleDomain(1, 6) u2 = CompositeDomain([u, u]) r1 = MutableFuzzySet(u2) r1.set_value_at((1, 1), 1) r1.set_value_at((2, 2), 1) r1.set_value_at((3, 3), 1) r1.set_value_at((4, 4), 1) r1.set_value_at((5, 5), 1) r1.set_value_at((3, 1), 0.5) r1.set_value_at((1, 3), 0.5) r2 = MutableFuzzySet(u2) r2.set_value_at((1, 1), 1) r2.set_value_at((2, 2), 1) r2.set_value_at((3, 3), 1) r2.set_value_at((4, 4), 1) r2.set_value_at((5, 5), 1) r2.set_value_at((3, 1), 0.5) r2.set_value_at((1, 3), 0.1) r3 = MutableFuzzySet(u2) r3.set_value_at((1, 1), 1) r3.set_value_at((2, 2), 1) r3.set_value_at((3, 3), 0.3) r3.set_value_at((4, 4), 1) r3.set_value_at((5, 5), 1) r3.set_value_at((1, 2), 0.6) r3.set_value_at((2, 1), 0.6) r3.set_value_at((2, 3), 0.7) r3.set_value_at((3, 2), 0.7) r3.set_value_at((3, 1), 0.5) r3.set_value_at((1, 3), 0.5) r4 = MutableFuzzySet(u2) r4.set_value_at((1, 1), 1) r4.set_value_at((2, 2), 1) r4.set_value_at((3, 3), 1) r4.set_value_at((4, 4), 1) r4.set_value_at((5, 5), 1) r4.set_value_at((1, 2), 0.4) r4.set_value_at((2, 1), 0.4) r4.set_value_at((2, 3), 0.5) r4.set_value_at((3, 2), 0.5) r4.set_value_at((1, 3), 0.4) r4.set_value_at((3, 1), 0.4) print("r1 je definiran nad UxU: {}".format(is_U_times_relation(r1))) print("r1 je simetricna: {}".format(is_symmetric(r1))) print("r2 je simetricna: {}".format(is_symmetric(r2))) print("r1 je refleksivna: {}".format(is_reflexive(r1))) print("r3 je refleksivna: {}".format(is_reflexive(r3))) print("r3 je max-min tranzitivna: {}".format(is_max_min_transitive(r3))) print("r4 je max-min tranzitivna: {}".format(is_max_min_transitive(r4)))
:param my_func: str ("step" or "gamma" etc) :return: None """ try: self.memberships = unitary_function(self.domain, my_func, **kwargs) except KeyError: self.memberships = my_func(self.domain) self.member_dict = dict([ (domain_element, value) for (domain_element, value) in zip(self.domain.domain_elements, self.memberships) ]) if __name__ == "__main__": simple_domain = SimpleDomain(1, 40, "Pero") my_fuzzy = MutableFuzzySet(simple_domain, "PeroSet") my_fuzzy.set_value_at(1, 0.4) try: my_fuzzy.set_value_at(2, 1.9) except ValueError: print("Correct error raised.") #my_fuzzy.print_fuzzy_set() lista = unitary_function(simple_domain, "l") # for index, item in enumerate(lista): # print("Index: {}, Element domene: {}, Vrijednost: {}".format(index, simple_domain.domain_elements[index], item)) my_calculated = CalculatedFuzzySet(simple_domain) simp1 = SimpleDomain(1, 5, "bla") simp2 = SimpleDomain(1, 5, "bla2") my_comp = CompositeDomain([simp1, simp2])
fuzzy_t.set_name = "Ham_T_norm_{}_{}".format(fuzzy_set1.set_name, fuzzy_set2.set_name) return fuzzy_t def hamSnorm(fuzzy_set1, fuzzy_set2, my_param=0.5): """ Implementation of Hamachers s-norm. This function receives two instances of FuzzySet and returns new instance of FuzzySet class with operation Hamachers s-norm calculated for memberships values of two input FuzzySets. :param fuzzy_set1: instance of FuzzySet :param fuzzy_set2: instance of FuzzySet :return: fuzzy_s: instance of FuzzySet """ new_memberships = [0]*len(fuzzy_set1.domain.domain_elements) for index, (value1, value2) in enumerate(zip(fuzzy_set1.memberships, fuzzy_set2.memberships)): new_memberships[index] = (value1+value2-(2-my_param)*(value1*value2))/(1 - (1-my_param)*value1*value2) fuzzy_s = CalculatedFuzzySet(fuzzy_set1.domain) fuzzy_s.memberships = new_memberships fuzzy_s.set_name = "Ham_T_norm_{}_{}".format(fuzzy_set1.set_name, fuzzy_set2.set_name) return fuzzy_s if __name__ == "__main__": my_domain = SimpleDomain(0, 20) my_calculated = CalculatedFuzzySet(my_domain) my_calculated2 = CalculatedFuzzySet(my_domain, my_func="l") my_set = zadeh_or(my_calculated, my_calculated2) t_norm = hamTnorm(my_calculated, my_calculated2) s_norm = hamSnorm(my_calculated2, my_calculated)