-
Notifications
You must be signed in to change notification settings - Fork 38
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
Introduce the Variables Handler #50
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2c83260
Implement tge first version of VariablesHandler class
GiulioRomualdi f27acff
Implement a simple test for the VariablesHandler class
GiulioRomualdi 43e5a2d
Enable the compilation of VariablesHandler
GiulioRomualdi 85a7125
Fix typo in VariablesHandler.h
GiulioRomualdi 63c1bc6
Fix include guard in VariablesHandler.h
GiulioRomualdi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/System/include/BipedalLocomotion/System/VariablesHandler.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* @file VariablesHandler.h | ||
* @authors Giulio Romualdi | ||
* @copyright 2020 Istituto Italiano di Tecnologia (IIT). This software may be modified and | ||
* distributed under the terms of the GNU Lesser General Public License v2.1 or any later version. | ||
*/ | ||
|
||
#include <string> | ||
#include <unordered_map> | ||
|
||
#include <iDynTree/Core/Utils.h> | ||
|
||
#ifndef BIPEDAL_LOCOMOTION_SYSTEM_VARIABLES_HANDLER_H | ||
#define BIPEDAL_LOCOMOTION_SYSTEM_VARIABLES_HANDLER_H | ||
|
||
namespace BipedalLocomotion | ||
{ | ||
namespace System | ||
{ | ||
/** | ||
* VariableHandler is useful to handle variables in an optimization problem, Their name, dimension | ||
* and position | ||
*/ | ||
class VariablesHandler | ||
{ | ||
|
||
std::unordered_map<std::string, iDynTree::IndexRange> m_variables; /**< Map containing the name | ||
of a variable and its | ||
index range */ | ||
std::size_t m_numberOfVariables{0}; /**< Total number of Variable seen as scalar */ | ||
|
||
public: | ||
/** | ||
* Add a new variable to the list | ||
* @param name of the variable | ||
* @param size the size of the variable | ||
* @return true/false in case of success/failure | ||
*/ | ||
bool addVariable(const std::string& name, const std::size_t& size) noexcept; | ||
|
||
/** | ||
* Get a variable from the list | ||
* @param name of the variable | ||
* @return the index range associated to the variable | ||
*/ | ||
iDynTree::IndexRange getVariable(const std::string& name) const noexcept; | ||
|
||
/** | ||
* Get the number of variables | ||
* @return the total number of variables | ||
*/ | ||
const std::size_t& getNumberOfVariables() const noexcept; | ||
}; | ||
} // namespace System | ||
} // namespace BipedalLocomotion | ||
#endif // BIPEDAL_LOCOMOTION_SYSTEM_VARIABLES_HANDLER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* @file VariablesHandler.cpp | ||
* @authors Giulio Romualdi | ||
* @copyright 2020 Istituto Italiano di Tecnologia (IIT). This software may be modified and | ||
* distributed under the terms of the GNU Lesser General Public License v2.1 or any later version. | ||
*/ | ||
|
||
#include <iostream> | ||
#include <BipedalLocomotion/System/VariablesHandler.h> | ||
|
||
using namespace BipedalLocomotion::System; | ||
|
||
bool VariablesHandler::addVariable(const std::string& name, const std::size_t& size) noexcept | ||
{ | ||
// if the variable already exist cannot be added again. | ||
if (m_variables.find(name) != m_variables.end()) | ||
{ | ||
std::cerr << "[VariableHandler::addVariable] The variable name " << name | ||
<< " already exists"; | ||
return false; | ||
} | ||
|
||
iDynTree::IndexRange indexRange; | ||
indexRange.size = size; | ||
indexRange.offset = m_numberOfVariables; | ||
m_variables.emplace(name, indexRange); | ||
m_numberOfVariables += size; | ||
|
||
return true; | ||
} | ||
|
||
iDynTree::IndexRange VariablesHandler::getVariable(const std::string& name) const noexcept | ||
{ | ||
auto variable = m_variables.find(name); | ||
|
||
// if the variable is present its IndexRange is returned otherwise an | ||
// invalid IndexRange is provided to the user | ||
|
||
if (variable != m_variables.end()) | ||
return variable->second; | ||
else | ||
return iDynTree::IndexRange::InvalidRange(); | ||
} | ||
|
||
const std::size_t& VariablesHandler::getNumberOfVariables() const noexcept | ||
{ | ||
return m_numberOfVariables; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Copyright (C) 2020 Istituto Italiano di Tecnologia (IIT). All rights reserved. | ||
# This software may be modified and distributed under the terms of the | ||
# GNU Lesser General Public License v2.1 or any later version. | ||
|
||
add_bipedal_test( | ||
NAME VariablesHandler | ||
SOURCES VariablesHandlerTest.cpp | ||
LINKS BipedalLocomotion::System) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* @file VariablesHandlerTest.cpp | ||
* @authors Giulio Romualdi | ||
* @copyright 2019 Istituto Italiano di Tecnologia (IIT). This software may be modified and | ||
* distributed under the terms of the GNU Lesser General Public License v2.1 or any later version. | ||
*/ | ||
|
||
// Catch2 | ||
#include <catch2/catch.hpp> | ||
|
||
#include <BipedalLocomotion/System/VariablesHandler.h> | ||
|
||
using namespace BipedalLocomotion::System; | ||
|
||
TEST_CASE("Test Variable Handler") | ||
{ | ||
// Instantiate the handler | ||
VariablesHandler handler; | ||
|
||
constexpr std::size_t variable1Size = 42; | ||
constexpr std::size_t variable2Size = 35; | ||
|
||
REQUIRE(handler.addVariable("variable_1", variable1Size)); | ||
REQUIRE(handler.addVariable("variable_2", variable2Size)); | ||
|
||
REQUIRE(handler.getVariable("variable_1").offset == 0); | ||
REQUIRE(handler.getVariable("variable_1").size == variable1Size); | ||
|
||
REQUIRE(handler.getVariable("variable_2").offset == variable1Size); | ||
REQUIRE(handler.getVariable("variable_2").size == variable2Size); | ||
|
||
REQUIRE(handler.getNumberOfVariables() == variable1Size + variable2Size); | ||
|
||
REQUIRE_FALSE(handler.getVariable("variable_3").isValid()); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you are using an IndexRange, I guess that the assumption is to store all the values in a vector. Does it make sense to add a container to store the values in this class? In this way, once you fill the whole buffer (maybe from the solution of the optimization variable), you can get the values directly using
getVariable
, maybe using a GenericVector as a output. In fact, you can usespan
to get a reference to a sub-vector, then you can use it to build a GenericVector.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After a short Fm2mF (Face-mask to mask-face) discussion we decided to avoid implementing this in the current PR. Since we are thinking to introduce
CasADi
in our pipeline, we may need different implementations of thehandler
.