Question about the source code of MocoSumSquaredStateGoal

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

Question about the source code of MocoSumSquaredStateGoal

Post by Matthew Lee » Mon Nov 14, 2022 12:32 pm

Dear Moco development team and Moco users,

Thank you all for your contributions to the biomechanics community!I want to customize a new cost function item about "State" in Moco, so firstly I read "MocoSumSquaredStateGoal.cpp" to learn how to write relevant codes.It can be found from pic1 that the variable 'integrand' is not initialized to 0 before the for loop starts in the function 'calcIntegrandImpl'.The difference is that the variable 'integrand' is initialized to 0 in MocoControlGoal.cpp (pic2).I also looked at some tracking goals (in these goals relevant value also need to be summed in the for loop), and found that the variable 'integrand' is also assigned to 0 before the for loop starts. So for MocoSumSquaredStateGoal, is it necessary to set the variable 'integrand' to 0 before the for loop starts? Or is the variable 'integrand' has been already set to 0 elsewhere, so it doesn't need to be assigned to 0 here?

https://github.com/opensim-org/opensim- ... teGoal.cpp
pic1.png
pic1.png (35.33 KiB) Viewed 973 times
https://github.com/opensim-org/opensim- ... olGoal.cpp
pic2.png
pic2.png (47.78 KiB) Viewed 973 times
I would be very grateful if someone could clarify the confusion for me.

Best Wishes,
Matthew

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

Re: Question about the source code of MocoSumSquaredStateGoal

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

Hi Matthew,

If you take a look at MocoGoal::calcIntegrand, you can see that the integrand value passed to calcIntegrandImpl in derived goals is initialized to zero.

So, it's not really necessary to initialize to zero again inside calcIntegrandImpl. But it doesn't change anything either, we probably did that in MocoControlGoal out of habit.

-Nick

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

Re: Question about the source code of MocoSumSquaredStateGoal

Post by Matthew Lee » Tue Nov 22, 2022 11:47 am

nbianco wrote:
Mon Nov 21, 2022 12:01 pm

If you take a look at MocoGoal::calcIntegrand, you can see that the integrand value passed to calcIntegrandImpl in derived goals is initialized to zero.
Hi Nick,

Thank you very much for your professional answer! I'm glad to know that the integrand is initialized to 0 in MocoGoal::calcIntegrand. This means whether it can be understood like this: when writing a goal derived from MocoGoal, whether to initialize the integrand to 0 in the function calcIntegrandImpl will not affect the final calculation result?

I recently successfully ran the exampleMocoCustomEffortGoal provided by Moco using visual studio 2017 C++ and CMake3.15.5, and then customized a new goal named MocoSumCubedStateGoal based on MocoSumSquaredStateGoal.cpp. This goal changes the sum of squares of states to the cubic sum of states by modifying just one line of code in MocoSumSquaredStateGoal.cpp. When buliding my new goal in visual studio 2017 C++, the compiler reports the following errors:
pic3.png
pic3.png (59.45 KiB) Viewed 792 times
Recently I've double-checked my codes and tried various methods of debugging, but still can't resolve these errors. These errors has bothered me for a few days and made me very distressed. :cry: You are the authoritative expert of Moco,could you please help me check where is the problem?My original codes are attached. Thanks a lot for your time and help!

Best Wishes,
Matthew
Last edited by Matthew Lee on Thu Dec 22, 2022 11:21 am, edited 1 time in total.

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

Re: Question about the source code of MocoSumSquaredStateGoal

Post by Nicholas Bianco » Wed Nov 23, 2022 11:44 am

This means whether it can be understood like this: when writing a goal derived from MocoGoal, whether to initialize the integrand to 0 in the function calcIntegrandImpl will not affect the final calculation result?
Yes, that's correct.

As for the compiler errors, looks like there's some linking issues with the spdlog library. Did you build spdlog along with the rest of the dependencies?

Also, I would recommend upgrading to Visual Studio 2019, as that's the version we use for OpenSim and Moco development.

-Nick

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

Re: Question about the source code of MocoSumSquaredStateGoal

Post by Matthew Lee » Fri Nov 25, 2022 8:47 am

nbianco wrote:
Wed Nov 23, 2022 11:44 am
As for the compiler errors, looks like there's some linking issues with the spdlog library. Did you build spdlog along with the rest of the dependencies?
Hi Nick,

Thank you very much for your reply and help! I am a beginner in C++ and CMake, please forgive my seemingly simple question.How should I build spdlog along with the rest of the dependencies? What codes do I need to add in CMakeLists.txt or in .h and .cpp files? The following are codes in my CMakeLists.txt file,I am looking forward to your valuable suggestions,thank you very much.

Code: Select all

cmake_minimum_required(VERSION 3.2)
project(OpenSim_MocoSumCubedStateGoal)

set(CMAKE_CXX_STANDARD 11)

find_package(OpenSim REQUIRED HINTS
    "${CMAKE_SOURCE_DIR}/../../../../../../")
include("${OpenSim_USE_FILE}")

add_library(osimMocoSumCubedStateGoal SHARED
        MocoSumCubedStateGoal.h
        MocoSumCubedStateGoal.cpp
        osimMocoSumCubedStateGoalDLL.h
        RegisterTypes_osimMocoSumCubedStateGoal.h
        RegisterTypes_osimMocoSumCubedStateGoal.cpp
        )
target_link_libraries(osimMocoSumCubedStateGoal
        osimTools osimExampleComponents osimMoco)

string(TOUPPER MocoSumCubedStateGoal example_name_upper)
set_target_properties(osimMocoSumCubedStateGoal PROPERTIES
        DEFINE_SYMBOL OSIM${example_name_upper}_EXPORTS
        )

add_executable(exampleMocoSumCubedStateGoal exampleMocoSumCubedStateGoal.cpp)
target_link_libraries(exampleMocoSumCubedStateGoal osimMocoSumCubedStateGoal)

# For Windows: make sure DLLs for dependencies are available.
OpenSimCopyDependencyDLLsForWin(DEP_NAME OpenSim
        DEP_BIN_DIR "${OpenSim_BIN_DIR}")
if(WIN32)
    add_dependencies(osimMocoSumCubedStateGoal Copy_OpenSim_DLLs)
endif()

file(COPY  DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
Best Wishes,
Matthew

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

Re: Question about the source code of MocoSumSquaredStateGoal

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

Hi Matthew,

I would refer to the CMake files from the examples for creating plug-ins that are included with OpenSim: https://github.com/opensim-org/opensim- ... eLists.txt.

When running CMake, you should just be able to provide a directory to an existing OpenSim installation (i.e., OPENSIM_INSTALL_DIR) and then create a standalone build of your plug-in.

-Nick

POST REPLY