Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to check component name duplication #76

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ifopt_core/include/ifopt/constraint_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 125,10 @@ class ConstraintSet : public Component {
* or shorthands to specific variable sets want to be saved for quicker
* access later. This function can be overwritten for that.
*/
virtual void InitVariableDependedQuantities(const VariablesPtr& x_init) {};
virtual void InitVariableDependedQuantities(const VariablesPtr&) {};

// doesn't exist for constraints, generated run-time error when used
void SetVariables(const VectorXd& x) final { assert(false); };
void SetVariables(const VectorXd&) final { assert(false); };
};


Expand Down
6 changes: 6 additions & 0 deletions ifopt_core/include/ifopt/problem.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 260,12 @@ class Problem {
*/
const Composite& GetCosts() const { return costs_; };

/**
* @brief Find duplicate component name
* @return Duplicate component name if found, otherwise empty
*/
std::string FindDuplicateComponentName() const;

private:
Composite::Ptr variables_;
Composite constraints_;
Expand Down
33 changes: 32 additions & 1 deletion ifopt_core/src/problem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 30,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <ifopt/problem.h>
#include <iostream>
#include <iomanip>
#include <set>


namespace ifopt {
Expand Down Expand Up @@ -221,7 222,37 @@ Problem::PrintCurrent() const
variables_->PrintAll();
constraints_.PrintAll();
costs_.PrintAll();
};
}

std::string
Problem::FindDuplicateComponentName () const
{
auto check_composite = [](const auto& components, std::string& name) -> bool {
std::set<std::string> names;
for (const auto& c : components) {
if (names.count(c->GetName())) {
name = c->GetName();
return true;
}
names.insert(c->GetName());
}
return false;
};

std::string ret = "";

if (check_composite(variables_->GetComponents(), ret)) {
return ret;
}
if (check_composite(constraints_.GetComponents(), ret)) {
return ret;
}
if (check_composite(costs_.GetComponents(), ret)) {
return ret;
}

return ret;
}

Problem::VectorXd
Problem::ConvertToEigen(const double* x) const
Expand Down