From c7ae2e18fb72670821f43c246483c5fb2a1cd51b Mon Sep 17 00:00:00 2001 From: Remi Ehounou Date: Sun, 23 May 2021 22:28:57 -0400 Subject: [PATCH] added automated tests rmved unused imports --- .vscode/settings.json | 12 ++- MDAF/MDAF.py | 21 ++--- tests/Bukin2.py | 29 ++++++ tests/SimmulatedAnnealing.py | 84 ++++++++++++++++++ tests/__init__.py | 0 tests/__pycache__/Bukin2.cpython-39.pyc | Bin 0 -> 410 bytes .../test_flows.cpython-39-pytest-6.2.4.pyc | Bin 0 -> 2431 bytes tests/test.py | 1 - tests/test_flows.py | 64 +++++++++++++ 9 files changed, 195 insertions(+), 16 deletions(-) create mode 100644 tests/Bukin2.py create mode 100644 tests/SimmulatedAnnealing.py delete mode 100644 tests/__init__.py create mode 100644 tests/__pycache__/Bukin2.cpython-39.pyc create mode 100644 tests/__pycache__/test_flows.cpython-39-pytest-6.2.4.pyc delete mode 100644 tests/test.py create mode 100644 tests/test_flows.py diff --git a/.vscode/settings.json b/.vscode/settings.json index e9d5e48..7876636 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,9 +1,17 @@ { "python.pythonPath": "/usr/sbin/python", "python.testing.pytestArgs": [ - "Sample codes" + "tests" ], "python.testing.unittestEnabled": 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" } \ No newline at end of file diff --git a/MDAF/MDAF.py b/MDAF/MDAF.py index 3195e59..d190ebb 100644 --- a/MDAF/MDAF.py +++ b/MDAF/MDAF.py @@ -5,16 +5,13 @@ import importlib.util import multiprocessing import time import re -from numpy import random as r -from numpy import * +from numpy import random as rand +from numpy import array, isnan, NaN, asarray import statistics from functools import partial import shutil # Surrogate modelling -import itertools -import matplotlib.pyplot as plt -from mpl_toolkits.mplot3d import Axes3D # Test function representation from rpy2 import robjects as robjs @@ -23,7 +20,6 @@ from rpy2 import rinterface # Test function characteristics import statistics as st -from scipy import signal, misc, ndimage 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] # 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 funcChars = representfunc(funcpath) @@ -109,7 +105,7 @@ def measure(heuristicpath, funcpath, args, connection): # 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 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 with open(funcpath, "r") as file: 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 repl = "\\1"+docstrs+"\t\\2" # 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) # Overwrite the test function file with open(funcpath,"w") as file: @@ -170,7 +166,7 @@ def representfunc(funcpath, forced = False): # Finding the function characteristics inside the docstring 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__) results = {} for charac in characs: @@ -226,7 +222,7 @@ def doe(heuristicpath, testfunctionpaths, args): funcnames = [path.splitext(path.basename(funcpath))[0] for funcpath in testfunctionpaths] #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 proc = [] @@ -242,7 +238,6 @@ def doe(heuristicpath, testfunctionpaths, args): # defining the response variables responses = {} failedfunctions = {} - processtiming = {} # Starting the subprocesses for each testfunction for idx,process in enumerate(proc): diff --git a/tests/Bukin2.py b/tests/Bukin2.py new file mode 100644 index 0000000..cdc5232 --- /dev/null +++ b/tests/Bukin2.py @@ -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 diff --git a/tests/SimmulatedAnnealing.py b/tests/SimmulatedAnnealing.py new file mode 100644 index 0000000..9756f56 --- /dev/null +++ b/tests/SimmulatedAnnealing.py @@ -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) + + + + diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/__pycache__/Bukin2.cpython-39.pyc b/tests/__pycache__/Bukin2.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..d845b425ffd8d68d4fd1e56c4946e66c91753c9b GIT binary patch literal 410 zcmYe~<>g`kf=zs@6Se{A#~=ZkXmG=5Up#fqhK5h5zEOhhlm*h1$AMfxtV#Hxuv-v zwT1>d2C-aRoG(*=?s^GwX?nHDI`@ZFE%q;g9EO)5Prd|&!%7BC##=0jMd`&wY(OrU z_+_AO tVrCx5o??*644g%rK+;c>5lkaYhOjt*a<@2aa`RJ4b5iXrDpg)#YMX(6E5+ax2jXksJCULMG z+8xcFwpYYIz+v}-RQO}K=gI{x`~f}jp65qrsvfG6$d3J-JU@T$eP39&8xXkuT>s;T zk6MKMg_GsWgUK~$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