Skip to content

Commit

Permalink
Implemented more efficient method for building constraint jacobian (#26)
Browse files Browse the repository at this point in the history
* Implemented more efficient method for building constraint jacobian

* Cleanup and code styling
  • Loading branch information
fbiemueller authored and awinkler committed Jul 17, 2018
1 parent ab655c1 commit 5ff15c1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
9 changes: 7 additions & 2 deletions ifopt_core/src/composite.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/******************************************************************************
/******************************************************************************
Copyright (c) 2017, Alexander W Winkler. All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -130,16 +130,21 @@ Composite::GetJacobian () const
Jacobian jacobian(GetRows(), n_var);

int row = 0;
std::vector< Eigen::Triplet<double> > triplet_list;

for (const auto& c : components_) {
const Jacobian& jac = c->GetJacobian();
triplet_list.reserve(triplet_list.size()+jac.nonZeros());

for (int k=0; k<jac.outerSize(); ++k)
for (Jacobian::InnerIterator it(jac,k); it; ++it)
jacobian.coeffRef(row+it.row(), it.col()) += it.value();
triplet_list.push_back(Eigen::Triplet<double>(row+it.row(), it.col(), it.value()));

if (!is_cost_)
row += c->GetRows();
}

jacobian.setFromTriplets(triplet_list.begin(), triplet_list.end());
return jacobian;
}

Expand Down
16 changes: 11 additions & 5 deletions ifopt_core/src/leaves.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/******************************************************************************
/******************************************************************************
Copyright (c) 2017, Alexander W Winkler. All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -50,20 +50,26 @@ ConstraintSet::GetJacobian () const
Jacobian jacobian(GetRows(), variables_->GetRows());

int col = 0;
Jacobian jac;
std::vector< Eigen::Triplet<double> > triplet_list;

for (const auto& vars : variables_->GetComponents()) {
int n = vars->GetRows();
Jacobian jac = Jacobian(GetRows(), n);
jac.resize(GetRows(), n);

FillJacobianBlock(vars->GetName(), jac);
// reserve space for the new elements to reduce memory allocation
triplet_list.reserve(triplet_list.size()+jac.nonZeros());

// insert the derivative in the correct position in the overall Jacobian
// create triplets for the derivative at the correct position in the overall Jacobian
for (int k=0; k<jac.outerSize(); ++k)
for (Jacobian::InnerIterator it(jac,k); it; ++it)
jacobian.coeffRef(it.row(), col+it.col()) = it.value();

triplet_list.push_back(Eigen::Triplet<double>(it.row(), col+it.col(), it.value()));
col += n;
}

// transform triplet vector into sparse matrix
jacobian.setFromTriplets(triplet_list.begin(), triplet_list.end());
return jacobian;
}

Expand Down

0 comments on commit 5ff15c1

Please sign in to comment.