-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpr_assignment2.py
More file actions
113 lines (99 loc) · 3.24 KB
/
Copy pathpr_assignment2.py
File metadata and controls
113 lines (99 loc) · 3.24 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import numpy as np
import copy
DNA_SIZE = 4 # DNA length
MUTATION_RATE = 0.2 # mutation probability
N_GENERATIONS = 20
DNA_final = []
final = 0.0
All_DNA = []
DNA1 = [str(i) for i in np.random.randint(0,2,4).tolist()]
DNA2 = [str(i) for i in np.random.randint(0,2,4).tolist()]
num = 0
# to find the maximum of this function
def F(x):
return 0.2*float(x[0]) + 0.3*float(x[1]) + 0.5*float(x[2]) + 0.1*float(x[3])
# to constraint the input
def constraint(pred):
if 0.5*float(pred[0]) + 1.0*float(pred[1]) + 1.5*float(pred[2]) + 0.1*float(pred[3]) <= 3.1 \
and 0.3*float(pred[0]) + 0.8*float(pred[1]) + 1.5*float(pred[2]) + 0.4*float(pred[3]) <= 2.5 \
and 0.2*float(pred[0]) + 0.2*float(pred[1]) + 0.3*float(pred[2]) + 0.1*float(pred[3]) <= 0.4:
return True
else:
return False
# select detect and add DNA
def select_detec_add_DNA(parent):
if constraint(parent) is True and d_recur(parent) is True:
All_DNA.append(parent)
else:
pass
# crossover the parents
def crossover(parent1,parent2):
child = parent1[0:1] + parent2[1:]
return child
# mutate the child
def mutate(child):
dna = copy.copy(child)
for point in range(DNA_SIZE):
if np.random.rand() < MUTATION_RATE:
child[point] = "1" if child[point] == "0" else "0"
if dna != child:
print dna, " mutation to ", child
return child
#detect recur
def d_recur(DNA):
if DNA not in All_DNA:
return True
else:
return False
# crossover, mutation and add to All list
def ge_al(ALL_DNA):
for i in range(0,len(All_DNA)-1):
print "Selected for crossover as parent1:", All_DNA[i]
for j in range(i + 1, len(All_DNA)):
print "Selected for crossover as parent2:", All_DNA[j]
child1 = crossover(All_DNA[i],All_DNA[j])
child2 = crossover(All_DNA[j],All_DNA[i])
child1 = mutate(child1)
child2 = mutate(child2)
select_detec_add_DNA(child1)
select_detec_add_DNA(child2)
# calculate return
def calculate(All_DNA):
global num
for k in All_DNA[num:len(All_DNA)]:
print "Each Population: ",k
result = F(k)
global final
print "calculate final:",final
num = num + 1
if result > final:
final = result
global DNA_final
DNA_final = k
print "Current Max return DNA:", k
print "Current Max return:", final
print "############### Finish one for loop ###############"
else:
final = final
# first two DNA
def ft(DNA):
if constraint(DNA) is True:
All_DNA.append(DNA)
else:
while constraint(DNA) is False:
DNA = [str(i) for i in np.random.randint(0, 2, 4).tolist()]
All_DNA.append(DNA)
ft(DNA1)
print "First DNA in ALL_DNA :",All_DNA
ft(DNA2)
print "Second DNA in ALL_DNA:",All_DNA
print "Mutation propability :",MUTATION_RATE
print "Population Size:", len(All_DNA)
print "###############"
for x in range(N_GENERATIONS):
ge_al(All_DNA)
calculate(All_DNA)
print "Population Size:",len(All_DNA)
print "################ Final Step ################"
print "Final Max DNA:", DNA_final
print "Max return:", final