40 using CostPtr = shared_ptr<Cost>;
41 bp::register_ptr_to_python<CostPtr>();
43 void (Cost::*compGrad1)(
const ConstVectorRef &, VectorRef)
const =
44 &Cost::computeGradient;
45 void (Cost::*compHess1)(
const ConstVectorRef &, MatrixRef)
const =
46 &Cost::computeHessian;
47 VectorXs (Cost::*compGrad2)(
const ConstVectorRef &)
const =
48 &Cost::computeGradient;
49 MatrixXs (Cost::*compHess2)(
const ConstVectorRef &)
const =
50 &Cost::computeHessian;
52 bp::class_<CostWrapper, bp::bases<context::C2Function>, boost::noncopyable>(
53 "CostFunctionBase", bp::no_init)
54 .def(bp::init<int, int>(bp::args(
"self",
"nx",
"ndx")))
55 .def(bp::init<const Manifold &>(bp::args(
"self",
"manifold")))
56 .def(
"call", bp::pure_virtual(&Cost::call), bp::args(
"self",
"x"))
57 .def(
"computeGradient", bp::pure_virtual(compGrad1),
58 bp::args(
"self",
"x",
"gout"))
59 .def(
"computeGradient", compGrad2, bp::args(
"self",
"x"),
60 "Compute and return the gradient.")
61 .def(
"computeHessian", bp::pure_virtual(compHess1),
62 bp::args(
"self",
"x",
"Hout"))
63 .def(
"computeHessian", compHess2, bp::args(
"self",
"x"),
64 "Compute and return the Hessian.")
68 +[](CostPtr
const &a, CostPtr
const &b) {
72 "__mul__", +[](CostPtr
const &self, Scalar a) {
return a * self; })
74 "__rmul__", +[](CostPtr
const &self, Scalar a) {
return a * self; })
78 bp::class_<func_to_cost<Scalar>, bp::bases<Cost>>(
80 "Wrap a scalar-values C2 function into a cost function.", bp::no_init)
81 .def(bp::init<
const shared_ptr<context::C2Function> &>(
82 bp::args(
"self",
"func")));
85 bp::register_ptr_to_python<shared_ptr<CostSum>>();
86 bp::class_<CostSum, bp::bases<Cost>>(
87 "CostSum",
"Sum of cost functions.",
88 bp::init<int, int, const std::vector<CostSum::BasePtr> &,
89 const std::vector<Scalar> &>(
90 bp::args(
"self",
"nx",
"ndx",
"components",
"weights")))
91 .def(bp::init<int, int>(bp::args(
"self",
"nx",
"ndx")))
92 .add_property(
"num_components", &CostSum::numComponents,
93 "Number of components.")
94 .def_readonly(
"weights", &CostSum::weights_)
95 .def(
"add_component", &CostSum::addComponent,
96 ((bp::arg(
"self"), bp::arg(
"comp"), bp::arg(
"w") = 1.)),
97 "Add a component to the cost.")
100 "__iadd__", +[](CostSum &a, CostSum
const &b) {
return a += b; })
102 "__iadd__", +[](CostSum &a, CostPtr
const &b) {
return a += b; })
104 "__imul__", +[](CostSum &a, Scalar b) {
return a *= b; })
106 .def(bp::self * Scalar())
107 .def(Scalar() * bp::self)
109 .def(bp::self_ns::str(bp::self));