Exemplo n.º 1
0
    for i in sorted(U.keys()):
        if i not in s:
            temp[i] = 0
        else:
            temp[i] = s[i]
    del s
    return temp


if __name__ == "__main__":
    clear()
    print("Fuzzy Logic: IF A THEN B ELSE C [ R = ( × B) ∪ (not A × C) ]\n")
    # a is from universe A
    A = get_universal_set("Universal A")
    # getting all elements of universe A even with zero membership
    a = get_set("A")
    a = sort_Fuzzy_set(a, A)
    neg_a = fuzzy_negation(a, A)

    # b and c are from universe Y
    Y = get_universal_set("Universal Y")
    b = get_set("B")
    b = sort_Fuzzy_set(b, Y)

    c = get_set("C")
    c = sort_Fuzzy_set(c, Y)

    while True:
        clear()
        print("A")
        print_set(a)
	for i in lst2:
		header += f"{i:^4}"
	print(header)

	# printing row by row
	index = 0
	for i in set1.keys():
		string = f'{i}| '+'  '.join(map(str,map(float,product[index])))
		print('_'*len(string))
		print(string)
		index += 1
	print('-'*40+'\n')

if __name__ == "__main__":
	clear()
	set1 = get_set()
	print_set(set1)
	set2 = get_set()
	print_set(set2)

	product = fuzz_product(set1, set2)
	
	print_product(set1,set2,product)
	print( "*" * 30)

"""
Example Input
2
a 0.4
b 0.6
3
Exemplo n.º 3
0
    clear = lambda: os.system('cls')
else:
    clear = lambda: os.system('clear')


def lambda_cutset(set1, l):
    new = defaultdict(float)
    for i, v in set1.items():
        if v >= l:
            new[i] = v
    return new


while True:
    clear()
    set1 = get_set()
    print_set(set1)
    lambda_value = float(input("Enter lambda value: "))
    print("New Lambda Cut Set: \n")
    print_set(lambda_cutset(set1, lambda_value))
    input()
"""
Example Input
5
2
a 0.2
b 0.5
c 0.9
d 0.25
e 0.35
0.3
Exemplo n.º 4
0
def intensify(fs):
    for i, v in fs.items():
        if v <= 0.5:
            fs[i] = round(2 * v * v, 2)
        else:
            fs[i] = round(1 - 2 * (1 - v)**2, 2)
    return fs


if __name__ == "__main__":
    clear()
    print("Fuzzy Rule Base System")

    # define a set alpha
    alpha = get_set("Alpha")
    flag = True

    while flag:
        clear()
        print("Alpha:")
        print_set(alpha)

        while flag:
            print("1. Very α")
            print("2. Very Very α")
            print("3. Plus α")
            print("4. Slightly α")
            print("5. Minus α")
            print("6. Intensify")
            print("7. Not alpha")
Exemplo n.º 5
0
def ask_query(query):
	# sets dictionary contain many fuzzy sets used in dictionary
	sets = {}

	# split string and check words not in keyword
	for q in query.split():
		if q .lower() not in ['and','or','not','very','intensely','plus','minus','slightly']:
			print(f"Define set: {q}")
			sets[q] = get_set(q)
	clear()
	# small = OrderedDict({1:1, 2:0.8, 3:0.6, 4:0.4, 5:0.2})
	# large = OrderedDict({1:0.2, 2:0.4, 3:0.6, 4:0.8, 5:1})

	# seperate the string based on 'or' occurance 
	or_seperated = query.split('or')

	# all values are set to 0 so that we can combine (using max operator) different or result 
	or_set = OrderedDict({1: 0, 2: 0, 3: 0, 4: 0, 5: 0})

	# iterate on each component seperated in 'or' split
	for q_or in or_seperated:
		print('*'*50)
		print("OR Clause: ",q_or)

		# seperate each of the or query on 'and' occurance
		and_seperated = q_or.strip().split('and')

		# all values are set to 1 so that we can combine different and result 
		and_set = OrderedDict({1: 1, 2: 1, 3: 1, 4: 1, 5: 1})

		# iterate on each of the and seperated query
		for q_and in and_seperated:
			print("\tAND Clause: ",q_and.strip())
			
			# spliting or clause further by 'and' values
			words = q_and.split()

			# checking for not prefix in clause
			if words[0] == 'not':
				not_prefix = True
				words.pop(0)
			else:
				not_prefix = False

			if len(words) == 3: 
					if words[0] == 'very' and words[1] == 'very':
						temp =  very_very_alpha( sets[words[2]] )
			elif len(words) == 2:
					if words[0] == 'very':
						temp = very_alpha( sets[words[1]] )
					elif words[0] == 'plus':
						temp = plus_alpha( sets[words[1]] )
					elif words[0] == 'slightly' or words[0] == 'slight':
						temp = Slightly_alpha( sets[words[1]] )
					elif words[0] == 'minus':
						temp = minus_alpha( sets[words[1]] )
					elif words[0] == 'intensify':
						temp = intensify( sets[words[1]] )
			elif len(words) == 1:
					temp = sets[words[0]]

			# if not is present in brgining than obtaing negation
			if not_prefix:
				temp = not_alpha(temp) 

			for i,v in temp.items():
				and_set[i] = min(and_set[i], v)
			print("\nResult of this AND clause: ")
			print_set(and_set)

		for i,v in and_set.items():
			or_set[i] = max(or_set[i], v)
		print('*'*50)

	print("\nFinal result: ")
	print_set(or_set)
from Fuzzy_set import get_set, print_set
from Cartesian_product_fuzzy import get_set, print_set, fuzz_product, print_product
from Crisp_Relation_Composition import Min_Max_comp, Max_product_comp
import os
import sys

if os.name == 'nt':
    clear = lambda: os.system('cls')
else:
    clear = lambda: os.system('clear')

if __name__ == "__main__":
    set1 = get_set("First")
    set2 = get_set("Second")
    set3 = get_set("Third")

    while True:
        clear()

        # get 3 sets to make relation by cross product
        print_set(set1)
        print_set(set2)
        print_set(set3)

        # obtain cartesian product
        prod1 = fuzz_product(set1, set2)
        prod2 = fuzz_product(set2, set3)

        print("Cartesian Product of set1 and set2: ")
        print_product(set1, set2, prod1, 'set1', 'set2')
        print("Cartesian Product of set2 and set3: ")
Exemplo n.º 7
0
from collections import OrderedDict
import os

if os.name == 'nt':
    clear = lambda: os.system('cls')
else:
    clear = lambda: os.system('clear')

if __name__ == "__main__":
    clear()
    print("1. Calculate R = (A x B) U (not A x Y)")
    print("2. Calculate B_prime = A_prime x R\n")
    # a is from universe A
    A = get_universal_set("Universal A")
    # getting all elements of universe A even with zero membership
    a = sort_Fuzzy_set(get_set("A"), A)
    a_prime = sort_Fuzzy_set(get_set("A_Prime"), A)
    neg_a = fuzzy_negation(a, A)

    # b from universe Y
    Y = get_universal_set("Universal Y")
    b = sort_Fuzzy_set(get_set("B"), Y)

    while True:
        clear()
        print("A:")
        print_set(a)
        print("Not A:")
        print_set(neg_a)
        print("A_Prime:")
        print_set(a_prime)