Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions biri.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ def select(L, solu):
ret = None
for sub in L:
#print (sub)
if not(sub in solu):
if len(sub) > max_inter:
max_inter = len(sub)
ret = sub
if sub not in solu and len(sub) > max_inter:
max_inter = len(sub)
ret = sub
Comment on lines -6 to +8
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function select refactored with the following changes:

return ret


Expand All @@ -16,40 +15,40 @@ def select(L, solu):
if __name__ == "__main__":
#Definicao da demanda
k = 2

#Definicao do grafo
#Tamanho dos conjuntos
#L = subconjuntos
#R = elementos
nL = 4
nR = 5

#Elementos
e1 = "e1"
e2 = "e2"
e3 = "e3"
e4 = "e4"
e5 = "e5"

#subconjuntos
s1 = [e2, e3]
s2 = [e1, e2, e3]
s3 = [e1, e2, e3, e4, e5]
s4 = [e2, e3, e5]

#Grafo
L = [s1, s2, s3, s4]
R = [e1, e2, e3, e4, e5]

#print (L)
#Heuristica
E = R
solucao = []

while len(solucao) < k:
selected = select(L, solucao)
E = [selected]
solucao += [selected]

Comment on lines -19 to +52
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found the following improvement in Lines 19-53:

print (solucao)

108 changes: 45 additions & 63 deletions biriBIRI.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ def biri(lastBiri):
if lastBiri <= 1:
lastBiri = 2
biriPorcent = randint(1, 2* (lastBiri))
if lastBiri % biriPorcent == 0:
return (True, biriPorcent)
else:
return (False, 0)
return (True, biriPorcent) if lastBiri % biriPorcent == 0 else (False, 0)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function biri refactored with the following changes:


def select(L, solu, biribi, rett = None)
biriInd = 0;
Expand All @@ -32,18 +29,10 @@ def select(L, solu, biribi, rett = None)
return (ret, biriRet)

def interc(A, B):
retu = 0
for ele in A:
if ele in B:
retu += 1
return retu
return sum(ele in B for ele in A)
Comment on lines -35 to +32
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function interc refactored with the following changes:


def intercc(A,B):
ret = []
for ele in A:
if ele in B:
ret += [ele]
return ret
return [ele for ele in A if ele in B]
Comment on lines -42 to +35
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function intercc refactored with the following changes:


def first(L1):
max = 0
Expand All @@ -57,92 +46,85 @@ def first(L1):
def execute(xazam):
initialBiri = 2
biribiri = initialBiri

resposta = ['1','3','4']

grafo = open('instancias/especiais/baixa/20 20 baixa.txt', 'r')
demanda = open('instancias/especiais/baixa/demanda.txt', 'r')
#Definicao da demanda
k = int(demanda.read())

demanda.close()
#Definicao do grafo
#Tamanho dos conjuntos
#L = subconjuntos
#R = elementos
vertices = grafo.readlines()
nL = int((vertices[0].split())[0])
nR = int((vertices[0].split())[1])

#print (vertices)

#print ("k = " +str(k))
#print ("L = " +str(nL))
#print ("R = " +str(nR))

L = []
R = []
for i in range(1,nL+1):
S = []
R += [i]
for j in range(0,nR):
if int((vertices[i].split())[j]) == 1:
S += [j+1]
#[int((vertices[i].split())[j]) == 1:]
L += [S]
#print (S)

grafo.close()


with open('instancias/especiais/baixa/20 20 baixa.txt', 'r') as grafo:
with open('instancias/especiais/baixa/demanda.txt', 'r') as demanda:
#Definicao da demanda
k = int(demanda.read())

#Definicao do grafo
#Tamanho dos conjuntos
#L = subconjuntos
#R = elementos
vertices = grafo.readlines()
nL = int((vertices[0].split())[0])
nR = int((vertices[0].split())[1])

#print (vertices)

#print ("k = " +str(k))
#print ("L = " +str(nL))
#print ("R = " +str(nR))

L = []
R = []
for i in range(1,nL+1):
R += [i]
S = [j+1 for j in range(nR) if int((vertices[i].split())[j]) == 1]
L += [S]
#print (S)

#print ("R = " +str(R))
#print ("L = " +str(L))

#Heuristica
E = R
solucao = []
E = first(L)
solucao += [first(L)]

#print (solucao)
#print (E)

while len(solucao) < k:
selected = select(L, solucao, biribiri,E)
E = selected[0]
solucao += [selected[0]]
biribiri = selected[1]
inter = intercc(solucao[0], solucao[1])

inter = intercc(solucao[0], solucao[1])
if k > 2:
for u in range(2,k):
inter = intercc(inter, solucao[u])

#print (resposta)
print (len(inter))
#print (inter)
#print (solucao)

Comment on lines -60 to +106
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function execute refactored with the following changes:

This removes the following comments ( why? ):

#[int((vertices[i].split())[j]) == 1:]

return (len(inter) == xazam, len(inter))


# Main
if __name__ == "__main__":
resposta = 3
acertos = 0
for i in range(0,100):

for _ in range(100):
max = 0
teste = execute(resposta)
if teste[0]:
acertos += 1
if teste[1] > max:
max = teste[1]



print ("------acertos-----")
print (acertos)

Comment on lines -132 to +127
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 132-145 refactored with the following changes:

print ("-----mais proximo----")
print (teste[1])