Skip to content
Snippets Groups Projects
Commit 9c2168ac authored by Adam Pawlaczek's avatar Adam Pawlaczek
Browse files

changed anneal.py

parent 686e653b
No related merge requests found
...@@ -7,13 +7,15 @@ Created on 08-04-2013 ...@@ -7,13 +7,15 @@ Created on 08-04-2013
''' '''
import random import random
import os import os
import math
class AnnealOptimalizer(): 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.func = func
self.a_vector = a_vector self.a_vector = a_vector
self.args = args self.args = args
self.out_dir = args['out_dir']
if T0 == None: if T0 == None:
raise IOError('You have to set some T0') raise IOError('You have to set some T0')
self.T0 = T0 self.T0 = T0
...@@ -23,7 +25,9 @@ class AnnealOptimalizer(): ...@@ -23,7 +25,9 @@ class AnnealOptimalizer():
self.changing_features = changing_features self.changing_features = changing_features
self.opt = opt self.opt = opt
self.verbose = verbose self.verbose = verbose
random.seed( seed )
def P(self, e, en, temp): def P(self, e, en, temp):
print "e: ", e, " en: ", en print "e: ", e, " en: ", en
if self.opt == "min": if self.opt == "min":
...@@ -63,15 +67,17 @@ class AnnealOptimalizer(): ...@@ -63,15 +67,17 @@ class AnnealOptimalizer():
iterations = 100 #ilosc iteracji symulacji iterations = 100 #ilosc iteracji symulacji
sum = 0 sum = 0
results = {} #histogram wyników results = {} #histogram wyników
for i in range(iterations): 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) result = self.func(vector, self.args)
sum += result sum += result
if result not in results.keys(): if result not in results.keys():
results[result] = 1 results[result] = 1
else: else:
results[result] += 1 results[result] += 1
vector = neightbour(vector) vector = self.neightbour(vector)
avg = sum / float(iterations)#obliczenie średniego wyniku avg = sum / float(iterations)#obliczenie średniego wyniku
k = 0 k = 0
deviation = 0 deviation = 0
...@@ -85,18 +91,16 @@ class AnnealOptimalizer(): ...@@ -85,18 +91,16 @@ class AnnealOptimalizer():
def anneal(self): def anneal(self):
temperature = self.T0 temperature = self.T0
i = 0 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 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) a_value = self.func(a_vector, self.args)
b_value = None b_value = None
i += 1 i += 1
while temperature > self.Tf and i < self.maxiter: while temperature > self.Tf and i < self.maxiter:
if 'out_dir' in self.args.keys(): if 'out_dir' in self.args:
self.args['out_dir'] = os.path.join(out_dir, i) self.args['out_dir'] = os.path.join(self.out_dir, "selection", str(i))
b_vector = self.neightbour(a_vector) b_vector = self.neightbour(a_vector)
b_value = self.func(b_vector, self.args) b_value = self.func(b_vector, self.args)
p = self.P(a_value, b_value, temperature) p = self.P(a_value, b_value, temperature)
...@@ -113,10 +117,32 @@ class AnnealOptimalizer(): ...@@ -113,10 +117,32 @@ class AnnealOptimalizer():
i += 1 i += 1
return (a_vector, a_value) 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"): 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) ao = AnnealOptimalizer(func, a_vector, args, T0, Tf, maxiter, temp_reducing_rate, changing_features, opt)
print ao.anneal() ao.anneal()
if __name__ == '__main__': if __name__ == '__main__':
def func(v, args): def func(v, args):
...@@ -130,4 +156,5 @@ if __name__ == '__main__': ...@@ -130,4 +156,5 @@ if __name__ == '__main__':
a = [False, False, False, False, False, False, False] 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") 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
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment