From 9dd8ccf35a128044e555a7712d4c1ffaf7188d77 Mon Sep 17 00:00:00 2001 From: Remi Ehounou Date: Mon, 19 Apr 2021 09:38:40 -0400 Subject: [PATCH] urgency --- SourceCode/AlgorithmAnalyser.py | 70 +++++++++++++++++++++- SourceCode/TestFunctions/Bukin2.py | 10 ++++ SourceCode/TestFunctions/Keane.py | 8 +++ SourceCode/TestFunctions/Leon.py | 8 +++ SourceCode/TestFunctions/Matyas.py | 18 ++++++ SourceCode/TestFunctions/McCormick.py | 16 +++++ SourceCode/TestFunctions/Miele_Cantrell.py | 16 +++++ SourceCode/TestFunctions/placeholder | 1 - 8 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 SourceCode/TestFunctions/Keane.py create mode 100644 SourceCode/TestFunctions/Leon.py create mode 100644 SourceCode/TestFunctions/Matyas.py create mode 100644 SourceCode/TestFunctions/McCormick.py create mode 100644 SourceCode/TestFunctions/Miele_Cantrell.py delete mode 100644 SourceCode/TestFunctions/placeholder diff --git a/SourceCode/AlgorithmAnalyser.py b/SourceCode/AlgorithmAnalyser.py index cad6a85..f53b301 100644 --- a/SourceCode/AlgorithmAnalyser.py +++ b/SourceCode/AlgorithmAnalyser.py @@ -1,10 +1,11 @@ # directly running the DOE because existing surrogates can be explored with another workflow - +from os import path import importlib.util import multiprocessing import time - +import re from numpy import random as r +import shutil @@ -47,6 +48,67 @@ def measure(heuristicpath, heuristic_name, funcpath, funcname, objs, args, scale connection.send(response) +def writerepresentation(funcpath, charas): + # Save a backup copy of the function file + shutil.copyfile(funcpath, funcpath + '.old') + + # create a string format of the representation variables + representation = '' + for line in list(charas): + representation += '\n\t#_# ' + line + ': ' + str(charas[line]) + representation+='\n' + + # Creating the new docstring to be inserted into the file + with open(funcpath, "r") as file: + content = file.read() + docstrs = re.findall("def main\(.*?\):.*?'''(.*?)'''.*?return\s+.*?", content, re.DOTALL)[0] + docstrs += representation + repl = "\\1"+docstrs+"\t\\2" + + # Create the new content of the file to replace the old. Overwriting the whole thing + pattrn = re.compile("(def main\(.*?\):.*?''').*?('''.*?return\s+.*?\n|$)", flags=re.DOTALL) + newContent = pattrn.sub(repl, content, count=1) + # Overwrite the test function file + with open(funcpath,"w") as file: + file.write(newContent) + +def representfunc(funcpath): + #defining the function name + funcname = path.splitext(path.basename(funcpath))[0] + # loading the function to be represented + spec = importlib.util.spec_from_file_location(funcname, funcpath) + funcmodule = importlib.util.module_from_spec(spec) + spec.loader.exec_module(funcmodule) + + # Finding the function characteristics inside the docstring + if funcmodule.main.__doc__: + regex = re.compile("#_#\s?(\w+):\s?([-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)") + characs = re.findall(regex, funcmodule.main.__doc__) + results = {} + for charac in characs: + results[charac[0]] = float(charac[1]) + + # Automatically generate the representation if the docstrings did not return anything + if not results: + print("Calculating the Characteristics") + + # Modality + + # Basins + + # Valleys + + # Separability + + # Dimensionality + + + # Writing the calculated representation into the test function file + writerepresentation(funcpath, results) + + + return results + def doe(heuristicpath, heuristic_name, testfunctionpaths, funcnames, objs, args, scale): @@ -91,4 +153,6 @@ def doe(heuristicpath, heuristic_name, testfunctionpaths, funcnames, objs, args, for process in proc: print(process.name + "____\n" + str(responses[process.name]) + "\n_________________") -doe (heuristicpath, heuristic_name, testfunctionpaths, funcnames, objs, args, scale) \ No newline at end of file +#doe (heuristicpath, heuristic_name, testfunctionpaths, funcnames, objs, args, scale) + +representfunc("/home/remi/Documents/MDAF-GitLAB/SourceCode/TestFunctions/Bukin2.py") \ No newline at end of file diff --git a/SourceCode/TestFunctions/Bukin2.py b/SourceCode/TestFunctions/Bukin2.py index 2d34d43..d68c7b4 100644 --- a/SourceCode/TestFunctions/Bukin2.py +++ b/SourceCode/TestFunctions/Bukin2.py @@ -1,2 +1,12 @@ def main(args): + ''' + + :param args: list of floats + :return: float + + + + ''' return 100*(args[1]-0.01*args[0]**2+1)+0.01*(args[0]+10)**2 + + return 0 diff --git a/SourceCode/TestFunctions/Keane.py b/SourceCode/TestFunctions/Keane.py new file mode 100644 index 0000000..9b7b900 --- /dev/null +++ b/SourceCode/TestFunctions/Keane.py @@ -0,0 +1,8 @@ +#Import math library +import math + + +def main(args): + for x in args: + if(x<0 | x>10): return 0 + return (math.sin(args[0]-args[1])**2*math.sin(args[0]+args[1])**2)/(math.sqrt(args[0]**2+args[1]**2)) diff --git a/SourceCode/TestFunctions/Leon.py b/SourceCode/TestFunctions/Leon.py new file mode 100644 index 0000000..224a210 --- /dev/null +++ b/SourceCode/TestFunctions/Leon.py @@ -0,0 +1,8 @@ +#Import math library + + +def main(args): + for x in args: + if x < -1.2 or x > 1.2: + return 0 + return (100*(args[1]-args[0])**2)+(1-args[0])**2 diff --git a/SourceCode/TestFunctions/Matyas.py b/SourceCode/TestFunctions/Matyas.py new file mode 100644 index 0000000..dcb840e --- /dev/null +++ b/SourceCode/TestFunctions/Matyas.py @@ -0,0 +1,18 @@ + +def main(args): + """ + >>> main([0,1]) + 0.26 + + :param args: list of floats + :return: float + + """ + for x in args: + if x < -10 or x > 10: + return 0 + return (0.26*(args[0]**2+args[1]**2))-(0.48*args[0]*args[1]) + +if __name__ == "__main__": + import doctest + doctest.testmod() \ No newline at end of file diff --git a/SourceCode/TestFunctions/McCormick.py b/SourceCode/TestFunctions/McCormick.py new file mode 100644 index 0000000..b7d2df2 --- /dev/null +++ b/SourceCode/TestFunctions/McCormick.py @@ -0,0 +1,16 @@ +import math +def main(args): + """ + >>>main([-0.547, -1.547]) + 0 + + :param args: + :return: + """ + for args[0] in args: + if args[0] < -1.5 or args[0] > 4: + return 0 + if args[1] < -3 or args[1] > 3: + return 0 + return math.sin(args[0]+args[1])+(args[0]-args[1])**2-(3*args[0]/2)+(5*args[1/2])+1 + diff --git a/SourceCode/TestFunctions/Miele_Cantrell.py b/SourceCode/TestFunctions/Miele_Cantrell.py new file mode 100644 index 0000000..807baae --- /dev/null +++ b/SourceCode/TestFunctions/Miele_Cantrell.py @@ -0,0 +1,16 @@ +import math + + +def main(args): + """ + >>>main([0, 1, 1, 1]) + 0 + :param args: + :return: + """ + for x in args: + if x < -1 or x > 1: + return 0 + return (math.exp(-args[0])-args[1])**4+(100*(args[1]-args[2])**6)+(math.tan(args[2]-args[3]))**4+args[0]**8 + + diff --git a/SourceCode/TestFunctions/placeholder b/SourceCode/TestFunctions/placeholder deleted file mode 100644 index 8d1c8b6..0000000 --- a/SourceCode/TestFunctions/placeholder +++ /dev/null @@ -1 +0,0 @@ -