Commit 35f23453 authored by Quentin Verout's avatar Quentin Verout
Browse files

Add resulats.pdf and runTest.py

parent 53798901
Loading
Loading
Loading
Loading
+30 −30
Original line number Diff line number Diff line
@@ -9,10 +9,9 @@ import copy
import statistics
import collections
import operator
import textdistance
from itertools import tee



class Robustness:
	def __init__(self, pwdContainer=None):
		"""
@@ -21,6 +20,7 @@ class Robustness:
		
		self.l = list()
		self.distance = leven.distance
		#self.distance = textdistance.jaro_winkler
		self.algo = self.random_permutation
		
		if type(pwdContainer) == str:
@@ -78,6 +78,7 @@ class Robustness:
			k -= 1
		after = time.time()
		cost = int(c * ratio)
		cost = c
		#print("Run an excursion and its cost rate in ", after-before, "seconds")
		return cost

@@ -226,7 +227,7 @@ class Robustness:
			word =''.join(avg_word)
		return word
		
	def k_means(self, k=2, max_iters=100, algo=2):
	def k_means(self, k=10, max_iters=100, algo=2):
		"""
		Performs k-means clustering with Levenshtein distance. This function return the cluster assigment.
		This function indicate also the number of iterations to find the optimal solutions.
@@ -244,9 +245,9 @@ class Robustness:
		"""
		
		#Centroids
		print(self.l)
		#print(self.l)
		centroids = random.sample(self.l, k)
		print("First centroids :", centroids,"\n")
		#print("First centroids :", centroids,"\n")
		
		#Current Average
		ex_centroids = [[] for i in range(k)]
@@ -266,8 +267,8 @@ class Robustness:
			
			#For every word	
			for w in copy_perm:
				clostest_word =''
				mini_dist = 30000
				clostest_word = ""
				mini_dist = 999999
				
				#Wich centroids is closest
				for i in centroids:
@@ -280,33 +281,29 @@ class Robustness:
			ex_centroids = copy.deepcopy(centroids)
			
			#Update centroids (middle of each cluster)
			print("Cluster", cluster)
			#print("Cluster", cluster)
			for i in range (len(centroids)):
				centroids[i] = self.avg_wordlist(cluster[i])
			#print("Update centroids :", centroids,"\n")
					
			print("Update centroids :", centroids,"\n")
			#Converge ? If yes, then break
			if ex_centroids == centroids or y == max_iters-1:
				print("Breaked at the %dth iterations" % (y))
				
				cost = 0
			
				for i in cluster:
					if algo == 1:
						cost += Robustness(i).cost_rate()
						print("cout", cost)
					else :
						closest = Robustness(i).closest_word()
						second_cost = 0
						for i in closest:
							second_cost = i[1]
						cost += second_cost
					
			#Converge ? If yes, then break
			if ex_centroids == centroids:
				print("Breaked at the %dth iterations" % (y))
				return cluster, cost
					
		
		print("Optimal solutions not found after %d th iterations" %(y))
		print("cost", cost)
				return cluster, cost
			print(y)
    
	def sampling(self, t, algo=1):
		"""
@@ -367,8 +364,11 @@ if __name__ == '__main__':
	
	before = time.time()
	
	cluster = R.k_means()
	print("yo",cluster)
	l=[]
	for i in range(10):
		l.append(R.min_cost())
	l = sorted(l)
	print(l[int(len(l)/2)])
	
	after = time.time()
	print("One complete random_permutation in %d seconds" % (after-before))

resultats.pdf

0 → 100644
+85.7 KiB

File added.

No diff preview for this file type.

runTest.py

0 → 100644
+216 −0
Original line number Diff line number Diff line
#!/usr/bin/env python3

import sys
import time
from distance import *

if __name__ == '__main__':
	
	print("\nThis test will run some function on two database to compare their robustness, The robustness of a database depends on its length, this is why the minimum length of the two files will be the maximum reference for both files\n")
	before = time.time()
	firstFile = Robustness(sys.argv[1])
	
		
	after = time.time()
	print("First file loaded in %d seconds." % (after-before))
	
	before = time.time()
	secondFile = Robustness(sys.argv[2])
	after = time.time()
	print("Second file loaded in %d seconds.\n" % (after-before),)
	
	mini_D = min(len(firstFile.l), len(secondFile.l))
	firstFile.k, secondFile.k = mini_D, mini_D
	print("Maximum D =", mini_D)
	
	res = []
	
	print("\n=============================================================\n")
	
	print("Function : COST_RATE")
	
	firstFile.k, secondFile.k = 1000, 1000
	
	l1, l2 = [], []
	
	for i in range(100):
		l1.append(firstFile.cost_rate())
		l2.append(secondFile.cost_rate())
		l1 = sorted(l1)
		l2 = sorted(l2)
	
	print("d =", firstFile.k," first file got a median of :", l1[int(len(l1)/2)], "while the second one got a median of: " , l2[int(len(l2)/2)], "		(100 iterations)")
	res.append(l1[int(len(l1)/2)]>l2[int(len(l2)/2)])
	
	firstFile.k, secondFile.k = 1000000, 1000000
	
	l1, l2 = [], []
	
	for i in range(100):
		l1.append(firstFile.cost_rate())
		l2.append(secondFile.cost_rate())
		l1 = sorted(l1)
		l2 = sorted(l2)
	
	print("d =", firstFile.k," first file got a median of :", l1[int(len(l1)/2)], "while the second one got a median of: " , l2[int(len(l2)/2)], "		(100 iterations)")
	res.append(l1[int(len(l1)/2)]>l2[int(len(l2)/2)])
	
	firstFile.k, secondFile.k = mini_D, mini_D
	
	l1, l2 = [], []
	
	for i in range(10):
		l1.append(firstFile.cost_rate())
		l2.append(secondFile.cost_rate())
		l1 = sorted(l1)
		l2 = sorted(l2)
	
	print("d =", firstFile.k," first file got a median of :", l1[int(len(l1)/2)], "while the second one got a median of: " , l2[int(len(l2)/2)], "		(10 iterations)")
	res.append(l1[int(len(l1)/2)]>l2[int(len(l2)/2)])
	
	print("\n=============================================================\n")
	
	print("Function : MIN_COST")

	firstFile.k, secondFile.k = 1000, 1000
	
	l1, l2 = [], []
	
	for i in range(100):
		l1.append(firstFile.min_cost())
		l2.append(secondFile.min_cost())
		l1 = sorted(l1)
		l2 = sorted(l2)
	
	print("d =", firstFile.k," first file got a median of :", l1[int(len(l1)/2)], "while the second one got a median of: " , l2[int(len(l2)/2)], "		(100 iterations)")
	res.append(l1[int(len(l1)/2)]>l2[int(len(l2)/2)])
	
	firstFile.k, secondFile.k = 100000, 100000
	
	l1, l2 = [], []
	
	for i in range(10):
		l1.append(firstFile.min_cost())
		l2.append(secondFile.min_cost())
		l1 = sorted(l1)
		l2 = sorted(l2)
	
	print("d =", firstFile.k," first file got a median of :", l1[int(len(l1)/2)], "while the second one got a median of: " , l2[int(len(l2)/2)], "		(10 iterations)")
	res.append(l1[int(len(l1)/2)]>l2[int(len(l2)/2)])
	
	firstFile.k, secondFile.k = 1000000, 1000000
	
	l1, l2 = [], []
	
	for i in range(10):
		l1.append(firstFile.min_cost())
		l2.append(secondFile.min_cost())
		l1 = sorted(l1)
		l2 = sorted(l2)
	
	print("d =", firstFile.k," first file got a median of :", l1[int(len(l1)/2)], "while the second one got a median of: " , l2[int(len(l2)/2)], "		(10 iterations)")
	res.append(l1[int(len(l1)/2)]>l2[int(len(l2)/2)])
	
	print("\n=============================================================\n")
	
	print("Function : CLOSEST_WORD_AMONG")
	
	l1, l2 = [], []
	
	for i in range(10):
		k = 10000
		perm1 = R.closest_word_among(m=10)
		perm2 = R.closest_word_among(m=10)
		for i in range(k):
			cost1 = next(perm1)[1]
			cost2 = next(perm2)[1]
		l1.append(cost1)
		l2.append(cost2)
	l1 = sorted(l1)
	l2 = sorted(l2)
	
	print("d =", k," and m = 10, first file got a median of :", l1[int(len(l1)/2)], "while the second one got a median of: " , l2[int(len(l2)/2)], "		(10 iterations)")
	res.append(l1[int(len(l1)/2)]>l2[int(len(l2)/2)])
	
	l1, l2 = [], []
	
	for i in range(10):
		k = 10000
		perm1 = R.closest_word_among(m=100)
		perm2 = R.closest_word_among(m=100)
		for i in range(k):
			cost1 = next(perm1)[1]
			cost2 = next(perm2)[1]
		l1.append(cost1)
		l2.append(cost2)
	l1 = sorted(l1)
	l2 = sorted(l2)
	
	print("d =", k," and m = 100, first file got a median of :", l1[int(len(l1)/2)], "while the second one got a median of: " , l2[int(len(l2)/2)], "		(10 iterations)")
	res.append(l1[int(len(l1)/2)]>l2[int(len(l2)/2)])
	
	l1, l2 = [], []
	
	for i in range(10):
		k = 100000
		perm1 = R.closest_word_among(m=10)
		perm2 = R.closest_word_among(m=10)
		for i in range(k):
			cost1 = next(perm1)[1]
			cost2 = next(perm2)[1]
		l1.append(cost1)
		l2.append(cost2)
	l1 = sorted(l1)
	l2 = sorted(l2)
	
	print("d =", k," and m = 10, first file got a median of :", l1[int(len(l1)/2)], "while the second one got a median of: " , l2[int(len(l2)/2)], "		(10 iterations)")
	res.append(l1[int(len(l1)/2)]>l2[int(len(l2)/2)])
	
	l1, l2 = [], []
	
	for i in range(10):
		k = 100000
		perm1 = R.closest_word_among(m=100)
		perm2 = R.closest_word_among(m=100)
		for i in range(k):
			cost1 = next(perm1)[1]
			cost2 = next(perm2)[1]
		l1.append(cost1)
		l2.append(cost2)
	l1 = sorted(l1)
	l2 = sorted(l2)
	
	print("d =", k," and m = 100, first file got a median of :", l1[int(len(l1)/2)], "while the second one got a median of: " , l2[int(len(l2)/2)], "		(10 iterations)")
	res.append(l1[int(len(l1)/2)]>l2[int(len(l2)/2)])
	
	print("\n=============================================================\n")
	
	print("Function : K-MEANS")
	
	l1, l2 = [], []
	
	perm1 = firstFile.random_permutation()
	perm2 = secondFile.random_permutation()
	for i in range(mini_D):
		l1.append(next(perm1))
		l2.append(next(perm2))
	
	cost1 = Robustness(l1).k_means(algo = 1)
	cost2 = Robustness(l2).k_means(algo = 1)
	print(x[1])
	
	print("d =", mini_D," and k = 10 with cost_rate, first file got a cost of :", cost1[1], "while the second one got a cost of: " , cost2[1])
	res.append(cost1[1]>l2cost2[2])
	
	print("\n=============================================================\n")
	
	print("CONCLUSION")
	
	res = sorted(res)
	
	if l[int(len(l)/2)] == True:
		print("On average, the first file is more robust than the second file.")
	else:
		print("On average, the second file is more robust than the first file.")