Questions in using "EndpointConstraint" mode of MocoInitialOutputGoal/MocoFinalOutputGoal

OpenSim Moco is a software toolkit to solve optimal control problems with musculoskeletal models defined in OpenSim using the direct collocation method.
POST REPLY
User avatar
Matthew Lee
Posts: 52
Joined: Sat Jun 20, 2020 7:46 pm

Questions in using "EndpointConstraint" mode of MocoInitialOutputGoal/MocoFinalOutputGoal

Post by Matthew Lee » Sun Nov 13, 2022 10:38 am

Dear Moco development team and Moco users,

Thank you all for your contributions to the biomechanics community! In the latest version of Moco,I have found the new goals called MocoInitialOutputGoal and MocoFinalOutputGoal which I think are very cool goals to enhance the potential of Moco. After reading relevant information about these goals from doxygen,I have known that these goals support both "Cost" (default) and "EndpointConstraint" modes.
I want to use these goals in the "EndpointConstraint" mode and set custom bounds.From OpenSim 4.4 API with Matlab, I have written codes as follows:

Code: Select all

goal1=MocoInitialOutputGoal('newgoal');
goal1.setMode('endpoint_constraint');
goal1.setOutputPath('/path/to/component|output_name'); %Just as an example template
ConInfo= goal1.updConstraintInfo();
ConInfo.setBounds(MocoBounds(6));
After running codes, the command line window shows the error as follows:
pic1.png
pic1.png (33.24 KiB) Viewed 1349 times
ConInfo is an instantiated object of class SWIGTYPE_p_MocoConstraintInfo
Then I run the code:

Code: Select all

methodsview(ConInfo)

The result shows as follows:
pic2.png
pic2.png (40.86 KiB) Viewed 1349 times

We can see from the figure that the Class SWIGTYPE_p_MocoConstraintInfo do not have the method setBounds.I tried to search for information from the source code of these goals, but I can't find the source codes whose name are MocoInitialOutputGoal.cpp and MocoFinalOutputGoal.cpp from OpenSim Core source code on github.https://github.com/opensim-org/opensim- ... o/MocoGoal Maybe I'm not looking for the right place, if anyone finds it please let me know.

As a summary, my main questions are listed below:

(1)I do not know how to custom bounds in the "EndpointConstraint" mode of MocoInitialOutputGoal/MocoFinalOutputGoal.I would appreciate if someone could give me some advice on how to achieve it.

(2)In addition,I found that when I try to use the method getModeAsString () ,getModeIsCost () and getModeIsEndpointConstraint () for any MocoGoal even common as MocoControlGoal, matlab will report the same error.Because these methods are Public,I am confused how should I resolve these errors.
pic3.png
pic3.png (41.13 KiB) Viewed 1349 times
Sorry for writing a lot, thank you all for your patience, I would appreciate if anyone could give me some help.


Best Wishes,
Matthew

User avatar
Nicholas Bianco
Posts: 980
Joined: Thu Oct 04, 2012 8:09 pm

Re: Questions in using "EndpointConstraint" mode of MocoInitialOutputGoal/MocoFinalOutputGoal

Post by Nicholas Bianco » Mon Nov 21, 2022 12:14 pm

Hi Matthew,

Thank you for bringing these issues to my attention.

It seems like we don't not properly support MocoConstraintInfo in Matlab and Python, which is preventing you from customizing your bounds. I'll submit a bug report to get this fixed soon.
I can't find the source codes whose name are MocoInitialOutputGoal.cpp and MocoFinalOutputGoal.cpp
The source code for these goals is in MocoOutputGoal.h and MocoOutputGoal.cpp. But you won't find the fix you're looking for there. This issue is related to the bindings, which is the code we use to support OpenSim/Moco through Matlab and Python.

The errors for getModeAsString() and related methods can be fixed by initializing the goal. You can do this manually by calling goal.initializeOnModel(model).

-Nick

User avatar
Matthew Lee
Posts: 52
Joined: Sat Jun 20, 2020 7:46 pm

Re: Questions in using "EndpointConstraint" mode of MocoInitialOutputGoal/MocoFinalOutputGoal

Post by Matthew Lee » Thu Nov 24, 2022 12:12 pm

nbianco wrote:
Mon Nov 21, 2022 12:14 pm
It seems like we don't not properly support MocoConstraintInfo in Matlab and Python, which is preventing you from customizing your bounds. I'll submit a bug report to get this fixed soon.
Hi Nick,

Thank you very much for your reply and help! Regarding the errors for getModeAsString(), I have fully understood how to solve it, thank you.

MocoInitialOutputGoal/MocoFinalOutputGoal are very cool goals,what a pity that the binding problem limits the potential of using them in Matlab. As you said, the problem occurs in the binding between Class MocoConstraintInfo and Matlab, which seems to affect the bounds customization of all path constraints and endpoint constraints provided by Moco in Matlab and Python. If I don't use updConstraintInfo to custom bounds when adding MocoInitialOutputGoal in Matlab, just run the following codes in Matlab:

Code: Select all

goal1=MocoInitialOutputGoal('newgoal');
goal1.setMode('endpoint_constraint');
goal1.setOutputPath('/path/to/component|output_name'); %Just as an example template
problem.addGoal(goal1);
(1) Whether MocoInitialOutputGoal can be included and solved normally in the optimal control problem as an endpoint constraint (that is, will the issues in binding between Class MocoConstraintInfo and Matlab affect the addition and solution of the MocoInitialOutputGoal as endpoint mode in the optimal control problem)?

(2) Because the updConstraintInfo is not used to custom bounds, does it mean that Moco will constrain the variable specified by setOutputPath to 0?

(3) I'm really looking forward to this bug being fixed soon so that the cool MocoInitialOutputGoal can reach its full potential.After this bug is fixed, will you release a new version of Moco in the near future, or will the fixed Moco be updated with the new version of OpenSim next year? If the latter is the case, since there is an urgent need to use MocoInitialOutputGoal in my current research project, could you please provide an alternative method of updConstraintInfo that can support Matlab to customize bounds in the current Moco version?(for example, provide a .dll plug-in that can achieve the same purpose, etc.) Thank you very much!


I really appreciate your professional answers and help,thanks again for your patience and time.

Best Wishes,
Matthew

User avatar
Nicholas Bianco
Posts: 980
Joined: Thu Oct 04, 2012 8:09 pm

Re: Questions in using "EndpointConstraint" mode of MocoInitialOutputGoal/MocoFinalOutputGoal

Post by Nicholas Bianco » Mon Nov 28, 2022 4:38 pm

Hi Matthew,

(1) MocoInitialOutputGoal will still be included as an endpoint constraint in the problem and (2) yes, it will be constrained to zero by default.

(3) I can't give a timeline for when this might be fixed. You've already made a foray into the C++ source code, and that might be the best bet if the fix you need is truly urgent. Here's the GitHub issue I filed: https://github.com/opensim-org/opensim-core/issues/3322.

Build instructions for the OpenSim source code are here: https://github.com/opensim-org/opensim- ... structions. It would probably be easiest to create a build of the current version of the code to make sure it works, and then modify the source to your needs. I'm happy to help with issue building the source here.

Best,
Nick

User avatar
Matthew Lee
Posts: 52
Joined: Sat Jun 20, 2020 7:46 pm

Re: Questions in using "EndpointConstraint" mode of MocoInitialOutputGoal/MocoFinalOutputGoal

Post by Matthew Lee » Tue Feb 07, 2023 11:41 am

nbianco wrote:
Mon Nov 28, 2022 4:38 pm
(1) MocoInitialOutputGoal will still be included as an endpoint constraint in the problem and (2) yes, it will be constrained to zero by default.
Hi Nick,

Thanks very much for your help! Based on your previous replies, I have conducted more explorations. But I encountered some problems during the simulation process, and I still need your help.

I use the squatToStand_3dof9musc.osim model provided by Moco for torque-driven predictive simulation. The cost function is MocoControlGoal.
My question are as follows:
Why does outcome obtained after the simulation of model does not meet the constraint tolerance requirements, but it shows that the solver is successfully solved?

I really appreciate your professional answers and help,thanks again for your patience and time.

Best Wishes,
Matthew
Last edited by Matthew Lee on Sun Mar 12, 2023 3:10 pm, edited 2 times in total.

User avatar
Nicholas Bianco
Posts: 980
Joined: Thu Oct 04, 2012 8:09 pm

Re: Questions in using "EndpointConstraint" mode of MocoInitialOutputGoal/MocoFinalOutputGoal

Post by Nicholas Bianco » Mon Feb 13, 2023 5:47 pm

Hi Matthew,

There might indeed be a bug with analyzeVec3(), specifically when using implicit mode. In implicit mode, the joint accelerations are variables in the optimal control problem. In analyzeVec3(), we should make sure to apply these accelerations to the model before computing any acceleration-level quantities.

I'll file an issue and hopefully get around to it soon.

Best,
Nick

User avatar
Matthew Lee
Posts: 52
Joined: Sat Jun 20, 2020 7:46 pm

Re: Questions in using "EndpointConstraint" mode of MocoInitialOutputGoal/MocoFinalOutputGoal

Post by Matthew Lee » Sat Feb 18, 2023 5:01 am

nbianco wrote:
Mon Feb 13, 2023 5:47 pm
we should make sure to apply these accelerations to the model before computing any acceleration-level quantities.
Hi Nick,

Thank you very much for your quick and professional reply! I still need your help with a few more questions(In the scenario of the following questions, I use MocoStudy to predict a new motion):

1.Does this bug affect the optimization calculation of Moco? In implicit dynamics mode, will Moco apply acceleration to the model during the optimization iterative calculation? In other words, if acceleration-level quantities are involved in the cost function or constraints of the optimal control problem, will these acceleration-level quantities be calculated correctly?
Or this bug does not affect the correctness of the Moco optimization calculation process, only the result reported by analyzeVec3() is incorrect?

2. I guess that the same bug may also exist in method analyzeMocoTrajectory() and prescribeControlsToModel()

3. I want to use C++ to customize the cost function related to joint reaction force. I plan to use the following codes in function calcIntegrandImpl():

Code: Select all

calcIntegrandImpl(const IntegrandInput& input, SimTK::Real& integrand) const {
Model_A=getModel();
Model_A.realizeAcceleration(input.state);
Const auto& reaction = Model_A.getJointSet().get("Joint1").calcReactionOnChildExpressedInGround(input.state);
}
As far as I know, before computing the joint reaction forces(or other acceleration-level quantities) ,controls must be prescribed to the model in order to compute the joint reaction forces correctly.

My questions are:
(1)If both Muscle and CoordinateActuator are included in the OpenSim model which used for Moco predictive simulation, after I use Model_A=getModel() to get the Model_A in above C++ codes, have all the controls(including muscle controls,controls of CoordinateActuator and acceleration in implicit mode) already been prescribed to the Model_A?
(2) Do I need to run a command like prescribeControlsToModel() before Model_A.realizeAcceleration so that all controls are prescribed to Model_A to ensure that the subsequent joint reaction force can be calculated correctly? (prescribeControlsToModel() is executed before calculating Integrand in examplePrototypeCustomGoal.m as shown below)
pic1.JPG
pic1.JPG (103.26 KiB) Viewed 745 times
(3)In order to understand this problem, I tried to use Model_A.print() to output the model in the above C++ codes, but the exported model cannot be opened in OpenSim GUI.

(4)If there is no need to prescribe control to the model in function calcIntegrandImpl(), then in which function have all the controls already been prescribed to the model?

4. Similar to question 3, I want to use C++ to customize path constraints related to joint reaction forces. In function calcPathConstraintErrorsImpl(), do I need to execute commands like prescribeControlsToModel() after Model_A=getModel() so that all controls are prescribed to the model?

5. I would like to customize a path constraint to constrain the product of two components of the joint reaction force within a certain range. Because customizing the path constraint plugin may somewhat different from customizing cost function plugin, could you please recommend me a similar template for constraining product of two joint reaction forces purposes?

Thanks again for your patience and time and I am looking forward to hearing from you!

Best Wishes,
Matthew

User avatar
Nicholas Bianco
Posts: 980
Joined: Thu Oct 04, 2012 8:09 pm

Re: Questions in using "EndpointConstraint" mode of MocoInitialOutputGoal/MocoFinalOutputGoal

Post by Nicholas Bianco » Sun Feb 19, 2023 12:09 pm

Hi Matthew,

1. The bug does not affect the optimization problem that Moco solves. It is only related to the post-hoc analyses you perform using analyze() or analyzeVec3().

2. Yes in analyzeMocoTrajectory(), but not in prescribeControlsToModel().

3. You shouldn't need to realize to Stage::Acceleration for joint forces, only Stage::Dynamics (i.e., for force-level quantities). You don't need to worry about prescribing controls to the model when creating a custom MocoGoal, that will happen automatically within the solver (we use a special controller internally in Moco to pass along the controls with the SimTK::State). Just realize to the Stage you need and compute the quantities you want from the model.

4. Same answer as 3.

5. We have no template for custom path constraints, but you can use one of the existing path constraints as a guide (e.g., MocoOutputConstraint).

Best,
Nick

User avatar
Matthew Lee
Posts: 52
Joined: Sat Jun 20, 2020 7:46 pm

Re: Questions in using "EndpointConstraint" mode of MocoInitialOutputGoal/MocoFinalOutputGoal

Post by Matthew Lee » Thu Feb 23, 2023 12:16 pm

Hi Nick,

Thanks for your professional reply! Your answer is very clear and easy to understand, thanks again! By the way, I hope the bug is easy to fix.

Best Wishes,
Matthew

POST REPLY