Exemple #1
0
def get_information(file_path):  # using target_MDG and a benchmark, create result
    """
    Execute Each algorithm for given file and return TurboMQ, cohesion, and coupling
    :param file_path: A path of dot file
    :return: Clustering result - value of TurboMQ, A list of [Cohesion, Coupling], A list of result clusters
    """
    targetMDG = make_target_MDG(file_path)
    methods = ['WCA', 'HC', 'WCA_HC', 'SA', 'WCA_SA', 'PSO', 'WCA_PSO']
    clusters_set = []
    TMQ = []
    cohe_coup = []
    print("====WCA start====\n")
    clusters_set.append(WCA(targetMDG))
    print("====WCA end====\n\n")
    
    print("====HC start====\n")
    clusters_set.append(HC.HC(targetMDG))
    print("====HC end====\n\n")
    
    print("====WCA_HC start====\n")
    clusters_set.append(HC.WCA_HC(targetMDG, WCA(targetMDG)))
    print("====WCA_HC end====\n\n")
    
    print("====SA start====\n")
    clusters_set.append(SA.SA(targetMDG))
    print("====SA end====\n\n")
    
    print("====WCA_SA start====\n")
    clusters_set.append(SA.WCA_SA(targetMDG, WCA(targetMDG)))
    print("====WCA_SA end====\n\n")
    
    print("====PSO start====\n\n")
    clusters_set.append(PSO.PSO(targetMDG))
    print("====PSO end====\n\n")
    
    print("====WCA_PSO start====\n\n")
    clusters_set.append(PSO.WCA_PSO(targetMDG, WCA(targetMDG)))
    print("====WCA_PSO end====\n\n")
    
    # get TMQ data
    for clusters in clusters_set:
        TMQ.append(TurboMQ.calculate_fitness(clusters, targetMDG))
        cohe_coup.append(TurboMQ.get_cohesion_coupling(clusters, targetMDG))
    # write result files
    for i in range(len(methods)):
        DotParser.write_file(file_path, methods[i], clusters_set[i], targetMDG)
        
    return TMQ, cohe_coup, clusters_set
def TSP(stops, Alg, steps, param, seed=None, coordfile='xycoords.txt'):
    '''A wrapper function that attempts to optimize the traveling
    salesperson problem using a specified algorithm. If coordfile
    exists, a preexisting set of coordinates will be used. Otherwise,
    a new set of "stops" coordinates will be generated for the person to
    traverse, and will be written to the specified file.'''

    # Create the distance matrix, which will be used to calculate
    # the fitness of a given path
    if os.path.isfile(coordfile):
        coords = scipy.genfromtxt(coordfile)
        distMat = DistanceMatrix(coords)
    else:
        distMat = GenerateMap(stops, fname=coordfile, seed=seed)

    if Alg == 'HC':
        # param is the number of solutions to try per step
        bestSol, fitHistory = hc.HillClimber(steps, param, distMat, seed)
    elif Alg == 'SA':
        # param is a placeholder
        bestSol, fitHistory = sa.SimulatedAnnealing(steps, param, distMat,
                                                    seed)
    elif Alg == 'MC3':
        # param is the number of chains
        bestSol, fitHistory = mc3.MCMCMC(steps, param, distMat, seed)
    elif Alg == 'GA':
        # param is the population size
        bestSol, fitHistory = ga.GeneticAlgorithm(steps, param, distMat, seed)
    else:
        raise ValueError('Algorithm must be "HC", "SA", "MC3", or "GA".')

    outfname = coordfile + '-' + Alg + '-' + \
        str(steps) + '-' + str(param) + '.txt'
    scipy.savetxt(outfname, scipy.array(bestSol), fmt='%i')
    return bestSol, fitHistory
Exemple #3
0
def main():

    parser = argparse.ArgumentParser(description='Modularize given dot file')
    parser.add_argument('file_path',
                        metavar='F',
                        type=str,
                        nargs='+',
                        help='File path for dot file')
    parser.add_argument(
        '-a',
        help=
        'Algorithm for modularization. All, WCA, HC, WCA_HC, SA, WCA_SA, PSO, WCA_PSO'
    )
    args = parser.parse_args()

    file_path = args.file_path
    if args.a:
        modularizeMethod = args.a
    else:
        modularizeMethod = 'All'

    for single_file in file_path:

        targetMDG = MakeResult.make_target_MDG(single_file)
        clusters = None
        if modularizeMethod == 'WCA':
            clusters = WCA(targetMDG)
        elif modularizeMethod == 'HC':
            clusters = HC.HC(targetMDG)
        elif modularizeMethod == 'WCA_HC':
            clusters = HC.WCA_HC(targetMDG, WCA(targetMDG))
        elif modularizeMethod == 'SA':
            clusters = SA.SA(targetMDG)
        elif modularizeMethod == 'WCA_SA':
            clusters = SA.WCA_SA(targetMDG, WCA(targetMDG))
        elif modularizeMethod == 'PSO':
            clusters = PSO.PSO(targetMDG)
        elif modularizeMethod == 'WCA_PSO':
            clusters = PSO.WCA_PSO(targetMDG, WCA(targetMDG))
        elif modularizeMethod == 'All':
            MakeResult.print_result(single_file)

        if modularizeMethod != 'All':
            DotParser.write_file(single_file, modularizeMethod, clusters,
                                 targetMDG)
Exemple #4
0
def index():
	Caster = request.args.get("Caster")
	Defense = (request.args.get("Defense"))
	if Defense:
		Defense = int(Defense)
	Armor = (request.args.get("Armor"))
	if Armor:
		Armor = int(Armor)
	Boxes = (request.args.get("Boxes"))
	if Boxes:
		Boxes = int(Boxes)	
	Focus = (request.args.get("Focus"))
	if Focus:
		Focus = int(Focus)
	if Caster and Defense and Armor and Boxes and Focus:
		percent = HC.survive_options(Caster, Defense, Armor, Boxes, Focus)
	else:
		percent = " "
	response = make_response(render_template("index.html", death=percent))
	return response
Exemple #5
0
    x = phi[i]
    means_cost += (((c[x][0] - cluster[i][0])**2 +
                    (c[x][1] - cluster[i][1])**2)**0.5)**2

#means_cost=means_cost/ float(len(cluster))

print '3-means cost is :', means_cost

print '\n Indexes are :'
print '******************************'
print 'Cluster 1:', indexes1
print '******************************'
print 'Cluster 2:', indexes2
print '******************************'
print 'Cluster 3:', indexes3
print '******************************'
""" Plotting the data """

title = "Clustering using Gonzalez algorithm "
HC.lloyds_plot(cluster, phi, c, title)
"""
print '\n Subsets are :'
print '******************************'
print 'Subset 1:', subset1
print '******************************'
print 'Subset 2:', subset2
print '******************************'
print 'Subset 3:', subset3
print '******************************'
"""
	print 'The set c now is :', b
	#print 'c[0][0] is :', c[0][0]
	#print 'c[1][0] is :', c[1][0]
	#print 'c[2][0] is :', c[2][1]
	
	
	for j in range(0, len(cluster)):
		dist1=((cluster[j][0]- c[0][0])**2 + (cluster[j][1] - c[0][1])**2)**0.5
		dist2=((cluster[j][0]- c[1][0])**2 + (cluster[j][1] - c[1][1])**2)**0.5
		dist3=((cluster[j][0]- c[2][0])**2 + (cluster[j][1] - c[2][1])**2)**0.5
		
		
		#print 'Dist1 is :', dist1
		#print 'Dist2 is :', dist2
		#print 'Dist3 is :', dist3
		min_dist=HC.my_min(dist1,dist2,dist3)
		
		#print 'Minimum is :', min_dist
		if min_dist==dist1:
			phi[j]=0
			c0_x+=cluster[j][0]
			c0_y+=cluster[j][1]
			c0_center+=1
		elif min_dist==dist2:
			phi[j]=1
			c1_x+=cluster[j][0]
			c1_y+=cluster[j][1]
			c1_center+=1
		elif min_dist==dist3:
			phi[j]=2		
			c2_x+=cluster[j][0]
slink_cluster=cluster
clink_cluster=cluster
meanlink_cluster=cluster


""" Single-link Hierarchical clustering """

while len(slink_cluster)!=4:
	for i in range(0,len(slink_cluster)):
		print 'Len is now :', len(slink_cluster)
		for j in range(0,len(slink_cluster)):
			if slink_cluster[i]==slink_cluster[j]:
				continue
			else:
				tdist=HC.single_link(slink_cluster[i],slink_cluster[j])
				print 'tdist here is:', tdist
				if tdist <= min:
					min=tdist
					print 'min at this point is :', min
					index=j
					c_cluster1=slink_cluster[i]
					c_cluster2=slink_cluster[j]
					print' Points are :', c_cluster1 ,'and :', c_cluster2
	
	print 'Cluster 1 going to be removed is :', c_cluster1
	print 'Cluster 2 going to be removed is :', c_cluster2
	slink_cluster.remove(c_cluster1)
	slink_cluster.remove(c_cluster2)
	#slink_cluster.insert(index,c_cluster1 + c_cluster2)
	slink_cluster.append(c_cluster1 + c_cluster2)
Exemple #8
0
	print 'The set c now is :', b
	#print 'c[0][0] is :', c[0][0]
	#print 'c[1][0] is :', c[1][0]
	#print 'c[2][0] is :', c[2][1]
	
	
	for j in range(0, len(cluster)):
		dist1=((cluster[j][0]- c[0][0])**2 + (cluster[j][1] - c[0][1])**2)**0.5
		dist2=((cluster[j][0]- c[1][0])**2 + (cluster[j][1] - c[1][1])**2)**0.5
		dist3=((cluster[j][0]- c[2][0])**2 + (cluster[j][1] - c[2][1])**2)**0.5
		
		
		#print 'Dist1 is :', dist1
		#print 'Dist2 is :', dist2
		#print 'Dist3 is :', dist3
		min_dist=HC.my_min(dist1,dist2,dist3)
		
		#print 'Minimum is :', min_dist
		if min_dist==dist1:
			phi[j]=0
			c0_x+=cluster[j][0]
			c0_y+=cluster[j][1]
			c0_center+=1
		elif min_dist==dist2:
			phi[j]=1
			c1_x+=cluster[j][0]
			c1_y+=cluster[j][1]
			c1_center+=1
		elif min_dist==dist3:
			phi[j]=2		
			c2_x+=cluster[j][0]
Exemple #9
0

clink_cluster=cluster



""" Complete-link hierarchical clustering """


while len(clink_cluster)!=4:
	for i in range(0,len(clink_cluster)):
		for j in range(0,len(clink_cluster)):
			if clink_cluster[i]==clink_cluster[j]:
				continue
			else:
				tdist=HC.complete_link(clink_cluster[i],clink_cluster[j])
				if tdist <= min:
					min=tdist
					#print 'min at this point is :', min
					c_cluster1=clink_cluster[i]
					c_cluster2=clink_cluster[j]
	
	#print 'Cluster 1 going to be removed is :', c_cluster1
	#print 'Cluster 2 going to be removed is :', c_cluster2
	clink_cluster.remove(c_cluster1)
	clink_cluster.remove(c_cluster2)
	clink_cluster.append(c_cluster1 + c_cluster2)
	#print 'Clusters at this stage are :', clink_cluster
	min=99999

Exemple #10
0
def hierarchical_clustering():
    G = load_data_by_networkx(email_path, type='U')
    #HC.HierarchicalClustering(G,42)
    result = HC.HierarchicalClustering(G)
    return result
	print '******************************'
	print 'Cluster 2:', indexes2
	print '******************************'
	print 'Cluster 3:', indexes3
	print '******************************'

	print 'Centers calculated are :', c

	print '3-means cost is :', new_cost

	indexes1=[]
	indexes2=[]
	indexes3=[]
	""" Plotting the data """

	title="Clustering using k-Means++ algorithm "
	HC.kmeansplusplus_plot(cluster,phi,c,title)


	val1=str(new_cost)
	val2=str(c)
	f = open( 'k_means+.txt', 'a' )
	#headers='3-means cost'
	#f.write(''.join(headers) + '\n')
	#f.write ( '3 means cost =' +'\n')
	f.write (val1 + '\t'+ val2 +'\n' )
	f.close()
	new_cost=0 	
   

 
				

print '\n Indexes are :'
print '******************************'
print 'Cluster 1:', indexes1
print '******************************'
print 'Cluster 2:', indexes2
print '******************************'
print 'Cluster 3:', indexes3
print '******************************'


""" Plotting the data """

title="Clustering using Gonzalez algorithm "
HC.lloyds_plot(cluster,phi,c,title)



"""
print '\n Subsets are :'
print '******************************'
print 'Subset 1:', subset1
print '******************************'
print 'Subset 2:', subset2
print '******************************'
print 'Subset 3:', subset3
print '******************************'
"""
			
        dist4 = (
            (cluster[j][0] - c[3][0]) ** 2
            + (cluster[j][1] - c[3][1]) ** 2
            + (cluster[j][2] - c[3][2]) ** 2
            + (cluster[j][3] - c[3][3]) ** 2
            + (cluster[j][4] - c[3][4]) ** 2
        ) * 0.5
        dist5 = (
            (cluster[j][0] - c[4][0]) ** 2
            + (cluster[j][1] - c[4][1]) ** 2
            + (cluster[j][2] - c[4][2]) ** 2
            + (cluster[j][3] - c[4][3]) ** 2
            + (cluster[j][4] - c[4][4]) ** 2
        ) * 0.5

        min_dist = HC.median5_min(dist1, dist2, dist3, dist4, dist5)

        # print 'Minimum is :', min_dist
        if min_dist == dist1:
            phi[j] = 0
            cluster1.append(cluster[j])
        elif min_dist == dist2:
            phi[j] = 1
            cluster2.append(cluster[j])
        elif min_dist == dist3:
            phi[j] = 2
            cluster3.append(cluster[j])
        elif min_dist == dist4:
            phi[j] = 3
            cluster4.append(cluster[j])
        elif min_dist == dist5:
Exemple #14
0
for i in range(0, len(point)):
    cluster += [[(x_coord[i], y_coord[i])]]
""" Making copies of the cluster list for the 3 variants """

slink_cluster = cluster
clink_cluster = cluster
meanlink_cluster = cluster
""" Mean-link Hierarchical clustering """

while len(meanlink_cluster) != 4:
    for i in range(0, len(meanlink_cluster)):
        for j in range(0, len(meanlink_cluster)):
            if meanlink_cluster[i] == meanlink_cluster[j]:
                continue
            else:
                tdist = HC.mean_link(meanlink_cluster[i], meanlink_cluster[j])
                if tdist <= mean:
                    mean = tdist
                    #print 'min at this point is :', min
                    c_cluster1 = meanlink_cluster[i]
                    c_cluster2 = meanlink_cluster[j]

    meanlink_cluster.remove(c_cluster1)
    meanlink_cluster.remove(c_cluster2)
    meanlink_cluster.append(c_cluster1 + c_cluster2)
    mean = 999

meanlink_cluster1 = meanlink_cluster[0]
meanlink_cluster2 = meanlink_cluster[1]
meanlink_cluster3 = meanlink_cluster[2]
meanlink_cluster4 = meanlink_cluster[3]
Exemple #15
0
        for his in cost_his:
            if len(his) > i:
                tmp.append(his[i])
        cost_his_t.append(tmp)

    cost_std = list(map(np.std, cost_his_t))
    cost_avg = list(map(np.average, cost_his_t))
    return np.array(cost_avg), np.array(cost_std)


# Problem (1): 8-queen problem (n = 8)
hc_cost = []
aver_cost, aver_runtime, num_succ = [], [], 0
for i in range(num_trails):
    prob = problem.N_QueenProblem(8)
    hc = HC.HC(prob, max_iter=30)
    hc.loop()
    sol, runtime, final_cost, cost_his = hc.result()
    aver_runtime.append(runtime)
    aver_cost.append(final_cost)
    if final_cost == 0:
        num_succ += 1
    del prob, hc
    hc_cost.append(cost_his)

print('Problem (1): 8-queen problem (n = 8)')
print('    Hill Climbing')
print('\tAverage Runtime :', np.average(aver_runtime))
print('\tAverage Number of Attacks :', np.average(aver_cost))
print('\tSuccess Rate :', num_succ / num_trails)
hc_avg, hc_std = statics(hc_cost)
""" Making copies of the cluster list for the 3 variants """

slink_cluster=cluster
clink_cluster=cluster
meanlink_cluster=cluster


""" Mean-link Hierarchical clustering """

while len(meanlink_cluster)!=4:
	for i in range(0,len(meanlink_cluster)):
		for j in range(0,len(meanlink_cluster)):
			if meanlink_cluster[i]==meanlink_cluster[j]:
				continue
			else:
				tdist=HC.mean_link(meanlink_cluster[i],meanlink_cluster[j])
				if tdist <= mean:
					mean=tdist
					#print 'min at this point is :', min
					c_cluster1=meanlink_cluster[i]
					c_cluster2=meanlink_cluster[j]
	
	meanlink_cluster.remove(c_cluster1)
	meanlink_cluster.remove(c_cluster2)
	meanlink_cluster.append(c_cluster1 + c_cluster2)
	mean=999


meanlink_cluster1=meanlink_cluster[0]
meanlink_cluster2=meanlink_cluster[1]
meanlink_cluster3=meanlink_cluster[2]
"""
Created on Thu Jan 16 23:33:44 2020

@author: Nicolai
----------------
"""

from __future__ import absolute_import
from __future__ import print_function

import sys
import pickle
import numpy as np

sys.path.append('../../Modules')
import HC

import Cross_3_3_day as c33

if __name__ == "__main__":

    # calculating
    print("start init random")
    x = np.array([90, 0, 5, 10, 15, 20, 25, 30, 35, 40])
    # print("read old pop")
    # with open('NSGA2_SaveList.pkl', 'rb') as pickle_file:
    #     backup = pickle.load(pickle_file)
    # x = backup[-1][0]
    print("finished init")
    opt4, fD4 = HC.hillClimbing(x, c33.function_Cross_3_3)
    for j in range(0, len(cluster)):
        dist1 = ((cluster[j][0] - c[0][0])**2 + (cluster[j][1] - c[0][1])**2 +
                 (cluster[j][2] - c[0][2])**2 + (cluster[j][3] - c[0][3])**2 +
                 (cluster[j][4] - c[0][4])**2) * 0.5
        dist2 = ((cluster[j][0] - c[1][0])**2 + (cluster[j][1] - c[1][1])**2 +
                 (cluster[j][2] - c[1][2])**2 + (cluster[j][3] - c[1][3])**2 +
                 (cluster[j][4] - c[1][4])**2) * 0.5
        dist3 = ((cluster[j][0] - c[2][0])**2 + (cluster[j][1] - c[2][1])**2 +
                 (cluster[j][2] - c[2][2])**2 + (cluster[j][3] - c[2][3])**2 +
                 (cluster[j][4] - c[2][4])**2) * 0.5
        dist4 = ((cluster[j][0] - c[3][0])**2 + (cluster[j][1] - c[3][1])**2 +
                 (cluster[j][2] - c[3][2])**2 + (cluster[j][3] - c[3][3])**2 +
                 (cluster[j][4] - c[3][4])**2) * 0.5

        min_dist = HC.median_min(dist1, dist2, dist3, dist4)

        #print 'Minimum is :', min_dist
        if min_dist == dist1:
            phi[j] = 0
            cluster1.append(cluster[j])
        elif min_dist == dist2:
            phi[j] = 1
            cluster2.append(cluster[j])
        elif min_dist == dist3:
            phi[j] = 2
            cluster3.append(cluster[j])
        elif min_dist == dist4:
            phi[j] = 3
            cluster4.append(cluster[j])
    cluster += [[(x_coord[i], y_coord[i])]]
""" Making copies of the cluster list for the 3 variants """

slink_cluster = cluster
clink_cluster = cluster
meanlink_cluster = cluster
""" Single-link Hierarchical clustering """

while len(slink_cluster) != 4:
    for i in range(0, len(slink_cluster)):
        print 'Len is now :', len(slink_cluster)
        for j in range(0, len(slink_cluster)):
            if slink_cluster[i] == slink_cluster[j]:
                continue
            else:
                tdist = HC.single_link(slink_cluster[i], slink_cluster[j])
                print 'tdist here is:', tdist
                if tdist <= min:
                    min = tdist
                    print 'min at this point is :', min
                    index = j
                    c_cluster1 = slink_cluster[i]
                    c_cluster2 = slink_cluster[j]
                    print ' Points are :', c_cluster1, 'and :', c_cluster2

    print 'Cluster 1 going to be removed is :', c_cluster1
    print 'Cluster 2 going to be removed is :', c_cluster2
    slink_cluster.remove(c_cluster1)
    slink_cluster.remove(c_cluster2)
    #slink_cluster.insert(index,c_cluster1 + c_cluster2)
    slink_cluster.append(c_cluster1 + c_cluster2)