added automated tests rmved unused imports

This commit is contained in:
Remi Ehounou
2021-05-23 22:28:57 -04:00
parent 4d9bf0e472
commit c7ae2e18fb
9 changed files with 195 additions and 16 deletions

12
.vscode/settings.json vendored
View File

@ -1,9 +1,17 @@
{ {
"python.pythonPath": "/usr/sbin/python", "python.pythonPath": "/usr/sbin/python",
"python.testing.pytestArgs": [ "python.testing.pytestArgs": [
"Sample codes" "tests"
], ],
"python.testing.unittestEnabled": false, "python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false, "python.testing.nosetestsEnabled": false,
"python.testing.pytestEnabled": true "python.testing.pytestEnabled": true,
"python.testing.unittestArgs": [
"-v",
"-s",
"./tests",
"-p",
"test_*.py"
],
"python.testing.cwd": "tests"
} }

View File

@ -5,16 +5,13 @@ import importlib.util
import multiprocessing import multiprocessing
import time import time
import re import re
from numpy import random as r from numpy import random as rand
from numpy import * from numpy import array, isnan, NaN, asarray
import statistics import statistics
from functools import partial from functools import partial
import shutil import shutil
# Surrogate modelling # Surrogate modelling
import itertools
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Test function representation # Test function representation
from rpy2 import robjects as robjs from rpy2 import robjects as robjs
@ -23,7 +20,6 @@ from rpy2 import rinterface
# Test function characteristics # Test function characteristics
import statistics as st import statistics as st
from scipy import signal, misc, ndimage
def installFalcoo(mirror = 'https://utstat.toronto.edu/cran/'): def installFalcoo(mirror = 'https://utstat.toronto.edu/cran/'):
@ -90,7 +86,7 @@ def measure(heuristicpath, funcpath, args, connection):
funcname = path.splitext(path.basename(funcpath))[0] funcname = path.splitext(path.basename(funcpath))[0]
# Seeding the random module for generating the initial point of the optimizer: Utilising random starting point for experimental validity # Seeding the random module for generating the initial point of the optimizer: Utilising random starting point for experimental validity
r.seed(int(time.time())) rand.seed(int(time.time()))
# guetting the representation of the function # guetting the representation of the function
funcChars = representfunc(funcpath) funcChars = representfunc(funcpath)
@ -109,7 +105,7 @@ def measure(heuristicpath, funcpath, args, connection):
# Defining random initial points to start testing the algorithms # Defining random initial points to start testing the algorithms
initpoints = [[r.random() * scale[i] + lower[i] for i in range(n)] for run in range(30)] #update the inner as [r.random() * scale for i in range(testfuncDimmensions)] initpoints = [[rand.random() * scale[i] + lower[i] for i in range(n)] for run in range(30)] #update the inner as [rand.random() * scale for i in range(testfuncDimmensions)]
# building the iterable arguments # building the iterable arguments
partfunc = partial(simulate, heuristic_name, heuristicpath, funcname, funcpath, args) partfunc = partial(simulate, heuristic_name, heuristicpath, funcname, funcpath, args)
@ -149,12 +145,12 @@ def writerepresentation(funcpath, charas):
# Creating the new docstring to be inserted into the file # Creating the new docstring to be inserted into the file
with open(funcpath, "r") as file: with open(funcpath, "r") as file:
content = file.read() content = file.read()
docstrs = re.findall("def main\(.*?\):.*?'''(.*?)'''.*?return\s+.*?", content, re.DOTALL)[0] docstrs = re.findall(r"def main\(.*?\):.*?'''(.*?)'''.*?return\s+.*?", content, re.DOTALL)[0]
docstrs += representation docstrs += representation
repl = "\\1"+docstrs+"\t\\2" repl = "\\1"+docstrs+"\t\\2"
# Create the new content of the file to replace the old. Replacing the whole thing # Create the new content of the file to replace the old. Replacing the whole thing
pattrn = re.compile("(def main\(.*?\):.*?''').*?('''.*?return\s+.*?\n|$)", flags=re.DOTALL) pattrn = re.compile(r"(def main\(.*?\):.*?''').*?('''.*?return\s+.*?\n|$)", flags=re.DOTALL)
newContent = pattrn.sub(repl, content, count=1) newContent = pattrn.sub(repl, content, count=1)
# Overwrite the test function file # Overwrite the test function file
with open(funcpath,"w") as file: with open(funcpath,"w") as file:
@ -170,7 +166,7 @@ def representfunc(funcpath, forced = False):
# Finding the function characteristics inside the docstring # Finding the function characteristics inside the docstring
if funcmodule.main.__doc__: if funcmodule.main.__doc__:
regex = re.compile("#_#\s?(\w+):(.+)?\n") # this regular expression matches the characteristics already specified in the docstring section of the function -- old exp: "#_#\s?(\w+):\s?([-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)" regex = re.compile(r"#_#\s?(\w+):(.+)?\n") # this regular expression matches the characteristics already specified in the docstring section of the function -- old exp: "#_#\s?(\w+):\s?([-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)"
characs = re.findall(regex, funcmodule.main.__doc__) characs = re.findall(regex, funcmodule.main.__doc__)
results = {} results = {}
for charac in characs: for charac in characs:
@ -226,7 +222,7 @@ def doe(heuristicpath, testfunctionpaths, args):
funcnames = [path.splitext(path.basename(funcpath))[0] for funcpath in testfunctionpaths] funcnames = [path.splitext(path.basename(funcpath))[0] for funcpath in testfunctionpaths]
#defining the heuristic's name #defining the heuristic's name
heuristic_name = path.splitext(path.basename(heuristicpath))[0] #heuristic_name = path.splitext(path.basename(heuristicpath))[0]
# logic variables to deal with the processes # logic variables to deal with the processes
proc = [] proc = []
@ -242,7 +238,6 @@ def doe(heuristicpath, testfunctionpaths, args):
# defining the response variables # defining the response variables
responses = {} responses = {}
failedfunctions = {} failedfunctions = {}
processtiming = {}
# Starting the subprocesses for each testfunction # Starting the subprocesses for each testfunction
for idx,process in enumerate(proc): for idx,process in enumerate(proc):

29
tests/Bukin2.py Normal file
View File

@ -0,0 +1,29 @@
def main(args):
'''
#_# dimmensions: 2
#_# upper: [-5, 3]
#_# lower: [-15, -3]
#_# minimum: [-10,0]
#_# cm_angle: array([[6.58088621e-01], [1.35233171e-01], [6.54044152e-01], [1.52081329e-01], [1.45731844e+02], [2.98120983e+01], [1.28649809e-01], [1.73025064e-02], [0.00000000e+00], [6.40000000e-02]])
#_# cm_conv: array([[0.09615385], [0.01923077], [0.42307692], [0.57692308], [0. ], [0.018 ]])
#_# cm_grad: array([[0.82437858], [0.05456595], [0. ], [0.043 ]])
#_# ela_conv: array([[0.00000000e+00], [0.00000000e+00], [2.80866245e+00], [2.80866245e+00], [1.00000000e+03], [7.40000000e-02]])
#_# ela_curv: array([[1.00518296e+02], [1.01095063e+02], [1.02141249e+02], [1.02064384e+02], [1.03133940e+02], [1.04368051e+02], [1.13463384e+00], [0.00000000e+00], [3.34695311e+00], [3.96371074e+00], [5.47417125e+00], [4.89639549e+00], [6.73878539e+00], [9.80920690e+00], [1.74329715e+00], [0.00000000e+00], [1.89555172e+00], [1.40375656e+04], [2.60278402e+31], [4.38343422e+04], [3.62929437e+07], [4.82936543e+33], [3.42288570e+32], [0.00000000e+00], [8.40000000e+03], [6.10000000e-01]])
#_# ela_distr: array([[-0.01486767], [-0.99717244], [ 2. ], [ 0. ], [ 0.031 ]])
#_# ela_local: array([[1.000e+00], [1.000e-02], [1.000e+00], [ nan], [1.000e+00], [0.000e+00], [1.000e+00], [1.000e+01], [1.000e+01], [1.000e+01], [1.000e+01], [1.000e+01], [1.000e+01], [0.000e+00], [1.001e+03], [4.800e-02]])
#_# ela_meta: array([[9.98374606e-01], [1.91755160e+02], [2.00004195e+01], [9.99759646e+01], [4.99869339e+00], [9.98371583e-01], [1.00000000e+00], [2.65966190e+14], [1.00000000e+00], [0.00000000e+00], [1.20000000e-02]])
#_# basic: array([[ 2.00000000e+00], [ 5.00000000e+02], [-1.50000000e+01], [-3.00000000e+00], [-5.00000000e+00], [ 3.00000000e+00], [-4.10842167e+02], [ 3.64212195e+02], [ 6.00000000e+00], [ 6.00000000e+00], [ 3.60000000e+01], [ 3.60000000e+01], [ 1.00000000e+00], [ 0.00000000e+00], [ 1.00000000e-03]])
#_# disp: array([[ 0.22440524], [ 0.31346911], [ 0.48861062], [ 0.76077562], [ 0.21100989], [ 0.28446768], [ 0.4354829 ], [ 0.66971954], [-3.29491221], [-2.91654754], [-2.17250449], [-1.01628242], [-3.19141264], [-2.89428076], [-2.2834342 ], [-1.33596253], [ 0. ], [ 0.015 ]])
#_# limo: array([[ 1.01966250e+02], [ 9.98531772e-01], [ 1.02116335e+02], [ 1.14726687e+00], [ 7.02953460e-02], [-9.92725363e-01], [ 5.46554366e+00], [ 1.72203656e+00], [ 4.37094029e+01], [ 4.99827778e+00], [ 2.92925669e+00], [ 3.23238656e-02], [ 0.00000000e+00], [ 4.50000000e-02]])
#_# nbc: array([[ 0.48943943], [ 0.87742233], [ 0.49655916], [ 0.15423538], [-0.18276363], [ 0. ], [ 0.035 ]])
#_# pca: array([[1. ], [1. ], [0.33333333], [0.66666667], [0.73531617], [0.50773903], [0.9997706 ], [0.66959168], [0. ], [0.003 ]])
#_# gcm: array([[1. ], [0.02777778], [0.97222222], [0. ], [1. ], [1. ], [1. ], [1. ], [ nan], [1. ], [1. ], [1. ], [1. ], [ nan], [1. ], [1. ], [1. ], [1. ], [1. ], [ nan], [1. ], [1. ], [0.02777778], [0. ], [0.03 ], [1. ], [0.02777778], [0.97222222], [0. ], [1. ], [1. ], [1. ], [1. ], [ nan], [1. ], [1. ], [1. ], [1. ], [ nan], [1. ], [1. ], [1. ], [1. ], [1. ], [ nan], [1. ], [1. ], [0.02777778], [0. ], [0.039 ], [1. ], [0.02777778], [0.97222222], [0. ], [1. ], [1. ], [1. ], [1. ], [ nan], [1. ], [1. ], [1. ], [1. ], [ nan], [1. ], [1. ], [1. ], [1. ], [1. ], [ nan], [1. ], [1. ], [0.02777778], [0. ], [0.035 ]])
#_# ic: array([[ 0.69451655], [ 2.02702703], [77.07027114], [ 1.94694695], [ 0.23694779], [ 0. ], [ 0.244 ]])
#_# Represented: 1
'''
return 100*(args[1]-0.01*args[0]**2+1)+0.01*(args[0]+10)**2

View File

@ -0,0 +1,84 @@
import math as m
import numpy as np
from numpy import random as r
import time
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
import copy as cp
#def func(Sc):
# x1 = Sc[0]
# x2 = Sc[1]
# return m.sqrt(x1**2+x2**2)
def tweak(St,p,sigma,high,low):
for i in range(len(St)):
if p > r.random():
while True:
n = r.normal(loc=0, scale=sigma)
if (high > St[i]+n) and (low < St[i]+n):
St[i]+=n
break
return St
def Quality(Sc,objective,func):
func_output = func(Sc)
if type(func_output) == list:
error = [func_output[i]-objective[i] for i in range(len(func_output))]
else:
error = func_output - objective
print("Error is: "+str(error))
return 1/abs(error)
def main(func, S, args):
r.seed(int(time.time()))
route = list()
#Parsing arguments
y = args["objs"]
t = args["t"]
p = args["p"]
high = 20
low = -20
Best = list()
Best[:] = cp.deepcopy(S)
sigma = 0.1
route.append(Best[:])
while True:
print('\n\n\n')
R = tweak(cp.deepcopy(S),p,sigma,high, low)
print(R)
print(S)
Qr = Quality(R,y,func)
Qs = Quality(S,y,func)
try:
P = m.e**((Qr-Qs)/t)
except:
pass
print('QUALITY_R///{}'.format(Qr))
print('QUALITY_S///{}'.format(Qs))
print('fraction is:{}'.format(P))
if (Qr > Qs) or (r.random() < P):
print('NEW_S')
S[:] = R[:]
if t > 0.01:
t-= t/10
print('t = {}'.format(t))
if (Quality(S,y,func) > Quality(Best,y,func)):
print('new Best****:{}'.format(Best))
Best[:] = S[:]
route.append(Best[:])
print(route)
if t < 0 or Quality(Best,y,func) > 50:
break
#print('the Best Quality obtained was:{}'.format(Quality(Best,y)))
print("Final Quality is: {}".format(Quality(Best,y,func)))
print("final Temperature is: {}".format(t))
return Quality(Best,y,func)

View File

Binary file not shown.

View File

@ -1 +0,0 @@

64
tests/test_flows.py Normal file
View File

@ -0,0 +1,64 @@
import unittest
import os
from MDAF.MDAF import representfunc
from MDAF.MDAF import installFalcoo
from MDAF.MDAF import doe
#target = __import__("MDAF.py")
# Testing the test function representation workflow
class Test_representfunc(unittest.TestCase):
def testoutput(self):
"""
Test that the function can calculate the representation and write to the function docstring
"""
funcpath = 'tests/Bukin2.py'
funcpath_backup = 'tests/Bukin2.py.old'
results = representfunc(funcpath, forced = True)
with open(funcpath,"r") as file:
content = file.read()
reprCheck = bool(content.find('#_# Represented: 1'))
os.remove(funcpath)
os.replace(funcpath_backup, funcpath)
self.assertTrue(reprCheck)
self.assertIsInstance(results, dict)
# Testing the flacco installation workflow
class Test_flaccoInstall(unittest.TestCase):
def testoutput(self):
"""
Test that the flacco packages are able to install automatically
"""
#installFalcoo()
# Testing the DOE execution workflow
class Test_DOE(unittest.TestCase):
def testoutput(self):
"""
Test that it can execute a DOE and output the dictionarry of the results
"""
testfunctionpaths = ["tests/Bukin2.py"]
heuristicpath = "tests/SimmulatedAnnealing.py"
args = {"t": 1000, "p": 0.95, "objs": 0}
data = doe (heuristicpath, testfunctionpaths, args)
self.assertIsInstance(data, dict)
# Testing the surrogate modelling workflow
class Test_surrogate(unittest.TestCase):
def testoutput(self):
"""
Test that it can generate a neural network approximation of the algorithm's performance expectation
"""
#tbd
if __name__ == '__main__':
unittest.main()