Skip to content

Refactor solver core and switch to typed CDDP solutions#159

Merged
astomodynamics merged 7 commits into
masterfrom
feat/refactoring-whole-repo
Mar 29, 2026
Merged

Refactor solver core and switch to typed CDDP solutions#159
astomodynamics merged 7 commits into
masterfrom
feat/refactoring-whole-repo

Conversation

@astomodynamics
Copy link
Copy Markdown
Owner

@astomodynamics astomodynamics commented Mar 29, 2026

Summary

This PR refactors the shared CDDP solver internals to reduce duplication across the CLDDP, LogDDP, IPDDP, and MSIPDDP implementations. It also replaces the string-keyed CDDPSolution map with a typed solution struct and updates examples and tests to consume the typed API directly.

Changes

  • Add CDDPSolverBase and move the common solve loop, line search handling, derivative precomputation, iteration-history recording, and solution assembly into a shared base implementation.
  • Replace the public CDDPSolution map-of-std::any result shape with a typed struct and migrate the in-tree examples and tests to field-based access.
  • Simplify constraint and helper utilities, including shared box-constraint handling and common default Hessian helpers.
  • Add cddp_example_utils.hpp and reuse it across many examples for initial trajectory setup, directory creation, time vectors, and component extraction.
  • Include the example-side fixes for cddp_lti_system and cddp_hcw, plus the supporting CMake update to compile the new solver-base source.

Test Plan

  • ./build/tests/test_logddp_solver
  • ./build/tests/test_ipddp_solver
  • ./build/tests/test_msipddp_solver

How to Test

  • From the repo root, run ./build/tests/test_logddp_solver.
  • From the repo root, run ./build/tests/test_ipddp_solver.
  • From the repo root, run ./build/tests/test_msipddp_solver.
  • Not run in this session: ./build/examples/cddp_lti_system and ./build/examples/cddp_hcw to sanity-check the typed solution access in the updated examples.

- Unify ControlConstraint/StateConstraint into BoxConstraint<> template
- Add default zero-Hessian implementations in Constraint base class
- Extract InteriorPointOptions/MultiShootingOptions base structs in options
- Create CDDPSolverBase with template method solve() pattern
- Migrate all 4 solvers (CLDDP, LogDDP, IPDDP, MSIPDDP) to use base class
- Add DynamicalSystem::makeZeroTensor() helper, update 16 dynamics models
- Consolidate 9 finite difference functions into 3 unified templates
Eliminates all std::any_cast usage across 41 files. Solution fields
are now accessed directly (e.g., solution.state_trajectory instead of
std::any_cast<vector<VectorXd>>(solution.at("state_trajectory"))).
Create cddp_example_utils.hpp with extractComponent, makeTimeVector,
ensurePlotDir, and makeInitialTrajectory helpers. Apply across all
examples to reduce boilerplate data extraction and directory setup.
- Skip convergence check after failed forward pass (prevents false
  convergence when dJ/dL are zero, which triggered LogDDP's abs check)
- Log unconditionally when ALL parallel forward pass threads fail
- Add checkEarlyConvergence hook so CLDDP checks inf_du before forward pass
- Record iteration history on early convergence path
- Fix BoxConstraint::computeViolation double-subtraction of ip_upper_bound_
- Add scale_factor_ to BoxConstraint<Control> control Jacobian for consistency
- Unify CDDPSolution::History and IterationHistory into single type
- Clean up solve() loop: remove continue, single printIteration call site
Both examples accessed solution.history.objective.back() but history
is only populated when return_iteration_info is enabled (default false).
Use solution.final_objective instead. Also fix ms-vs-microseconds label.
The template method hardcoded backward pass regularization exhaustion
to NotConverged. LogDDP originally treated this as Converged since the
barrier-penalized trajectory is typically still usable. Add virtual
handleBackwardPassRegularizationLimit hook with LogDDP override.
- Re-add throwing Hessian overrides for PoleConstraint and
  SecondOrderConeConstraint (nonlinear — zero Hessians are wrong)
- Keep zero-return default in base Constraint (correct for linear)
- Remove stale #include <any> from cddp_core.hpp
- Remove .claude/setting.json from tracked files
@astomodynamics astomodynamics merged commit a24cfba into master Mar 29, 2026
2 checks passed
@astomodynamics astomodynamics deleted the feat/refactoring-whole-repo branch March 29, 2026 20:17
@astomodynamics astomodynamics self-assigned this Mar 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant