Commit 53798901 authored by Quentin Verout's avatar Quentin Verout
Browse files

Correct k-means

parent f8368902
Loading
Loading
Loading
Loading
+41 −34
Original line number Diff line number Diff line
@@ -9,6 +9,8 @@ import copy
import statistics
import collections
import operator
from itertools import tee



class Robustness:
@@ -89,6 +91,7 @@ class Robustness:
		Return values:
			- min_c: (Interger) - Minimum cost found among the random_permutation.
		"""
		
		before = time.time()
		min_c = self.cost_rate()
		after = time.time()
@@ -196,7 +199,6 @@ class Robustness:
		Return values:
			- word: (string) - The average word found for the list l.
		"""
		
		avg_word=[]
		avg_len = 0
		
@@ -243,59 +245,68 @@ class Robustness:
		
		#Centroids
		print(self.l)
		Centroids = random.sample(self.l, k)
		print("First centroids :", Centroids,"\n")
		centroids = random.sample(self.l, k)
		print("First centroids :", centroids,"\n")
		
		#Current Average
		average = [[] for i in range(k)]
		ex_centroids = [[] for i in range(k)]
		
		#Converge ? 
		ex_average = [[] for i in range(k)]

		#Random permutations
		perm = self.algo()
		
		for y in range(max_iters):
			
			#Random permutations
			perm = self.algo()    #=> problèmes que se soit une permutations aléatoire a chaque nouveau tour ? 
			#Copy the genrator
			perm, copy_perm = tee(perm)
			
			cluster = [[] for i in range(k)]
			
			#For every word	
			for w in perm:
			for w in copy_perm:
				clostest_word =''
				mini_dist = 30000
				
				#Wich centroids is closest
				for i in Centroids:
				for i in centroids:
					if self.distance(i,w) < mini_dist:
						closest_word = i
						mini_dist = self.distance(i,w)
				cluster[Centroids.index(closest_word)].append(w)
				cluster[centroids.index(closest_word)].append(w)
				
			#Update average for next turn
			ex_average = copy.deepcopy(average)
			ex_centroids = copy.deepcopy(centroids)
			
			#Update centroids (middle of each cluster)
			print("Cluster", cluster)
			for i in range (len(centroids)):
				centroids[i] = self.avg_wordlist(cluster[i])
			
			print("Update centroids :", centroids,"\n")
			
			cost = 0
			
			#Update average (each average is a cost_rate function)
			for i in range(0, len(cluster)):
			for i in cluster:
				if algo == 1:
					average[i] = Robustness(cluster[i]).cost_rate()/len(cluster[i]) #=> cost_rate() cout différent (presque) a chaque nouveau lancer
					cost += Robustness(i).cost_rate()
				else :
					closest = Robustness(cluster[i]).closest_word() #=> Closest_word() qui choisis le premier mot en random problème ? 
					for wrd, cost in closest:
						avg = cost
					average[i] = avg/len(cluster[i])
					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_average == average:
			if ex_centroids == centroids:
				print("Breaked at the %dth iterations" % (y))
				return cluster
				return cluster, cost
					
			#Update centroids (middle of each cluster)
			print("Cluster", cluster)
			for i in range (len(Centroids)):
				Centroids[i] = self.avg_wordlist(cluster[i])
		
			print("Update centroids :", Centroids,"\n")
		print("Optimal solutions not found after %d th iterations" %(y))
		return cluster, average
		print("cost", cost)
		return cluster, cost
    
	def sampling(self, t, algo=1):
		"""
@@ -341,11 +352,7 @@ class Robustness:
			cost = smp.min_cost()
		
		elif algo == 5:
			cost = 0
			clt = smp.k_means()[0]
			avg = smp.k_means()[1]
			for i in range(len(clt[1])):
				cost += avg[i]*len(clt[i])
			cost = smp.k_means()[1]
			
		return cost*t
			
@@ -361,7 +368,7 @@ if __name__ == '__main__':
	before = time.time()
	
	cluster = R.k_means()
	print(cluster)
	print("yo",cluster)
	
	after = time.time()
	print("One complete random_permutation in %d seconds" % (after-before))