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
https://github.com/opensim-org/opensim- ... olGoal.cpp
I would be very grateful if someone could clarify the confusion for me.
Best Wishes,
Matthew
Question about the source code of MocoSumSquaredStateGoal
- Matthew Lee
- Posts: 52
- Joined: Sat Jun 20, 2020 7:46 pm
- Nicholas Bianco
- Posts: 1041
- Joined: Thu Oct 04, 2012 8:09 pm
Re: Question about the source code of MocoSumSquaredStateGoal
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
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
- Matthew Lee
- Posts: 52
- Joined: Sat Jun 20, 2020 7:46 pm
Re: Question about the source code of MocoSumSquaredStateGoal
Hi Nick,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.
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:
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. 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.
- Nicholas Bianco
- Posts: 1041
- Joined: Thu Oct 04, 2012 8:09 pm
Re: Question about the source code of MocoSumSquaredStateGoal
Yes, that's correct.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?
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
- Matthew Lee
- Posts: 52
- Joined: Sat Jun 20, 2020 7:46 pm
Re: Question about the source code of MocoSumSquaredStateGoal
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}")
Matthew
- Nicholas Bianco
- Posts: 1041
- Joined: Thu Oct 04, 2012 8:09 pm
Re: Question about the source code of MocoSumSquaredStateGoal
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
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