Usecase_3_Active_Learn / L1interface.py
kode24's picture
Upload 4 files
1dd761f verified
Raw History Blame Contribute Delete
2.67 kB
from L1Problems import *
import numpy as np
def writeCoordinates(x):
#=== Write coordinates (unit hypercube)
np.savetxt("variables.inp", x.T, fmt='%.16E', delimiter=' ')
def L1interface(x,InfoCase,FidelityLevel,noise):
flag = 0 # 0: function value only, >0: function and gradient value (only implemented for Rosenbrock and ALOS functions)
#Write x to variables.inp
writeCoordinates(x)
if (InfoCase[0:8] == '1DSASENA' or InfoCase[0:8] == '1dsasena'):
[x, f, df] = Sasena(FidelityLevel, flag)
if (InfoCase[0:11] == '1DFORRESTER' or InfoCase[0:11] == '1dforrester'):
[x, f, df] = Forrester(FidelityLevel, flag)
if (InfoCase[0:6] == '1DALOS' or InfoCase[0:6] == '1dalos'):
[x, f, df] = Alos1(FidelityLevel, flag)
if (InfoCase[0:6] == '2DALOS' or InfoCase[0:6] == '2dalos'):
[x, f, df] = Alos2(FidelityLevel, flag)
if (InfoCase[0:6] == '3DALOS' or InfoCase[0:6] == '3dalos'):
[x, f, df] = Alos3(FidelityLevel, flag)
if (InfoCase[0:12] == '2DROSENBROCK' or InfoCase[0:12] == '2drosenbrock'):
[x, f, df] = Rosenbrock2(FidelityLevel, flag)
if (InfoCase[0:12] == '5DROSENBROCK' or InfoCase[0:12] == '5drosenbrock'):
[x, f, df] = Rosenbrock5(FidelityLevel, flag)
if (InfoCase[0:13] == '10DROSENBROCK' or InfoCase[0:13] == '10drosenbrock'):
[x, f, df] = Rosenbrock10(FidelityLevel, flag)
if (InfoCase[0:11] == '2DRASTRIGIN' or InfoCase[0:11] == '2drastrigin'):
[x, f, df] = Rastrigin2(FidelityLevel, flag)
if (InfoCase[0:11] == '5DRASTRIGIN' or InfoCase[0:11] == '5drastrigin'):
[x, f, df] = Rastrigin5(FidelityLevel, flag)
if (InfoCase[0:12] == '10DRASTRIGIN' or InfoCase[0:12] == '10drastrigin'):
[x, f, df] = Rastrigin10(FidelityLevel, flag)
if (InfoCase[0:6] == '2DMASS' or InfoCase[0:6] == '2dmass'):
[x, f, df] = Mass(FidelityLevel, flag)
if (InfoCase[0:8] == '2DSPRING' or InfoCase[0:8] == '2dspring'):
[x, f, df] = Spring(FidelityLevel, flag)
if (InfoCase[0:12] == '4DSPRINGMASS' or InfoCase[0:12] == '4dspringmass'):
[x, f, df] = SpringMass(FidelityLevel, flag)
# === Write output ===
print()
if (flag == 0):
print(x, f)
else:
print(x, f, df)
print()
f = f + np.random.normal(0,noise)
return f
def EvaluateL1(x,InfoCase,FidelityLevel,noise):
samples = np.shape(x)[0]
y = np.empty([samples, 1])
y[:] = np.nan
for i in np.arange(samples):
y[i] = L1interface(x[i], InfoCase, FidelityLevel,noise)
return np.array(y)