proxsuite-nlp  0.10.0
A primal-dual augmented Lagrangian-type solver for nonlinear programming on manifolds.
Loading...
Searching...
No Matches
expose-cost.cpp
Go to the documentation of this file.
1
5
6#include "boost/python/operators.hpp"
7
8namespace proxsuite {
9namespace nlp {
10namespace python {
11
12using context::ConstMatrixRef;
13using context::ConstVectorRef;
14using context::Cost;
16using context::MatrixRef;
17using context::MatrixXs;
18using context::Scalar;
19using context::VectorRef;
20using context::VectorXs;
21
22struct CostWrapper : Cost, bp::wrapper<Cost> {
24
25 using Cost::Cost;
26
27 Scalar call(const ConstVectorRef &x) const { return get_override("call")(x); }
28 void computeGradient(const ConstVectorRef &x, VectorRef out) const {
29 get_override("computeGradient")(x, out);
30 }
31 void computeHessian(const ConstVectorRef &x, MatrixRef out) const {
32 get_override("computeHessian")(x, out);
33 }
34};
35
38
39void exposeCost() {
40 using CostPtr = shared_ptr<Cost>;
41 bp::register_ptr_to_python<CostPtr>();
42
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;
51
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.")
65 // define non-member operators
66 .def(
67 "__add__",
68 +[](CostPtr const &a, CostPtr const &b) {
69 return a + b;
70 }) // see cost_sum.hpp / returns CostSum<Scalar>
71 .def(
72 "__mul__", +[](CostPtr const &self, Scalar a) { return a * self; })
73 .def(
74 "__rmul__", +[](CostPtr const &self, Scalar a) { return a * self; })
75 // see cost_sum.hpp / returns CostSum<Scalar>
76 ;
77
78 bp::class_<func_to_cost<Scalar>, bp::bases<Cost>>(
79 "CostFromFunction",
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")));
83
84 using CostSum = CostSumTpl<Scalar>;
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.")
98 // expose inplace operators
99 .def(
100 "__iadd__", +[](CostSum &a, CostSum const &b) { return a += b; })
101 .def(
102 "__iadd__", +[](CostSum &a, CostPtr const &b) { return a += b; })
103 .def(
104 "__imul__", +[](CostSum &a, Scalar b) { return a *= b; })
105 // printing
106 .def(bp::self * Scalar())
107 .def(Scalar() * bp::self)
108 .def(-bp::self)
109 .def(bp::self_ns::str(bp::self));
110
112}
113
114} // namespace python
115} // namespace nlp
116} // namespace proxsuite
#define PROXSUITE_NLP_DYNAMIC_TYPEDEFS(Scalar)
Definition math.hpp:26
void exposeQuadraticCosts()
Expose specific cost functions.
Main package namespace.
Definition bcl-params.hpp:5
Base class for differentiable cost functions.
Definition fwd.hpp:89
Defines the sum of one or more cost functions .
Definition cost-sum.hpp:12
void computeHessian(const ConstVectorRef &x, MatrixRef out) const
Scalar call(const ConstVectorRef &x) const
void computeGradient(const ConstVectorRef &x, VectorRef out) const