mirror of
https://github.com/ejeanboris/MDAF.git
synced 2025-12-15 21:08:15 +00:00
flacco has been integrated w rpy2 - basic function
This commit is contained in:
@@ -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")
|
||||
|
||||
|
||||
# %%
|
||||
|
||||
Reference in New Issue
Block a user