Skip to content

Commit

Permalink
Merge branch 'fix-deps' into robust-armijo
Browse files Browse the repository at this point in the history
  • Loading branch information
zfergus committed Dec 8, 2023
2 parents dc4e1aa + 287071b commit c7b03c1
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 50 deletions.
99 changes: 56 additions & 43 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -181,26 +181,11 @@ target_compile_definitions(polysolve_linear PUBLIC POLYSOLVE_JSON_SPEC_DIR="${PR
# Dependencies
################################################################################

# polysolve_linear
target_link_libraries(polysolve PUBLIC polysolve::linear)

# CppNumericalSolvers
include(cppoptlib)
target_link_libraries(polysolve PUBLIC cppoptlib)
# ------
# Linear
# ------

# LBFGSpp
include(LBFGSpp)
target_link_libraries(polysolve PUBLIC LBFGSpp::LBFGSpp)

# JSON Specification Engine library
include(jse)
target_link_libraries(polysolve_linear PUBLIC jse::jse)

# spdlog
include(spdlog)
target_link_libraries(polysolve_linear PUBLIC spdlog::spdlog)

# Accelerate solver
# Accelerate solver (Include before Eigen)
if(POLYSOLVE_WITH_ACCELERATE)
include(CPM)
CPMAddPackage(
Expand All @@ -213,26 +198,23 @@ if(POLYSOLVE_WITH_ACCELERATE)
find_package(BLAS REQUIRED)
find_package(LAPACK REQUIRED)
target_link_libraries(polysolve_linear PRIVATE BLAS::BLAS LAPACK::LAPACK)
endif()

# Extra warnings
include(polysolve_warnings)
target_link_libraries(polysolve_linear PRIVATE polysolve::warnings)
target_link_libraries(polysolve PRIVATE polysolve::warnings)

# Sanitizers
if(POLYSOLVE_WITH_SANITIZERS)
include(sanitizers)
add_sanitizers(polysolve_linear)
add_sanitizers(polysolve)
target_compile_definitions(polysolve_linear PRIVATE -DPOLYSOLVE_WITH_ACCELERATE)
endif()

include(eigen)
target_link_libraries(polysolve_linear PUBLIC Eigen3::Eigen)

# finite-diff (include this after Eigen)
include(finite-diff)
target_link_libraries(polysolve PUBLIC finitediff::finitediff)
# spdlog
include(spdlog)
target_link_libraries(polysolve_linear PUBLIC spdlog::spdlog)

# JSON (MIT)
include(json)
target_link_libraries(polysolve_linear PUBLIC nlohmann_json::nlohmann_json)

# JSON Specification Engine library
include(jse)
target_link_libraries(polysolve_linear PUBLIC jse::jse)

# Hypre (GNU Lesser General Public License)
if(POLYSOLVE_WITH_HYPRE)
Expand All @@ -244,15 +226,6 @@ if(POLYSOLVE_WITH_HYPRE)
endif()
endif()

# JSON
include(json)
target_link_libraries(polysolve_linear PUBLIC nlohmann_json::nlohmann_json)

# Accelerate solvers
if(POLYSOLVE_WITH_ACCELERATE)
target_compile_definitions(polysolve_linear PRIVATE -DPOLYSOLVE_WITH_ACCELERATE)
endif()

# CHOLMOD solver
if(POLYSOLVE_WITH_CHOLMOD)
include(suitesparse)
Expand Down Expand Up @@ -321,6 +294,46 @@ if(POLYSOLVE_WITH_CUSOLVER)
endif()
endif()

# Sanitizers
if(POLYSOLVE_WITH_SANITIZERS)
include(sanitizers)
add_sanitizers(polysolve_linear)
endif()

# Extra warnings (include last for highest priority)
include(polysolve_warnings)
target_link_libraries(polysolve_linear PRIVATE polysolve::warnings)

# ---------
# Nonlinear
# ---------

# polysolve::linear
target_link_libraries(polysolve PUBLIC polysolve::linear)

# CppNumericalSolvers
include(cppoptlib)
target_link_libraries(polysolve PUBLIC cppoptlib)

# LBFGSpp
include(LBFGSpp)
target_link_libraries(polysolve PUBLIC LBFGSpp::LBFGSpp)

# finite-diff (include this after eigen)
include(finite-diff)
target_link_libraries(polysolve PUBLIC finitediff::finitediff)

# Sanitizers
if(POLYSOLVE_WITH_SANITIZERS)
include(sanitizers)
add_sanitizers(polysolve)
endif()

# Extra warnings (include last for highest priority)
include(polysolve_warnings)
target_link_libraries(polysolve PRIVATE polysolve::warnings)


################################################################################
# Compiler options
################################################################################
Expand Down
2 changes: 1 addition & 1 deletion cmake/recipes/jse.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ endif()
message(STATUS "Third-party: creating target 'jse::jse'")

include(CPM)
CPMAddPackage("gh:geometryprocessing/json-spec-engine#1261dc89478c7646ff99cbed8bc5357c2813565d")
CPMAddPackage("gh:geometryprocessing/json-spec-engine#49f1a30f8c2912814916ec3d6108a649b23cb243")
2 changes: 2 additions & 0 deletions src/polysolve/nonlinear/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ set(SOURCES
Solver.cpp
Problem.hpp
Problem.cpp
PostStepData.hpp
PostStepData.cpp
)

source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" PREFIX "Source Files" FILES ${SOURCES})
Expand Down
11 changes: 11 additions & 0 deletions src/polysolve/nonlinear/PostStepData.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "PostStepData.hpp"

namespace polysolve::nonlinear
{
PostStepData::PostStepData(const int iter_num,
const Eigen::VectorXd &x,
const Eigen::VectorXd &grad)
: iter_num(iter_num), x(x), grad(grad)
{
}
} // namespace polysolve::nonlinear
19 changes: 19 additions & 0 deletions src/polysolve/nonlinear/PostStepData.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <polysolve/Types.hpp>

namespace polysolve::nonlinear
{

class PostStepData
{
public:
PostStepData(const int iter_num,
const Eigen::VectorXd &x,
const Eigen::VectorXd &grad);

const int iter_num;
const Eigen::VectorXd &x;
const Eigen::VectorXd &grad;
};
} // namespace polysolve::nonlinear
4 changes: 3 additions & 1 deletion src/polysolve/nonlinear/Problem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <polysolve/Types.hpp>

#include "PostStepData.hpp"

#include <cppoptlib/problem.h>

#include <memory>
Expand Down Expand Up @@ -33,7 +35,7 @@ namespace polysolve::nonlinear

virtual void line_search_begin(const TVector &x0, const TVector &x1) {}
virtual void line_search_end() {}
virtual void post_step(const int iter_num, const TVector &x) {}
virtual void post_step(const PostStepData &data) {}

virtual void set_project_to_psd(bool val) {}

Expand Down
8 changes: 5 additions & 3 deletions src/polysolve/nonlinear/Solver.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

#include "Solver.hpp"

#include "PostStepData.hpp"

#include "descent_strategies/BFGS.hpp"
#include "descent_strategies/Newton.hpp"
#include "descent_strategies/GradientDescent.hpp"
Expand Down Expand Up @@ -126,8 +128,8 @@ namespace polysolve::nonlinear
this->setStopCriteria(criteria);

use_grad_norm_tol = solver_params["line_search"]["use_grad_norm_tol"];

first_grad_norm_tol = solver_params["first_grad_norm_tol"];
allow_out_of_iterations = solver_params["allow_out_of_iterations"];

use_grad_norm_tol *= characteristic_length;
first_grad_norm_tol *= characteristic_length;
Expand Down Expand Up @@ -191,7 +193,7 @@ namespace polysolve::nonlinear
objFunc.solution_changed(x);
}

objFunc.post_step(this->m_current.iterations, x);
objFunc.post_step(PostStepData(this->m_current.iterations, x, grad));

const auto g_norm_tol = this->m_stop.gradNorm;
this->m_stop.gradNorm = first_grad_norm_tol;
Expand Down Expand Up @@ -371,7 +373,7 @@ namespace polysolve::nonlinear
m_logger.debug("[{}][{}] Objective decided to stop", name(), m_line_search->name());
}

objFunc.post_step(this->m_current.iterations, x);
objFunc.post_step(PostStepData(this->m_current.iterations, x, grad));

if (f_delta < this->m_stop.fDelta)
f_delta_step_cnt++;
Expand Down
9 changes: 7 additions & 2 deletions src/polysolve/nonlinear/descent_strategies/Newton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,13 @@ namespace polysolve::nonlinear
polysolve::StiffnessMatrix &hessian)

{
objFunc.set_project_to_psd(true);
objFunc.hessian(x, hessian);
if (x.size() != x_cache.size() || x != x_cache)
{
objFunc.set_project_to_psd(true);
objFunc.hessian(x, hessian_cache);
x_cache = x;
}
hessian = hessian_cache;
if (reg_weight > 0)
{
hessian += reg_weight * sparse_identity(hessian.rows(), hessian.cols());
Expand Down
3 changes: 3 additions & 0 deletions src/polysolve/nonlinear/descent_strategies/Newton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ namespace polysolve::nonlinear
double reg_weight_max;
double reg_weight_inc;

TVector x_cache;
polysolve::StiffnessMatrix hessian_cache;

double reg_weight; ///< Regularization Coefficients
protected:
void compute_hessian(Problem &objFunc,
Expand Down

0 comments on commit c7b03c1

Please sign in to comment.