flacco has been integrated w rpy2 - basic function

This commit is contained in:
Remi Ehounou
2021-05-19 22:44:17 -04:00
parent e3ff427d76
commit bcf73992f0
31 changed files with 232 additions and 168 deletions

View File

@ -1,5 +1,6 @@
# directly running the DOE because existing surrogates can be explored with another workflow
from os import path
from os import sys
import importlib.util
import multiprocessing
import time
@ -15,6 +16,11 @@ import itertools
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Test function representation
from rpy2 import robjects as robjs
from rpy2.robjects.packages import importr
from rpy2 import rinterface
# Test function characteristics
import statistics as st
from scipy import signal, misc, ndimage
@ -140,93 +146,30 @@ def representfunc(funcpath):
# Automatically generate the representation if the docstrings did not return anything
if not ('Represented' in results):
print("Warning, the Representation of the Test Function has not specified\n===\n******Calculating the Characteristics******")
print("Warning, the Representation of the Test Function has not been specified\n===\n******Calculating the Characteristics******")
n = int(results['dimmensions'])
# pickle these steps
coords = arange(-10,10,0.5)
samplemx = array([*itertools.product(coords, repeat=n)])
funcmap = array([* map(funcmodule.main, samplemx)])
# Arrays for plotting the test function
X = array([tp[0] for tp in samplemx])
Y = array([tp[1] for tp in samplemx])
Z = array(funcmap)
# reshaping the array into a 3D topology
topology = reshape(Z,(coords.size,coords.size))
ck = topology
# Plotting the test function
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(X, Y, Z)
# plt.show()
execpath = sys.executable
# creating the r functions
rlist = robjs.r['list']
rapply = robjs.r['apply']
rtestfunc = rinterface.rternalize(funcmodule.main)
# Number of Modes filter the data for local optima: look for circle like shapes, or squares or rectangles of very low derivative (tip of modes)
###
lower =-10
upper = 10
X = flacco.createInitialSample(n_obs = 500, dim = 2, control = rlist(init_sample_type = 'lhs', init_sample_lower = lower, init_sample_upper = upper))
y = rapply(X, 1, rtestfunc)
testfuncobj = flacco.createFeatureObject(X = X, y = y, fun = rtestfunc, lower = lower, upper = upper, blocks = 10)
rawfeats = flacco.calculateFeatureSet(testfuncobj, set='ela_meta')
pyfeat = asarray(rawfeats)
# Valleys and Bassins
# Alternative filter used for calculating derivatives
#derfilt = array([1.0, -2, 1.0], dtype=float32)
#alpha = signal.sepfir2d(ck, derfilt, [1]) + signal.sepfir2d(ck, [1], derfilt)
# Currently used filter for Valley detection
hor = array([[0,1,1],[-1,0,1], [-1,-1,0]])
vert = array([[-1,-1,0], [-1,0,1], [0,1,1]])
for i in range(1): betaH = signal.convolve(ck,hor,mode='valid')
for i in range(1): betaV = signal.convolve(ck,vert, mode='valid')
beta = sqrt(betaH ** 2 + betaV ** 2)
#beta = beta[5:-5][5:-5]
norm = linalg.norm(beta)
beta/= norm # normalized matrix
# custom filter for detection should light up the locaton of pattern
kernel = array([[1,1,1], [1,100,1], [1,1,1]])
beta = beta < average(beta)
beta = beta * 1
for i in range(100):
beta = ndimage.convolve(beta,kernel)
beta = beta >= 101
beta = beta * 1
if any(beta): results['Valleys'] = True
# Separability: calculate the derivatives in one dimension and see if independant from other dimension
# Dimensionality: number of objectives, inputs: call function once and see what it gives | for number of inputs call until it works; try catch
# Pareto fronts:
# Noisyness: use the previously generated DOE and calculate a noisyness factor; average of derivative
# Displaying the plots for development purposes
#img1 = plt.figure()
#ax2 = img1.add_subplot(111)
#ax2.imshow(alpha)
img2 = plt.figure()
ax3 = img2.add_subplot(111)
ax3.imshow(beta)
plt.show()
# Writing the calculated representation into the test function file
# results['Represented'] = True
writerepresentation(funcpath, results)
@ -285,14 +228,23 @@ if __name__ == '__main__':
funcnames = ["Bukin2", "Bukin4", "Brown"]
# testfunctionpaths = ["/home/remi/Documents/MDAF-GitLAB/SourceCode/TestFunctions/Bukin4.py"]
# funcnames = ["Bukin4"]
# Installing the packages needed for FLACCO
utils = importr('utils')
#utils.install_packages('flacco', repos='https://utstat.toronto.edu/cran/')
#utils.install_packages('list', repos='https://utstat.toronto.edu/cran/')
####utils.install_packages('reticulate', repos='https://utstat.toronto.edu/cran/')
reticulate = importr('reticulate')
flacco = importr('flacco')
objs = 0
args = {"high": 200, "low": -200, "t": 1000, "p": 0.95}
scale = 1
data = doe (heuristicpath, heuristic_name, testfunctionpaths, funcnames, objs, args, scale)
print(data['Bukin2'][1][2])
#representfunc("TestFunctions/Bukin6.py")
# data = doe (heuristicpath, heuristic_name, testfunctionpaths, funcnames, objs, args, scale)
# print([point[2] for point in data['Bukin2'][1]])
representfunc("TestFunctions/Bukin2.py")
# %%

View File

@ -72,7 +72,7 @@ def main(func, obj, S, args):
route.append(Best[:])
print(route)
if t < 0 or Quality(Best,y,func) > 200:
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)))

View File

@ -5,88 +5,5 @@ def main(args):
'''
#_# dimmensions: 2
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# dimmensions: 2.0
#_# Valleys: True
#_# dimmensions: 2.0
#_# Valleys: True
#_# dimmensions: 2.0
#_# Valleys: True
#_# dimmensions: 2.0
#_# Valleys: True
#_# dimmensions: 2.0
#_# Valleys: True
#_# dimmensions: 2.0
#_# Valleys: True
#_# dimmensions: 2.0
#_# Valleys: True
#_# dimmensions: 2.0
#_# Valleys: True
#_# dimmensions: 2.0
#_# Valleys: True
#_# dimmensions: 2.0
#_# Valleys: True
'''
return 100*sqrt(fabs(args[1]-0.01*args[0]**2))+0.01*fabs(args[0]+10)

View File

@ -0,0 +1,31 @@
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
print(Z[1])
# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
# Customize the z axis.
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()

31
SourceCode/calcFeatures.R Normal file
View File

@ -0,0 +1,31 @@
library(reticulate)
library(flacco)
library(parallelMap)
library(parallel)
# Other packages to install
# plyr
# RANN
# numDeriv
# e1071
# mda - needs to install gcc-fortran on Operating system
genTestFeatures <- function(path,pypath, upper=10, lower=-10, n) {
use_python(pypath)
source_python(path)
X = createInitialSample(n.obs = 500, dim = n, control = list(init_sample.type = 'lhs', init_sample.lower = lower, init_sample.upper = upper))
y = apply(X, 1, main)
testfunc <- createFeatureObject(X = X, y = y, fun = main, lower = lower, upper = upper, blocks = 10)
n.cores = detectCores()
parallelStart(mode = "local", logging = FALSE, show.info = FALSE)
system.time((levelset.par = calculateFeatures(testfunc , control = list('cm_angle.show_warnings'=TRUE, 'cm_grad.show_warnings'=TRUE, 'ic.show_warnings'=TRUE))))
parallelStop()
}