-
Notifications
You must be signed in to change notification settings - Fork 0
/
Genetic_algorithm.py
79 lines (56 loc) · 2.03 KB
/
Genetic_algorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import sys
import pandas as pd
import random
from deap import base
from deap import creator
from deap import tools
from deap import algorithms
def compute_HammingDistance(X):
return (X[:, None, :] != X).sum(2)
dm_list = list()
for gene_id,gene in gene_dict.items():
snp = list()
for sample in sample_list:
snp.append(gene_dict[sample]['genotype_vector'])
dm = compute_HammingDistance(np.array(snp))
dm_list.append([dm,gene_id])
dm_dict = dict()
index = 0
for i in dm_list:
dm_dict[index] = i
index += 1
cluster = [1,3,4,2,4,1,2,3,1,2,1,3,2,4,
1,2,3,2,3,3,1,3,3,3,3,2,4,3,
2,1,1,2,2,3,1,2,3,4,4,2,3,3,
3,3,1,4,1,4,3,2,4,2,3]
IND_SIZE = len(dm_list)
pop_size = 1500
mutrate = 0.05
cxpb = 0.7
mutpb = 0.07
ngen = 1000
hof = tools.HallOfFame(10)
k_sel = 1500
tournsize = int(0.20*pop_size)
matrix_p = np.mean(np.array([value[0] for key,value in dm_dict.items()]),axis=0)
silhouette_p = metrics.silhouette_score(matrix_p,cluster,metric="precomputed")
def evaluate(individual):
matrix = np.mean(np.array([value[0] for key,value in dm_dict.items() if individual[key] == 1]),axis=0)
silhouette = metrics.silhouette_score(matrix,cluster,metric="precomputed")
return silhouette,
creator.create("FitnessMax",base.Fitness,weights=(1.0,))
creator.create("Individual",list,fitness=creator.FitnessMax)
toolbox = base.Toolbox()
toolbox.register("attr_int",random.randint,a=0,b=1)
toolbox.register("individual", tools.initRepeat,creator.Individual,toolbox.attr_int,n=IND_SIZE)
toolbox.register("population",tools.initRepeat,list,toolbox.individual)
toolbox.register("mate",tools.cxTwoPoint)
toolbox.register("mutate",tools.mutUniformInt,low=0,up=1,indpb=mutrate)
toolbox.register("select",tools.selTournament,tournsize=tournsize,fit_attr='fitness')
toolbox.register("evaluate",evaluate)
pop = toolbox.population(pop_size)
algorithms.eaSimple(population=pop,toolbox=toolbox,
cxpb=cxpb,mutpb=mutpb,ngen=ngen,
halloffame=hof,verbose=True)
hof.keys