-
Notifications
You must be signed in to change notification settings - Fork 1
/
my genetic algo.py
79 lines (60 loc) · 2.14 KB
/
my genetic algo.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
73
74
75
76
77
78
79
import numpy as np
f=[0]*int(input("Enter no. of variable including the d value : "))
for i in range(len(f)):
print("Enter value : ",end="")
f[i]=int(input())
print(f)
n=int(input("Enter no. of chromosome/population : "))
print("Objective Function is : ",n)
ch=np.random.randint(0,30,(n,len(f)-1))
print("chromosomes \n",ch,"\n")
epoch=int(input("Enter no. of epoch required : "))
i=1
while i<=epoch:
print("Epoch No. : ",i)
# Computing the Fitness objective
fit_obj=np.array([0.0]*ch.shape[0])
for j in range(len(f)-1):
fit_obj += f[j]*ch[:,j]
fit_obj -=f[-1]
print("Fitness object: ", fit_obj )
# Computing the Fitness
fit=1/(1+ fit_obj)
print("Fitness : ",fit)
# Calculating the probability
prob = fit / fit.sum()
print("Probability : ",prob)
# Rollutte
cum_prob = np.cumsum(prob)
print("Cumulative prob : ",cum_prob)
ran_num = np.random.random((ch.shape[0]))
print("Random no. for cal Rollutte : ",ran_num)
new_ch= np.zeros(ch.shape)
for j in range(len(cum_prob)):
for k in range(len(cum_prob)):
if cum_prob[k] > ran_num[j]:
new_ch[j,:] = ch[k,:]
break
print("new set of chromosomes \n", new_ch)
# Crossover
cr=np.random.random()
print("Cross over rate : ",cr)
cross_candid=[]
for j in range(ch.shape[0]):
if np.random.random() < cr:
cross_candid+=[j]
print("Candidate for crossover ",cross_candid)
ch=new_ch.copy()
for j in range(len(cross_candid)):
len_ch=np.random.randint(0,ch.shape[1])
ch[cross_candid[j],:]=list(new_ch[cross_candid[j],0:len_ch])+list(new_ch[cross_candid[(j+1)%len(cross_candid)],len_ch:])
print("After Crossover \n",ch)
# Mutation
for j in range(int(np.random.random()*ch.size)):
mut_no=np.random.randint(0,ch.size)
row=mut_no//ch.shape[1]
col=mut_no%ch.shape[1]
ch[row,col]=np.random.randint(0,ch.size)
print("After Mutation \n",ch)
i+=1
print("\n\n")