Skip to content

Commit

Permalink
replace static variable falsely shared between contraints and costs
Browse files Browse the repository at this point in the history
  • Loading branch information
awinkler committed Jan 21, 2021
1 parent a44204f commit 27451ce
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 3 additions & 0 deletions ifopt_core/include/ifopt/composite.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ class Composite : public Component {
private:
ComponentVec components_;
bool is_cost_;
// The number of variables for costs/constraint composites (not set for variables).
// Is initialized the first the GetJacobian() is called.
mutable size_t n_var = -1;
};


Expand Down
8 changes: 5 additions & 3 deletions ifopt_core/src/composite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,13 @@ Composite::SetVariables (const VectorXd& x)

Composite::Jacobian
Composite::GetJacobian () const
{ // static: get number of variables only the first time this function is called,
{ // set number of variables only the first time this function is called,
// since number doesn't change during the optimization. Improves efficiency.
static int n_var = components_.empty() ? 0 : components_.front()->GetJacobian().cols();
Jacobian jacobian(GetRows(), n_var);
if (n_var == -1)
n_var = components_.empty() ? 0 : components_.front()->GetJacobian().cols();

Jacobian jacobian(GetRows(), n_var);

if (n_var == 0) return jacobian;

int row = 0;
Expand Down

0 comments on commit 27451ce

Please sign in to comment.