copp 0.2.2

Convex-objective path parameterization for robotic trajectory planning.
Documentation
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
//! Python wrappers for COPP2 problem descriptors and solvers.

use super::clarabel::{
    PyClarabelLinearSolverInfo, PyClarabelOptions, PyClarabelSolverStatus,
    clarabel_options_or_default,
};
use crate::copp::copp2::opt2::copp2_socp::objective_value_copp2_opt;
use crate::diag::CoppError;
use crate::ffi::python::objective::{OwnedObjective, parse_objectives};
use crate::ffi::python::robot::{
    PyRobot, PyRobotModel, SharedRobot, shared_has_inverse_dynamics, with_shared_robot_result,
};
use crate::solver::copp2_socp::{
    ClarabelOptions as RustClarabelOptions, Copp2Problem as RustCopp2Problem, Copp2ProblemBuilder,
    copp2_socp as rust_copp2_socp, copp2_socp_expert_with_info as rust_copp2_socp_expert,
};
use clarabel::solver::{DefaultSolution, LinearSolverInfo};
use numpy::PyArray1;
use pyo3::prelude::*;

/// Register COPP2 problem, option, result, and solver APIs.
pub(super) fn register(m: &Bound<'_, PyModule>) -> PyResult<()> {
    m.add_class::<PyCopp2Problem>()?;
    m.add_class::<PyCopp2SocpResult>()?;
    m.add_function(wrap_pyfunction!(copp2_socp, m)?)?;
    m.add_function(wrap_pyfunction!(copp2_socp_expert, m)?)?;
    Ok(())
}

/// Python-owned expert result for COPP2-SOCP.
///
/// The accepted profile and objective breakdown are `None` when the Clarabel
/// status is not accepted by the provided [`PyClarabelOptions`]. Raw Clarabel
/// vectors are still available regardless of status for diagnostics.
#[pyclass(name = "Copp2SocpResult", module = "copp_py._native")]
pub(crate) struct PyCopp2SocpResult {
    /// Accepted `a = (ds/dt)^2` profile, when available.
    a: Option<Vec<f64>>,
    /// Raw Clarabel primal solution vector.
    x: Vec<f64>,
    /// Raw Clarabel dual solution vector.
    z: Vec<f64>,
    /// Raw Clarabel primal-cone slack vector.
    s: Vec<f64>,
    /// Final Clarabel solver status.
    solver_status: PyClarabelSolverStatus,
    /// Primal objective value reported by Clarabel.
    obj_val: f64,
    /// Dual objective value reported by Clarabel.
    obj_val_dual: f64,
    /// Clarabel solve time in seconds.
    solve_time: f64,
    /// Number of interior-point iterations.
    iterations: u32,
    /// Primal residual reported by Clarabel.
    r_prim: f64,
    /// Dual residual reported by Clarabel.
    r_dual: f64,
    /// Linear-solver metadata reported by Clarabel.
    linsolver: PyClarabelLinearSolverInfo,
    /// Weighted COPP objective value computed from accepted `a`, when available.
    objective_value: Option<f64>,
    /// Per-objective unweighted values computed from accepted `a`, when available.
    objective_terms: Option<Vec<f64>>,
}

#[pymethods]
impl PyCopp2SocpResult {
    /// Return the accepted `a` profile, or `None` when unavailable.
    #[getter]
    fn a<'py>(&self, py: Python<'py>) -> Option<Bound<'py, PyArray1<f64>>> {
        self.a.as_ref().map(|a| PyArray1::from_vec(py, a.clone()))
    }

    /// Return the raw Clarabel primal solution vector.
    #[getter]
    fn x<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray1<f64>> {
        PyArray1::from_vec(py, self.x.clone())
    }

    /// Return the raw Clarabel dual solution vector.
    #[getter]
    fn z<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray1<f64>> {
        PyArray1::from_vec(py, self.z.clone())
    }

    /// Return the raw Clarabel primal-cone slack vector.
    #[getter]
    fn s<'py>(&self, py: Python<'py>) -> Bound<'py, PyArray1<f64>> {
        PyArray1::from_vec(py, self.s.clone())
    }

    /// Return the final Clarabel solver status.
    #[getter]
    fn solver_status(&self) -> PyClarabelSolverStatus {
        self.solver_status
    }

    /// Return the primal objective value reported by Clarabel.
    #[getter]
    fn obj_val(&self) -> f64 {
        self.obj_val
    }

    /// Return the dual objective value reported by Clarabel.
    #[getter]
    fn obj_val_dual(&self) -> f64 {
        self.obj_val_dual
    }

    /// Return Clarabel solve time in seconds.
    #[getter]
    fn solve_time(&self) -> f64 {
        self.solve_time
    }

    /// Return the number of interior-point iterations.
    #[getter]
    fn iterations(&self) -> u32 {
        self.iterations
    }

    /// Return the primal residual reported by Clarabel.
    #[getter]
    fn r_prim(&self) -> f64 {
        self.r_prim
    }

    /// Return the dual residual reported by Clarabel.
    #[getter]
    fn r_dual(&self) -> f64 {
        self.r_dual
    }

    /// Return linear-solver metadata reported by Clarabel.
    #[getter]
    fn linsolver(&self) -> PyClarabelLinearSolverInfo {
        self.linsolver.clone()
    }

    /// Return weighted COPP objective value computed from accepted `a`, if any.
    #[getter]
    fn objective_value(&self) -> Option<f64> {
        self.objective_value
    }

    /// Return per-objective unweighted values computed from accepted `a`, if any.
    #[getter]
    fn objective_terms<'py>(&self, py: Python<'py>) -> Option<Bound<'py, PyArray1<f64>>> {
        self.objective_terms
            .as_ref()
            .map(|terms| PyArray1::from_vec(py, terms.clone()))
    }

    /// Return a compact representation for interactive Python sessions.
    fn __repr__(&self) -> String {
        format!(
            "Copp2SocpResult(a_available={}, solver_status={:?}, iterations={}, objective_value={:?})",
            self.a.is_some(),
            self.solver_status,
            self.iterations,
            self.objective_value
        )
    }
}

impl PyCopp2SocpResult {
    /// Convert Rust expert outputs into a Python-owned result object.
    fn from_solution(
        a_profile: Option<Vec<f64>>,
        solution: DefaultSolution<f64>,
        linsolver: LinearSolverInfo,
        objective_breakdown: Option<(f64, Vec<f64>)>,
    ) -> Self {
        let DefaultSolution {
            x,
            z,
            s,
            status,
            obj_val,
            obj_val_dual,
            solve_time,
            iterations,
            r_prim,
            r_dual,
        } = solution;
        let (objective_value, objective_terms) = match objective_breakdown {
            Some((value, terms)) => (Some(value), Some(terms)),
            None => (None, None),
        };

        Self {
            a: a_profile,
            x,
            z,
            s,
            solver_status: PyClarabelSolverStatus::from_rust(status),
            obj_val,
            obj_val_dual,
            solve_time,
            iterations,
            r_prim,
            r_dual,
            linsolver: linsolver.into(),
            objective_value,
            objective_terms,
        }
    }
}

/// Python descriptor for a COPP2 problem.
#[pyclass(name = "Copp2Problem", module = "copp_py._native")]
pub(crate) struct PyCopp2Problem {
    /// Robot kept alive for borrowed Rust problem construction.
    robot: Py<PyRobot>,
    /// Shared robot storage used by detached solver calls.
    shared: SharedRobot,
    /// Python-owned objective descriptors.
    objectives: Vec<OwnedObjective>,
    /// Closed station-index interval `(idx_s_start, idx_s_final)`.
    idx_s_interval: (usize, usize),
    /// Endpoint state tuple `(a_start, a_final)`.
    a_boundary: (f64, f64),
}

#[pymethods]
impl PyCopp2Problem {
    /// Construct a COPP2 problem descriptor from a robot and objective list.
    ///
    /// The descriptor stores the Python robot and owned objective data, then
    /// rebuilds a validated borrowed Rust [`Copp2ProblemBuilder`] problem for
    /// each solver call.
    #[new]
    #[pyo3(
        signature = (robot, objectives, idx_s_interval, a_boundary = (0.0, 0.0)),
        text_signature = "(robot, objectives, idx_s_interval, a_boundary=(0.0, 0.0))"
    )]
    fn new(
        py: Python<'_>,
        robot: Py<PyRobot>,
        objectives: &Bound<'_, PyAny>,
        idx_s_interval: (usize, usize),
        a_boundary: (f64, f64),
    ) -> PyResult<Self> {
        let shared = robot.bind(py).borrow().shared_robot();
        let problem = Self {
            robot,
            shared,
            objectives: parse_objectives(objectives)?,
            idx_s_interval,
            a_boundary,
        };
        problem.validate(py)?;
        Ok(problem)
    }

    /// Return the robot referenced by this problem descriptor.
    #[getter]
    fn robot(&self, py: Python<'_>) -> Py<PyRobot> {
        self.robot.clone_ref(py)
    }

    /// Return the number of objectives stored in this descriptor.
    #[getter]
    fn objective_count(&self) -> usize {
        self.objectives.len()
    }

    /// Return the closed station-index interval.
    #[getter]
    fn idx_s_interval(&self) -> (usize, usize) {
        self.idx_s_interval
    }

    /// Return endpoint values `(a_start, a_final)`.
    #[getter]
    fn a_boundary(&self) -> (f64, f64) {
        self.a_boundary
    }

    /// Return the number of station samples in the closed interval.
    #[getter]
    fn s_len(&self) -> usize {
        self.idx_s_interval.1.saturating_sub(self.idx_s_interval.0) + 1
    }

    /// Validate the descriptor against the current robot and objectives.
    fn validate(&self, py: Python<'_>) -> PyResult<()> {
        self.with_rust_problem(py, |_| Ok(()))
    }

    /// Return a compact representation for interactive Python sessions.
    fn __repr__(&self) -> String {
        format!(
            "Copp2Problem(idx_s_interval=({}, {}), a_boundary=({}, {}), objective_count={})",
            self.idx_s_interval.0,
            self.idx_s_interval.1,
            self.a_boundary.0,
            self.a_boundary.1,
            self.objectives.len()
        )
    }
}

impl PyCopp2Problem {
    /// Build a validated Rust COPP2 problem and pass it to `f`.
    fn with_rust_problem<R>(
        &self,
        py: Python<'_>,
        f: impl for<'a> FnOnce(&RustCopp2Problem<'a, PyRobotModel>) -> Result<R, CoppError>,
    ) -> PyResult<R> {
        let _ = py;
        Self::with_shared_problem(
            &self.shared,
            &self.objectives,
            self.idx_s_interval,
            self.a_boundary,
            f,
        )
    }

    /// Build a validated COPP2 problem from shared robot state and call `f`.
    fn with_shared_problem<R>(
        shared: &SharedRobot,
        objectives_owned: &[OwnedObjective],
        idx_s_interval: (usize, usize),
        a_boundary: (f64, f64),
        f: impl for<'a> FnOnce(&RustCopp2Problem<'a, PyRobotModel>) -> Result<R, CoppError>,
    ) -> PyResult<R> {
        with_shared_robot_result(shared, |robot| {
            let objectives = objectives_owned
                .iter()
                .map(OwnedObjective::as_rust)
                .collect::<Vec<_>>();
            let problem =
                Copp2ProblemBuilder::new(robot, idx_s_interval, a_boundary, objectives.as_slice())
                    .build()?;
            f(&problem)
        })
    }

    /// Solve this COPP2 problem with validated Rust Clarabel SOCP options.
    fn solve_copp2_socp(
        &self,
        py: Python<'_>,
        options: &RustClarabelOptions,
    ) -> PyResult<Vec<f64>> {
        let shared = self.shared.clone();
        let objectives = self.objectives.clone();
        let idx_s_interval = self.idx_s_interval;
        let a_boundary = self.a_boundary;
        if shared_has_inverse_dynamics(&shared)? {
            Self::with_shared_problem(
                &shared,
                &objectives,
                idx_s_interval,
                a_boundary,
                |problem| rust_copp2_socp(problem, options),
            )
        } else {
            py.detach(move || {
                Self::with_shared_problem(
                    &shared,
                    &objectives,
                    idx_s_interval,
                    a_boundary,
                    |problem| rust_copp2_socp(problem, options),
                )
            })
        }
    }

    /// Solve this COPP2 problem with expert Clarabel SOCP diagnostics.
    fn solve_copp2_socp_expert(
        &self,
        py: Python<'_>,
        options: &RustClarabelOptions,
    ) -> PyResult<PyCopp2SocpResult> {
        let shared = self.shared.clone();
        let objectives = self.objectives.clone();
        let idx_s_interval = self.idx_s_interval;
        let a_boundary = self.a_boundary;
        if shared_has_inverse_dynamics(&shared)? {
            Self::with_shared_problem(
                &shared,
                &objectives,
                idx_s_interval,
                a_boundary,
                |problem| {
                    let expert = rust_copp2_socp_expert(problem, options)?;
                    let objective_breakdown = expert.result.as_ref().map(|a| {
                        objective_value_copp2_opt(
                            problem.robot,
                            problem.idx_s_interval.0,
                            problem.objectives,
                            a,
                        )
                    });
                    Ok(PyCopp2SocpResult::from_solution(
                        expert.result,
                        expert.solution,
                        expert.linsolver,
                        objective_breakdown,
                    ))
                },
            )
        } else {
            py.detach(move || {
                Self::with_shared_problem(
                    &shared,
                    &objectives,
                    idx_s_interval,
                    a_boundary,
                    |problem| {
                        let expert = rust_copp2_socp_expert(problem, options)?;
                        let objective_breakdown = expert.result.as_ref().map(|a| {
                            objective_value_copp2_opt(
                                problem.robot,
                                problem.idx_s_interval.0,
                                problem.objectives,
                                a,
                            )
                        });
                        Ok(PyCopp2SocpResult::from_solution(
                            expert.result,
                            expert.solution,
                            expert.linsolver,
                            objective_breakdown,
                        ))
                    },
                )
            })
        }
    }
}

/// Solve COPP2 with the Clarabel SOCP backend.
#[pyfunction]
#[pyo3(signature = (problem, options = None), text_signature = "(problem, options=None)")]
fn copp2_socp<'py>(
    py: Python<'py>,
    problem: PyRef<'py, PyCopp2Problem>,
    options: Option<PyRef<'py, PyClarabelOptions>>,
) -> PyResult<Bound<'py, PyArray1<f64>>> {
    let options = clarabel_options_or_default(options)?;
    let a = problem.solve_copp2_socp(py, &options)?;
    Ok(PyArray1::from_vec(py, a))
}

/// Solve COPP2 with the Clarabel SOCP backend and return diagnostics.
#[pyfunction]
#[pyo3(signature = (problem, options = None), text_signature = "(problem, options=None)")]
fn copp2_socp_expert<'py>(
    py: Python<'py>,
    problem: PyRef<'py, PyCopp2Problem>,
    options: Option<PyRef<'py, PyClarabelOptions>>,
) -> PyResult<PyCopp2SocpResult> {
    let options = clarabel_options_or_default(options)?;
    problem.solve_copp2_socp_expert(py, &options)
}