Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
bchretien committed Aug 27, 2016
2 parents c3e08d5 + 1da076b commit c8630b7
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 19 deletions.
9 changes: 9 additions & 0 deletions fixture.hh
Expand Up @@ -30,6 +30,10 @@

# include <log4cxx/xml/domconfigurator.h>

# ifdef __linux__
# include <fenv.h>
# endif // __linux__

# include <roboptim/core/numeric-linear-function.hh>
# include <roboptim/core/twice-differentiable-function.hh>
# include <roboptim/core/io.hh>
Expand All @@ -46,6 +50,11 @@ struct TestSuiteConfiguration

lt_dlinit();
BOOST_REQUIRE_EQUAL (lt_dlsetsearchpath (PLUGIN_PATH), 0);

#if (defined __linux__ && defined ENABLE_SIGFPE)
// Enable floating-point exceptions (except FE_INEXACT)
feenableexcept(FE_ALL_EXCEPT & ~FE_INEXACT);
#endif // (defined __linux__ && defined ENABLE_SIGFPE)
}

~TestSuiteConfiguration ()
Expand Down
2 changes: 1 addition & 1 deletion roboptim/CMakeLists.txt
Expand Up @@ -14,7 +14,7 @@
# along with roboptim-core. If not, see <http://www.gnu.org/licenses/>.

IF(NOT DEFINED ROBOPTIM_PROBLEMS)
SET(ROBOPTIM_PROBLEMS distance-to-sphere)
SET(ROBOPTIM_PROBLEMS distance-to-sphere simple-square)
ENDIF()

FOREACH(PROBLEM ${ROBOPTIM_PROBLEMS})
Expand Down
126 changes: 126 additions & 0 deletions roboptim/simple-square.cc
@@ -0,0 +1,126 @@
// Copyright (C) 2016 by Benjamin Chrétien, CNRS-AIST JRL.
//
// This file is part of the roboptim.
//
// roboptim is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// roboptim is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with roboptim. If not, see <http://www.gnu.org/licenses/>.

#include "common.hh"

#include <roboptim/core/numeric-quadratic-function.hh>

namespace roboptim
{
namespace simple_square
{
template <typename T>
struct F : public GenericNumericQuadraticFunction<T>
{
ROBOPTIM_DIFFERENTIABLE_FUNCTION_FWD_TYPEDEFS_ (
GenericNumericQuadraticFunction<T>);

explicit F ()
: GenericNumericQuadraticFunction<T> (
matrix_t (1, 1), vector_t::Zero (1), vector_t::Zero (1))
{
initialize ();
}

void initialize ();

~F () {}
};

template <>
void F<EigenMatrixSparse>::initialize ()
{
// Fill matrix A.
Eigen::MatrixXd denseA (1, 1);
denseA << 4.;
denseA *= 0.5;
this->A () = denseA.sparseView ();

// Fill vector b.
this->b () << -3.;

// Fill c.
this->c () << 5.;
}

template <typename T>
void F<T>::initialize ()
{
// Fill matrix A.
this->A () << 4.;
this->A () *= 0.5;

// Fill vector b.
this->b () << -3.;

// Fill c.
this->c () << 5.;
}
} // end of namespace unconstrained
} // end of namespace roboptim

BOOST_FIXTURE_TEST_SUITE (roboptim, TestSuiteConfiguration)

BOOST_AUTO_TEST_CASE (roboptim_simple_square)
{
using namespace roboptim;
using namespace roboptim::simple_square;

// Tolerances for Boost checks.
double f0_tol = 1e-6;
double x_tol = 1e-5;
double f_tol = 1e-4;

ExpectedResult expectedResult;
expectedResult.f0 = 257.;
expectedResult.x = (ExpectedResult::argument_t (1) << 0.75).finished ();
expectedResult.fx = 3.875;

// Build cost function.
boost::shared_ptr<F<functionType_t> > f (new F<functionType_t> ());

// Build problem.
solver_t::problem_t problem (f);

// Load starting point
F<functionType_t>::argument_t x (1);
x << 12.;
problem.startingPoint () = x;

BOOST_CHECK_SMALL_OR_CLOSE ((*f) (x)[0], expectedResult.f0, f0_tol);

// Initialize solver.
SolverFactory<solver_t> factory (SOLVER_NAME, problem);
solver_t& solver = factory ();

// Set optimization logger
SET_OPTIMIZATION_LOGGER (solver, "roboptim/simple-square");

// Set optional log file for debugging
SET_LOG_FILE (solver);

// Compute the minimum and retrieve the result.
solver_t::result_t res = solver.minimum ();

// Display solver information.
std::cout << solver << std::endl;

// Process the result
PROCESS_RESULT_UNCONSTRAINED ();
}

BOOST_AUTO_TEST_SUITE_END ()
65 changes: 47 additions & 18 deletions util.hh
Expand Up @@ -22,10 +22,55 @@
# include <boost/archive/text_oarchive.hpp>
# include <boost/archive/text_iarchive.hpp>

# include <roboptim/core/util.hh>

# include "serialize.hh"

typedef boost::filesystem::path path_t;

namespace
{
template <typename M, typename A>
inline M genericReadMatrix (const path_t& file)
{
typedef M matrix_t;
typedef A inputArchive_t;

matrix_t m;

path_t full_path = path_t (TESTS_DATA_DIR) / file;

std::ifstream ifs (full_path.c_str ());
inputArchive_t ia (ifs);

# if (defined ROBOPTIM_HAS_FENV_H && defined ENABLE_SIGFPE)
// Disable SIGFPE (implementation relies on subnormal numbers)
roboptim::detail::DisableFPE d;
# endif //! (defined ROBOPTIM_HAS_FENV_H && defined ENABLE_SIGFPE)

ia >> m;

return m;
}

template <typename M, typename A>
inline void genericWriteMatrix (const path_t& file, const M& m)
{
typedef A ouputArchive_t;

path_t full_path = path_t (TESTS_DATA_DIR) / file;

std::ofstream ofs (full_path.c_str ());
ouputArchive_t oa (ofs);

oa << m;
}
} // end of unnamed namespace

/// ** NOTE **
/// We use text archives since binary archives are not portable according to
/// the Boost documentation.

/// \brief Load matrix from a data file.
///
/// This can be used for comparison at a given threshold, rather than
Expand All @@ -39,18 +84,7 @@ typedef boost::filesystem::path path_t;
template <typename M>
M readMatrix (const path_t& file)
{
typedef M matrix_t;

matrix_t m;

path_t full_path = path_t (TESTS_DATA_DIR) / file;

std::ifstream ifs (full_path.c_str ());
boost::archive::text_iarchive ia (ifs);

ia >> m;

return m;
return genericReadMatrix<M, boost::archive::text_iarchive> (file);
}

/// \brief Write matrix to a data file.
Expand All @@ -64,12 +98,7 @@ M readMatrix (const path_t& file)
template <typename M>
void writeMatrix (const path_t& file, const M& m)
{
path_t full_path = path_t (TESTS_DATA_DIR) / file;

std::ofstream ofs (full_path.c_str ());
boost::archive::text_oarchive oa (ofs);

oa << m;
genericWriteMatrix<M, boost::archive::text_oarchive> (file, m);
}

# endif //! ROBOPTIM_SHARED_TESTS_UTIL_HH

0 comments on commit c8630b7

Please sign in to comment.