From 2d9bf6889336272e05838dd649810b83b6de4bc8 Mon Sep 17 00:00:00 2001 From: kvoss Date: Fri, 11 Jun 2021 17:17:56 -0600 Subject: [PATCH] #7 adds missing functions --- README.md | 6 ++ bss.cc | 149 +++++++++++++++++++++++++++++++++++++++-- bss.h | 6 ++ cbench.cc | 21 ++++-- sanity-test-golden.log | 6 ++ testit.sh | 5 +- 6 files changed, 181 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 121e8fb..c8174d4 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bss.cc b/bss.cc index 4e655a0..85b026a 100644 --- a/bss.cc +++ b/bss.cc @@ -428,12 +428,12 @@ goldstein_price(const std::vector& 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; } @@ -699,6 +699,143 @@ styblinski_tang(const std::vector& xs) return ret; } +double +powell(const std::vector& 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& 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& 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& 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& 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& 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) // { diff --git a/bss.h b/bss.h index d1e7b1b..743e59c 100644 --- a/bss.h +++ b/bss.h @@ -49,6 +49,12 @@ double beale(const std::vector& xs); double branin(const std::vector& xs); double colville(const std::vector& xs); double styblinski_tang(const std::vector& xs); +double powell(const std::vector& xs); +double shekel(const std::vector& xs); +double forrester(const std::vector& xs); +double hartmann_3d(const std::vector& xs); +double hartmann_4d(const std::vector& xs); +double hartmann_6d(const std::vector& xs); #endif // _BSS_H diff --git a/cbench.cc b/cbench.cc index 6102437..bd058af 100644 --- a/cbench.cc +++ b/cbench.cc @@ -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[] = { @@ -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} }; diff --git a/sanity-test-golden.log b/sanity-test-golden.log index ef29d7b..680ab8d 100644 --- a/sanity-test-golden.log +++ b/sanity-test-golden.log @@ -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 @@ -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 @@ -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 diff --git a/testit.sh b/testit.sh index c502113..024a4e4 100644 --- a/testit.sh +++ b/testit.sh @@ -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