diff --git a/README.md b/README.md index 111091f..8d94a84 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,8 @@ x cross_in_tray x eggholder x gramacy_lee x holder_table +x levy +x levy13 x six_hump_camel_back x deceptive3 x drop_wave diff --git a/bss.cc b/bss.cc index 8960e11..25b395c 100644 --- a/bss.cc +++ b/bss.cc @@ -99,6 +99,44 @@ holder_table(const std::vector& xs) } +double +levy(const std::vector& xs) +{ + // The function is usually evaluated on the hypercube xi ∈ [-10, 10], for all i = 1, ..., d + const std::size_t len_xs = xs.size(); + std::vector ws(len_xs, 0.); + + double ret = 0.; + for (std::size_t i=0; i < len_xs; ++i) { + ws[i] = 1. + (xs[i] - 1.)/4.; + + if (i == (len_xs-1)) + continue; + + ret += (ws[i]-1.)*(ws[i]-1.) \ + * (1. + 10.*std::sin(pi*ws[i]+1.)*std::sin(pi*ws[i]+1.)); + } + ret += std::sin(pi*ws[0])*std::sin(pi*ws[0]) + \ + + (ws[len_xs-1]-1.)*(ws[len_xs-1]-1.) \ + * (1. + std::sin(2.*pi*ws[len_xs-1])*std::sin(2.*pi*ws[len_xs-1])); + return ret; +} + + +double +levy13(const std::vector& xs) +{ + // The function is usually evaluated on the square xi ∈ [-10, 10], for all i = 1, 2 + double x1 = xs.at(0); + double x2 = xs.at(1); + + double ret = std::sin(3.*pi*x1) * std::sin(3.*pi*x1) \ + + (x1 - 1.) * (x1 - 1.) * (1. + std::sin(3.*pi*x2)*std::sin(3.*pi*x2)) \ + + (x2 - 1.) * (x2 - 1.) * (1. + std::sin(2.*pi*x2)*std::sin(2.*pi*x2)); + return ret; +} + + double six_hump_camel_back(const std::vector& xs) { diff --git a/bss.h b/bss.h index 33415d0..d6a269a 100644 --- a/bss.h +++ b/bss.h @@ -10,6 +10,8 @@ double cross_in_tray(const std::vector& xs); double eggholder(const std::vector& xs); double gramacy_lee(const std::vector& xs); double holder_table(const std::vector& xs); +double levy(const std::vector& xs); +double levy13(const std::vector& xs); double six_hump_camel_back(const std::vector& xs); double dejong5(const std::vector& xs); double deceptive3(const std::vector& xs); diff --git a/cbench.cc b/cbench.cc index 632c832..74f4c21 100644 --- a/cbench.cc +++ b/cbench.cc @@ -1,6 +1,7 @@ #include "Python.h" #include "bss.h" + namespace { PyObject * py_bench(PyObject *, PyObject *args, double (*fun)(const std::vector&)) @@ -38,7 +39,6 @@ py_bench(PyObject *, PyObject *args, double (*fun)(const std::vector&)) #define PYWRAP( name ) PyObject* py_ ## name (PyObject *self, PyObject *args){return py_bench(self, args, name);} #endif - PYWRAP(ackley); PYWRAP(alpine); PYWRAP(bukin_f6); @@ -46,6 +46,8 @@ PYWRAP(cross_in_tray); PYWRAP(eggholder); PYWRAP(gramacy_lee); PYWRAP(holder_table); +PYWRAP(levy); +PYWRAP(levy13); PYWRAP(six_hump_camel_back); PYWRAP(dejong5); PYWRAP(deceptive3); @@ -77,6 +79,8 @@ static PyMethodDef CBenchMethods[] = { {"eggholder", py_eggholder, METH_VARARGS, "eggholder function"}, {"gramacy_lee", py_gramacy_lee, METH_VARARGS, "Gramacy Lee function"}, {"holder_table", py_holder_table, METH_VARARGS, "holder table function"}, + {"levy", py_levy, METH_VARARGS, "levy function"}, + {"levy13", py_levy13, METH_VARARGS, "levy13 function"}, {"six_hump_camel_back", py_six_hump_camel_back, METH_VARARGS, "six_hump_camel_back function"}, {"dejong5", py_dejong5, METH_VARARGS, "dejong5 function"}, {"deceptive3", py_deceptive3, METH_VARARGS, "deceptive3 function"}, diff --git a/testit.sh b/testit.sh index f687b85..94609f5 100644 --- a/testit.sh +++ b/testit.sh @@ -12,6 +12,6 @@ for el in dir(cb): ' -echo "Timing test.." -python -m timeit -s 'from cbench import michalewicz; xs = [0.1, 0.2, 0.3, 0.4, 0.51]' 'michalewicz(xs)' +# echo "Timing test.." +# python -m timeit -s 'from cbench import michalewicz; xs = [0.1, 0.2, 0.3, 0.4, 0.51]' 'michalewicz(xs)'