Skip to content

Commit

Permalink
Merge pull request #52 from darioizzo/pr/only_docs
Browse files Browse the repository at this point in the history
Pr/only docs
  • Loading branch information
darioizzo authored Nov 14, 2019
2 parents 48bcb52 + 74128f7 commit 3f93e3d
Show file tree
Hide file tree
Showing 42 changed files with 1,770 additions and 411 deletions.
20 changes: 20 additions & 0 deletions dcgpy/common_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,26 @@ inline std::vector<std::vector<double>> to_vv<double>(const bp::object &o)
+ "' to a vector of vector_double: only lists of doubles and NumPy arrays of doubles are supported");
}

// Convert a vector of arithmetic types into a 1D numpy array.
template <typename T>
inline bp::object vector_to_ndarr(const std::vector<T> &v)
{
// The dimensions of the array to be created.
npy_intp dims[] = {boost::numeric_cast<npy_intp>(v.size())};
// Attempt creating the array.
PyObject *ret = PyArray_SimpleNew(1, dims, cpp_npy<T>::value);
if (!ret) {
dcgpy_throw(PyExc_RuntimeError, "couldn't create a NumPy array: the 'PyArray_SimpleNew()' function failed");
}
// Hand over to BP for exception-safe behaviour.
bp::object retval{bp::handle<>(ret)};
if (v.size()) {
// Copy over the data.
std::copy(v.begin(), v.end(), static_cast<T *>(PyArray_DATA(reinterpret_cast<PyArrayObject *>(ret))));
}
return retval;
}

// Convert a vector of vectors of arithmetic types into a 2D numpy array.
template <typename T>
inline bp::object vvector_to_ndarr(const std::vector<std::vector<T>> &v)
Expand Down
18 changes: 17 additions & 1 deletion dcgpy/docstrings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ case, aside the Mean Squared Error, the model complexity will be considered as a

std::string symbolic_regression_init_doc()
{
return R"(__init__(points, labels, rows, columns, levels_back, arity, kernels, n_eph, multi_objective, parallel_batches=0)
return R"(__init__(points, labels, rows = 1, columns=16, levels_back=17, arity=2, kernels, n_eph=0, multi_objective=False, parallel_batches=0)
Constructs a symbolic_regression optimization problem compatible with the pagmo UDP interface.
Expand Down Expand Up @@ -1127,6 +1127,22 @@ Constructs a symbolic_regression optimization problem compatible with the pagmo
)";
}

std::string symbolic_regression_predict_doc()
{
return R"(predict(points, chromosome)
Predicts the labels of *points* using the model encoded in *x*.
Args:
points (2D NumPy float array or ``list of lists`` of ``float`` or the 1D equivalents): the input point / points
chromosome (1D NumPy array or ``list`` of ``int``): the encoded model.
Raises:
unspecified: any exception thrown by failures at the intersection between C++ and Python (e.g.,
type conversion errors, mismatched function signatures, etc.)
)";
}

std::string generic_uda_get_seed_doc()
{
return R"(get_seed()
Expand Down
2 changes: 2 additions & 0 deletions dcgpy/docstrings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ std::string expression_ann_sgd_doc();
// UDPs
std::string symbolic_regression_doc();
std::string symbolic_regression_init_doc();
std::string symbolic_regression_predict_doc();

// UDAs
std::string generic_uda_get_seed_doc();
std::string es4cgp_doc();
Expand Down
23 changes: 17 additions & 6 deletions dcgpy/expose_symbolic_regression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,28 @@ void expose_symbolic_regression()
kernels_v, n_eph, multi_objective, parallel_batches);
},
bp::default_call_policies(),
(bp::arg("points"), bp::arg("labels"), bp::arg("rows"), bp::arg("cols"), bp::arg("levels_back"),
bp::arg("arity"), bp::arg("kernels"), bp::arg("n_eph"), bp::arg("multi_objective"),
bp::arg("parallel_batches") = 0u)),
(bp::arg("points"), bp::arg("labels"), bp::arg("rows") = 1, bp::arg("cols") = 16,
bp::arg("levels_back") = 17, bp::arg("arity") = 2, bp::arg("kernels"), bp::arg("n_eph") = 0u,
bp::arg("multi_objective") = false, bp::arg("parallel_batches") = 0u)),
symbolic_regression_init_doc().c_str())
.def(
"pretty", +[](const dcgp::symbolic_regression &instance,
const bp::object &x) { return instance.pretty(to_v<double>(x)); })
.def(
"prettier", +[](const dcgp::symbolic_regression &instance, const bp::object &x) {
return instance.prettier(to_v<double>(x));
});
"prettier", +[](const dcgp::symbolic_regression &instance,
const bp::object &x) { return instance.prettier(to_v<double>(x)); })
.def(
"predict",
+[](const dcgp::symbolic_regression &instance, const bp::object &points, const bp::object &x) {
try { //
return vvector_to_ndarr(instance.predict(to_vv<double>(points), to_v<double>(x)));
} catch (...) {
PyErr_Clear();
return vector_to_ndarr(instance.predict(to_v<double>(points), to_v<double>(x)));
}
},
(bp::arg("points"), bp::arg("chromosome")), symbolic_regression_predict_doc().c_str())
.def("__repr__", &dcgp::symbolic_regression::get_extra_info);

// We expose the UDAs
// ES-4CGP (Evolutionary Strategy for Cartesian Genetic Programming)
Expand Down
Loading

0 comments on commit 3f93e3d

Please sign in to comment.