Page 1 of 3
Can't save solutions when using path constraint
Posted: Wed Aug 24, 2022 6:48 am
by hojin95
Hi All,
I was trying using a MocoFrameDistanceContraints() on my simulation but figured out it blocks not only saving solutions in .sto but also visualising solutions.
Code: Select all
SimTK::UnitVec3 proj_vec_x(1, 0, 0);
auto* pathCon_x = problem.addPathConstraint<MocoFrameDistanceConstraint>();
pathCon_x->setName("foot_to_origin");
pathCon_x->addFramePair("/ground", "/bodyset/calcn_r", 0, 0);
pathCon_x->setProjection("vector");
pathCon_x->setProjectionVector(proj_vec_x);
Code: Select all
MocoSolution solution = study.solve();
study.print("osimSquatGoal.xml");
solution.unseal();
solution.write("predictionSolution.sto");
std::cout << "Solution status: " << solution.getStatus() << std::endl;
study.visualize(solution);
- stopped.PNG (28.9 KiB) Viewed 1232 times
The image above was taken from force stopping the optimization but same thing happened when the optimization is stopped due to exceeding maximum number of iterations. I unsealed the unconverged solution so I can see it although it did not converged, but when using the path constraints, it returns 2 lines saying "[Info] Set log level to Info" instead show me the result. Is this what it supposed to happen when using a path constraint or did I miss something?
Thank you.
Hojin
Re: Can't save solutions when using path constraint
Posted: Wed Aug 24, 2022 7:59 am
by nbianco
Hi Hojin,
I don't think the frame distance constraint is "blocking" saving solutions or visualization. Could you attach the full output file from a problem that fails normally (i.e., don't manually quit).
The executable is "CustomSquat3" -- do you have any custom MocoGoals in this problem?
-Nick
Re: Can't save solutions when using path constraint
Posted: Wed Aug 24, 2022 11:44 am
by hojin95
Hi Nick,
Yes, I have several custom MocoGoals in the problem. Unfortunately, I can't attach the output file for the solution using a frame distant constraint because it did not save anything. Would attaching my source code be helpful instead?
Best,
Hojin
Re: Can't save solutions when using path constraint
Posted: Wed Aug 24, 2022 2:19 pm
by nbianco
If you remove the MocoFrameDistanceGoal, does the problem fail in the same way? It's hard to say what is causing the issue given the error message in the screenshot.
Since you're working directly in C++, you could try running a debugger to see exactly where the error occurs.
Re: Can't save solutions when using path constraint
Posted: Thu Aug 25, 2022 3:46 am
by hojin95
Hi Nick,
If I remove the frame distance constraint, it works fine (i.e. it shows and saves results whether or not the solution is found or failed). I have ran a debugger as you suggested and found that error has occurred at where it starts solving a problem. Would there be any chance that existing moco functions can crash with the custom moco goals?
- Debug.PNG (124.81 KiB) Viewed 1154 times
Re: Can't save solutions when using path constraint
Posted: Thu Aug 25, 2022 7:05 am
by nbianco
Based on the metadata in the "solution" variable (bottom left of screen), it seems like the problem is solving. But some sort of exception is occurring after the solution is returned? Not sure.
Are there any other path constraints in the problem?
I see an exception related to the "fmt" library (bottom right of screen). Not sure where that is coming from.
You can use the debugger to "step in" to the solve() function to determine the specific location where the exception is thrown. It might be happening right after the solve() function returns the solution, but often exceptions are thrown in the functions called internally by solve().
Lastly, make sure you are using the "RelWithDebInfo" build type and not "Debug". "RelWithDebInfo" will still let you use the debugger. "Debug" requires all the dependencies to have "Debug" builds as well, which we usually don't have by default.
Re: Can't save solutions when using path constraint
Posted: Wed Sep 07, 2022 2:01 am
by hojin95
Hi Nick,
Unfortunately, I couldn't manage to fix the problem. The debugger says a .dll file is missing, but it's working fine when a path constraint is not present, so I'm still clueless. In the meantime, I've found an alternative way to use kinematic constraints from OpenSim (either constant-distant-constraint or point-on-line constraint). And regarding your checklists, yes I'm always using RelWithDebInfo, and there was no other path constraint given in the problem. I have a feeling that I need to come back to this problem in the near future, but for now, I'll try to use kinematic constraints.
Thank you
Hojin
Re: Can't save solutions when using path constraint
Posted: Wed Sep 07, 2022 10:23 am
by nbianco
Which DLL is missing? If it's a dependency issue, or an issue with Moco not finding the library files for your new goal it should be relatively easy to resolve.
Moco already supports kinematic constraints, so if that's what you're trying to accomplish, it might be easier to just add a new kinematic constraint to OpenSim, rather than creating a new goal.
-Nick
Re: Can't save solutions when using path constraint
Posted: Thu Sep 08, 2022 1:40 pm
by hojin95
Hi Nick,
The debugger says osimSimulation.DLL is missing. But I'm not sure if that's the real issue because so far (until I began using path constraints), the moco problem was solved well, and dependencies were copied to the build file when I generated the problem with CMake.
On the other hand, I didn't build my customs goal in separate files; instead, I built them directly in the source code (as written below). Would this be a not good practice that might cause such an error?
I'm considering creating a custom path constraint that prevents contact forces on spheres from being greater than 0 (i.e. foot not leaving the ground). Would there be any function in Moco (or OpenSim) that can achieve this? For now, I'm trying to minimize the body accelerations of the feet but the toes are leaving the ground
Code: Select all
class MocoCOMGoal : public MocoGoal {
OpenSim_DECLARE_CONCRETE_OBJECT(MocoCOMGoal, MocoGoal);
public:
MocoCOMGoal() {}
MocoCOMGoal(std::string name) : MocoGoal(std::move(name)) {}
MocoCOMGoal(std::string name, double weight) : MocoGoal(std::move(name), weight) {}
protected:
Mode getDefaultModeImpl() const override { return Mode::Cost; }
bool getSupportsEndpointConstraintImpl() const override { return true; }
void initializeOnModelImpl(const Model&) const override { setRequirements(1, 1); }
void calcIntegrandImpl(
const IntegrandInput& input, double& integrand) const override {
getModel().realizeAcceleration(input.state);
//COM
const auto& CoM = getModel().getMatterSubsystem().calcSystemMassCenterLocationInGround(input.state);
integrand = CoM[1];
}
void calcGoalImpl(
const GoalInput& input, SimTK::Vector& cost) const override {
cost[0] = input.integral;
}
};
Re: Can't save solutions when using path constraint
Posted: Thu Sep 08, 2022 2:49 pm
by nbianco
Hi Hojin,
If you add a class directly to the API, you need to
update the CMakeLists file to include the .h/.cpp files for your goal, register the type with the API (
here), and include the header for your new goal
here.
You also need make sure you add the macro "OSIMMOCO_API" in front of the class definition (
as in here).
I'm considering creating a custom path constraint that prevents contact forces on spheres from being greater than 0 (i.e. foot not leaving the ground). Would there be any function in Moco (or OpenSim) that can achieve this?
No, this would be a new addition!
Best,
Nick