GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bindings/python/crocoddyl/core/solvers/kkt.cpp Lines: 41 42 97.6 %
Date: 2024-02-13 11:12:33 Branches: 28 56 50.0 %

Line Branch Exec Source
1
///////////////////////////////////////////////////////////////////////////////
2
// BSD 3-Clause License
3
//
4
// Copyright (C) 2020-2023, LAAS-CNRS, Heriot-Watt University
5
// Copyright note valid unless otherwise stated in individual files.
6
// All rights reserved.
7
///////////////////////////////////////////////////////////////////////////////
8
9
#include "crocoddyl/core/solvers/kkt.hpp"
10
11
#include "python/crocoddyl/core/core.hpp"
12
#include "python/crocoddyl/utils/copyable.hpp"
13
14
namespace crocoddyl {
15
namespace python {
16
17
20
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(SolverKKT_solves, SolverKKT::solve, 0, 5)
18
10
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(SolverKKT_computeDirections,
19
                                       SolverKKT::computeDirection, 0, 1)
20
20
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(SolverKKT_trySteps, SolverKKT::tryStep,
21
                                       0, 1)
22
23
10
void exposeSolverKKT() {
24
10
  bp::register_ptr_to_python<boost::shared_ptr<SolverKKT> >();
25
26
10
  bp::class_<SolverKKT, bp::bases<SolverAbstract> >(
27
      "SolverKKT",
28
      "KKT solver.\n\n"
29
      "The KKT solver computes a primal and dual optimal by inverting\n"
30
      "the kkt matrix \n"
31
      ":param shootingProblem: shooting problem (list of action models along "
32
      "trajectory.)",
33
10
      bp::init<boost::shared_ptr<ShootingProblem> >(
34
20
          bp::args("self", "problem"),
35
          "Initialize the vector dimension.\n\n"
36
          ":param problem: shooting problem."))
37
      .def("solve", &SolverKKT::solve,
38
10
           SolverKKT_solves(
39
20
               bp::args("self", "init_xs", "init_us", "maxiter", "isFeasible",
40
                        "regInit"),
41
               "Compute the optimal primal(xopt, uopt) and dual(Vx) terms.\n\n"
42
               ":param init_xs: initial guess for state trajectory with T+1 "
43
               "elements (default []).\n"
44
               ":param init_us: initial guess for control trajectory with T "
45
               "elements (default []) (default []).\n"
46
               ":param maxiter: maximun allowed number of iterations (default "
47
               "100).\n"
48
               ":param isFeasible: true if the init_xs are obtained from "
49
               "integrating the init_us (rollout) (default "
50
               "False).\n"
51
               ":param regInit: initial guess for the regularization value. "
52
               "Very low values are typical\n"
53
               "                used with very good guess points (init_xs, "
54
               "init_us) (default None).\n"
55
               ":returns the optimal trajectory xopt, uopt and a boolean that "
56
10
               "describes if convergence was reached."))
57
      .def("computeDirection", &SolverKKT::computeDirection,
58
10
           SolverKKT_computeDirections(
59
20
               bp::args("self", "recalc"),
60
               "Compute the search direction (dx, du), lambdas for the current "
61
               "guess (xs, us).\n\n"
62
               "You must call setCandidate first in order to define the "
63
               "current\n"
64
               "guess. A current guess defines a state and control trajectory\n"
65
               "(xs, us) of T+1 and T elements, respectively.\n"
66
               ":params recalc: true for recalculating the derivatives at "
67
               "current state and control.\n"
68
               ":returns the search direction dx, du and the dual lambdas as "
69
10
               "lists of T+1, T and T+1 lengths."))
70
      .def("tryStep", &SolverKKT::tryStep,
71
10
           SolverKKT_trySteps(
72
20
               bp::args("self", " stepLength=1"),
73
               "Rollout the system with a predefined step length.\n\n"
74
               ":param stepLength: step length\n"
75
10
               ":returns the cost improvement."))
76
20
      .def("stoppingCriteria", &SolverKKT::stoppingCriteria, bp::args("self"),
77
           "Return a sum of positive parameters whose sum quantifies the DDP "
78
10
           "termination.")
79
      .def("expectedImprovement", &SolverKKT::expectedImprovement,
80
           bp::return_value_policy<bp::copy_const_reference>(),
81
20
           bp::args("self"),
82
           "Return two scalars denoting the quadratic improvement model\n\n"
83
           "For computing the expected improvement, you need to compute first\n"
84
           "the search direction by running computeDirection. The quadratic\n"
85
           "improvement model is described as dV = f_0 - f_+ = d1*a + "
86
10
           "d2*a**2/2.")
87
      .add_property(
88
          "kkt",
89
10
          make_function(&SolverKKT::get_kkt,
90
10
                        bp::return_value_policy<bp::copy_const_reference>()),
91
10
          "kkt")
92
      .add_property(
93
          "kktref",
94
10
          make_function(&SolverKKT::get_kktref,
95
10
                        bp::return_value_policy<bp::copy_const_reference>()),
96
10
          "kktref")
97
      .add_property(
98
          "primaldual",
99
10
          make_function(&SolverKKT::get_primaldual,
100
10
                        bp::return_value_policy<bp::copy_const_reference>()),
101
10
          "primaldual")
102
      .add_property(
103
          "lambdas",
104
10
          make_function(&SolverKKT::get_lambdas,
105
10
                        bp::return_value_policy<bp::copy_const_reference>()),
106
10
          "lambdas")
107
      .add_property(
108
          "dxs",
109
10
          make_function(&SolverKKT::get_dxs,
110
10
                        bp::return_value_policy<bp::copy_const_reference>()),
111
10
          "dxs")
112
      .add_property(
113
          "dus",
114
10
          make_function(&SolverKKT::get_dus,
115
10
                        bp::return_value_policy<bp::copy_const_reference>()),
116
10
          "dus")
117
10
      .def(CopyableVisitor<SolverKKT>());
118
10
}
119
120
}  // namespace python
121
}  // namespace crocoddyl