proxsuite-nlp  0.10.0
A primal-dual augmented Lagrangian-type solver for nonlinear programming on manifolds.
Loading...
Searching...
No Matches
expose-constraint.cpp
Go to the documentation of this file.
1
3
5
6#include <eigenpy/std-vector.hpp>
7
8#include <vector>
9
10namespace proxsuite {
11namespace nlp {
12namespace python {
13
17using context::ConstVectorRef;
18using context::Scalar;
19using eigenpy::StdVectorPythonVisitor;
23
24template <typename T>
25auto exposeSpecificConstraintSet(const char *name, const char *docstring) {
26 return bp::class_<T, bp::bases<ConstraintSet>>(name, docstring, bp::no_init)
28}
29
30template <typename ConstraintType>
31context::Constraint make_constraint(const shared_ptr<context::C2Function> &f) {
32 return context::Constraint(f, ConstraintType{});
33}
34
35static void exposeConstraintTypes();
36
39
41 auto cls =
42 bp::class_<ConstraintSet, boost::noncopyable>(
43 "ConstraintSet",
44 "Base class for constraint sets or nonsmooth penalties.", bp::no_init)
45 .def(
46 "evaluate", &ConstraintSet::evaluate, ("self"_a, "z"),
47 "Evaluate the constraint indicator function or nonsmooth penalty "
48 "on the projection/prox map of :math:`z`.")
49 .def("projection", &ConstraintSet::projection,
50 ("self"_a, "z", "zout"))
51 .def(
52 "projection",
53 +[](const ConstraintSet &c, const ConstVectorRef &z) {
54 context::VectorXs zout(z.size());
55 c.projection(z, zout);
56 return zout;
57 },
58 ("self"_a, "z"))
59 .def("normalConeProjection", &ConstraintSet::normalConeProjection,
60 ("self"_a, "z", "zout"))
61 .def(
62 "normalConeProjection",
63 +[](const ConstraintSet &c, const ConstVectorRef &z) {
64 context::VectorXs zout(z.size());
65 c.normalConeProjection(z, zout);
66 return zout;
67 },
68 ("self"_a, "z"))
69 .def("applyProjectionJacobian",
70 &ConstraintSet::applyProjectionJacobian, ("self"_a, "z", "Jout"),
71 "Apply the projection Jacobian.")
72 .def("applyNormalProjectionJacobian",
73 &ConstraintSet::applyNormalConeProjectionJacobian,
74 ("self"_a, "z", "Jout"),
75 "Apply the normal cone projection Jacobian.")
76 .def("computeActiveSet", &ConstraintSet::computeActiveSet,
77 ("self"_a, "z", "out"))
78 .def("evaluateMoreauEnvelope", &ConstraintSet::evaluateMoreauEnvelope,
79 ("self"_a, "zin", "zproj"),
80 "Evaluate the Moreau envelope with parameter :math:`\\mu`.")
81 .def("setProxParameter", &ConstraintSet::setProxParameter,
82 ("self"_a, "mu"), "Set proximal parameter.")
83 .add_property("mu", &ConstraintSet::mu, "Current proximal parameter.")
84 .def(bp::self == bp::self);
85 bp::scope current_scope;
86 current_scope.attr("ConstraintBase") = cls;
87
88 bp::class_<Constraint>(
89 "ConstraintObject", "Packs a constraint set together with a function.",
90 bp::init<shared_ptr<C2Function>, const polymorphic<ConstraintSet> &>(
91 ("self"_a, "func", "constraint_set")))
92 .add_property("nr", &Constraint::nr, "Constraint dimension.")
93 .def_readonly("func", &Constraint::func_, "Underlying function.")
94 .def_readonly("set", &Constraint::set_, "Constraint set.");
95
96 StdVectorPythonVisitor<std::vector<context::Constraint>, true>::expose(
97 "StdVec_ConstraintObject");
98
100}
101
104 "EqualityConstraintSet", "Cast a function into an equality constraint")
105 .def(bp::init<>("self"_a));
106
108 "NegativeOrthant",
109 "Cast a function into a negative inequality constraint h(x) \\leq 0")
110 .def(bp::init<>("self"_a));
111
113 "BoxConstraint",
114 "Box constraint of the form :math:`z \\in [z_\\min, z_\\max]`.")
115 .def(bp::init<context::ConstVectorRef, context::ConstVectorRef>(
116 ("self"_a, "lower_limit", "upper_limit")))
117 .def_readwrite("upper_limit", &BoxConstraint::upper_limit)
118 .def_readwrite("lower_limit", &BoxConstraint::lower_limit);
119
120 bp::def("createEqualityConstraint",
122 "Convenience function to create an equality constraint from a "
123 "C2Function.");
124 bp::def("createInequalityConstraint",
126 "Convenience function to create an inequality constraint from a "
127 "C2Function.");
128
129 exposeSpecificConstraintSet<L1Penalty>("NonsmoothPenaltyL1",
130 "1-norm penalty function.")
131 .def(bp::init<>(("self"_a)));
132
134 "ConstraintSetProduct", "Cartesian product of constraint sets.")
135 .def(bp::init<std::vector<polymorphic<ConstraintSet>>,
136 std::vector<Eigen::Index>>(
137 ("self"_a, "components", "blockSizes")))
138 .add_property("components",
139 bp::make_function(&ConstraintSetProduct::components,
140 bp::return_internal_reference<>()))
141 .add_property("blockSizes",
142 bp::make_function(&ConstraintSetProduct::blockSizes,
143 bp::return_internal_reference<>()),
144 "Dimensions of each component of the cartesian product.");
145
146 StdVectorPythonVisitor<std::vector<polymorphic<ConstraintSet>>>::expose(
147 "StdVec_ConstraintObject",
148 eigenpy::details::overload_base_get_item_for_std_vector<
149 std::vector<polymorphic<ConstraintSet>>>());
150}
151
152} // namespace python
153} // namespace nlp
154} // namespace proxsuite
ConstraintObjectTpl< Scalar > Constraint
Definition context.hpp:19
void register_polymorphic_to_python()
Expose a polymorphic value type, e.g. xyz::polymorphic<T, A>.
context::Constraint make_constraint(const shared_ptr< context::C2Function > &f)
auto exposeSpecificConstraintSet(const char *name, const char *docstring)
Main package namespace.
Definition bcl-params.hpp:5
Twice-differentiable function, with method Jacobian and vector-hessian product evaluation.
Definition fwd.hpp:76
Packs a ConstraintSetTpl and C2FunctionTpl together.
Definition fwd.hpp:112
Cartesian product of multiple constraint sets. This class makes computing multipliers and Jacobian ma...
const std::vector< Eigen::Index > & blockSizes() const
const std::vector< xyz::polymorphic< Base > > & components() const
Base constraint set type.
Definition fwd.hpp:104
Negative orthant, for constraints .
Composite -penalty function .