Skip to content

Commit

Permalink
#7 adds missing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kvoss committed Jun 11, 2021
1 parent d1a512f commit 2d9bf68
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 12 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ x beale
x branin
x colville
x styblinski_tang
x powell
x shekel
x forrester
x hartmann_3d
x hartmann_4d
x hartmann_6d
```

## Contributions
Expand Down
149 changes: 143 additions & 6 deletions bss.cc
Original file line number Diff line number Diff line change
Expand Up @@ -428,12 +428,12 @@ goldstein_price(const std::vector<double>& xs)
double x1 = xs.at(0);
double x2 = xs.at(1);

acc1 = 1 \
+ SQ(x1 + x2 + 1) \
* (19. - 14*x1 + 3*x1*x1 - 14*x2 + 6*x1*x2 + 3*x2*x2);
acc2 = 30. \
+ SQ(2*x1 - 3*x2) \
* (18. - 32*x1 + 12*x1*x1 + 48*x2 - 36*x1*x2 + 27*x2*x2);
acc1 = 1.
+ SQ(x1 + x2 + 1)
* (19. - 14*x1 + 3*x1*x1 - 14*x2 + 6*x1*x2 + 3*x2*x2);
acc2 = 30.
+ SQ(2*x1 - 3*x2)
* (18. - 32*x1 + 12*x1*x1 + 48*x2 - 36*x1*x2 + 27*x2*x2);
ret = acc1 * acc2;
return ret;
}
Expand Down Expand Up @@ -699,6 +699,143 @@ styblinski_tang(const std::vector<double>& xs)
return ret;
}

double
powell(const std::vector<double>& xs)
{
double ret = 0.;
std::size_t max_d = std::floor(xs.size() / 4);
for (std::size_t i=1; i<=max_d; ++i) {
double xm0 = xs.at(4 * i - 1);
double xm1 = xs.at(4 * i - 2);
double xm2 = xs.at(4 * i - 3);
double xm3 = xs.at(4 * i - 4);
ret += SQ(xm3 + 10.*xm2)
+ 5. * SQ(xm1 - xm0)
+ SQ(SQ(xm2 - 2.*xm1))
+ 10. * SQ(SQ(xm3 - xm0));
}
return ret;
}

double
shekel(const std::vector<double>& xs)
{
// 4-d function
const std::size_t m = 10;
const double b[m] = {.1, .2, .2, .4, .4, .6, .3, .7, .5, .5};
const double c[4][m] = {
{4., 1., 8., 6., 3., 2., 5., 8., 6., 7.},
{4., 1., 8., 6., 7., 9., 3., 1., 2., 3.6},
{4., 1., 8., 6., 3., 2., 5., 8., 6., 7.},
{4., 1., 8., 6., 7., 9., 3., 1., 2., 3.6}
};

double ret = 0.;
for (std::size_t i=0; i<m; ++i) {
double lret = b[i];
for (std::size_t j=0; j<4; ++j)
lret += SQ(xs.at(j) - c[j][i]);
ret += 1. / lret;
}
ret *= -1.;
return ret;
}

double
forrester(const std::vector<double>& xs)
{
// 1-d function
double x1 = xs.at(0);
double ret = SQ(6.*x1 - 2.) * std::sin(12.*x1 - 4.);
return ret;
}

double
hartmann_3d(const std::vector<double>& xs)
{
const double al[] = {1., 1.2, 3., 3.2};
const double a[4][3] = {
{3., 10., 30.},
{.1, 10., 35.},
{3., 10., 30.},
{.1, 10., 35.}
};
const double p[4][3] = {
{.3689, .1170, .2673},
{.4699, .4387, .7470},
{.1091, .8732, .5547},
{.0381, .5743, .8828}
};

double ret = 0.;
for (std::size_t i=0; i<4; ++i) {
double lret = 0.;
for (std::size_t j=0; j<3; ++j)
lret += SQ(xs.at(j) - p[i][j]) * a[i][j];
ret += al[i] * std::exp(-1. * lret);
}
ret *= -1.;
return ret;
}

double
hartmann_4d(const std::vector<double>& xs)
{
const double al[] = {1., 1.2, 3., 3.2};
const double a[4][4] = {
{10., 3., 17., 3.5},
{.05, 10., 17., .1},
{ 3., 3.5, 1.7, 10.},
{17., 8., .05, 10.}
};
const double p[4][4] = {
{.1312, .1696, .5569, .0124},
{.2329, .4135, .8307, .3736},
{.2348, .1451, .3522, .2883},
{.4047, .8828, .8732, .5743}
};

double ret = 0.;
for (std::size_t i=0; i<4; ++i) {
double lret = 0.;
for (std::size_t j=0; j<4; ++j)
lret += SQ(xs.at(j) - p[i][j]) * a[i][j];
ret += al[i] * std::exp(-1. * lret);
}
ret *= -1.;
ret += 1.1;
ret /= .839;
return ret;
}

double
hartmann_6d(const std::vector<double>& xs)
{
const double al[] = {1., 1.2, 3., 3.2};
const double a[4][6] = {
{10., 3., 17., 3.5, 1.7, 8.},
{.05, 10., 17., .1, 8., 14.},
{ 3., 3.5, 1.7, 10., 17., 8.},
{17., 8., .05, 10., .1, 14.}
};
const double p[4][6] = {
{.1312, .1696, .5569, .0124, .8283, .5886},
{.2329, .4135, .8307, .3736, .1004, .9991},
{.2348, .1451, .3522, .2883, .3047, .6650},
{.4047, .8828, .8732, .5743, .1091, .0381}
};

double ret = 0.;
for (std::size_t i=0; i<4; ++i) {
double lret = 0.;
for (std::size_t j=0; j<6; ++j)
lret += SQ(xs.at(j) - p[i][j]) * a[i][j];
ret += al[i] * std::exp(-1. * lret);
}
ret *= -1.;
return ret;
}

// int
// main (int argc, char** argv)
// {
Expand Down
6 changes: 6 additions & 0 deletions bss.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ double beale(const std::vector<double>& xs);
double branin(const std::vector<double>& xs);
double colville(const std::vector<double>& xs);
double styblinski_tang(const std::vector<double>& xs);
double powell(const std::vector<double>& xs);
double shekel(const std::vector<double>& xs);
double forrester(const std::vector<double>& xs);
double hartmann_3d(const std::vector<double>& xs);
double hartmann_4d(const std::vector<double>& xs);
double hartmann_6d(const std::vector<double>& xs);

#endif // _BSS_H

21 changes: 16 additions & 5 deletions cbench.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ PYWRAP(beale);
PYWRAP(branin);
PYWRAP(colville);
PYWRAP(styblinski_tang);

PYWRAP(powell);
PYWRAP(shekel);
PYWRAP(forrester);
PYWRAP(hartmann_3d);
PYWRAP(hartmann_4d);
PYWRAP(hartmann_6d);

static PyMethodDef
CBenchMethods[] = {
Expand Down Expand Up @@ -132,10 +137,16 @@ CBenchMethods[] = {
{"trefethen4", py_trefethen4, METH_VARARGS, "trefethen4 function"},
{"three_hump_camel_back", py_three_hump_camel_back, METH_VARARGS, "three hump camel function"},
{"dixon_price", py_dixon_price, METH_VARARGS, "dixon price function"},
{"beale", py_beale, METH_VARARGS, "beale function"},
{"branin", py_branin, METH_VARARGS, "branin function"},
{"colville", py_colville, METH_VARARGS, "colville function"},
{"styblinski_tang", py_styblinski_tang, METH_VARARGS, "styblinski_tang function"},
{"beale", py_beale, METH_VARARGS, "Beale function"},
{"branin", py_branin, METH_VARARGS, "Branin function"},
{"colville", py_colville, METH_VARARGS, "Colville function"},
{"styblinski_tang", py_styblinski_tang, METH_VARARGS, "Styblinski_tang function"},
{"powell", py_powell, METH_VARARGS, "Powell function"},
{"shekel", py_shekel, METH_VARARGS, "Shekel function"},
{"forrester", py_forrester, METH_VARARGS, "Forrester function"},
{"hartmann_3d", py_hartmann_3d, METH_VARARGS, "Hartmann_3d function"},
{"hartmann_4d", py_hartmann_4d, METH_VARARGS, "Hartmann_4d function"},
{"hartmann_6d", py_hartmann_6d, METH_VARARGS, "Hartmann_6d function"},
{NULL, NULL, 0, NULL}
};

Expand Down
6 changes: 6 additions & 0 deletions sanity-test-golden.log
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
-> drop_wave: -0.958928083376883
-> easom: -1.6344141224711163e-08
-> eggholder: -26.34161710307163
-> forrester: -0.6565767743055739
-> goldstein_price: 899.4140086400001
-> gramacy_lee: 0.6561
-> griewank: 0.07377871010504211
-> hartmann_3d: -0.7329114876560024
-> hartmann_4d: -1.8805100051796992
-> hartmann_6d: -1.352784307845224
-> holder_table: -0.24769346068827464
-> langermann: 0.902701275155354
-> levy: 0.7980912375904945
Expand All @@ -27,6 +31,7 @@
-> parabola: 0.5601
-> penalty1: 5.880180416027215
-> perm0db: 458.075937106786
-> powell: 4.5666
-> rastrigin: 60.54036728428271
-> rosenbrock: 34.53
-> rotated_hyperellipsoid: 1.0601
Expand All @@ -35,6 +40,7 @@
-> schaffers_f6: 0.049217281823751846
-> schwefels: 2095.758798134426
-> schwefels_p222: 1.511224
-> shekel: -0.49197124278910376
-> shubert: -1.839613667262635
-> six_hump_camel_back: -0.09380966666666667
-> sphere: 0.5601
Expand Down
5 changes: 4 additions & 1 deletion testit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ for el in dir(cb):
continue
print(f"-> {el}: ", end="", flush=True)
fn = getattr(cb, el)
ans = fn(xs)
if el == "hartmann_6d":
ans = fn([0.1, 0.2, 0.3, 0.4, 0.51, 0.63])
else:
ans = fn(xs)
print(ans)
' > sanity-test-instance.log
Expand Down

0 comments on commit 2d9bf68

Please sign in to comment.