This commit is contained in:
Remi Ehounou
2021-04-19 09:38:40 -04:00
parent bff340453f
commit 9dd8ccf35a
8 changed files with 143 additions and 4 deletions

View File

@@ -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)
#doe (heuristicpath, heuristic_name, testfunctionpaths, funcnames, objs, args, scale)
representfunc("/home/remi/Documents/MDAF-GitLAB/SourceCode/TestFunctions/Bukin2.py")