Skip to content

Commit

Permalink
#2 adds Levy and Levy 13 functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kvoss committed Jun 9, 2021
1 parent b7f175d commit 1c6be20
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions bss.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,44 @@ holder_table(const std::vector<double>& xs)
}


double
levy(const std::vector<double>& 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<double> 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<double>& 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<double>& xs)
{
Expand Down
2 changes: 2 additions & 0 deletions bss.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ double cross_in_tray(const std::vector<double>& xs);
double eggholder(const std::vector<double>& xs);
double gramacy_lee(const std::vector<double>& xs);
double holder_table(const std::vector<double>& xs);
double levy(const std::vector<double>& xs);
double levy13(const std::vector<double>& xs);
double six_hump_camel_back(const std::vector<double>& xs);
double dejong5(const std::vector<double>& xs);
double deceptive3(const std::vector<double>& xs);
Expand Down
6 changes: 5 additions & 1 deletion cbench.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Python.h"
#include "bss.h"


namespace {
PyObject *
py_bench(PyObject *, PyObject *args, double (*fun)(const std::vector<double>&))
Expand Down Expand Up @@ -38,14 +39,15 @@ py_bench(PyObject *, PyObject *args, double (*fun)(const std::vector<double>&))
#define PYWRAP( name ) PyObject* py_ ## name (PyObject *self, PyObject *args){return py_bench(self, args, name);}
#endif


PYWRAP(ackley);
PYWRAP(alpine);
PYWRAP(bukin_f6);
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);
Expand Down Expand Up @@ -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"},
Expand Down
4 changes: 2 additions & 2 deletions testit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)'

0 comments on commit 1c6be20

Please sign in to comment.