Last active
September 8, 2021 20:36
-
-
Save Marceeaax/b4bf3bea1b21b7e70049752ac5a33d4f to your computer and use it in GitHub Desktop.
An algorithm that generates a transitive, symmetric and reflexive fuzzy relation using random generated numbers. A better approach is required since for N > 3 the algorithm becomes extremely slow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def reflexive(X): | |
firstvalue = X[0][0] | |
if(firstvalue != 1): | |
return False | |
else: | |
for i in range(1,len(X)): | |
if(X[i][i] != firstvalue): | |
return False | |
return True | |
def symmetric(X): | |
for i in range(len(X)): | |
for j in range(len(X)): | |
if (X[i][j] != X[j][i]): | |
return False | |
return True | |
def transitive(X): | |
for i in range(len(X)): | |
for j in range(len(X)): | |
for k in range(len(X)): | |
if (X[j][k] < max(X[j][k],min(X[j][i],X[i][k]))): | |
return False | |
return True | |
""" | |
As an example, if you want to generate a symmetric, transitive and not reflexive fuzzy relations, assign the values | |
(0,1,1) the 'generatematrix(r,s,t)' function | |
""" | |
""" | |
This is a naive implementation because the algorithm only fills the cells with randon numbers and then checks if | |
all constraints are met. Backtracking or dynamic programming could potentially optimize the algorithm. | |
I'll probably modify it later | |
""" | |
def generatematrix(r,s,t): | |
conditionmeet = 0 | |
N = random.randint(3,4) | |
A = [[None]*N for _ in range(N)] | |
""" | |
A = [[0,1,1], | |
[1,0,1], | |
[1,1,0]] | |
""" | |
while(conditionmeet == 0): | |
if(r == 1): | |
for i in range(len(A)): | |
A[i][i] = 1 | |
for i in range(len(A)): | |
for j in range(len(A)): | |
if(not(r == 1 and i == j)): | |
A[i][j] = round(random.uniform(0, 1),1) | |
#print(A) | |
if(((transitive(A) == t) and (symmetric(A) == s)) == True): | |
conditionmeet = 1 | |
return A | |
print(generatematrix(1,1,0)) | |
print("matriz final") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment