-
Notifications
You must be signed in to change notification settings - Fork 0
/
GA1.py
172 lines (128 loc) · 5.07 KB
/
GA1.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/python
#Advances in Artificial Intelligence - Coursework 1
#first import the required python packages, mostly from numpy I think
import numpy #can be used with matlab?
import numpy.random
import random
import array
import sys
import pprint as pp
numpy.set_printoptions(threshold=sys.maxint) #numpy likes to print large arrays wierd, supress this
#+++++++++++++++++++++File input args+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=
bf = open('./data/bestfit.txt','a') #opens bestfit file, arg 'a' opens the file for appending data
mf = open('./data/meanfit.txt','a')
#specify the population size and the genome size - do this later with raw_input
pop = 10 #population
gen = 10 #number of bits or genome size
fitval = []
totfit = 0 #total fitness used in roulette wheel selection
meanfit = 0
a = 0
b= 0
randparent = []
randind = 0
randfit = 0
##################################################################
def pop_array(rows, cols):
matrix = []
for row in range(rows):
array = []
for col in range(cols):
array.append(random.randint(0,1))
matrix.append(array)
return matrix
#################################################################
def write_bestfit(topfit):
#write best fitness of current population to file
bfval = str(topfit)
bf.write(bfval)
bf.write('\n')
#################################################################
#################################################################
def write_meanfit(fitnessval):
#write mean fitness of current population to file
meanfit = sum(fitnessval, 0.0) / len(fitnessval)
mfval = str(meanfit)
mf.write(mfval)
mf.write('\n')
################################################################
#####################################################################################################################
def roulette(totfit):
randfit = random.randint(0 , totfit) #choose a random fitness between 0 and the fitness of the population
randparent = []
randind = 0
resum = 0
for row in a:
randind = randind + 1 # find the index of the element where you stop
resum = resum + row.count(1)
if resum > randfit:
#print "randfit reached"
randparent = row
break
#print "randfit, randparent and totfit are in function = ", randfit, randparent, totfit
print "the current random parent ", randparent
return randparent, randfit, randind
print "fitness of all genes ",fitval
print "ENTERED"
#####################################################################################################################
a = pop_array(pop,gen)# function call to create an array and populate it
b = numpy.array(a) #convert the python list array to a numpy array - probably just for printing
print b.sum() # total fitness of the population
print "Initial Population created"
#print a
pp.pprint(a)
#print a[0],[0]
#print a[1],[1]
#j = a[0].count(1)
#print j
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# FITNESS CALCULATIONS
for row in a:
fitval.append(row.count(1))
totfit = sum(fitval) #find the total fitness of the population
topfit = max(fitval) #find the highest fitness in the fitness list
write_bestfit(topfit) #write the highest fitness found to file
write_meanfit(fitval) #write the mean fitness of the current population to a file
topfitx = fitval.count(topfit) #take the highest fitness value and count the number of times it appears in the list
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# ROULETTE WHEEL SELECTION
print "calling roulette wheel selection"
#now need to create a new array of new parents
newarr = []
#########################Populate a new array containing parents from roulette wheel selection##################################
for row in range(pop):
r = roulette(totfit)
if r == 0:
print "wahhhh roulette came up with a 0"
randparent = r[0]
randfit = r[1]
randind = r[2]
newarr.append(randparent)
#print "im in row ", row
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
print "the total fitness of the population is = ",totfit
print "the highest fitness of ", topfit, " Occurs ", topfitx, " times"
print "-----------------------------------------------------------------"
print"- Roulette Wheel Selection Stuff -"
print "- -"
print "- Total fitness of the population = ", totfit, " -"
print "- -"
print "- A random number between 0 and total fitness = ", randfit, " -"
print "- -"
print "- -"
print "-----------------------------------------------------------------"
print "the random parents are", randparent
print "the index of that parent is ", randind
print "the new array after roulette wheel selection "
pp.pprint(newarr)
#End of Line
bf.close() #close these files
mf.close()
#print b[49,:] how to print one line of an array
#sum the indexed array element
#for row in b:
# for col in b:
# print b[row,col]
# u = u + b[row,col]
# print "test total fitness for b", u
# u = 0