diff --git a/scripts/chunker_scripts/feature_selection/anneal.py b/scripts/chunker_scripts/feature_selection/anneal.py index bbdad209e0b1f09c39ee0e0066fe7ddc1e01620e..c9b9b99365df67b9d9f2794b94cfd0966e2d2bec 100644 --- a/scripts/chunker_scripts/feature_selection/anneal.py +++ b/scripts/chunker_scripts/feature_selection/anneal.py @@ -7,13 +7,15 @@ Created on 08-04-2013 ''' import random import os +import math class AnnealOptimalizer(): - def __init__(self, func, a_vector, args, T0, Tf=1e-12, maxiter=400, temp_reducing_rate=0.95, changing_features=2, opt="min", verbose=False): + def __init__(self, func, a_vector, args, seed, T0, Tf=1e-12, maxiter=400, temp_reducing_rate=0.95, changing_features=2, opt="min", verbose=False): self.func = func self.a_vector = a_vector self.args = args + self.out_dir = args['out_dir'] if T0 == None: raise IOError('You have to set some T0') self.T0 = T0 @@ -23,7 +25,9 @@ class AnnealOptimalizer(): self.changing_features = changing_features self.opt = opt self.verbose = verbose - + + random.seed( seed ) + def P(self, e, en, temp): print "e: ", e, " en: ", en if self.opt == "min": @@ -63,15 +67,17 @@ class AnnealOptimalizer(): iterations = 100 #ilosc iteracji symulacji sum = 0 results = {} #histogram wyników + for i in range(iterations): - + if 'out_dir' in self.args: + self.args['out_dir'] = os.path.join(self.out_dir, "tempest", str(i)) result = self.func(vector, self.args) sum += result if result not in results.keys(): results[result] = 1 else: results[result] += 1 - vector = neightbour(vector) + vector = self.neightbour(vector) avg = sum / float(iterations)#obliczenie średniego wyniku k = 0 deviation = 0 @@ -85,18 +91,16 @@ class AnnealOptimalizer(): def anneal(self): temperature = self.T0 i = 0 - out_dir = self.args['out_dir'] - if 'out_dir' in self.args: - self.args['out_dir'] = os.path.join(out_dir, str(i)) a_vector = self.a_vector + self.args['out_dir'] = os.path.join(self.out_dir, "selection", str(i)) a_value = self.func(a_vector, self.args) b_value = None i += 1 while temperature > self.Tf and i < self.maxiter: - if 'out_dir' in self.args.keys(): - self.args['out_dir'] = os.path.join(out_dir, i) + if 'out_dir' in self.args: + self.args['out_dir'] = os.path.join(self.out_dir, "selection", str(i)) b_vector = self.neightbour(a_vector) b_value = self.func(b_vector, self.args) p = self.P(a_value, b_value, temperature) @@ -113,10 +117,32 @@ class AnnealOptimalizer(): i += 1 return (a_vector, a_value) + + def calculate_temp(self): + sum = 0 + results = [] + for (path, dirs, files) in os.walk(os.path.join(self.out_dir, "tempest")): + for file in files: + if os.path.basename(file) == "result.csv": + f = open(os.path.join(path, file), 'r') + line = f.readline() + line = f.readline() + fmeasure = float(line.split(":")[4][:-1].strip()) + results.append(fmeasure) + sum += fmeasure + f.close() + avg = sum / float(len(results)) + k = 0 + deviation = 0 + for result in results: + deviation += result * ((k - avg) ** 2) + k += 1 + self.T0 = math.sqrt(deviation / len(results)) + return self.T0 def main(func, a_vector, args={}, T0=None, Tf=1e-12, maxiter=400, temp_reducing_rate=0.95, changing_features=2, opt="min"): ao = AnnealOptimalizer(func, a_vector, args, T0, Tf, maxiter, temp_reducing_rate, changing_features, opt) - print ao.anneal() + ao.anneal() if __name__ == '__main__': def func(v, args): @@ -130,4 +156,5 @@ if __name__ == '__main__': a = [False, False, False, False, False, False, False] - main(func, a, args={}, T0=0.1, Tf=1e-12, maxiter=200, temp_reducing_rate=0.95, changing_features=2, opt="max") \ No newline at end of file + main(func, a, args={}, T0=0.1, Tf=1e-12, maxiter=200, temp_reducing_rate=0.95, changing_features=2, opt="max") + \ No newline at end of file