-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbind_solver.cpp
More file actions
664 lines (593 loc) · 27 KB
/
bind_solver.cpp
File metadata and controls
664 lines (593 loc) · 27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
#include <array>
#include <sstream>
#include <string_view>
#include <pybind11/eigen.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "cddp_core/cddp_core.hpp"
namespace py = pybind11;
namespace {
template <typename Func>
auto callWrapped(bool requires_gil, Func &&func) -> decltype(func()) {
if (requires_gil) {
py::gil_scoped_acquire gil;
return func();
}
return func();
}
template <std::size_t N>
bool isExactCoreType(const py::handle &object,
const std::array<std::string_view, N> &type_names) {
const py::type object_type = py::type::of(object);
const std::string module = py::str(object_type.attr("__module__"));
if (module != "pycddp._pycddp_core") {
return false;
}
const std::string name = py::str(object_type.attr("__name__"));
for (std::string_view type_name : type_names) {
if (name == type_name) {
return true;
}
}
return false;
}
bool nativeDynamicsCanSkipGil(const py::handle &object) {
static constexpr std::array<std::string_view, 23> kNativeDynamicsTypes = {
"Pendulum", "Unicycle", "Bicycle",
"Car", "CartPole", "DubinsCar",
"Forklift", "Acrobot", "Quadrotor",
"QuadrotorRate", "Manipulator", "HCW",
"SpacecraftLinearFuel", "SpacecraftNonlinear",
"DreyfusRocket", "SpacecraftLanding2D",
"SpacecraftTwobody",
"LTISystem", "Usv3Dof", "EulerAttitude",
"QuaternionAttitude", "MrpAttitude",
};
return isExactCoreType(object, kNativeDynamicsTypes);
}
bool nativeObjectiveCanSkipGil(const py::handle &object) {
static constexpr std::array<std::string_view, 1> kNativeObjectiveTypes = {
"QuadraticObjective",
};
return isExactCoreType(object, kNativeObjectiveTypes);
}
bool nativeConstraintCanSkipGil(const py::handle &object) {
static constexpr std::array<std::string_view, 8> kNativeConstraintTypes = {
"ControlConstraint", "StateConstraint",
"LinearConstraint", "BallConstraint",
"PoleConstraint", "SecondOrderConeConstraint",
"ThrustMagnitudeConstraint", "MaxThrustMagnitudeConstraint",
};
return isExactCoreType(object, kNativeConstraintTypes);
}
bool isExactConstraintBase(const py::handle &object) {
static constexpr std::array<std::string_view, 1> kConstraintBaseTypes = {
"Constraint",
};
return isExactCoreType(object, kConstraintBaseTypes);
}
bool isExactDynamicsBase(const py::handle &object) {
static constexpr std::array<std::string_view, 1> kDynamicsBaseTypes = {
"DynamicalSystem",
};
return isExactCoreType(object, kDynamicsBaseTypes);
}
bool isExactObjectiveBase(const py::handle &object) {
static constexpr std::array<std::string_view, 1> kObjectiveBaseTypes = {
"Objective",
};
return isExactCoreType(object, kObjectiveBaseTypes);
}
bool isKnownSolverName(const std::string &solver_name) {
return solver_name == "CLDDP" || solver_name == "CLCDDP" ||
solver_name == "LogDDP" || solver_name == "LOGDDP" ||
solver_name == "IPDDP" || solver_name == "MSIPDDP" ||
solver_name == "ALDDP" ||
cddp::CDDP::isSolverRegistered(solver_name);
}
bool isUnknownSolverStatus(const std::string &status_message) {
return status_message.rfind("UnknownSolver", 0) == 0;
}
void validateInitialTrajectory(cddp::CDDP &solver,
const std::vector<Eigen::VectorXd> &X,
const std::vector<Eigen::VectorXd> &U) {
int state_dim = 0;
int control_dim = 0;
try {
state_dim = solver.getStateDim();
control_dim = solver.getControlDim();
} catch (const std::exception &e) {
throw py::value_error(
std::string("set_initial_trajectory failed while querying dimensions "
"(is a dynamical system set?): ") + e.what());
}
const std::size_t expected_state_count =
static_cast<std::size_t>(solver.getHorizon() + 1);
const std::size_t expected_control_count =
static_cast<std::size_t>(solver.getHorizon());
if (X.size() != expected_state_count || U.size() != expected_control_count) {
std::ostringstream stream;
stream << "set_initial_trajectory expected X length "
<< expected_state_count << " and U length "
<< expected_control_count << ", got X length " << X.size()
<< " and U length " << U.size() << ".";
throw py::value_error(stream.str());
}
for (std::size_t i = 0; i < X.size(); ++i) {
if (X[i].size() != state_dim) {
std::ostringstream stream;
stream << "set_initial_trajectory expected state vector " << i
<< " to have dimension " << state_dim << ", got "
<< X[i].size() << ".";
throw py::value_error(stream.str());
}
}
for (std::size_t i = 0; i < U.size(); ++i) {
if (U[i].size() != control_dim) {
std::ostringstream stream;
stream << "set_initial_trajectory expected control vector " << i
<< " to have dimension " << control_dim << ", got "
<< U[i].size() << ".";
throw py::value_error(stream.str());
}
}
}
// These wrappers let the C++ solver hold Python-defined solver objects without
// adopting the Python object's raw allocation. The py::object keeps the
// original Python instance alive, the raw pointer only forwards virtual calls,
// and each forwarded method only reacquires the GIL when the object is truly
// Python-backed. Native extension types can stay on the fast path.
class PythonBackedDynamicalSystem : public cddp::DynamicalSystem {
public:
PythonBackedDynamicalSystem(py::object owner, cddp::DynamicalSystem *wrapped,
bool requires_gil)
: cddp::DynamicalSystem(wrapped->getStateDim(), wrapped->getControlDim(),
wrapped->getTimestep(),
wrapped->getIntegrationType()),
owner_(std::move(owner)), wrapped_(wrapped),
requires_gil_(requires_gil) {}
~PythonBackedDynamicalSystem() override {
try {
if (Py_IsInitialized()) {
py::gil_scoped_acquire gil;
owner_.release().dec_ref();
}
} catch (...) {}
}
Eigen::VectorXd getContinuousDynamics(const Eigen::VectorXd &state,
const Eigen::VectorXd &control,
double time) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->getContinuousDynamics(state, control, time);
});
}
cddp::VectorXdual2nd
getContinuousDynamicsAutodiff(const cddp::VectorXdual2nd &state,
const cddp::VectorXdual2nd &control,
double time) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->getContinuousDynamicsAutodiff(state, control, time);
});
}
Eigen::VectorXd getDiscreteDynamics(const Eigen::VectorXd &state,
const Eigen::VectorXd &control,
double time) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->getDiscreteDynamics(state, control, time);
});
}
Eigen::MatrixXd getStateJacobian(const Eigen::VectorXd &state,
const Eigen::VectorXd &control,
double time) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->getStateJacobian(state, control, time);
});
}
Eigen::MatrixXd getControlJacobian(const Eigen::VectorXd &state,
const Eigen::VectorXd &control,
double time) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->getControlJacobian(state, control, time);
});
}
std::vector<Eigen::MatrixXd> getStateHessian(const Eigen::VectorXd &state,
const Eigen::VectorXd &control,
double time) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->getStateHessian(state, control, time);
});
}
std::vector<Eigen::MatrixXd>
getControlHessian(const Eigen::VectorXd &state,
const Eigen::VectorXd &control,
double time) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->getControlHessian(state, control, time);
});
}
std::vector<Eigen::MatrixXd> getCrossHessian(const Eigen::VectorXd &state,
const Eigen::VectorXd &control,
double time) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->getCrossHessian(state, control, time);
});
}
private:
py::object owner_;
cddp::DynamicalSystem *wrapped_;
bool requires_gil_;
};
class PythonBackedObjective : public cddp::Objective {
public:
PythonBackedObjective(py::object owner, cddp::Objective *wrapped,
bool requires_gil)
: owner_(std::move(owner)), wrapped_(wrapped),
requires_gil_(requires_gil) {}
~PythonBackedObjective() override {
try {
if (Py_IsInitialized()) {
py::gil_scoped_acquire gil;
owner_.release().dec_ref();
}
} catch (...) {}
}
double evaluate(const std::vector<Eigen::VectorXd> &states,
const std::vector<Eigen::VectorXd> &controls) const override {
return callWrapped(requires_gil_,
[&] { return wrapped_->evaluate(states, controls); });
}
double running_cost(const Eigen::VectorXd &state,
const Eigen::VectorXd &control,
int index) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->running_cost(state, control, index);
});
}
double terminal_cost(const Eigen::VectorXd &final_state) const override {
return callWrapped(requires_gil_,
[&] { return wrapped_->terminal_cost(final_state); });
}
Eigen::VectorXd
getRunningCostStateGradient(const Eigen::VectorXd &state,
const Eigen::VectorXd &control,
int index) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->getRunningCostStateGradient(state, control, index);
});
}
Eigen::VectorXd
getRunningCostControlGradient(const Eigen::VectorXd &state,
const Eigen::VectorXd &control,
int index) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->getRunningCostControlGradient(state, control,
index);
});
}
Eigen::VectorXd
getFinalCostGradient(const Eigen::VectorXd &final_state) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->getFinalCostGradient(final_state);
});
}
Eigen::MatrixXd
getRunningCostStateHessian(const Eigen::VectorXd &state,
const Eigen::VectorXd &control,
int index) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->getRunningCostStateHessian(state, control, index);
});
}
Eigen::MatrixXd
getRunningCostControlHessian(const Eigen::VectorXd &state,
const Eigen::VectorXd &control,
int index) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->getRunningCostControlHessian(state, control,
index);
});
}
Eigen::MatrixXd
getRunningCostCrossHessian(const Eigen::VectorXd &state,
const Eigen::VectorXd &control,
int index) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->getRunningCostCrossHessian(state, control, index);
});
}
Eigen::MatrixXd
getFinalCostHessian(const Eigen::VectorXd &final_state) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->getFinalCostHessian(final_state);
});
}
Eigen::VectorXd getReferenceState() const override {
return callWrapped(requires_gil_,
[&] { return wrapped_->getReferenceState(); });
}
std::vector<Eigen::VectorXd> getReferenceStates() const override {
return callWrapped(requires_gil_,
[&] { return wrapped_->getReferenceStates(); });
}
void setReferenceState(const Eigen::VectorXd &reference_state) override {
callWrapped(requires_gil_,
[&] { wrapped_->setReferenceState(reference_state); });
}
void setReferenceStates(
const std::vector<Eigen::VectorXd> &reference_states) override {
callWrapped(requires_gil_,
[&] { wrapped_->setReferenceStates(reference_states); });
}
private:
py::object owner_;
cddp::Objective *wrapped_;
bool requires_gil_;
};
class PythonBackedConstraint : public cddp::Constraint {
public:
PythonBackedConstraint(py::object owner, cddp::Constraint *wrapped,
bool requires_gil)
: cddp::Constraint(wrapped->getName()), owner_(std::move(owner)),
wrapped_(wrapped), requires_gil_(requires_gil) {}
~PythonBackedConstraint() override {
try {
if (Py_IsInitialized()) {
py::gil_scoped_acquire gil;
owner_.release().dec_ref();
}
} catch (...) {}
}
int getDualDim() const override {
return callWrapped(requires_gil_, [&] { return wrapped_->getDualDim(); });
}
Eigen::VectorXd evaluate(const Eigen::VectorXd &state,
const Eigen::VectorXd &control,
int index) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->evaluate(state, control, index);
});
}
Eigen::VectorXd getLowerBound() const override {
return callWrapped(requires_gil_,
[&] { return wrapped_->getLowerBound(); });
}
Eigen::VectorXd getUpperBound() const override {
return callWrapped(requires_gil_,
[&] { return wrapped_->getUpperBound(); });
}
Eigen::MatrixXd getStateJacobian(const Eigen::VectorXd &state,
const Eigen::VectorXd &control,
int index) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->getStateJacobian(state, control, index);
});
}
Eigen::MatrixXd getControlJacobian(const Eigen::VectorXd &state,
const Eigen::VectorXd &control,
int index) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->getControlJacobian(state, control, index);
});
}
double computeViolation(const Eigen::VectorXd &state,
const Eigen::VectorXd &control,
int index) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->computeViolation(state, control, index);
});
}
double computeViolationFromValue(const Eigen::VectorXd &g) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->computeViolationFromValue(g);
});
}
Eigen::VectorXd getCenter() const override {
return callWrapped(requires_gil_, [&] { return wrapped_->getCenter(); });
}
std::vector<Eigen::MatrixXd> getStateHessian(const Eigen::VectorXd &state,
const Eigen::VectorXd &control,
int index) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->getStateHessian(state, control, index);
});
}
std::vector<Eigen::MatrixXd>
getControlHessian(const Eigen::VectorXd &state,
const Eigen::VectorXd &control,
int index) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->getControlHessian(state, control, index);
});
}
std::vector<Eigen::MatrixXd> getCrossHessian(const Eigen::VectorXd &state,
const Eigen::VectorXd &control,
int index) const override {
return callWrapped(requires_gil_, [&] {
return wrapped_->getCrossHessian(state, control, index);
});
}
private:
py::object owner_;
cddp::Constraint *wrapped_;
bool requires_gil_;
};
std::unique_ptr<cddp::DynamicalSystem> makeOwnedDynamicalSystem(
py::object system) {
if (isExactDynamicsBase(system)) {
throw py::type_error(
"pycddp.DynamicalSystem is an abstract base class. "
"Pass a concrete built-in model or a Python subclass that "
"implements the required methods.");
}
auto *wrapped = system.cast<cddp::DynamicalSystem *>();
const bool requires_gil = !nativeDynamicsCanSkipGil(system);
return std::make_unique<PythonBackedDynamicalSystem>(
std::move(system), wrapped, requires_gil);
}
std::unique_ptr<cddp::Objective> makeOwnedObjective(py::object objective) {
if (isExactObjectiveBase(objective)) {
throw py::type_error(
"pycddp.Objective is an abstract base class. "
"Pass a concrete built-in objective or a Python subclass that "
"implements the required methods.");
}
auto *wrapped = objective.cast<cddp::Objective *>();
const bool requires_gil = !nativeObjectiveCanSkipGil(objective);
return std::make_unique<PythonBackedObjective>(
std::move(objective), wrapped, requires_gil);
}
std::unique_ptr<cddp::Constraint> makeOwnedConstraint(py::object constraint) {
if (isExactConstraintBase(constraint)) {
throw py::type_error(
"pycddp.Constraint is an abstract base class. "
"Pass a concrete built-in constraint or a Python subclass that "
"implements the required methods.");
}
auto *wrapped = constraint.cast<cddp::Constraint *>();
const bool requires_gil = !nativeConstraintCanSkipGil(constraint);
return std::make_unique<PythonBackedConstraint>(
std::move(constraint), wrapped, requires_gil);
}
} // namespace
void bind_solver(py::module_ &m) {
py::class_<cddp::CDDPSolution::History>(m, "SolutionHistory")
.def_readonly("objective", &cddp::CDDPSolution::History::objective)
.def_readonly("merit_function",
&cddp::CDDPSolution::History::merit_function)
.def_readonly("step_length_primal",
&cddp::CDDPSolution::History::step_length_primal)
.def_readonly("step_length_dual",
&cddp::CDDPSolution::History::step_length_dual)
.def_readonly("dual_infeasibility",
&cddp::CDDPSolution::History::dual_infeasibility)
.def_readonly("primal_infeasibility",
&cddp::CDDPSolution::History::primal_infeasibility)
.def_readonly("complementary_infeasibility",
&cddp::CDDPSolution::History::complementary_infeasibility)
.def_readonly("barrier_mu", &cddp::CDDPSolution::History::barrier_mu)
.def_readonly("regularization",
&cddp::CDDPSolution::History::regularization);
py::class_<cddp::CDDPSolution>(m, "CDDPSolution")
.def_readonly("solver_name", &cddp::CDDPSolution::solver_name)
.def_readonly("status_message", &cddp::CDDPSolution::status_message)
.def_readonly("iterations_completed",
&cddp::CDDPSolution::iterations_completed)
.def_readonly("solve_time_ms", &cddp::CDDPSolution::solve_time_ms)
.def_readonly("final_objective", &cddp::CDDPSolution::final_objective)
.def_readonly("final_step_length",
&cddp::CDDPSolution::final_step_length)
.def_readonly("final_regularization",
&cddp::CDDPSolution::final_regularization)
.def_readonly("time_points", &cddp::CDDPSolution::time_points)
.def_readonly("state_trajectory",
&cddp::CDDPSolution::state_trajectory)
.def_readonly("control_trajectory",
&cddp::CDDPSolution::control_trajectory)
.def_readonly("feedback_gains", &cddp::CDDPSolution::feedback_gains)
.def_readonly("final_primal_infeasibility",
&cddp::CDDPSolution::final_primal_infeasibility)
.def_readonly("final_dual_infeasibility",
&cddp::CDDPSolution::final_dual_infeasibility)
.def_readonly("final_complementary_infeasibility",
&cddp::CDDPSolution::final_complementary_infeasibility)
.def_readonly("final_barrier_mu",
&cddp::CDDPSolution::final_barrier_mu)
.def_readonly("history", &cddp::CDDPSolution::history);
py::class_<cddp::CDDP>(m, "CDDP")
.def(py::init([](const Eigen::VectorXd &initial_state,
const Eigen::VectorXd &reference_state, int horizon,
double timestep, const cddp::CDDPOptions &options) {
return std::make_unique<cddp::CDDP>(initial_state,
reference_state, horizon,
timestep, nullptr, nullptr,
options);
}),
py::arg("initial_state"), py::arg("reference_state"),
py::arg("horizon"), py::arg("timestep"),
py::arg("options") = cddp::CDDPOptions())
.def("set_initial_state", &cddp::CDDP::setInitialState,
py::arg("initial_state"))
.def("set_reference_state", &cddp::CDDP::setReferenceState,
py::arg("reference_state"))
.def("set_reference_states", &cddp::CDDP::setReferenceStates,
py::arg("reference_states"))
.def("set_horizon", &cddp::CDDP::setHorizon, py::arg("horizon"))
.def("set_timestep", &cddp::CDDP::setTimestep, py::arg("timestep"))
.def("set_options", &cddp::CDDP::setOptions, py::arg("options"))
.def("set_dynamical_system",
[](cddp::CDDP &self, py::object system) {
self.setDynamicalSystem(makeOwnedDynamicalSystem(
std::move(system)));
},
py::arg("system"),
"The solver keeps a Python reference that keeps this dynamics "
"object alive. Mutating or sharing the object after this call "
"may produce unexpected behavior.")
.def("set_objective",
[](cddp::CDDP &self, py::object objective) {
self.setObjective(makeOwnedObjective(std::move(objective)));
},
py::arg("objective"),
"The solver keeps a Python reference that keeps this objective "
"alive. Mutating or sharing the object after this call may "
"produce unexpected behavior.")
.def("add_constraint",
[](cddp::CDDP &self, const std::string &name, py::object constraint) {
self.addPathConstraint(name,
makeOwnedConstraint(
std::move(constraint)));
},
py::arg("name"), py::arg("constraint"),
"The solver keeps a Python reference that keeps this path "
"constraint alive. Mutating or sharing the object after this "
"call may produce unexpected behavior.")
.def("add_terminal_constraint",
[](cddp::CDDP &self, const std::string &name, py::object constraint) {
self.addTerminalConstraint(
name, makeOwnedConstraint(std::move(constraint)));
},
py::arg("name"), py::arg("constraint"),
"The solver keeps a Python reference that keeps this terminal "
"constraint alive. Mutating or sharing the object after this "
"call may produce unexpected behavior.")
.def("remove_constraint", &cddp::CDDP::removePathConstraint,
py::arg("name"))
.def("remove_terminal_constraint",
&cddp::CDDP::removeTerminalConstraint, py::arg("name"))
.def("set_initial_trajectory",
[](cddp::CDDP &self, const std::vector<Eigen::VectorXd> &X,
const std::vector<Eigen::VectorXd> &U) {
validateInitialTrajectory(self, X, U);
self.setInitialTrajectory(X, U);
},
py::arg("X"), py::arg("U"))
.def("solve", py::overload_cast<cddp::SolverType>(&cddp::CDDP::solve),
py::arg("solver_type") = cddp::SolverType::CLDDP,
py::call_guard<py::gil_scoped_release>())
.def("solve_by_name",
[](cddp::CDDP &self, const std::string &solver_type) {
if (!isKnownSolverName(solver_type)) {
throw py::value_error("Unknown solver '" + solver_type +
"'.");
}
cddp::CDDPSolution solution;
{
py::gil_scoped_release release;
solution = self.solve(solver_type);
}
if (isUnknownSolverStatus(solution.status_message)) {
throw py::value_error("Unknown solver '" + solver_type +
"'.");
}
return solution;
},
py::arg("solver_type"))
.def_property_readonly("initial_state", &cddp::CDDP::getInitialState)
.def_property_readonly("reference_state",
&cddp::CDDP::getReferenceState)
.def_property_readonly("horizon", &cddp::CDDP::getHorizon)
.def_property_readonly("timestep", &cddp::CDDP::getTimestep)
.def_property_readonly("state_dim", &cddp::CDDP::getStateDim)
.def_property_readonly("control_dim", &cddp::CDDP::getControlDim)
.def_property_readonly("options", &cddp::CDDP::getOptions);
}