-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGAv2.py
283 lines (224 loc) · 13.8 KB
/
GAv2.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# Genetic Algorithm script for clustering
# First Run: 6/29/11
# Last Update: 10/20/11
# Description:
# See Kapetanios Journal of Economic Dynamics & Control 30 (2006) 1389-1408 for theory and background.
# I've probably over commented this code, but if I don't, I won't remember what I did 6 months from now.
# Author:
# Taylor Smith
# University of Mississippi
# jtsmith2@gmail.com
from numpy import *
import random
import time
import os.path
#Defining global constants
population = 20 #Size of population, i.e., number of genetic strings per generation (MUST BE EVEN)
strlength = 3728 #Length of genetic string (number of units being clustered)
parameters = 13 #Number of variables in X (including constant term)
crossover_rate = 1.00 #Set from 0-1 on how often crossover occurs
#mutation_rate = 0.1 #Set form 0-1 on how often mutation occurs
mutation_rate = 1-((0.3)**(1.0/(1.0*strlength))) #From Dorsey code. Bases mutation rate on number of strlength being estimated
reinsert_gen = 15 #determines at which generation the best estimated string so far is reinserted into the population of strings
reinsert_step = 10 #The number of generations between inserting the "best" string so far back into the generation
maxgen = 5000 #Maximum number of generations per loop
#maxgen = strlength*(2000+(strlength-2)*250) #From Dorsey code
stop_delta = 1.0E-8 #If improvement between loops is less than this, program terminates
loops = 500 #Number of times the optimization will run
max_clusters = 100
min_clusters = 8
#Defining global variables
top_value=0 #Stores the highest value of the objective function found
top_string = [] #Stores the string that generated top_value
top_loop_value = 0 #Stores the highest value of the objective function found in a given loop
top_loop_string = [] #Stores the string that generated top_loop_value
dataX = []
dataY = []
save_location = 'D:\Documents\Working Papers\School Quality on Home Prices\Genetic-Algorithm---Clustering\\'
clusters = 0
#initalizing some values...
generationnum = 0
strnum = 0
top_value_start = 0
reinsert_gen2 = reinsert_gen
# Defining functions to use later
def main_program(): #Main program run by last line of this code.
global top_value
global top_string
global top_loop_value
global top_loop_string
global dataX
global dataY
global generationnum
global reinsert_gen2
global top_value_start
global clusters
print "Starting Algorithm"
print "Max Generations:", maxgen
print "Mutation Rate:", mutation_rate
print "Start Time:", time.strftime("%I:%M:%S")
dataX = genfromtxt('StataDataX.csv', delimiter=",")
dataY = genfromtxt('StataDataY.csv', delimiter=",")
gen = zeros((population, strlength)) #Create a 2-D array of size population by strlength. This will be the "generation".
for k in xrange(min_clusters, max_clusters):
clusters = k
print "---------------------------------------------------------------------"
print "Clusters:", k
#This loops the optimization a specified number of times so that we're not relient on the inital random assignment
for x in xrange(loops):
print "---------------------------------------------------"
print "Loop:", x
top_loop_value = 0
top_loop_string = []
reinsert_gen2 = reinsert_gen
#Randomly assign each element in gen a cluster number (Can this be done better than randomly?)
for i in xrange(population):
for j in xrange(strlength):
gen[i,j] = random.randrange(clusters)
start_value = objfunc(gen[0,:]) #Pass first string to get inital value of objective function
top_loop_value = start_value #save the value of the best string so far
top_loop_string = gen[0,:] #the best string so far (the only string so far)
print "Intial value of objective function:", start_value
if x == 0:
top_value = top_loop_value
top_string = top_loop_string
if os.path.isfile(save_location + 'TopString' + str(clusters) + '.npy'): #Loads best string if the file exists (in event of power loss)
gen[0] = load(save_location + 'TopString' + str(clusters) + '.npy')
top_value_start = top_value #Records what the best value was at the start of the loop.
genruntime = zeros(500) #Stores last 500 runtimes
#The genetic algorithm
for gener in xrange(maxgen):
t0 = time.clock() #Record start time for this iteration
generationnum = gener
if gener % 500 == 0: #Gives some summary stats every so often, also saves best string in case of power loss, etc
print "Generation:", gener, "| Time:", time.strftime("%I:%M:%S"), "| Top Value:", top_value, "| Avg Runtime:", genruntime.mean()
with open(save_location + 'Output' + str(clusters) + '.txt', 'a') as f:
f.write("Generation: " + str(gener) + " | Time: " + time.strftime("%I:%M:%S") + " | Top Value: " + str(top_value) + " | Avg Runtime: " + str(genruntime.mean()) + "\n")
if gener % 500 == 0: #could be set to happen less frequently if the above output happens very frequently.
savetxt(save_location + 'TopString' + str(clusters) + '.txt', top_string) #For easy input into excel
save(save_location + 'TopString' + str(clusters) + '.npy', top_string) #Easy to read back into program
prob = calc_fitness(gen) #generates an array of probabilies based on fitness level
newgen = draw_from_current(gen, prob) #draws new group from old using above calculated probabilities
newgen = crossover(newgen, gener) #crossover step where strings are paired and combined to form new strings
gen = mutate(newgen, gener) #mutate step to randomly assign some bits to new clusters, and if reinsert_gen, does that
genruntime[gener % 500] = time.clock() - t0 #Saves runtime of gen to array
print "Best value of objective function after cycle:", top_loop_value
#Stop condition
if abs(top_value_start - top_value) < stop_delta and x > 1: #If there's been no (or little) improvement in the loop, stop.
print "Stop condition met."
break;
print "Program Complete"
print "Optimal value of objective function", top_value
print "Optimal string:"
print top_string
def calc_fitness(generation): #Calculates fitness values and generates the draw probabilities from those values
global top_loop_value
global top_loop_string
global top_value
global top_string
global strnum
fitness = zeros((population))
for x in xrange(population):
strnum = x
fitness[x] = objfunc(generation[x]) #Calculate objective function for each string
if fitness[x] > top_loop_value: #Replace top value and string if necessary
top_loop_value = fitness[x]
top_loop_string = generation[x]
if top_loop_value > top_value:
top_value = top_loop_value
top_string = top_loop_string
fitness = fitness - fitness.min()
if fitness.sum() == 0:
fitsum = 1
print "All zero fitness"
else:
fitsum = fitness.sum()
result = fitness / float(fitsum) #Normalize the fitness values to probabilities that sum to 1
return result
def draw_from_current(generation, probability): #draws the pool for the new generation with replacement based on probabilities generated in calc_fitness above
warray = zeros((1000, strlength))
probability = probability*1000
#Creates a new array based on the probabilities figured. For example, say there are 4 strings: A,B,C,D with probabilities .5, .2, .2, .1 respectivily.
#We generate an array of the form [A,A,A,A,A,B,B,C,C,D] and then randomly generate an index 0-9 and assign the string at that index to the result.
#Now, the below code does the exact same thing, just with 1000 entries (more precision) instead of 10.
index = 0
for w in xrange(probability.shape[0]):
for x in xrange(int(probability[w])):
warray[index] = generation[w]
index += 1
result = zeros((population, strlength))
for i in xrange(population):
result[i] = warray[random.randint(0,warray.shape[0]-1)]
return result
def crossover(generation, gennum): #Performs the crossover step of the GA based on a set probability
for x in xrange(0,population,2):
if random.random() < crossover_rate:
cut = random.randint(1,strlength-2)
gen1 = generation.copy()
generation[x,cut:], generation[x+1,cut:] = gen1[x+1,cut:], gen1[x,cut:]
#Example:
# Gen[0] = [1,2,3,4,5]
# Gen[1] = [6,7,8,9,0]
# cut_index = 3
# *perform crossover*
# Gen[0] = [1,2,3,9,0]
# Gen[1] = [6,7,8,4,5]
return generation
def mutate(generation, gennum): #Randomly mutates individual string bits based on a set probability
global reinsert_gen
global reinsert_gen2
for i in xrange(population):
for j in xrange(strlength):
if random.random() < mutation_rate:
generation[i,j] = random.randrange(clusters)
if gennum == reinsert_gen2: #reseeds the best string so far back into the population
generation[random.randrange(population)] = top_string
reinsert_gen2 += reinsert_step
return generation
def objfunc(str): #Returns the value of the objective function for the given string
#Creates empty 2-D arrays for each cluster of the correct size (X and Y), and appends them to a list
clusterdataX = list([])
clusterdataY = list([])
#Sorts data by cluster
for x in xrange(clusters):
indicies = where(str==x) #list of indicies for cluster X
try:
clusterdataX.append(dataX[indicies]) #Uses advanced slicing to return data from X and Y only at indicies
clusterdataY.append(dataY[indicies])
except:
print "Element:", indicies
set_printoptions(threshold=5000)
print "String:", str
raise
#Performs the OLS regression on each cluster, and stores the SSR
ssr = zeros((clusters))
for i in xrange(clusters):
if clusterdataX[i].shape[0] > parameters: #basic check for full rank
clusterdataX[i] = varianceCheck(clusterdataX[i]) #gets rid of constant or colinear columns
ssr[i] = ols(clusterdataY[i], clusterdataX[i], i)
else:
ssr[i] = 0
#calculates the AIC based on the SSRs of the above regressions
return -log(ssr.sum()/strlength) - 2*clusters*parameters/strlength
def varianceCheck(x):
#Check to see if Age is all 0s and one other number (makes Age and Age^2 columns colinear)
uni = unique(x[:,1]) #finds unique values in column #2 (Age)
if uni.shape[0] == 2 and uni.min()==0: #if there's only 2 unique values and one of them is 0, X.T*X will be singular...
x = delete(x, 2, 1) # so we drop Age^2.
#Removes any columns where there is no variation (except for the first column of ones)
v = x.var(axis=0) #get variance of each column
indicies = where(v==0) #list where variance == 0
x = delete(x, indicies[0][1:], 1) #deletes those columns
return x
def ols(y,x,c=0):
try:
inv_xx = linalg.inv(dot(x.T,x))
except linalg.LinAlgError:
set_printoptions(threshold=5000)
print "Singular Matrix encountered", x, generationnum, strnum, c
return 0
xy = dot(x.T,y)
b = dot(inv_xx,xy)
e = y - dot(x,b)
return dot(e.T,e)
main_program() #runs the main program