Commit 3975e131 authored by David Bommes's avatar David Bommes

added NonLinearSolvers and adapted cmake build system

git-svn-id: http://www.openflipper.org/svnrepo/CoMISo/trunk@54 1355f012-dd97-4b2f-ae87-10fa9f823a57
parent b9a37aa9
......@@ -39,8 +39,43 @@ endif ()
#added by CAT
find_package (SUITESPARSE)
if (NOT SUITESPARSE_FOUND )
message (FATAL_ERROR "SUITESPARSE not found!")
if (SUITESPARSE_FOUND )
set (COMISO_SUITESPARSE_CONFIG_FILE_SETTINGS "#define COMISO_SUITESPARSE_AVAILABLE 1" )
else ()
message (STATUS "SUITESPARSE not found!")
set (COMISO_SUITESPARSE_CONFIG_FILE_SETTINGS "#define COMISO_SUITESPARSE_AVAILABLE 0" )
endif ()
find_package (MPI)
if (MPI_FOUND )
set (COMISO_MPI_CONFIG_FILE_SETTINGS "#define COMISO_MPI_AVAILABLE 1" )
else ()
message (STATUS "MPI not found!")
set (COMISO_MPI_CONFIG_FILE_SETTINGS "#define COMISO_MPI_AVAILABLE 0" )
endif ()
find_package (PETSC)
if (PETSC_FOUND )
set (COMISO_PETSC_CONFIG_FILE_SETTINGS "#define COMISO_PETSC_AVAILABLE 1" )
else ()
message (STATUS "PETSC not found!")
set (COMISO_PETSC_CONFIG_FILE_SETTINGS "#define COMISO_PETSC_AVAILABLE 0" )
endif ()
find_package (TAO)
if (TAO_FOUND )
set (COMISO_TAO_CONFIG_FILE_SETTINGS "#define COMISO_TAO_AVAILABLE 1" )
else ()
message (STATUS "PETSC not found!")
set (COMISO_TAO_CONFIG_FILE_SETTINGS "#define COMISO_TAO_AVAILABLE 0" )
endif ()
find_package (IPOPT)
if (IPOPT_FOUND )
set (COMISO_IPOPT_CONFIG_FILE_SETTINGS "#define COMISO_IPOPT_AVAILABLE 1" )
else ()
message (STATUS "IPOPT not found!")
set (COMISO_IPOPT_CONFIG_FILE_SETTINGS "#define COMISO_IPOPT_AVAILABLE 0" )
endif ()
if( EXISTS "${CMAKE_SOURCE_DIR}/Examples/factored_solver/CMakeLists.txt" )
......@@ -60,8 +95,6 @@ if( EXISTS "${CMAKE_SOURCE_DIR}/Examples/small_factored_example/CMakeLists.txt"
add_subdirectory (Examples/small_factored_example)
endif()
include_directories (
..
${CMAKE_CURRENT_SOURCE_DIR}
......@@ -69,6 +102,10 @@ include_directories (
${Boost_INCLUDE_DIR}
${GMM_INCLUDE_DIR}
${SUITESPARSE_INCLUDE_DIRS}
${MPI_INCLUDE_PATH}
${PETSC_INCLUDE_DIRS}
${TAO_INCLUDE_DIRS}
${IPOPT_INCLUDE_DIR}
)
# generate dllexport macros on windows
......@@ -79,12 +116,17 @@ endif ()
link_directories (
${SUITESPARSE_LIBRARY_DIRS}
# ${MPI_LIBRARY_DIR}
${PETSC_LIBRARY_DIR}
${TAO_LIBRARY_DIR}
${IPOPT_LIBRARY_DIR}
)
# source code directories
set (directories
.
Solver
Solver
NSolver
Config
Utils
QtWidgets
......@@ -142,26 +184,17 @@ else(NOT APPLE)
${QT_LIBRARIES}
${SUITESPARSE_LIBRARIES}
${BLAS_LIBRARIES}
${PETSC_LIBRARIES}
${TAO_LIBRARIES}
${MPI_LIBRARIES}
${IPOPT_LIBRARIES}
)
endif(NOT APPLE)
# display results
acg_print_configure_header (COMISO "CoMISo")
if ( SUITESPARSE_FOUND )
set (COMISO_SUITESPARSE_COFIG_FILE_SETTINGS "#define COMISO_ENABLE_SUITESPARSE" )
else ()
set (COMISO_SUITESPARSE_COFIG_FILE_SETTINGS "#define COMISO_DISABLE_SUITESPARSE" )
endif ()
# write config file
configure_file ("${CMAKE_CURRENT_SOURCE_DIR}/Config/config.hh.in"
"${CMAKE_CURRENT_SOURCE_DIR}/Config/config.hh" @ONLY IMMEDIATE)
// Build time dependencies for CoMiso
@COMISO_SUITESPARSE_COFIG_FILE_SETTINGS@
@COMISO_SUITESPARSE_CONFIG_FILE_SETTINGS@
@COMISO_MPI_CONFIG_FILE_SETTINGS@
@COMISO_PETSC_CONFIG_FILE_SETTINGS@
@COMISO_TAO_CONFIG_FILE_SETTINGS@
@COMISO_IPOPT_CONFIG_FILE_SETTINGS@
include (ACGCommon)
find_package(CoMISo)
find_package(SUITESPARSE)
find_package(MPI)
include_directories (
..
......@@ -7,9 +10,13 @@ include_directories (
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
${COMISO_INCLUDE_DIR}
${SUITESPARSE_INCLUDE_DIRS}
)
link_directories (
${SUITESPARSE_LIBRARY_DIRS}
${TAO_LIBRARY_DIR}
${PETSC_LIBRARY_DIR}
)
# source code directories
......@@ -33,8 +40,15 @@ else ()
acg_add_executable (factored_solver ${sources} ${headers} )
endif ()
# enable rpath linking
set_target_properties(factored_solver PROPERTIES INSTALL_RPATH_USE_LINK_PATH 1)
target_link_libraries (factored_solver
CoMISo
${SUITESPARSE_LIBRARIES}
${MPI_LIBRARY}
${PETSC_LIBRARY}
${TAO_LIBRARY}
)
if (APPLE)
......
include (ACGCommon)
find_package(CoMISo)
find_package(SUITESPARSE)
find_package(MPI)
include_directories (
..
......@@ -7,11 +9,13 @@ include_directories (
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
${COMISO_INCLUDE_DIR}
${SUITESPARSE_INCLUDE_DIRS}
)
link_directories (
# ${CMAKE_CURRENT_BINARY_DIR}/Build/lib/CoMISo
#/data/home1/zimmer/projects/ConstrainedSolver/package/CoMISo/build/Build/lib/CoMISo
${SUITESPARSE_LIBRARY_DIRS}
${TAO_LIBRARY_DIR}
${PETSC_LIBRARY_DIR}
)
# source code directories
......@@ -35,8 +39,15 @@ else ()
acg_add_executable (quadratic_solver ${sources} ${headers} )
endif ()
# enable rpath linking
set_target_properties(quadratic_solver PROPERTIES INSTALL_RPATH_USE_LINK_PATH 1)
target_link_libraries (quadratic_solver
CoMISo
${SUITESPARSE_LIBRARIES}
${MPI_LIBRARY}
${PETSC_LIBRARY}
${TAO_LIBRARY}
)
if (APPLE)
......@@ -49,4 +60,3 @@ if (APPLE)
MACOSX_BUNDLE_INFO_STRING "CoMISo quadratic_solver"
)
endif ()
include (ACGCommon)
find_package(CoMISo)
find_package(SUITESPARSE)
find_package(MPI)
include_directories (
..
......@@ -7,9 +9,13 @@ include_directories (
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
${COMISO_INCLUDE_DIR}
${SUITESPARSE_INCLUDE_DIRS}
)
link_directories (
${SUITESPARSE_LIBRARY_DIRS}
${TAO_LIBRARY_DIR}
${PETSC_LIBRARY_DIR}
)
# source code directories
......@@ -33,8 +39,15 @@ else ()
acg_add_executable (small_factored_solver ${sources} ${headers} )
endif ()
# enable rpath linking
set_target_properties(small_factored_solver PROPERTIES INSTALL_RPATH_USE_LINK_PATH 1)
target_link_libraries (small_factored_solver
CoMISo
${SUITESPARSE_LIBRARIES}
${MPI_LIBRARY}
${PETSC_LIBRARY}
${TAO_LIBRARY}
)
if (APPLE)
......@@ -48,3 +61,4 @@ if (APPLE)
)
endif ()
include (ACGCommon)
find_package(CoMISo)
find_package(SUITESPARSE)
find_package(MPI)
include_directories (
..
......@@ -7,9 +9,13 @@ include_directories (
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
${COMISO_INCLUDE_DIR}
${SUITESPARSE_INCLUDE_DIRS}
)
link_directories (
${SUITESPARSE_LIBRARY_DIRS}
${TAO_LIBRARY_DIR}
${PETSC_LIBRARY_DIR}
)
# source code directories
......@@ -33,8 +39,15 @@ else ()
acg_add_executable (small_quadratic_solver ${sources} ${headers} )
endif ()
# enable rpath linking
set_target_properties(small_quadratic_solver PROPERTIES INSTALL_RPATH_USE_LINK_PATH 1)
target_link_libraries (small_quadratic_solver
CoMISo
${SUITESPARSE_LIBRARIES}
${MPI_LIBRARY}
${PETSC_LIBRARY}
${TAO_LIBRARY}
)
if (APPLE)
......@@ -48,3 +61,4 @@ if (APPLE)
)
endif ()
//=============================================================================
//
// CLASS LinearConstraintHandlerElimination - IMPLEMENTATION TEMPLATES
//
//=============================================================================
//== INCLUDES =================================================================
#include "LinearConstraintHandlerElimination.hh"
//== NAMESPACES ===============================================================
namespace ACG {
//== IMPLEMENTATION ==========================================================
void
LinearConstraintHandlerElimination::
initialize_identity(int _n)
{
n_ = _n;
n_red_ = _n;
m_ = 0;
b_.resize(n_);
gmm::resize(C_, n_, n_);
gmm::resize(Ct_, n_, n_);
gmm::clear(C_);
gmm::clear(Ct_);
for(int i=0; i<n_; ++i)
{
b_[i] = 0.0;
Ct_(i,i) = 1.0;
C_(i,i) = 1.0;
}
}
//-----------------------------------------------------------------------------
void
LinearConstraintHandlerElimination::
initialize( const std::vector<double>& _c)
{
if( _c.size() )
initialize( (double*) &(_c[0]));
}
//-----------------------------------------------------------------------------
void
LinearConstraintHandlerElimination::
initialize( double* _c)
{
// TODO
}
//-----------------------------------------------------------------------------
void
LinearConstraintHandlerElimination::
transform_x( const std::vector<double>& _x, std::vector<double>& _xC)
{
_xC.resize(n_red_);
if( _x.size() && _xC.size())
transform_x((double*)&(_x[0]), &(_xC[0]));
}
//-----------------------------------------------------------------------------
void
LinearConstraintHandlerElimination::
transform_x( double* _x, double* _xC)
{
// directly exploit orthogonality of QR-factorization
// _xC = Ct_*(_x-b_)
Vtemp_.resize(n_);
gmm::add(VectorPT(_x,n_), gmm::scaled(b_,-1.0), Vtemp_);
gmm::mult(Ct_,Vtemp_, VectorPT(_xC,n_red_));
// // set up least squares problem
// // Ct (_x - b_) = CtC _xC
//
// Vtemp_.resize(n_);
// Vtemp2_.resize(n_red_);
// gmm::add(VectorPT(_x,n_), gmm::scaled(b_,-1.0), Vtemp_);
// gmm::mult(Ct_,Vtemp_, Vtemp2_);
//
// // solve least squares problem
// if( n_red_)
// chol_CtC_.solve(_xC, (double*)(&(Vtemp2_[0])));
}
//-----------------------------------------------------------------------------
void
LinearConstraintHandlerElimination::
inv_transform_x( const std::vector<double>& _xC, std::vector<double>& _x)
{
_x.resize(n_);
if( _x.size())
inv_transform_x( (double*)&(_xC[0]), &(_x[0]));
}
//-----------------------------------------------------------------------------
void
LinearConstraintHandlerElimination::
inv_transform_x( double* _xC, double* _x)
{
gmm::copy(b_, VectorPT(_x, n_));
gmm::mult_add(C_, VectorPT(_xC, n_red_), VectorPT(_x, n_));
}
//-----------------------------------------------------------------------------
void
LinearConstraintHandlerElimination::
transform_gradient( const std::vector<double>& _g, std::vector<double>& _gC)
{
_gC.resize(n_red_);
if( _g.size() && _gC.size())
transform_gradient( (double*)&(_g[0]), &(_gC[0]));
}
//-----------------------------------------------------------------------------
void
LinearConstraintHandlerElimination::
transform_gradient( double* _g, double* _gC)
{
gmm::mult( Ct_, VectorPT(_g, n_), VectorPT(_gC, n_red_));
}
//-----------------------------------------------------------------------------
void
LinearConstraintHandlerElimination::
transform_hessian( const RMatrix& _H, RMatrix& _HC)
{
// resize and clear matrix
gmm::resize(_HC, n_red_, n_red_);
gmm::clear(_HC);
gmm::resize(Mtemp_, n_, n_red_);
gmm::mult(_H,C_, Mtemp_);
gmm::mult(Ct_, Mtemp_, _HC);
}
//-----------------------------------------------------------------------------
//=============================================================================
} // namespace ACG
//=============================================================================
//=============================================================================
//
// CLASS LinearConstraintHandlerElimination
//
//=============================================================================
#ifndef ACG_LINEARCONSTRAINTHANDLERELIMINATION_HH
#define ACG_LINEARCONSTRAINTHANDLERELIMINATION_HH
//== INCLUDES =================================================================
#include <iostream>
#include <gmm/gmm.h>
//== FORWARDDECLARATIONS ======================================================
//== NAMESPACES ===============================================================
namespace ACG {
//== CLASS DEFINITION =========================================================
/** \class LinearConstraintHandler LinearConstraintHandler.hh <ACG/.../LinearConstraintHandler.hh>
Brief Description.
A more elaborate description follows.
*/
class LinearConstraintHandlerElimination
{
public:
typedef gmm::col_matrix< gmm::wsvector< double> > CMatrix;
typedef gmm::row_matrix< gmm::wsvector< double> > RMatrix;
// use c-arrays as vectors for gmm
typedef gmm::array1D_reference<double*> VectorPT;
/// Constructor
LinearConstraintHandlerElimination() {initialize_identity(0);}
// initialize Constructor
template<class MatrixT, class VectorT>
LinearConstraintHandlerElimination( const MatrixT& _C, const VectorT& _c)
{initialize(_C, _c); }
/// Destructor
~LinearConstraintHandlerElimination() {}
// number of variables
int n() {return n_;}
// number of reduced variables (after elimination)
int n_reduced() {return n_red_;}
// number of linearly independent constraints (n-n_reduced)
int n_constraints() { return m_;}
// initialize new constraints
template<class MatrixT, class VectorT>
void initialize( const MatrixT& _C, const VectorT& _c);
// no constraints
void initialize_identity(int _n);
// initialize new constraints rhs only
void initialize( const std::vector<double>& _c);
void initialize( double* _c);
// transform x vector (least squares solution, fulfilling the constraints)
void transform_x( const std::vector<double>& _x, std::vector<double>& _xC);
void transform_x( double* _x, double* _xC);
// inverse transform x ( x_reduced -> x)
void inv_transform_x( const std::vector<double>& _xC, std::vector<double>& _x);
void inv_transform_x( double* _xC, double* _x);
// transform gradient
void transform_gradient( const std::vector<double>& _g, std::vector<double>& _gC);
void transform_gradient( double* _g, double* _gC);
// transform hessian
void transform_hessian( const RMatrix& _H, RMatrix& _HC);
private:
// Constraints in basis transformation form x_orig = b_ + C_*x_reduced
// notice that C_ is a basis of the nullspace of the constraints
RMatrix C_;
RMatrix Ct_;
std::vector<double> b_;
// number of variables
int n_;
// number of constraints (linear independent)
int m_;
// number of reduced variables
int n_red_;
// temp matrix to transform hessian and temp vectors
RMatrix Mtemp_;
std::vector<double> Vtemp_;
};
//=============================================================================
} // namespace ACG
//=============================================================================
#if defined(INCLUDE_TEMPLATES) && !defined(ACG_LINEARCONSTRAINTHANDLERELIMINATION_C)
#define ACG_LINEARCONSTRAINTHANDLERELIMINATION_TEMPLATES
#include "LinearConstraintHandlerEliminationT.cc"
#endif
//=============================================================================
#endif // ACG_LINEARCONSTRAINTHANDLERELIMINATION_HH defined
//=============================================================================
//=============================================================================
//
// CLASS LinearConstraintHandlerElimination - IMPLEMENTATION
//
//=============================================================================
#define ACG_LINEARCONSTRAINTHANDLERELIMINATION_C
//== INCLUDES =================================================================
#include "LinearConstraintHandlerElimination.hh"
#include <CoMISo/Solver/SparseQRSolver.hh>
//== NAMESPACES ===============================================================
namespace ACG {
//== IMPLEMENTATION ==========================================================
template<class MatrixT, class VectorT>
void
LinearConstraintHandlerElimination::
initialize( const MatrixT& _C, const VectorT& _c)
{
// no constraints?
if( gmm::mat_nrows(_C) == 0)
{
initialize_identity( gmm::mat_ncols(_C));
}
else
{
// Construct constraints basis form via QR-factorization (see Nocedal 426...)
// Constraints in basis transformation form x_orig = b_ + C_*x_reduced
// notice that C_ is a basis of the nullspace of the constraints
// and _C*b_ = _c (means b_ is one solution of the constraints)
std::cerr << "Initialize Linear Constraint handler...\n";
COMISO::SparseQRSolver sqr;
// sqr.calc_system_gmm(_C);
CMatrix Q;
CMatrix R;
std::vector<int> P;
int rank = sqr.factorize_system_gmm(gmm::transposed(_C), Q, R, P);
// Q[0..m-1,rank..m-1] is basis of the nullspace -> C_, Ct_
int m = gmm::mat_nrows(Q);
gmm::resize(C_, m, m-rank);
gmm::clear (C_);
gmm::copy( gmm::sub_matrix(Q, gmm::sub_interval(0,m), gmm::sub_interval(rank,m-rank)), C_);
gmm::resize(Ct_, gmm::mat_ncols(C_), gmm::mat_nrows(C_));
gmm::copy( gmm::transposed(C_), Ct_);