-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_es.py
259 lines (227 loc) · 9.92 KB
/
simple_es.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
import random,copy,os,pickle
from deap import base
from deap import creator
from deap import tools
from deap import benchmarks
from rbm import RBM
from optimizers import sgd_optimizer
import numpy as np
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pylab as plt
import theano
from custom_dataset import SequenceDataset
import pdb
def ensure_dir(f):
d = os.path.dirname(f)
if not os.path.exists(d):
os.makedirs(d)
class KnapsackData(object):
"""docstring for Knapsack"""
def __init__(self, name, knapsacks, items, values, capacities, constraints):
super(KnapsackData, self).__init__()
self.name = name
self.knapsacks = knapsacks
self.items = items
self.values = values
self.capacities = capacities
self.constraints = constraints
class Genotypes(object):
def __init__(self, min=True):
super(Genotypes, self).__init__()
self.genotype_array = []
self.keys = {}
self.min = min
self.window = 100
def add_genotypes(self,genotypes,uniques = False):
if len(self.genotype_array) >= self.window:
removing = self.genotype_array.pop()
if uniques:
hashed_g = str(self.genotype_array)
for g in removing:
hashed_genotype = str(g)
if hashed_genotype not in hashed_g:
del self.keys[hashed_genotype]
unique_genotypes = []
for g in genotypes:
if uniques:
if str(g.genotype) not in self.keys:
unique_genotypes.append(g)
self.keys[str(g.genotype)] = 1
else:
unique_genotypes.append(g)
self.genotype_array.insert(0,unique_genotypes)
def flatten(self):
return [item for sublist in self.genotype_array for item in sublist]
def top_x_percent(self,x=0.2):
s = self.flatten()
s = sorted(s,key=lambda member:member.fitness)
if self.min == False:
s.reverse()
s = [_.genotype for _ in s]
self.top_x = s[0:int(len(s)*x)]
return self.top_x
def get_and_save_top_x(self,x=0.2,path="experiments",experiment=0,generation=0):
top_x = self.top_x_percent(x)
# self.top_x_genotypes_to_file(top_x,path=path,experiment=experiment,generation=generation)
def top_x_genotypes_to_file(self,top_20,path="experiments",experiment=0,generation=0):
np.savetxt("{0}_{1}/top_genotypes_{2}.dat".format(path,experiment,generation),top_20)
np.savetxt("{0}_{1}/top_genotypes_fitnesses_{2}.dat".format(path,experiment,generation),[f.fitness.values[0] for f in top_20])
class Individual(object):
def __init__(self):
super(Individual, self).__init__()
self.genotype = None
self.fitness = None
self.normalised_fitness = 0
class ES(object):
def __init__(self,knapsack_file="weing1.pkl"):
super(ES, self).__init__()
# GA stuff
self.generations = 100
self.knapsack = pickle.load(open(knapsack_file))
print "k:",self.knapsack
self.N = int(self.knapsack.items)
# RMB stuff
self.RBM = RBM(n_visible=self.N,n_hidden=50)
self.sample_RBM()
# Stats stuff
self.population_snapshots = []
self.genotypes_history = Genotypes(min=False)
def create_individual(self,N):
I = Individual()
I.genotype = [random.choice([0,1]) for i in range(N)]
I.fitness = 0
return I
def fitness_function(self,individual,knapsack=None):
weights = []
for i,c in enumerate(knapsack.capacities):
weights.append(np.sum(np.array(knapsack.constraints[i])*individual.genotype))
over = 0
for i,w in enumerate(weights):
if w > knapsack.capacities[i]:
over += (w - knapsack.capacities[i])
if over > 0:
return -over
else:
return np.sum(np.array(knapsack.values)*individual.genotype)
def evaluate_population(self,population,params=None):
for p in population:
p.fitness = self.fitness_function(p,params)
def normalise_fitnesses(self,population):
max_fitness = np.max([p.fitness for p in population])
min_fitness = np.min([p.fitness for p in population])
for p in population:
p.normalised_fitness = (p.fitness + min_fitness)/(min_fitness+max_fitness)
def offspring_from_sample(self,individual_to_copy):
individual = copy.deepcopy(individual_to_copy)
individual_genome = np.array(individual.genotype).reshape(1,-1)
output = self.sample_from_RBM(np.array(individual_genome))
# print "output:",output
individual.genotype[:] = output[0][:]
return individual
def train_RBM(self,k=20,lr=0.1):
train_data = self.genotypes_history.top_x_percent()
train_set = SequenceDataset(train_data,batch_size=20,number_batches=None)
inputs,params,cost,monitor,updates,consider_constant = self.RBM.build_RBM(k=k)
sgd_optimizer(params,[inputs],cost,train_set,updates_old=updates,monitor=monitor,
consider_constant=[consider_constant],lr=0.1,num_epochs=10)
def sample_RBM(self,k=20):
v,v_sample,updates = self.RBM.sample_RBM(k=k)
self.sample_from_RBM = theano.function([v],v_sample,updates=updates)
def run_1_plus_1(self, path= "", experiment = 0):
random.seed(random.uniform(0,1000000))
print("Start of evolution")
parent = self.create_individual(self.N)
# Evaluate the parent
parent.fitness = self.fitness_function(parent,self.knapsack)
self.genotypes_history.add_genotypes([parent])
self.genotypes_history.get_and_save_top_x(1.0)
self.train_RBM()
# Begin the evolution
for g in range(self.generations):
print("-- Generation %i --" % (g + 1))
offspring = self.offspring_from_sample(parent)
offspring.fitness = self.fitness_function(parent,self.knapsack)
print "parent_fitness:",parent.fitness
print "offspring_fitness:",offspring.fitness
if offspring.fitness > parent.fitness:
print "offspring replacing parent"
parent = offspring
self.genotypes_history.add_genotypes([offspring])
self.genotypes_history.get_and_save_top_x(1.0)
self.train_RBM()
print("-- End of (successful) evolution --")
return parent
def run_mu_plus_lambda(self, path= "", experiment = 0):
population_size = 50
random.seed(random.uniform(0,1000000))
print("Start of evolution")
population = [self.create_individual(self.N) for i in range(population_size)]
# Evaluate the population
self.evaluate_population(population,self.knapsack)
self.genotypes_history.add_genotypes(population)
self.genotypes_history.get_and_save_top_x(1.0)
self.train_RBM()
# Begin the evolution
for g in range(self.generations):
print("-- Generation %i --" % (g + 1))
offspring = []
for ind in population:
offspring.append(self.offspring_from_sample(ind))
self.evaluate_population(offspring,self.knapsack)
self.genotypes_history.add_genotypes(offspring)
self.genotypes_history.get_and_save_top_x(1.0)
self.train_RBM()
new_population = []
population = population + offspring
while len(new_population) < population_size:
# tournament selection on combined population
a = int(len(population) * random.random())
b = int(len(population) * random.random())
while a == b:
b = int(len(population) * random.random())
if population[a].fitness > population[b].fitness:
new_population.append(population.pop(a))
else:
new_population.append(population.pop(b))
population = new_population
print "average fitness:",np.mean([p.fitness for p in population])
print "max fitness:",np.max([p.fitness for p in population])
print "min fitness:",np.min([p.fitness for p in population])
print("-- End of (successful) evolution --")
return parent
class RandomSearch(ES):
"""docstring for RandomSearch"""
def __init__(self,knapsack_file="weing1.pkl"):
super(RandomSearch, self).__init__(knapsack_file="weing1.pkl")
self.solutions = []
print knapsack_file
def run(self,experiment_name,trials):
mean = []
min = []
max = []
for i in range(trials):
individual = self.create_individual(self.N)
individual.fitness = self.fitness_function(individual,self.knapsack)
self.solutions.append(individual)
if i% 100 == 0:
all_fitnesses = [s.fitness for s in self.solutions]
mean.append(np.mean(all_fitnesses))
max.append(np.max(all_fitnesses))
min.append(np.min(all_fitnesses))
self.solutions = sorted(self.solutions,key = lambda s:s.fitness)
self.solutions.reverse()
all_fitnesses = [s.fitness for s in self.solutions]
print "max", np.max(all_fitnesses)
print "min", np.min(all_fitnesses)
print "mean", np.mean(all_fitnesses)
ensure_dir("results/random/hard_knapsack/")
np.savetxt("results/random/hard_knapsack/means_{0}".format(experiment_name),mean)
np.savetxt("results/random/hard_knapsack/max_{0}".format(experiment_name),max)
np.savetxt("results/random/hard_knapsack/min_{0}".format(experiment_name),min)
if __name__ == "__main__":
# e = ES(knapsack_file="weing1.pkl")
# e.run_mu_plus_lambda()
for i in range(0,10):
r = RandomSearch(knapsack_file="weing8.pkl")
r.run(i,200000)