From d0451b3f36c85288b22ae6c04680fabe52cc48af Mon Sep 17 00:00:00 2001 From: remi Date: Sat, 15 May 2021 17:36:11 -0400 Subject: [PATCH] Right before pooling the randomized inner-sampling --- .gitignore | 3 + SourceCode/AlgorithmAnalyser.py | 68 ++++++++++++++---- .../SampleAlgorithms/SimmulatedAnnealing.py | 2 +- .../SimmulatedAnnealing.cpython-38.pyc | Bin 2062 -> 2055 bytes SourceCode/TestFunctions/Bukin6.py | 3 + SourceCode/TestFunctions/Bukin6.py.old | 3 + .../__pycache__/Bukin6.cpython-38.pyc | Bin 1374 -> 1417 bytes 7 files changed, 64 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index e3953c6..4d11aec 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,10 @@ *.log *.log.* *.sig +*.py.old +*.cpython-38.pyc pkg/ src/ snippets/ + diff --git a/SourceCode/AlgorithmAnalyser.py b/SourceCode/AlgorithmAnalyser.py index 3f61813..3154240 100644 --- a/SourceCode/AlgorithmAnalyser.py +++ b/SourceCode/AlgorithmAnalyser.py @@ -6,6 +6,7 @@ import time import re from numpy import random as r from numpy import * +import statistics import shutil @@ -19,6 +20,16 @@ import statistics as st from scipy import signal, misc, ndimage +class counter: + #wraps a function, to keep a running count of how many + #times it's been called + def __init__(self, func): + self.func = func + self.count = 0 + + def __call__(self, *args, **kwargs): + self.count += 1 + return self.func(*args, **kwargs) @@ -39,20 +50,49 @@ def measure(heuristicpath, heuristic_name, funcpath, funcname, objs, args, scale func = importlib.util.module_from_spec(testspec) testspec.loader.exec_module(func) - # Defining a random initial point to start testing the algorithms - initpoint = [r.random() * scale, r.random() * scale] + responses = array([0,0]) + convergence = empty(0) + best = empty(0) - #This timer calculates directly the CPU time of the process (Nanoseconds) - tic = time.process_time_ns() - # running the test by calling the heuritic script with the test function as argument - best = heuristic.main(func, objs, initpoint, args) - toc = time.process_time_ns() - # ^^ The timer ends right above this; the CPU time is then calculated below by simple difference ^^ + with pool(processes = 10) as pool: + + for run in range(30): + # defining a countable test function + @counter + def testfunc(args): + return func.main(args) + # Defining a random initial point to start testing the algorithms + initpoint = [r.random() * scale, r.random() * scale] - # Building the response - response = "The optimum point obtained is: " + str(best) + "\nThe CPU time of the process was: " + str((toc - tic)*(10**-9)) + " Seconds" + try: + #This timer calculates directly the CPU time of the process (Nanoseconds) + tic = time.process_time_ns() + # running the test by calling the heuritic script with the test function as argument + best = append(best, heuristic.main(testfunc, objs, initpoint, args)) + toc = time.process_time_ns() + # ^^ The timer ends right above this; the CPU time is then calculated below by simple difference ^^ + # CPU time in seconds + cpuTime = (toc - tic)*(10**-9) + numCalls = testfunc.count + converged = 1 + + except: + best = NaN + cpuTime = NaN + numCalls = testfunc.count + converged = 0 + + # Building the response + responses = vstack([responses, array([cpuTime,numCalls])]) + convergence = append(convergence,[converged]) + + responses = delete(responses,[0],axis=0) + results = dict() + results['stdevs'] = array([statistics.stdev(responses[:,[0]].flatten()), statistics.stdev(responses[:,[1]].flatten())]) + results['means'] = array([statistics.mean(responses[:,[0]].flatten()), statistics.mean(responses[:,[1]].flatten())]) + results['convrate'] = statistics.mean(convergence) - connection.send(response) + connection.send(results) def writerepresentation(funcpath, charas): # Save a backup copy of the function file @@ -229,7 +269,7 @@ def doe(heuristicpath, heuristic_name, testfunctionpaths, funcnames, objs, args, connections[run][1].close() # display output - print("\n\n||||| Responses |||||") + print("\n\n||||| Responses: [cpuTime,numCalls] |||||") for process in proc: print(process.name + "____\n" + str(responses[process.name]) + "\n_________________") if __name__ == '__main__': @@ -244,9 +284,9 @@ if __name__ == '__main__': args = {"high": 200, "low": -200, "t": 1000, "p": 0.95} scale = 1 - #doe (heuristicpath, heuristic_name, testfunctionpaths, funcnames, objs, args, scale) + doe (heuristicpath, heuristic_name, testfunctionpaths, funcnames, objs, args, scale) - representfunc("TestFunctions/Bukin6.py") + #representfunc("TestFunctions/Bukin6.py") # %% diff --git a/SourceCode/SampleAlgorithms/SimmulatedAnnealing.py b/SourceCode/SampleAlgorithms/SimmulatedAnnealing.py index 3492c16..8628680 100644 --- a/SourceCode/SampleAlgorithms/SimmulatedAnnealing.py +++ b/SourceCode/SampleAlgorithms/SimmulatedAnnealing.py @@ -23,7 +23,7 @@ def tweak(St,p,sigma,high,low): return St def Quality(Sc,objective,func): - func_output = func.main(Sc) + func_output = func(Sc) if type(func_output) == list: error = [func_output[i]-objective[i] for i in range(len(func_output))] else: diff --git a/SourceCode/SampleAlgorithms/__pycache__/SimmulatedAnnealing.cpython-38.pyc b/SourceCode/SampleAlgorithms/__pycache__/SimmulatedAnnealing.cpython-38.pyc index 8c9b131c5f1b2c91f508fcd6b0ceab5240bd47fe..3306afe2fb39b59925b21591c9f85bfc51e07a8d 100644 GIT binary patch delta 245 zcmeAZXcyoO<>lpK0D>PA<|m|Xf}~-TLCqMeh|xO@+Ec~0FIM8L;wH) delta 271 zcmZn{=o8=#<>lpK0D=!3^AoZ*@;+h=i2(9ym=-Y9Ff3&3WGG?mU}$D6VajGIGHGXM zXG~*CVN79a;V5A)VFAfBGr9oDTIM{74u%d!35Htc64n&v61E!VW=3(K3?oAdO9}gA zN2X)!9Jg3<6EpKBb1*0Ia53^Q$}w>;3NRKaY%XJd$;2o%Ie|?|RT^k{kr0Sb01~$t zb8j)G-eOD3FUn0UDPjT&X$lm{O`gxD#;7>?7@GlWkpxg|@*lP+4G=?95M)J3Mv)ds nGizRHZb9WOrn~~M&LR$=V3FG7I(AzDbr2J7O_9Xp)9f|?>Rmq& diff --git a/SourceCode/TestFunctions/Bukin6.py b/SourceCode/TestFunctions/Bukin6.py index c99cc2b..e01548a 100644 --- a/SourceCode/TestFunctions/Bukin6.py +++ b/SourceCode/TestFunctions/Bukin6.py @@ -83,6 +83,9 @@ def main(args): #_# dimmensions: 2.0 #_# Valleys: True + #_# dimmensions: 2.0 + #_# Valleys: True + #_# dimmensions: 2.0 #_# Valleys: True ''' diff --git a/SourceCode/TestFunctions/Bukin6.py.old b/SourceCode/TestFunctions/Bukin6.py.old index 741ad14..c99cc2b 100644 --- a/SourceCode/TestFunctions/Bukin6.py.old +++ b/SourceCode/TestFunctions/Bukin6.py.old @@ -80,6 +80,9 @@ def main(args): #_# dimmensions: 2.0 #_# Valleys: True + #_# dimmensions: 2.0 + #_# Valleys: True + #_# dimmensions: 2.0 #_# Valleys: True ''' diff --git a/SourceCode/TestFunctions/__pycache__/Bukin6.cpython-38.pyc b/SourceCode/TestFunctions/__pycache__/Bukin6.cpython-38.pyc index 66e72d17ed043e1732a8d62f6ba41a57741d1cae..3311c1ac5a5f9137379a8c88e148d573d5013281 100644 GIT binary patch delta 38 scmcb|)yd5p%FD~e00fol^AlE1hRkCOfmb0RYeH3+?~_