Commit 8b5389aa authored by Konstantinos Chatzilygeroudis's avatar Konstantinos Chatzilygeroudis
Browse files

Added new test functions and better plotting for regression benchs [ci skip]

parent b34f596e
#ifndef BENCHMARKS_REGRESSION_TEST_FUNCTIONS_HPP
#define BENCHMARKS_REGRESSION_TEST_FUNCTIONS_HPP
// Rastrigin Function: https://www.sfu.ca/~ssurjano/rastr.html
struct Rastrigin {
double operator()(const Eigen::VectorXd& x) const
{
......@@ -25,6 +26,7 @@ struct Rastrigin {
}
};
// Ackley Function: https://www.sfu.ca/~ssurjano/ackley.html
struct Ackley {
double operator()(const Eigen::VectorXd& x) const
{
......@@ -56,6 +58,7 @@ struct Ackley {
}
};
// Bukin N.6 Function: https://www.sfu.ca/~ssurjano/bukin6.html
struct Bukin {
double operator()(const Eigen::VectorXd& x) const
{
......@@ -79,6 +82,7 @@ struct Bukin {
}
};
// Cross-In-Tray Function: https://www.sfu.ca/~ssurjano/crossit.html
struct CrossInTray {
double operator()(const Eigen::VectorXd& x) const
{
......@@ -103,6 +107,7 @@ struct CrossInTray {
}
};
// Drop-Wave Function: https://www.sfu.ca/~ssurjano/drop.html
struct DropWave {
double operator()(const Eigen::VectorXd& x) const
{
......@@ -124,6 +129,7 @@ struct DropWave {
}
};
// Gramacy & Lee 2012 Function: https://www.sfu.ca/~ssurjano/grlee12.html
struct GramacyLee {
double operator()(const Eigen::VectorXd& x) const
{
......@@ -145,6 +151,7 @@ struct GramacyLee {
}
};
// A simple step function
struct Step {
double operator()(const Eigen::VectorXd& x) const
{
......@@ -169,6 +176,7 @@ struct Step {
}
};
// Holder-Table Function: https://www.sfu.ca/~ssurjano/holder.html
struct HolderTable {
double operator()(const Eigen::VectorXd& x) const
{
......@@ -193,6 +201,7 @@ struct HolderTable {
}
};
// Levy Function: https://www.sfu.ca/~ssurjano/levy.html
struct Levy {
double operator()(const Eigen::VectorXd& x) const
{
......@@ -225,6 +234,7 @@ struct Levy {
}
};
// Schwefel Function: https://www.sfu.ca/~ssurjano/schwef.html
struct Schwefel {
double operator()(const Eigen::VectorXd& x) const
{
......@@ -252,6 +262,7 @@ struct Schwefel {
}
};
// Six-Hump-Camel Function: https://www.sfu.ca/~ssurjano/camel6.html
struct SixHumpCamel {
double operator()(const Eigen::VectorXd& x) const
{
......@@ -283,6 +294,7 @@ struct SixHumpCamel {
}
};
// Hartmann6 Function: https://www.sfu.ca/~ssurjano/hart6.html
struct Hartmann6 {
double operator()(const Eigen::VectorXd& x) const
{
......@@ -324,4 +336,263 @@ struct Hartmann6 {
}
};
// Robot Arm Function: https://www.sfu.ca/~ssurjano/robot.html
struct RobotArm {
double operator()(const Eigen::VectorXd& x) const
{
Eigen::VectorXd q = x.head(4);
Eigen::VectorXd L = x.tail(4);
double u = 0.0, v = 0.0;
for (int i = 0; i < 4; i++) {
u += L(i) * std::cos(q.head(i + 1).sum());
v += L(i) * std::sin(q.head(i + 1).sum());
}
return std::sqrt(v * v + u * u);
}
std::vector<Eigen::VectorXd> bounds() const
{
Eigen::VectorXd tmp(2);
tmp << 0., 2. * M_PI;
std::vector<Eigen::VectorXd> b;
b.push_back(tmp);
b.push_back(tmp);
b.push_back(tmp);
b.push_back(tmp);
tmp << 0., 1.;
b.push_back(tmp);
b.push_back(tmp);
b.push_back(tmp);
b.push_back(tmp);
return b;
}
int dims() const
{
return 8;
}
};
// OTL Circuit Function: https://www.sfu.ca/~ssurjano/otlcircuit.html
struct OTLCircuit {
double operator()(const Eigen::VectorXd& x) const
{
double Rb1 = x(0);
double Rb2 = x(1);
double Rf = x(2);
double Rc1 = x(3);
double Rc2 = x(4);
double beta = x(5);
double Vb1 = 12. * Rb2 / (Rb1 + Rb2);
double term1 = (Vb1 + 0.74) * beta * (Rc2 + 9.) / (beta * (Rc2 + 9.) + Rf);
double term2 = 11.35 * Rf / (beta * (Rc2 + 9.) + Rf);
double term3 = 0.74 * Rf * beta * (Rc2 + 9.) / ((beta * (Rc2 + 9.) + Rf) * Rc1);
return term1 + term2 + term3;
}
std::vector<Eigen::VectorXd> bounds() const
{
std::vector<Eigen::VectorXd> b;
Eigen::VectorXd tmp(2);
tmp << 50., 150.;
b.push_back(tmp);
tmp << 25., 70.;
b.push_back(tmp);
tmp << 0.5, 3.;
b.push_back(tmp);
tmp << 1.2, 2.5;
b.push_back(tmp);
tmp << 0.25, 1.2;
b.push_back(tmp);
tmp << 50., 300.;
b.push_back(tmp);
return b;
}
int dims() const
{
return 6;
}
};
// Piston Simulation Function: https://www.sfu.ca/~ssurjano/piston.html
struct PistonSimulation {
double operator()(const Eigen::VectorXd& x) const
{
double M = x(0);
double S = x(1);
double V_0 = x(2);
double k = x(3);
double P_0 = x(4);
double T_a = x(5);
double T_0 = x(6);
double A = P_0 * S + 19.62 * M - k * V_0 / S;
double V = S * (std::sqrt(A * A + 4. * k * P_0 * V_0 * T_a / T_0) - A) / (2. * k);
return 2. * M_PI * std::sqrt(M / (k + S * S * P_0 * V_0 * T_a / (T_0 * V * V)));
}
std::vector<Eigen::VectorXd> bounds() const
{
std::vector<Eigen::VectorXd> b;
Eigen::VectorXd tmp(2);
tmp << 30., 60.;
b.push_back(tmp);
tmp << 0.005, 0.020;
b.push_back(tmp);
tmp << 0.002, 0.010;
b.push_back(tmp);
tmp << 1000., 5000.;
b.push_back(tmp);
tmp << 90000., 110000.;
b.push_back(tmp);
tmp << 290., 296.;
b.push_back(tmp);
tmp << 340., 360.;
b.push_back(tmp);
return b;
}
int dims() const
{
return 7;
}
};
// Inverse Dynamics of a 2dof planar arm I (the first torque)
struct PlanarInverseDynamicsI {
double operator()(const Eigen::VectorXd& x) const
{
Eigen::VectorXd ddq = x.head(2);
Eigen::VectorXd dq = x.segment(2, 2);
Eigen::VectorXd q = x.segment(4, 2);
// double m1 = x(6), l1 = x(7);
double m1 = 0.5, l1 = 0.5;
double r1 = l1 / 2.;
// double m2 = x(8), l2 = x(9);
double m2 = 0.5, l2 = 0.5;
double r2 = l2 / 2.;
double I1 = m1 * l1 * l1 / 12.;
double I2 = m2 * l2 * l2 / 12.;
double a = I1 + I2 + m1 * r1 * r1 + m2 * (l1 * l1 + r2 * r2);
double b = m2 * l1 * r2;
double delta = I2 + m2 * r2 * r2;
Eigen::MatrixXd M(2, 2);
M << a + 2. * b * std::cos(q(1)), delta + b * std::cos(q(1)),
delta + b * std::cos(q(1)), delta;
// std::cout << M << std::endl
// << std::endl;
Eigen::MatrixXd C(2, 2);
C << -b * std::sin(q(1)) * dq(1), -b * std::sin(q(1)) * (dq(0) + dq(1)),
b * std::sin(q(1)) * dq(0), 0.;
// std::cout << C << std::endl;
Eigen::VectorXd tau = M * ddq + C * dq;
return tau(0);
}
std::vector<Eigen::VectorXd> bounds() const
{
std::vector<Eigen::VectorXd> b;
for (int i = 0; i < 4; i++) {
Eigen::VectorXd tmp(2);
tmp << -2. * M_PI, 2. * M_PI;
b.push_back(tmp);
}
for (int i = 0; i < 2; i++) {
Eigen::VectorXd tmp(2);
tmp << -M_PI, M_PI;
b.push_back(tmp);
}
// for (int i = 0; i < 4; i++) {
// Eigen::VectorXd tmp(2);
// tmp << 0.1, 1.;
// b.push_back(tmp);
// }
return b;
}
int dims() const
{
return 6;
}
};
// Inverse Dynamics of a 2dof planar arm II (the first torque)
struct PlanarInverseDynamicsII {
double operator()(const Eigen::VectorXd& x) const
{
Eigen::VectorXd ddq = x.head(2);
Eigen::VectorXd dq = x.segment(2, 2);
Eigen::VectorXd q = x.segment(4, 2);
// double m1 = x(6), l1 = x(7);
double m1 = 0.5, l1 = 0.5;
double r1 = l1 / 2.;
// double m2 = x(8), l2 = x(9);
double m2 = 0.5, l2 = 0.5;
double r2 = l2 / 2.;
double I1 = m1 * l1 * l1 / 12.;
double I2 = m2 * l2 * l2 / 12.;
double a = I1 + I2 + m1 * r1 * r1 + m2 * (l1 * l1 + r2 * r2);
double b = m2 * l1 * r2;
double delta = I2 + m2 * r2 * r2;
Eigen::MatrixXd M(2, 2);
M << a + 2. * b * std::cos(q(1)), delta + b * std::cos(q(1)),
delta + b * std::cos(q(1)), delta;
// std::cout << M << std::endl
// << std::endl;
Eigen::MatrixXd C(2, 2);
C << -b * std::sin(q(1)) * dq(1), -b * std::sin(q(1)) * (dq(0) + dq(1)),
b * std::sin(q(1)) * dq(0), 0.;
// std::cout << C << std::endl;
Eigen::VectorXd tau = M * ddq + C * dq;
return tau(1);
}
std::vector<Eigen::VectorXd> bounds() const
{
std::vector<Eigen::VectorXd> b;
for (int i = 0; i < 4; i++) {
Eigen::VectorXd tmp(2);
tmp << -2. * M_PI, 2. * M_PI;
b.push_back(tmp);
}
for (int i = 0; i < 2; i++) {
Eigen::VectorXd tmp(2);
tmp << -M_PI, M_PI;
b.push_back(tmp);
}
// for (int i = 0; i < 4; i++) {
// Eigen::VectorXd tmp(2);
// tmp << 0.1, 1.;
// b.push_back(tmp);
// }
return b;
}
int dims() const
{
return 6;
}
};
#endif
......@@ -103,9 +103,12 @@ def compile_regression_benchmarks(bld, json_file):
gps_learn_code = ""
gps_query_code = ""
model_names = []
for m in range(len(models)):
model = models[m]
gp_type = 'GP'
model_name = 'GP'
kernel_type = 'SquaredExpARD'
optimize_noise = 'true'
kernel_params = {}
......@@ -116,6 +119,9 @@ def compile_regression_benchmarks(bld, json_file):
optimizer = 'Rprop'
optimizer_params = []
if 'name' in model:
model_name = model['name']
model_names.append(model_name)
if 'type' in model:
gp_type = model['type']
if 'kernel' in model:
......@@ -241,7 +247,7 @@ def compile_regression_benchmarks(bld, json_file):
gps_query_code += ' ofs_res << std::setprecision(std::numeric_limits<long double>::digits10 + 1);\n'
for m in range(len(models)):
gps_query_code += ' ofs_res << time_' + str(m) + ' / double(1000000.0) << \" \" << time2_' + str(m) + ' * 1e-3 / double(N_test) << \" \" << err_' + str(m) + ' << std::endl;\n'
gps_query_code += ' ofs_res << time_' + str(m) + ' / double(1000000.0) << \" \" << time2_' + str(m) + ' * 1e-3 / double(N_test) << \" \" << err_' + str(m) + ' << " ' + model_names[m] + '" << std::endl;\n'
cpp_tpl = cpp_tpl.replace('@NMODELS', str(len(models)))
cpp_tpl = cpp_tpl.replace('@FUNCS', cpp_body)
......
......@@ -91,31 +91,36 @@ def load_data():
func = func[:-4]
var = 'GPy'
exp = exp[4:]
# print(bench, func, var)
# if not func in data[bench][var]:
# data[bench][var][func] = []
# data[bench][var][func].append(np.loadtxt(f))
text_file = open(f, "r")
txt_d = text_file.readlines()
for i in range(0, len(txt_d), 2):
n_models = int(txt_d[0].strip().split()[-1])
var_base = var
for i in range(0, len(txt_d), n_models+1):
line = txt_d[i].strip().split()
line2 = txt_d[i+1].strip().split()
dim = int(line[0])
pts = int(line[1])
time_learn = float(line2[0])
time_query = float(line2[1])
mse = float(line2[2])
# print(bench,var,func,dim)
if not (var in points[bench][func][dim]):
points[bench][func][dim][var] = []
times_learn[bench][func][dim][var] = []
times_query[bench][func][dim][var] = []
mses[bench][func][dim][var] = []
points[bench][func][dim][var].append(pts)
times_learn[bench][func][dim][var].append(time_learn)
times_query[bench][func][dim][var].append(time_query)
mses[bench][func][dim][var].append(mse)
for j in range(0, n_models):
if n_models > 1:
var = var_base + '-' + str(j+1)
line2 = txt_d[i+j+1].strip().split()
time_learn = float(line2[0])
time_query = float(line2[1])
mse = float(line2[2])
if len(line2) == 4:
var = var_base + '-' + line2[-1]
# print(bench,var,func,dim)
if not (var in points[bench][func][dim]):
points[bench][func][dim][var] = []
times_learn[bench][func][dim][var] = []
times_query[bench][func][dim][var] = []
mses[bench][func][dim][var] = []
points[bench][func][dim][var].append(pts)
times_learn[bench][func][dim][var].append(time_learn)
times_query[bench][func][dim][var].append(time_query)
mses[bench][func][dim][var].append(mse)
return points,times_learn,times_query,mses
def custom_ax(ax):
......@@ -182,8 +187,6 @@ def plot(points,times_learn,times_query,mses):
fig.savefig(name+'.png')
close()
def plot_all():
if not plot_ok:
print_log('YELLOW', "No plot")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment