RobotLocomotion / drake

Model-based design and verification for robotics.
https://drake.mit.edu
Other
3.35k stars 1.27k forks source link

Update Coefficients in Cost with Copying Whole Vector #22131

Open cohnt opened 1 week ago

cohnt commented 1 week ago

In scenarios where we're solving a sequence of similar mathematical programs (e.g. #22084), we have to copy over the entire coefficient vector to update the cost. It would be much more efficient if we could just modify the internally held coefficient vector in-place. I specifically care about LinearCost, but I imagine it would be applicable to other costs as well.

cc @AlexandreAmice

hongkai-dai commented 5 days ago

@cohnt Currently UpdateCoefficients just copies the new coefficient vector to the internally held vector. I think you are suggesting something like this

LinearCost::UpdateCoefficientEntry(int i, double val) {
  a_[i] = val;
}

I wonder why this "update-in-place" is more efficient than copying a vector?

cohnt commented 5 days ago

That method would be perfect for what I need.

If we have a large vector and we only need to change one or two entries of it (as in the generic ConvexSet::IsBounded method), I would think it's significantly faster to just modify the entry, rather than keeping an entirely separate vector around, changing the entry there, and then copying it in.