leggedrobotics / ocs2

Optimal Control for Switched Systems
https://leggedrobotics.github.io/ocs2
BSD 3-Clause "New" or "Revised" License
874 stars 225 forks source link

Empty multiplier with preJumpStateEqualityLagrangian #48

Closed ghost closed 2 years ago

ghost commented 2 years ago

Version: 11.0 Code:

scalar_t MultidimensionalPenalty::getValue(scalar_t t, const vector_t& h, const vector_t* l) const {
  const auto numConstraints = h.rows();
  assert(penaltyPtrArray_.size() == 1 || penaltyPtrArray_.size() == numConstraints);

  scalar_t penalty = 0;
  for (size_t i = 0; i < numConstraints; i++) {
    const auto& penaltyTerm = (penaltyPtrArray_.size() == 1) ? penaltyPtrArray_[0] : penaltyPtrArray_[i];
    penalty += penaltyTerm->getValue(t, getMultiplier(l, i), h(i));
  }

  return penalty;
}

The getMultiplier triggered segmentation fault because l is an uninitialized empty vector. The problem occurs the first time the preJump constraint is active. The horizon is 1s, at 1s there is a mode switch, and the constraint is expected active at the switch.

scalar_t getMultiplier(const vector_t* l, size_t ind) {
  return (l == nullptr) ? 0.0 : (*l)(ind);
}
farbod-farshidian commented 2 years ago

I added a unittest for the pre-jump and final time equality constraints (Tag 11.1). The unittest successfully passes. So I cannot replicate your issue. It will help if you create a minimal example such as this unittest. I will close this issue for now.

ghost commented 2 years ago

I added a unittest for the pre-jump and final time equality constraints (Tag 11.1). The unittest successfully passes. So I cannot replicate your issue. It will help if you create a minimal example such as this unittest. I will close this issue for now.

As far as I know, the problem only occurs if the prejump time is exactly equal to the final time.