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

Introduce the Variables Handler #50

Merged
merged 5 commits into from
Jun 16, 2020
Merged
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
10 changes: 6 additions & 4 deletions src/System/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

# set target name
add_bipedal_locomotion_library(
NAME System
PUBLIC_HEADERS include/BipedalLocomotion/System/Advanceable.h
IS_INTERFACE #This will be removed when adding also sources.
)
NAME System
PUBLIC_HEADERS include/BipedalLocomotion/System/Advanceable.h include/BipedalLocomotion/System/VariablesHandler.h
SOURCES src/VariablesHandler.cpp
PUBLIC_LINK_LIBRARIES iDynTree::idyntree-core
SUBDIRECTORIES tests
)
56 changes: 56 additions & 0 deletions src/System/include/BipedalLocomotion/System/VariablesHandler.h
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
Copy link
Member

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 use span to get a reference to a sub-vector, then you can use it to build a GenericVector.

Copy link
Member Author

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 the handler.

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
48 changes: 48 additions & 0 deletions src/System/src/VariablesHandler.cpp
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;
}
8 changes: 8 additions & 0 deletions src/System/tests/CMakeLists.txt
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)
35 changes: 35 additions & 0 deletions src/System/tests/VariablesHandlerTest.cpp
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());
}