Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed terminal computation in model classes derivated in Python #1204

Merged

Conversation

cmastalli
Copy link
Member

@cmastalli cmastalli commented Dec 29, 2023

When a C++ implementation performs a terminal computation in a Python-derivated code, we pass u=0 instead of calling calc(data, x) or calcDiff(data, x). This behaviour in Python doesn't replicate our C++ code. Moreover, this creates bug in Python developers that are hard to track.

This PR solves the issue above-mentioned by calling the right function. To do so, the trick is to define unone_ = NAN for Python code and call the expected virtual function implemented in Python. This problem is fixed in all the classes, i.e.,

  • action models
  • differential action models
  • constraint models
  • cost models
  • residual models

To be clear, when we wish to run a terminal computation in Python, then we pass u=None. This is how users are expected to implement their classes, e.g.,

import crocoddyl

class MyActionModel(crocoddyl.ActionModelAbstract):
    def __init__(self, state, nu):
        crocoddyl.ActionModelAbstract.__init__(self, state, nu)

    def calc(self, data, x, u=None):
        if u is None:
            # We run terminal node computations. For action models, the terminal value of `xnext = x`
            data.xnext[:] = x
            data.cost = "my value"  
        else:
            # We run running node computations.
            data.xnext[:] = "my value"
            data.cost = "my value"

    def calcDiff(self, data, x, u=None):
        if u is None:
            # We run terminal node computations. For action models, we just need to update Lx and Lxx
            data.Lx[:] = "my value"
            data.Lxx[:, :] = "my value"
        else:
            # We run running node computations.
            data.Lx[:] =  "my value"
            data.Lu[:] =  "my value"
            data.Lxx[:, :] =  "my value"
            data.Luu[:, :] =  "my value"
            data.Lxu[:, :] =  "my value"

Additionally, the PR extends Python-derived classes used for examples and unit testing. Finally, the PR extends the Python bindings test for the action, differential action and cost models.

@cmastalli cmastalli merged commit eb01abb into loco-3d:devel Dec 30, 2023
7 of 8 checks passed
@cmastalli cmastalli deleted the topic/fix-terminal-computation-python branch December 30, 2023 05:36
nim65s added a commit to nim65s/robotpkg that referenced this pull request May 31, 2024
Changed in v2.1.0:

* Updated black + isort + flake8 to ruff in loco-3d/crocoddyl#1256
* Exported version for Python in loco-3d/crocoddyl#1254
* Added pinocchio 3 preliminary support in loco-3d/crocoddyl#1253
* Updated CMake packaging in loco-3d/crocoddyl#1249
* Fixed ruff reported error in loco-3d/crocoddyl#1248
* Fixed yapf reported errors in loco-3d/crocoddyl#1238
* Tested Python stubs in Conda CI in loco-3d/crocoddyl#1228
* Fixed Rviz display in loco-3d/crocoddyl#1227
* Improved CI, updated cmake and fixed launch file in loco-3d/crocoddyl#1220
* Introduced a Rviz display in loco-3d/crocoddyl#1216
* Enabled display of thrust and simplied displayers code in loco-3d/crocoddyl#1215
* Introduced floating base thruster actuation model in loco-3d/crocoddyl#1213
* Fixed quadruped and biped examples in loco-3d/crocoddyl#1208
* Fixed terminal computation in Python models in loco-3d/crocoddyl#1204
* Fixed handling of unbounded values for `ActivationBounds` in loco-3d/crocoddyl#1191
nim65s added a commit to nim65s/robotpkg that referenced this pull request Jun 20, 2024
Changed in v2.1.0:

* Updated black + isort + flake8 to ruff in loco-3d/crocoddyl#1256
* Exported version for Python in loco-3d/crocoddyl#1254
* Added pinocchio 3 preliminary support in loco-3d/crocoddyl#1253
* Updated CMake packaging in loco-3d/crocoddyl#1249
* Fixed ruff reported error in loco-3d/crocoddyl#1248
* Fixed yapf reported errors in loco-3d/crocoddyl#1238
* Tested Python stubs in Conda CI in loco-3d/crocoddyl#1228
* Fixed Rviz display in loco-3d/crocoddyl#1227
* Improved CI, updated cmake and fixed launch file in loco-3d/crocoddyl#1220
* Introduced a Rviz display in loco-3d/crocoddyl#1216
* Enabled display of thrust and simplied displayers code in loco-3d/crocoddyl#1215
* Introduced floating base thruster actuation model in loco-3d/crocoddyl#1213
* Fixed quadruped and biped examples in loco-3d/crocoddyl#1208
* Fixed terminal computation in Python models in loco-3d/crocoddyl#1204
* Fixed handling of unbounded values for `ActivationBounds` in loco-3d/crocoddyl#1191
nim65s added a commit to nim65s/robotpkg that referenced this pull request Jul 1, 2024
Changed in v2.1.0:

* Updated black + isort + flake8 to ruff in loco-3d/crocoddyl#1256
* Exported version for Python in loco-3d/crocoddyl#1254
* Added pinocchio 3 preliminary support in loco-3d/crocoddyl#1253
* Updated CMake packaging in loco-3d/crocoddyl#1249
* Fixed ruff reported error in loco-3d/crocoddyl#1248
* Fixed yapf reported errors in loco-3d/crocoddyl#1238
* Tested Python stubs in Conda CI in loco-3d/crocoddyl#1228
* Fixed Rviz display in loco-3d/crocoddyl#1227
* Improved CI, updated cmake and fixed launch file in loco-3d/crocoddyl#1220
* Introduced a Rviz display in loco-3d/crocoddyl#1216
* Enabled display of thrust and simplied displayers code in loco-3d/crocoddyl#1215
* Introduced floating base thruster actuation model in loco-3d/crocoddyl#1213
* Fixed quadruped and biped examples in loco-3d/crocoddyl#1208
* Fixed terminal computation in Python models in loco-3d/crocoddyl#1204
* Fixed handling of unbounded values for `ActivationBounds` in loco-3d/crocoddyl#1191
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