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

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()